I am trying to visualize ATR trailing stop on 5 minutes futures data.
But sometimes the visualization doesnot match the backtest. For example:
In number 1 I should be out earlier as Low was lower than stop-line.
In number 2 the exit does not correspond with the stop-line at all.
The code I am using:
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
SetTradeDelays(0,0,0,0);
SetPositionSize(1, spsShares);
tn = TimeNum();
dn = DateNum();
Buy = tn==090000;
BuyPrice = O;
Sell = dn != Ref(dn,1);
SellPrice = Close;
stopLoss=3*Ref(ATR(5),-1);
ApplyStop(stopTypeTrailing, stopModePoint, stopLoss, True, True );
Equity( 1, 0 );
InTrade = Flip( Buy, Sell );
SetOption("EveryBarNullCheck", True );
stopline = IIf( Ref(InTrade,-1), HighestSince( Buy, H-stopLoss), Null );
PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
Plot( stopline, "trailing stop line", colorRed );
Any hint why is not the ApplyStop function showing the same results like the plot function?
Thank you.