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;

Long_Trade_Entry = Long_Trade_Stoploss = Long_Trade_Target = 0;
Short_Trade_Entry = Short_Trade_Stoploss = Short_Trade_Target = 0;

bflag  = 0;
sflag = 0;

TSL_LONG = Null;
TSL_SHORT = 999999;



for( i = 4; i < BarCount; i++ )
{
	//BUY CONDITION
    if( BUY0[i] AND bflag == 0 AND sflag == 0)
    {
		longexit = shortexit = 0;
		
		//Here is where we are getting into the buy trade.
        Buy[i] = 1;
        bflag = 1;
        
        if(BUY1[i])
        {
			BuyPrice[i] = FirstRangeWithBufferHi[i];
			Long_Trade_Stoploss = FirstRangeWithBufferLo[i];
		} else {
			BuyPrice[i] = SecondRangeWithBufferHi[i];
			Long_Trade_Stoploss = SecondRangeWithBufferLo[i];
		}
        Long_Trade_Entry = BuyPrice[i];
		Long_Trade_Risk = abs(Long_Trade_Entry - Long_Trade_Stoploss);
		
        Long_Trade_Target = Long_Trade_Entry + (Long_Trade_Risk * TARGET_TIMES);
        
        LONG_TARGET [i] = Long_Trade_Target;
        LONG_STOPLOSS[i] = Long_Trade_Stoploss;
		
		plotline( colorGreen, Long_Trade_Entry + (Long_Trade_Risk * 1), i, 1 );
		plotline( colorGreen, Long_Trade_Entry + (Long_Trade_Risk * 2), i, 1 );
    }
    if( bflag )
    {
		first_target=Long_Trade_Entry + (Long_Trade_Risk * 1);
		
        // Trailing stoploss for buy trades
        if( Buy[i] ) {
			TSL_LONG[i] = Long_Trade_Stoploss; //Max( TSL_LONG[i - 1], Long_Trade_Stoploss );
		}
        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;
			Buy[i] = sigScaleOut;
			BuyPrice[i] = first_target;
		}
       
    }
    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];
		
		if(L[i] <= Long_Trade_Stoploss) {
			SellPrice[i] = Long_Trade_Stoploss;
		} 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;
			SellPrice[i] = Long_Trade_Target;
		}
		
		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];
			Short_Trade_Stoploss = FirstRangeWithBufferHi[i];
        } else {
			ShortPrice[i] = SecondRangeWithBufferLo[i];
			Short_Trade_Stoploss = SecondRangeWithBufferHi[i];
        }
       
        Short_Trade_Entry = ShortPrice[i];
        
        Short_Trade_Risk =  abs(Short_Trade_Entry - Short_Trade_Stoploss);
       
        Short_Trade_Target = Short_Trade_Entry - (Short_Trade_Risk * TARGET_TIMES);
        
        SHORT_TARGET[i] = Short_Trade_Target;
        SHORT_STOPLOSS[i] = Short_Trade_Stoploss;
		
		plotline( colorGreen, Short_Trade_Entry - (Short_Trade_Risk * 1), i, 1 );
		plotline( colorGreen, Short_Trade_Entry - (Short_Trade_Risk * 2), i, 1 );
    }
	if( sflag )
    {
		first_target=Short_Trade_Entry - (Short_Trade_Risk * 1);
        if( Short[i] ) {
			TSL_SHORT[i] = Short_Trade_Stoploss; //Min( TSL_SHORT[i - 1], Short_Trade_Stoploss);
		} 
		//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];
        
		if(H[i] >= Short_Trade_Stoploss) {
			CoverPrice[i] = Short_Trade_Stoploss;
		} 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;
			CoverPrice[i] = Short_Trade_Target;
		}
		
		Cover[i] = shortexit + 1;
		shortexit = 0;
		
		//if(BUY0[i]) {
		//	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.

image

image

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