Scale Out properly if first target gets hit at the first candle itself

Hi,

I am currently writing a strategy where 50% of the position will be closed when the price hits the first target and remaining position at the 2nd target. I was able to code this, but the problem is, in some scenarios in the same candle it is getting entry and as well as hitting the first target. How to handle this kind of scenario. If buy condition is satisfied then Buy array will have 1 assigned, but when at the same data point if it hits the first target then we are supposed to assign sigScaleOut to the Buy array and that will erase 1 from Buy array. Due to this, the trade is getting ignored completely during backtesting, How to handle this scenario? Please help? Below is the code.

``````Buy = Sell = Short = Cover = BuyPrice = ShortPrice = SellPrice = CoverPrice = Null;

LONG_TARGET = LONG_STOPLOSS = SHORT_TARGET = SHORT_STOPLOSS = Null;

bflag  = 0;
sflag = 0;

TSL_LONG = Null;
TSL_SHORT = 999999;

for( i = 4; i < BarCount; i++ )
{
if( BUY0[i] AND bflag == 0 AND sflag == 0)
{
longexit = shortexit = 0;

bflag = 1;

{
} else {
}

}
if( bflag )
{

}
else
/*{

if(H[i] >= first_target) {
TSL_LONG[i] = Max( TSL_LONG[i - 1], Long_Trade_Stoploss + (Long_Trade_Risk - TickSize) + (H[i] - first_target) );
} else {
TSL_LONG[i] = Max( TSL_LONG[i - 1], Long_Trade_Stoploss );
}

}*/
if(H[i] >= first_target) {
TSL_LONG[i] = Max( TSL_LONG[i - 1], Long_Trade_Stoploss + (Long_Trade_Risk - TickSize) + (H[i] - first_target) );
} else {
TSL_LONG[i] = Max( TSL_LONG[i - 1], Long_Trade_Stoploss );
}

//SACLEOUT on acheive of first Target..
if(longexit == 0 AND H[i] >= first_target) {
longexit = 1;
}

}
if( ( 	tn[i-1] == TradeEndTime  OR L[i] <= Long_Trade_Stoploss OR L[i] <= TSL_LONG[i - 1]  OR H[i] >= Long_Trade_Target) AND
bflag == 1 )
{

longexit = 3;

bflag = 0;sflag=0;
SellPrice[i] = C[i];

} else if(L[i] <= TSL_LONG[i - 1] ) {
SellPrice[i] = TSL_LONG[i - 1];
} else if(tn[i-1] == TradeEndTime) {
SellPrice[i] = O[i];
} else if(H[i] >= Long_Trade_Target ) {
longexit = 2;
}

Sell[i] = longexit + 1;

longexit = 0;

//if(SHORT0[i]) {
//	i--;
//	continue;
//}

}

//SHORT CONDITION
if( SHORT0[i] AND sflag == 0 AND bflag == 0 )
{
longexit = shortexit = 0;

Short[i] = 1;
sflag = 1;

if(SHORT1[i])
{
ShortPrice[i] = FirstRangeWithBufferLo[i];
} else {
ShortPrice[i] = SecondRangeWithBufferLo[i];
}

}
if( sflag )
{
if( Short[i] ) {
}
//else {

//	if(L[i] <= first_target) {
//		TSL_SHORT[i] = Min( TSL_SHORT[i - 1], Short_Trade_Stoploss - (Short_Trade_Risk + TickSize) - (first_target - L[i]));
//	} else {
//		TSL_SHORT[i] = Min( TSL_SHORT[i - 1], Short_Trade_Stoploss);
//	}

//}
if(L[i] <= first_target) {
TSL_SHORT[i] = Min( TSL_SHORT[i - 1], Short_Trade_Stoploss - (Short_Trade_Risk + TickSize) - (first_target - L[i]));
} else {
TSL_SHORT[i] = Min( TSL_SHORT[i - 1], Short_Trade_Stoploss);
}
//SACLEOUT on acheive of first Target..
if(shortexit == 0 AND L[i] <= first_target) {
shortexit = 1;
Short[i] = sigScaleOut;
ShortPrice[i] = first_target;
}
}
if( ( tn[i-1] == TradeEndTime  OR H[i] >= Short_Trade_Stoploss OR H[i] >= TSL_SHORT[i-1] OR L[i] <= Short_Trade_Target) AND
sflag == 1 )
{
shortexit = 3;

bflag = 0;sflag=0;
CoverPrice[i] = C[i];

} else if(H[i] >= TSL_SHORT[i-1]) {
CoverPrice[i] = TSL_SHORT[i-1];
} else if(tn[i-1] == TradeEndTime) {
CoverPrice[i] = O[i];
} else if(L[i] <= Short_Trade_Target ) {
shortexit = 2;
}

Cover[i] = shortexit + 1;
shortexit = 0;

//	i--;
//	continue;
//}

}

}
TSL_SHORT = IIf( TSL_SHORT == 999999, Null, TSL_SHORT );

SetPositionSize(LOTSIZE, spsShares);
// scale out 50% of position when first trget is acheived...
SetPositionSize( 50, spsPercentOfPosition * ( Short == sigScaleOut OR Buy == sigScaleOut ) );

Plot( TSL_LONG, "TSL Buy", colorBlue, styleDashed  );
Plot( TSL_SHORT, "TSL Short", colorBlue, styleDashed  );
``````

Thanks,
Vinay

Here are some of the examples where price hits to both first and second targets in the first candle itself. During that scenario Buy array will have a value other than 1, How to handle such situations when scaling out? In the first image, the first candle hit both the targets. Second Image, first candle its first target and came down.

Scaling out properly when first candle hits first targets or both the targets is challenging. Is it even possible to solve this problem in Amibroker? Or do we need to write low level CBT to handle such situations? if so can you please point to some examples? @fxshrat @Tomasz

Thanks,
Vinay