Need Help in AFL code for pyramiding in Intraday

I need Help regarding placing multiple orders from amibroket to trading terminal, when the buy signal is generated. Here is my code :-

Buy=Cover=Cross(C,tsl) ;
Sell=Short=Cross(tsl,C) ;

Plot(tsl, _DEFAULT_NAME(), tsl_col, styleStaircase);

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);

Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);

/* Placing Order  */

if (LastValue(Buy) == True )
{
tradeType = "BUY";
q1 = q9 = (AT_QUANTITY * 2.17/100);
q2 = q8 = (AT_QUANTITY * 4.35/100);
q3 = q7 = (AT_QUANTITY * 8.70/100);
q4 = q6 = (AT_QUANTITY * 17.39/100);
q5 = (AT_QUANTITY * 34.78/100);
target = ((((BuyPrice-tsl)+BuyPrice)-BuyPrice)*3);
stoploss = (BuyPrice-tsl);
trailsl = IIf(stoploss>1,(stoploss/0.05),20);
increment = target/9;
//_TRACE("Which Symbol = " + AT_SYMBOL);
//_TRACE("BuyPrice = " + BuyPrice);
//_TRACE("What is tsl = " + tsl);
//_TRACE("What is Target? = " + target);
//_TRACE("What is Stop Loss? = " + stoploss);
//_TRACE("What is Trailing Stop Loss? = " + trailsl);
//_TRACE("What is Increment? = " + increment);

placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q1, BuyPrice, target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q2, (BuyPrice+increment), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q3, (BuyPrice+(2*increment)), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q4, (BuyPrice+(3*increment)), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q5, (BuyPrice+(4*increment)), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q6, (BuyPrice+(5*increment)), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q7, (BuyPrice+(6*increment)), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q8, (BuyPrice+(7*increment)), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q9, (BuyPrice+(8*increment)), target, stoploss, trailsl, 1);
}

Here, I am facing the problem because of the code "Buy=ExRem(Buy,Sell);", only one order i.e. 1st Buy order is getting sent to trading terminal and rest 8 buy orders are getting skipped.

Please HELP me to resolve this issue.

1 Like

You should begin here: How do I debug my formula?

I tried that, yet I am not able to figure it out. A little help in this regard is requested. PLease guide me guys...

1 Like

First, I hope that your "trading terminal" is only a paper-trading account, because trying to auto-trade using a language and environment that you have not yet mastered is an excellent way to lose money.

Second, I'm not confident that you understand which of your variables are scalars (single values), and which are arrays. I also don't see any reference to the built-in Filter variable in your AFL, which tells me that you have not used an Exploration to see what your code is doing. For example, if you think your problem is with ExRem(), then use Explore to see "before" and "after" versions of the arrays. Please keep going through the tutorials and other documentation, and ask specific questions when you get stuck.

2 Likes

@mradtke is spot on. You should LEARN the program for at least ONE YEAR, before even thinking about automated trading.

2 Likes

Things that are perceived "simple" might not be necessarily "easy". As a beginner myself, I sometimes feel the pain too of not being able to express programatically either because of lack of basic understanding of a concept or unavailability of concise documentation beyond the general needs. Shall try to explain as much as I can. Presenting in parts to avoid a very long post.

Part I

The concept of Pyramiding is explained in this article and must be dealt very precisely as it is an advanced level method. Having this said and probably read by you, it would be noteworthy to mention that without the knowledge of the strategy used or the rules in detail it would be wrong on my part to make assumptions. However, shall try to overlay certain outliers that might help you debug your code or built a correct one from scratch on your own. Also it would be even lovely if experts join-in to share their tips or tricks.

1. Limitation of Exrem or Flip functions: It has nothing to do with the platform (in this case AB) but more to do with logic. As per definition, Exrem( ARRAY1, ARRAY2 ) removes excessive signals; returns 1 on the first occurence of "true" signal in Array1 then returns 0 until Array2 is true even if there are "true" signals in Array1. Now, what if by virtue of the strategy used, both Array1 and Array2 becomes true at the same time? The below code can be used to control such specific situation otherwise Exrem function is well-off - depends on the strategy.

//Using For-Loop to generate Buy/Sell Signal instead of using ExRem
_SECTION_BEGIN( "For-Loop Buy/Sell" );
	 //A Simple Demo strategy
	 Per = Param( "Periods", 15, 3, 144, 1 );	 
	 HigherHighs = HHV( H, Per );
	 LowerLows = LLV( L, Per );	 
	 _Buy = Cross( H, Ref( HigherHighs, -1 ) );
	 _Short = Cross( Ref( LowerLows, -1 ), L );	 
	 
	 //Array Initialization
	 Buy = Sell = Short = Cover = Null;
	 LongFlag = ShortFlag = 0; //Simple Flag arrays to identify whether in a Long or Short position
	 
	 //Using Loop to generate signals
	 for( i = 0; i < BarCount; i++ )
	 {
		 //Long Positions
		 if( _Buy[ i ] AND LongFlag == 0 )
		 {
			 Buy[ i ] = 1;
			 LongFlag = 1;  //To record that we are in Long position
		 }		 
		 if( _Short[ i ] AND LongFlag == 1 )
		 {
			 Sell[ i ] = 1;  //Selling-off the Long position
			 LongFlag = 0;   //Reseting LongFlag back to False, to denote that we are no longer in "Long" position
		 }
		 
		 //Short Positions
		 if( _Short[ i ] AND ShortFlag == 0 )
		 {
			 Short[ i ] = 1;
			 ShortFlag = 1;	  //To record that we are in Short position
		 } 
		 if( _Buy[ i ] AND ShortFlag == 1 )
		 {
			 Cover[ i ] = 1;  //Covering the Short position
			 ShortFlag = 0;   //Reseting ShortFlag back to False, to denote that we are no longer in "Short" position
		 }
	 }
	 
	 //Plotting
	 Plot( C, "", colorDefault, styleBar | styleThick );
	 Plot( HigherHighs, "Highs", colorDarkGreen );
	 Plot( LowerLows, "Lows", colorBrown );
	 
	 PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -30 ); //Long Entry
	 PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), colorBrown, 0, H, -15 ); //Long Exit
	 
	 PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, 0, H, -30 ); //Short Entry
	 PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorDarkGreen, 0, L, -15 ); //Short Exit
_SECTION_END();

2. Controlled Trailing Stop-Loss (TSL): It is necessary to ensure that TSL always goes up for Long positions and goes down for Short positions or remains constant to the previous value. "Percentage or Points" based TSLs does not account Volatility, so it is not dynamic. "Standard Deviation" based TSL (for e.g. Kase DevStop) or "ATR" based TSLs are ideal. For demonstration using ATR based TSL here:

_SECTION_BEGIN( "Controlled TSL" );
	 MultATR = Param( "ATR Multiplier", 2, 1, 4, 0.5 );
	 PerATR = Param( "ATR Period", 14, 3, 89, 1 );
	 _ATR = ATR( PerATR );
	 
	 //A Simple Demo strategy
	 Per = Param( "Swing Periods", 15, 3, 144, 1 );
	 HigherHighs = HHV( H, Per );
	 LowerLows = LLV( L, Per );
	 _Buy = Cross( H, Ref( HigherHighs, -1 ) );
	 _Short = Cross( Ref( LowerLows, -1 ), L );
	 
	 //Array Initialization
	 Buy = Sell = Short = Cover = TSL = Null;
	 LongFlag = ShortFlag = 0; //Simple flags
	 
	 //Using Loop to generate signals
	 for( i = 0; i < BarCount; i++ )
	 {
		 //Long Positions
		 if( _Buy[ i ] AND LongFlag == 0 )
		 {
			 Buy[ i ] = 1;
			 LongFlag = 1; //To record that we are in Long position
		 }		 
		 if( LongFlag )
		 {
			 if( TSL[ i - 1 ] > H[ i ] - _ATR[ i ] * MultATR )
				 TSL[ i ] = TSL[ i - 1 ];
			 else
				 TSL[ i ] = H[ i ] - _ATR[ i ] * MultATR;
		 }
		 
		 if( ( _Short[ i ] OR ( L[ i ] < TSL[ i - 1 ] ) ) AND LongFlag == 1 )
		 {
			 Sell[ i ] = 1; //Selling-off the Long position
			 LongFlag = 0;  //Reseting LongFlag back to False, to denote that we are no longer in "Long" position
		 }
		 
		 //Short Positions
		 if( _Short[ i ] AND ShortFlag == 0 )
		 {
			 Short[ i ] = 1;
			 ShortFlag = 1;	 //To record that we are in Short position
		 }
		 if( ShortFlag )
		 {
			 if( TSL[ i - 1 ] < L[ i ] + _ATR[ i ] * MultATR )
				 TSL[ i ] = TSL[ i - 1 ];
			 else
				 TSL[ i ] = L[ i ] + _ATR[ i ] * MultATR;
		 }
		 
		 if( ( _Buy[ i ] OR H[ i ] > TSL[ i - 1 ] ) AND ShortFlag == 1 )
		 {
			 Cover[ i ] = 1; //Covering the Short position
			 ShortFlag = 0;  //Reseting ShortFlag back to False, to denote that we are no longer in "Short" position
		 }
	 }
	 
	 //Plotting
	 Plot( C, "", colorDefault, styleBar | styleThick );
	 Plot( HigherHighs, "Highs", colorDarkGreen, styleDashed | styleNoRescale );
	 Plot( LowerLows, "Lows", colorBrown, styleDashed | styleNoRescale );
	 
	 PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -30 ); //Long Entry
	 PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), colorBrown, 0, H, -15 ); //Long Exit
	 
	 PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, 0, H, -30 ); //Short Entry
	 PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorDarkGreen, 0, L, -15 ); //Short Exit
	 
	 Plot( TSL, "TSL", colorPink, styleThick | styleNoRescale ); //Trailing Stop-Loss
_SECTION_END();

3. Time-based Exits: It is quintessential to Exit Intraday positions before the day's session ends otherwise the position will be carried over to the next day. Here in this example we are not initiating any new trade after quarter to three in the afternoon also liquidating the open ones.

_SECTION_BEGIN( "Time-Based Exits" );
	 tn = TimeNum();

	 MultATR = Param( "ATR Multiplier", 2, 1, 4, 0.5 );
	 PerATR = Param( "ATR Period", 14, 3, 89, 1 );
	 _ATR = ATR( PerATR );
	 
	 //A Simple Demo strategy
	 Per = Param( "Swing Periods", 15, 3, 144, 1 );
	 HigherHighs = HHV( H, Per );
	 LowerLows = LLV( L, Per );
	 _Buy = Cross( H, Ref( HigherHighs, -1 ) );
	 _Short = Cross( Ref( LowerLows, -1 ), L );
	 
	 //Array Initialization
	 Buy = Sell = Short = Cover = TSL = Null;
	 LongFlag = ShortFlag = 0; //Simple flags
	 
	 //Using Loop to generate signals
	 for( i = 0; i < BarCount; i++ )
	 {
		 //Long Positions
		 if( _Buy[ i ] AND LongFlag == 0 AND tn[ i ] < 144500 )
		 {
			 Buy[ i ] = 1;
			 LongFlag = 1; //To record that we are in Long position
		 }		 
		 if( LongFlag )
		 {
			 if( TSL[ i - 1 ] > H[ i ] - _ATR[ i ] * MultATR )
				 TSL[ i ] = TSL[ i - 1 ];
			 else
				 TSL[ i ] = H[ i ] - _ATR[ i ] * MultATR;
		 }
		 
		 if( ( _Short[ i ] OR ( L[ i ] < TSL[ i - 1 ] ) OR tn[ i ] >= 144500 ) AND LongFlag == 1 )
		 {
			 Sell[ i ] = 1; //Selling-off the Long position
			 LongFlag = 0;  //Reseting LongFlag back to False, to denote that we are no longer in "Long" position
		 }
		 
		 //Short Positions
		 if( _Short[ i ] AND ShortFlag == 0 AND tn[ i ] < 144500 )
		 {
			 Short[ i ] = 1;
			 ShortFlag = 1;	 //To record that we are in Short position
		 }
		 if( ShortFlag )
		 {
			 if( TSL[ i - 1 ] < L[ i ] + _ATR[ i ] * MultATR )
				 TSL[ i ] = TSL[ i - 1 ];
			 else
				 TSL[ i ] = L[ i ] + _ATR[ i ] * MultATR;
		 }
		 
		 if( ( _Buy[ i ] OR H[ i ] > TSL[ i - 1 ] OR tn[ i ] >= 144500 ) AND ShortFlag == 1 )
		 {
			 Cover[ i ] = 1; //Covering the Short position
			 ShortFlag = 0;  //Reseting ShortFlag back to False, to denote that we are no longer in "Short" position
		 }
	 }
	 
	 //Plotting
	 Plot( C, "", colorDefault, styleBar | styleThick );
	 Plot( HigherHighs, "Highs", colorDarkGreen, styleDashed | styleNoRescale );
	 Plot( LowerLows, "Lows", colorBrown, styleDashed | styleNoRescale );
	 
	 PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L, -30 ); //Long Entry
	 PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), colorBrown, 0, H, -15 ); //Long Exit
	 
	 PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, 0, H, -30 ); //Short Entry
	 PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorDarkGreen, 0, L, -15 ); //Short Exit
	 
	 Plot( TSL, "TSL", colorPink, styleThick | styleNoRescale ); //Trailing Stop-Loss
_SECTION_END();
10 Likes

Part II

4. Trade Execution Prices: This bit completely depends on the strategy. If the strategy demands to enter at Close price, so be it. If the strategy asks to exit at next bar open, so be it. We will now apply custom-based Execution prices explicitly using a for-loop on the demo strategy that we are discussing.

_SECTION_BEGIN( "Trade Execution Prices" );
	 tn = TimeNum();

	 MultATR = Param( "ATR Multiplier", 2, 1, 4, 0.5 );
	 PerATR = Param( "ATR Period", 14, 3, 89, 1 );
	 _ATR = ATR( PerATR );
	 
	 //A Simple Demo strategy
	 Per = Param( "Swing Periods", 15, 3, 144, 1 );
	 HigherHighs = HHV( H, Per );
	 LowerLows = LLV( L, Per );
	 _Buy = Cross( H, Ref( HigherHighs, -1 ) );
	 _Short = Cross( Ref( LowerLows, -1 ), L );
	 
	 //Array Initialization
	 Buy = Sell = Short = Cover = TSL = Null;
	 LongFlag = ShortFlag = 0; //Simple flags
	 
	 //Using Loop to generate signals
	 for( i = 0; i < BarCount; i++ )
	 {
		 //Long Positions
		 if( _Buy[ i ] AND LongFlag == 0 AND tn[ i ] < 144500 )
		 {
			 Buy[ i ] = 1;
			 LongFlag = 1; //To record that we are in Long position
			 BuyPrice[ i ] = HigherHighs[ i ];
		 }		 
		 if( LongFlag )
		 {
			 if( TSL[ i - 1 ] > H[ i ] - _ATR[ i ] * MultATR )
				 TSL[ i ] = TSL[ i - 1 ];
			 else
				 TSL[ i ] = H[ i ] - _ATR[ i ] * MultATR;
		 }
		 
		 if( ( _Short[ i ] OR L[ i ] < TSL[ i - 1 ] OR tn[ i ] >= 144500 ) AND LongFlag == 1 )
		 {
			 Sell[ i ] = 1; //Selling-off the Long position
			 LongFlag = 0;  //Reseting LongFlag back to False, to denote that we are no longer in "Long" position
			 
			 if( _Short[ i ] )
				 SellPrice[ i ] = LowerLows[ i ];
			 else if( L[ i ] < TSL[ i - 1 ] )
				 SellPrice[ i ] = TSL[ i - 1 ];
			 else if( tn[ i ] >= 144500 )
				 SellPrice[ i ] = O[ i ];
		 }
		 
		 if( Buy[ i ] ) PlotText( "Buy @" + NumToStr( BuyPrice[ i ], 1.2 ), i, L[ i ] - _ATR[ i ], colorBrightGreen, colorBlack );
		 if( Sell[ i ] ) PlotText( "Sell @" + NumToStr( SellPrice[ i ], 1.2 ), i, H[ i ] + _ATR[ i ], colorBrown, colorBlack );
		 
		 //Short Positions
		 if( _Short[ i ] AND ShortFlag == 0 AND tn[ i ] < 144500 )
		 {
			 Short[ i ] = 1;
			 ShortFlag = 1;	 //To record that we are in Short position
			 ShortPrice[ i ] = LowerLows[ i ];
		 }
		 if( ShortFlag )
		 {
			 if( TSL[ i - 1 ] < L[ i ] + _ATR[ i ] * MultATR )
				 TSL[ i ] = TSL[ i - 1 ];
			 else
				 TSL[ i ] = L[ i ] + _ATR[ i ] * MultATR;
		 }
		 
		 if( ( _Buy[ i ] OR H[ i ] > TSL[ i - 1 ] OR tn[ i ] >= 144500 ) AND ShortFlag == 1 )
		 {
			 Cover[ i ] = 1; //Covering the Short position
			 ShortFlag = 0;  //Reseting ShortFlag back to False, to denote that we are no longer in "Short" position
			 
			 if( Cover[ i ] )
				 CoverPrice[ i ] = HigherHighs[ i ];
			 else if( H[ i ] > TSL[ i - 1 ] )
				 CoverPrice[ i ] = TSL[ i - 1 ];
			 else if( tn[ i ] >= 144500 )
				 CoverPrice[ i ] = O[ i ];
		 }
		 
		 if( Short[ i ] ) PlotText( "Short @" + NumToStr( ShortPrice[ i ], 1.2 ), i, H[ i ] + _ATR[ i ], colorRed, colorBlack );
		 if( Cover[ i ] ) PlotText( "Cover @" + NumToStr( CoverPrice[ i ], 1.2 ), i, L[ i ] - _ATR[ i ], colorDarkGreen, colorBlack );
	 }
	 
	 //Plotting
	 Plot( C, "", colorDefault, styleBar | styleThick );
	 Plot( HigherHighs, "Highs", colorDarkGreen, styleDashed | styleNoRescale | styleNoLabel );
	 Plot( LowerLows, "Lows", colorBrown, styleDashed | styleNoRescale | styleNoLabel );
	 
	 PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, L - _ATR ); //Long Entry
	 PlotShapes( IIf( Sell, shapeSmallDownTriangle, shapeNone ), colorBrown, 0, H + _ATR, -20 ); //Long Exit
	 
	 PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, 0, H + _ATR, -20 ); //Short Entry
	 PlotShapes( IIf( Cover, shapeSmallUpTriangle, shapeNone ), colorDarkGreen, 0, L - _ATR ); //Short Exit
	 
	 Plot( TSL, "TSL", colorPink, styleThick | styleNoRescale ); //Trailing Stop-Loss
_SECTION_END();

In similar manner we could also define other requirements like exiting trades at a Target price, etc.

5. Re-painting Signals: What if a signal gets triggered at any given moment during a candle formation and disappears by the time the candle closes? The solution is to shift signals one candle and use Open of a bar for Execution (either from the Backtester Settings or as explained in the previous code). Instead of using the previous demo strategy, this time we will use a simple MA crossover strategy. Use the Bar-Replay Tool and play with shifting / non-shifting of the signals in the code to see how they differ.

_SECTION_BEGIN( "Re-Painting Signals" );
	 FastPer = Param( "Fast Period", 13, 3, 144, 1 );
	 SlowPer = Param( "Slow Period", 34, 3, 144, 1 );
	 
	 //FastMA = EMA( C, FastPer );
	 //SlowMA = EMA( C, SlowPer );
	 
	 FastMA = Ref( EMA( C, FastPer ), -1 );
	 SlowMA = Ref( EMA( C, SlowPer ), -1 );
	 
	 Buy = Cross( FastMA, SlowMA );
	 Sell = Cross( SlowMA, FastMA );	 
	 Short = Sell;
	 Cover = Buy;
	 
	 //Plotting
	 Plot( C, "", colorDefault, styleBar | styleThick );
	 
	 PlotShapes( Buy * shapeUpArrow, colorBrightGreen, 0, L, -30 ); //Long Entry
	 PlotShapes( Sell * shapeSmallDownTriangle, colorBrown, 0, H, -15 ); //Long Exit
	 
	 PlotShapes( Short * shapeDownArrow, colorRed, 0, H, -15 ); //Short Entry
	 PlotShapes( Cover * shapeSmallUpTriangle, colorDarkGreen, 0, L, -15 ); //Short Exit
_SECTION_END();

Thank you for reading!
Amen!

2 Likes

I am Modified my Code for debugging through exploration. Here is my modified code as you had suggested.

res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
tsl=IIf(avn==1,sup,res);
Buy=Cover=Cross(C,tsl) ;
Sell=Short=Cross(tsl,C) ;

Plot(tsl, _DEFAULT_NAME(), tsl_col, styleStaircase);

Filter = 1; // show all bars 
AddColumn( C, "Close" );
AddColumn( Buy, "Buy" );
AddColumn( Sell, "Sell" );
AddColumn( res, "res" );
AddColumn( sup, "sup" );
AddColumn( avd, "avd" );
AddColumn( avn, "avn" );
AddColumn( tsl, "tsl" );

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);

Filter = 1; // show all bars 
AddColumn( ExRem(Buy,Sell), "ExremBuy" );
AddColumn( ExRem(Sell,Buy), "ExremSell" );

Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);

BuyPrice=

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,Low,-15);  
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorYellow, 0,High,-15);  
PlotShapes(IIf(Cover, shapeHollowCircle, shapeNone),colorWhite, 0,Close,0);  
PlotShapes(IIf(Short, shapeHollowCircle, shapeNone),colorYellow, 0,Close,0); 



SetPositionSize(300,spsShares);
ApplyStop(0,1,10,1);
//-----------end--------------
Long=Flip(Buy,Sell); 
Shrt=Flip(Sell,Buy); 

BuyPrice=ValueWhen(Buy,C);
SellPrice=ValueWhen(Sell,C);

Filter = 1; // show all bars 
AddColumn( BuyPrice, "BuyPrice" );
AddColumn( SellPrice, "SellPrice" );
AddColumn( Long, "Long" );
AddColumn( Shrt, "Shrt" );


if (LastValue(Buy) == True )
{
tradeType = "BUY";
q1 = q9 = (AT_QUANTITY * 2.17/100);
q2 = q8 = (AT_QUANTITY * 4.35/100);
q3 = q7 = (AT_QUANTITY * 8.70/100);
q4 = q6 = (AT_QUANTITY * 17.39/100);
q5 = (AT_QUANTITY * 34.78/100);
target = ((((BuyPrice-tsl)+BuyPrice)-BuyPrice)*3);
stoploss = (BuyPrice-tsl);
trailsl = IIf(stoploss&gt;1,(stoploss/0.05),20);
increment = target/9;
//_TRACE("Which Symbol = " + AT_SYMBOL);
//_TRACE("BuyPrice = " + BuyPrice);
//_TRACE("What is tsl = " + tsl);
//_TRACE("What is Target? = " + target);
//_TRACE("What is Stop Loss? = " + stoploss);
//_TRACE("What is Trailing Stop Loss? = " + trailsl);
//_TRACE("What is Increment? = " + increment);

placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q1, BuyPrice, target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q2, (BuyPrice+increment), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q3, (BuyPrice+(2*increment)), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q4, (BuyPrice+(3*increment)), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q5, (BuyPrice+(4*increment)), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q6, (BuyPrice+(5*increment)), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q7, (BuyPrice+(6*increment)), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q8, (BuyPrice+(7*increment)), target, stoploss, trailsl, 1);
placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, q9, (BuyPrice+(8*increment)), target, stoploss, trailsl, 1);
}

I have also attached screen shot of my exploration table. here I can see that before exrem when Buy signal is generated its value becomes '1' and ExremBuy also become 1. But only one Buyorder is getting sent to trading terminal. I want to send 9 Buy orders to trading terminal when 1 Buy signal is generated. Here is the Screenshot of the Exploration.

Regards

Exploration

I assume that your placeBracketOrder() function is supposed to be sending orders to the trading terminal. From your code it appears that if that function is called once then it will be called all 9 times, but have you confirmed that? Also, you are passing arrays (BuyPrice, target, etc.) into that function as arguments. Does the function handle that correctly?

placeBracketOrder() command stores the Buy orders in a csv file. From that csv file the orders are getting directed to trading terminal.

In my case only one placeBracketOrder() gets stored in csv file. Rest 8 orders are not getting stored in csv file and are getting rejected because of exrembuy function. This is what i think.

YEs the function is handling BuyPrice, target, etc. arguments correctly and one Buy order is getting executed in trading terminal. But not the rest eight orders.

I also ran the Backtest with detailed log. Here is the screenshot of the Backtest log.

Backtest

It seems more likely to me that placeBracketOrder() is not writing the CSV file correctly. For example, are you using the mode "a" (for append) instead of "w" (for write) when you're calling fopen()?

In any case, you should put some TRACE() statements inside placeBracketOrder() to confirm how many times it's being called. There's no way for anyone here to help you debug code that you have not shared.

Here is the screenshots of Trace() statements and what is being written in orders.csv file.

Trace

orderscsv

Correct me if I'm wrong, but that looks like a Sell order, not a Buy order.

Are you sure this code isn't preventing you from issuing any Buy commands via placeBracketOrder() at all?

That code probably makes sense for real-time trading, but not for backtesting because it's only looking at the final bar in the array.