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);