Profit target scale-out, then the rest as a separate exit all on the same bar issue

I have a low level strategy with the following two exits. The first is a scale-out, the second is the remaining full exit.

			// Midway Profit Target -----------------------------------------------------------
			if (mp == 1  AND UsePrftTgt1 AND High[i] >= MarkHalfwayPrftTgt AND OkToScaleOut1 AND BarsInTrade > 1)
			{
				if ( PrftTgt1_PctToExit < 100 )
				{
					Buy[ i ] = sigScaleOut;
					BuyPrice[i] = Max(Open[i], MarkHalfwayPrftTgt);
					OkToScaleOut1 = 0;
					HitPT1 = 1;
				}
				else
				{
					Sell[i] = 1;
					SellPrice[i] = Max(Open[i], MarkHalfwayPrftTgt);

					mp = 0;
					MarkEntryPrc = 0;
					MarkEntryBN = 0;
					MarkEntryDN = 0;
					MarkEntryTN = 0;
					MarkExitATRstopPrc = 0;
					MarkLatestHHprftTgt = 0;
					MarkHalfwayPrftTgt = 0;
					BarsInTrade = 0;
				}
			}
		
			// Profit Target -----------------------------------------------------------
			if (mp == 1 AND UsePrftTgt2 AND High[i] >= MarkLatestHHprftTgt AND OkToScaleOut2 AND BarsInTrade > 1)
			{
				if ( PrftTgt2_PctToExit < 100 )
				{
					Buy[ i ] = sigScaleOut;
					BuyPrice[i] = Max(Open[i], MarkLatestHHprftTgt);
					HitPT2 = 1;
					OkToScaleOut2 = 0;
				}
				else
				{
					Sell[i] = 1;
					SellPrice[i] = Max(Open[i], MarkLatestHHprftTgt);

					mp = 0;
					MarkEntryPrc = 0;
					MarkEntryBN = 0;
					MarkEntryDN = 0;
					MarkEntryTN = 0;
					MarkExitATRstopPrc = 0;
					MarkLatestHHprftTgt = 0;
					MarkHalfwayPrftTgt = 0;
					BarsInTrade = 0;
				}
			}

The position sizing looks like this:

SetPositionSize( PositionSizeDollarsOrPctInput, spsValue ); 
		if (HitPT1) { SetPositionSize( PrftTgt1_PctToExit, spsPercentOfPosition * ( Buy == sigScaleOut ) ); }
		if (HitPT2) { SetPositionSize( PrftTgt2_PctToExit, spsPercentOfPosition * ( Buy == sigScaleOut ) ); }

The issue is that when both of these exits get hit on the same bar, it actually erroneously buys again on this exit bar. These exits work fine for all other bars if both exits do not hit on the same bar. How do I get a scale-out, and full exit on the same bar to work? Can AB handle multi-stage position sizing all from within one bar?

@vjsworld Since you seem to hit a BUY I would first look at your SetPositionSize (the only place you have shown us your Buy). I am unable to test your SetPositionSize code so debug that one (try an Explore to make sure your calculations are doing what you think they should be).

Next idea, what if you nest your loops such that your first decision is if you SELL the full position, and if not then your inner loop assesses the possible ScaleOut signal?

If your full SELL is coded such that your market position becomes zero, then I would think only one of the two conditions will be executed on bars where both are true (in theory the full position SELL signal will execute and the inner loop for scaling out never comes into play).

Regarding the nesting so that the full exit hits first.... That has actually been my work-around so far. I wrote it so if they both hit on the same bar, to fully exit at the second PT. This works programmatically, but in reality we would have exited a portion of the position at the first PT, and then the rest at the second PT. Not all of it at the second PT, so this "solution" is not quite good enough....

If you want that level of control over the order in which entries and exits are processed as well as the multiple prices at which you want them to be executed, you should probably write your own CBT.

Did anyone come up with the solution? I am facing the same issue. Any help would be greatly appreciated.