Because I was seeing MAE much greater than permitted by my trailing stop, I decided to test ApplyStops stopTypeTrailing to ensure that I truly understood how it works.
I performed three backtests as detailed below, and concluded that I definitely do not understand stopTypeTrailing.
Test 1:
In a backtest, I used Norgate Data, on the SPY symbol, using From-To dates of 12/13/2018 to 12/14/2018, I forced a Buy signal on 12/13, at the closing price of 265.37. The trailing stop was set using stopModePoint, using 4 points and to exit intraday at the stop price.
// Test of ApplyStop trailing stop
// Using SPY from 12/13/2018 to 12/14/2018
SetOption( "ActivateStopsImmediately", False );
SetOption( "AllowSameBarExit", False );
SetTradeDelays( 0, 0, 0, 0 );
BuyPrice = C;
SellPrice = C;
Buy = 1;
Sell = Short = Cover = 0;
ApplyStop( stopTypeTrailing, stopModePoint, 4, True, False );
On 12/14, the low of the day was 259.85, so the stop would definitely have been triggered. However, the sell price should have been $261.37, but AB used the open price of $262.96 which is only $2.41 below the entry. AB exited on the correct bar, but at an impossible price, because the stop had not yet been hit at the open.
Test 2:
Using the same setup as in Test 1, I used the ApplyStop stopTypeLoss, also with stopModePoint, for a 4 point loss. The code was the same, except for the stop:
ApplyStop( stopTypeLoss, stopModePoint, 4, True, False );
Unlike the trailing stop, stopTypeLoss exited at the expected price of $261.37.
Test 3:
Also using Norgate Data, on the SPY symbol, using From-To dates of 11/23/2018 to 11/28/2018, I forced a Buy signal on 11/28, at the closing price of $263.25. The trailing stop was set using stopModePoint, using 4 points and to exit intraday at the stop price.
// Test of ApplyStop trailing stop
// Using SPY from 11/23/2018 to 12/31/2018
SetOption( "ActivateStopsImmediately", False );
SetOption( "AllowSameBarExit", False );
SetTradeDelays( 0, 0, 0, 0 );
BuyPrice = C;
SellPrice = C;
Buy = 1;
Sell = Short = Cover = 0;
ApplyStop( stopTypeTrailing, stopModePoint, 4, True, False );
On 11/26, the high was the close at $274.58, which was also the highest high since entry. AB exited at the open of $269.60. I understand the reasoning for exiting on this bar (the low of $268.33 was more than 4 points below the highest high since entry) but using the opening price for the exit doesn't make sense to me because at the time of the open, it was the highest high.
Conclusion:
Based on the above tests, it seems that stopTypeTrailing uses the opening price instead of the intraday stop price. I haven't seen this documented in the function reference at https://www.amibroker.com/guide/afl/applystop.html, in the knowledge base, or in this forum.
Am I missing something here? Should stopTypeTrailing really be using the open price for exiting, when the stop was not actually hit until later in the day?