Implement Grid Trading

I want to implement Grid Trading in Amibroker. I mean, when reaching to a pre established level, close the profitable trade and open a new pair of long/short in the same bar and instrument. Then repeat when reaching another level.

I have seen in this forum that opening longs and shorts in the same bar and instrument can only be done thru the Custom BackTester, otherwise the longs have preference.

I have tried to implement the logic in the CBT with no success. The following code only opens the short trade when a Buy signal arrives. Hoy can I modify it to also open the long trade in the same bar and instrument? Thank you

SetOption( "ReverseSignalForcesExit", False );
SetOption( "AllowSameBarExit", False );

Buy = 1;
Short = Sell = Cover = 0;
 
SetCustomBacktestProc("");

if (Status("action") == actionPortfolio)
{
    bo = GetBacktesterObject();	//  Get backtester object
    
    bo.PreProcess();	//  Do pre-processing
    
    for (i = 0; i < BarCount; i++)	//  Loop through all bars
    {
        for (sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
        {	//  Loop through all signals at this bar        
           
           if ( sig.IsEntry() AND sig.IsLong() )
			{               
                bo.EnterTrade( i, sig.Symbol, False, sig.Price, sig.PosSize );
                bo.EnterTrade( i, sig.Symbol, True, sig.Price, sig.PosSize );    //does not open long
			}
        }	
        
        bo.HandleStops( i );	//  Handle programmed stops at this bar
        bo.UpdateStats( i, 1 );	//  Update MAE/MFE stats for bar
        bo.UpdateStats( i, 2 );	//  Update stats at bar's end
    }	//  End of for loop over bars
    
    bo.PostProcess();	//  Do post-processing
}

Thanks for your anwser. I am not trying to implement opposite trades in correlated symbols (pairs trading). I am trying to place a buy and short in the same bar and same instrument while closing the profitable previous trade (Grid Trading). I haven't found any references searching this forum.

1 Like

how does that work you buy and sell at each level? Do you then chose currency pairs trading at 2 different exchanges or do you use 2 accounts? Or 2 brokers :slight_smile:

Some forex brokers allow you "hedge mode" so you buy and short in the same instrument at the same time but these are 2 different positions. The short does not close the long.

The idea is that you hedge and remove the hedge with a profit when the market moves, and then you initiate another pair or trades. As you are consolidating profits, you only need a small correction to finish the day in positive. This strategy fails if the market keeps moving in one direction, but that is not very common.

thanks. Yes I have seen that these people using metatrader who I believe usually trade forex are able to chose multiple exchanges (i think) to trade from the same metatrader platform. This way they can do arbitrage trading. I did not know about the "hedge mode". Is this possible at IB?

I made several grid trading codes but not using this principle since I did not believe it was possible at IB to trade this way. At which broker would you want to trade this method?

Btw there are also codes available, see for instance: Buy the 'Grid Trading EA' expert advisor for Metatrader (MT4/MT5)

But obviously it is not difficult to code in Amibroker. The question can you trade this method using IB with a single account?

No, IB does not allow "hedge mode", and that is a problem because I cannot automate it with Amibroker if choosing a broker different than IB.

This is a list of brokers that allow hedging:

My idea is to validate the approach with Amibroker. If it works as beautifully as they claim then I should find a broker and then a platform to automate it. Thanks :)))

ok thanks. Will get back to you. I am looking for codes I wrote in the past. I also posted some code on this forum but I can't find it again. I think I posted a code that uses a grid less than 3 years ago. Will do some searching

1 Like

??? That is rather brave (dare I say, unfounded) assumption.

2 Likes

here I posted some code that uses a grid. The trading method is however different. But it sets up a grid. To test your idea it needs adaptation. I will also do some testing.

1 Like

Thank you, that helps :))))

Yes, you are right, at least that is what supporters claim. And that's why I want to test the strategy in Amibroker. I haven't tried that kind of approach yet.
Tomasz, does Amibroker allow a long and a short in the same bar, instrument and price? Thanks

It does, in custom-backtester only (low level). In your code you can't open second trade because you have run out of funds. Use smaller position size.

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

1 Like

Thank you! Now it works perfectly

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.