Precedence of stop orders, was: Applystop Issue

I am trying to combine a profit applystop and a trailing applystop in the code below. I noticed an issue where maybe I've coded something incorrectly or I am not understanding how Amibroker processes the code.

In the example below using symbol PENN, I have a short signal on 1/13/22. I have trade delays set to 1 and so the short entry is processed on 1/14/22 at the Open ($46.06). The ATR based profit target is $42.70 (O - 1ATR) and the trailing stop is $45.30 (L of 1/14 + 0.5ATR). I have the trailstop ExitAtStop set to 2 (check High-Low prices but exit NEXT BAR on regular trade price). My understanding is that since the 1/14 high was $46.06, isn't the trailing stop triggered and the stop then executed at next bar regular trade price (Open, 1/18 $43.81)? The backtest results instead show no trailing stop triggered and the trade is held on the next bar when the profit stop is hit ($42.70).

Any help would be much appreciated.

BuyPrice = SellPrice = ShortPrice = CoverPrice = O; 
TD = 1; 
SetTradeDelays(TD, TD, TD, TD);

SetOption("InitialEquity", 100000);
SetOption("AccountMargin", 100/Scenario); 
SetOption ("MaxOpenPositions", PosNum);
SetOption ("MaxOpenLong", PosNum/Scenario);
SetOption ("MaxOpenShort", PosNum/Scenario);
SetOption("SeparateLongShortRank", True );
SetOption("AllowPositionShrinking", False); 
Setoption("AllowSameBarExit", True); 
SetOption("SettlementDelay", 1); 
SetOption("ActivateStopsImmediately", True);

buytrigger code...
shortrigger code...

Buy = 	buytrigger 
		AND (NorgateIndexConstituentTimeSeries("$SPX") OR NorgateIndexConstituentTimeSeries("$NDX"));
Sell = 	0; 

Short = shorttrigger
		AND (NorgateIndexConstituentTimeSeries("$SPX") OR NorgateIndexConstituentTimeSeries("$NDX"));
Cover = 0; 

ATRperiod = 63; 
ProfitATR = 1; 
StopATR = 0.5;
ApplyStop(stopTypeProfit, stopModePoint, ATR(ATRperiod)*ProfitATR, 1, False, 0);
ApplyStop(stopTypeTrailing, stopModePoint, ATR(ATRperiod)*StopATR, 2, False, 0);


I think the issue is that the profit stop is prioritized above the trailing stop. So even though my trailstop is triggered on prior bar (1/14), Amibroker evaluates the profit stop first on (1/18) and executes that first.

Is there a way for me to tell Amibroker which stop to prioritize?

Yes, the function is called SetStopPrecedence().


Well, I found a post that I thought had the solution. I tried this:
SetStopPrecedence( stopTypeNBar, stopTypeLoss, stopTypeTrailing, stopTypeProfit );

Even with this code after my ApplyStops the backtester is still executing the profit stop on 1/18 at $42.70. If I remove the profit stop from my code, the backtester exits the the trade on 1/18 at the the Open price of $43.81, which is what I expected.

Still not sure what I'm doing wrong.

Ahh... I had another Applystop further down in code. Once I fixed that and moved SetStopPrecedence() below all the Applystops the problem was fixed.

Thanks for responding @mradtke

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.