Market On Open exit higher priority than Profit Target exit later same day

How to prioritize market on open exit even if later that same day profit target limit order would have been true?

I trade long/short on daily bars and have three types of exits

  1. PreviousClose exit: if yesterday’s close is higher than 2 days ago close (for longs) then exit next day at open
  2. NBar exit: If it’s been 5 days, exit on the 6th day at open
  3. ProfitTarget exit: If a 5% profit target has been reached, exit same bar at limit order

I want to make exits #1 and #2 higher priority than exit #3. As an example, if I went long at $100, next day the stock closed up at $102, I would want to exit Market On Open the following day with my exit #1. The problem is sometimes after the open that stock keeps on going up to say $105, and my ProfitTarget exit kicks in instead, which is not feasible in real life, so I would want to prevent exit #3 in this scenario in a backtest…

// 1) PreviousClose exit 
// SellPrice = Open; 
// Sell = YesterdaysClose > TwoDaysAgoClose;

// 2) NBar exit
NBarExit = 5;
ApplyStop( stopTypeNBar, stopModeBars, NBarExit, ExitAtStop = 1 ); // N-bar exit; If ExitAtStop = 1 then N-bar stop has highest priority and it is evaluated before all other stops.

// 3) ProfitTarget exit
ProfitTargetNormal = 5;
ProfitTargetDisabled = 100;
LongProfit = IIf ( YesterdaysClose > TwoDaysAgoClose OR BarsSince( Buy ) > NBarExit, ProfitTargetDisabled, ProfitTargetNormal ); // If PreviousClose or NBars exit is true then do not want ProfitTarget exit to kick in, want to exit at Open
ShortProfit = IIf ( YesterdaysClose < TwoDaysAgoClose OR BarsSince( Short ) > NBarExit, ProfitTargetDisabled, ProfitTargetNormal ); // If PreviousClose or NBars exit is true then do not want ProfitTarget exit to kick in, want to exit at Open
ProfitTarget = IIf( Short, ShortProfit, LongProfit );
ApplyStop( stopTypeProfit, stopModePercent, ProfitTarget, ExitAtStop = 1 );

You need recent version and
http://www.amibroker.com/f?setstopprecedence

Thank you, Tomasz.
a) If I did
SetStopPrecedence( stopTypeNBar, stopTypeProfit );
in newer AB, how could I reference my non ApplyStop PreviousClose exit?
b) why doesn’t my current code
LongProfit = IIf ( YesterdaysClose > TwoDaysAgoClose OR BarsSince( Buy ) > NBarExit, ProfitTargetDisabled, ProfitTargetNormal ); // If PreviousClose or NBars exit is true then do not want ProfitTarget exit to kick in, want to exit at Open
work?

a) If I did
SetStopPrecedence( stopTypeNBar, stopTypeProfit );
in newer AB, how could I reference my non ApplyStop PreviousClose exit?
b) why doesn’t my current code
LongProfit = IIf ( YesterdaysClose > TwoDaysAgoClose OR BarsSince( Buy ) > NBarExit, ProfitTargetDisabled, ProfitTargetNormal ); // If PreviousClose or NBars exit is true then do not want ProfitTarget exit to kick in, want to exit at Open
work?