Hi I'm learning to program the ATR trailing stop loss and I have some questions.
1. I'm having trouble with the stop loss not always triggering when it hits the ATR line.
I've circled the areas in the screengrab where I thought the stop loss should have triggered according to what was programmed. I'm testing this on a Weekly chart of the XAO Index.
Is there something incorrect with my code?
2. The stop loss doesn't trigger immediately. It triggers on the following bar.
For example a stop loss is triggered at 13/03/2020 at 5834.8, when I expected it to trigger on the ATR line at 06/03/2020 at 6311.34
I've had a look at the forums and I've aimed to base my code and settings on Scenario 2 of this page AFL Function Reference - APPLYSTOP
Would anyone be able advise what I'm doing incorrectly?
My code is attached below.
Many thanks
SetOption("AllowSameBarExit",true);
SetTradeDelays(0,0,0,0);
SetOption("ActivateStopsImmediately", False );
// trade price set in the "Trades' tab in 'Settings'
BuySignal = C > MA(C, 150);
StopMultiplier =Param("Multiplier",7, 0.5, 1000, 0.5 );
StopATRrange =Param("ATR Range",20, 1, 1000, 1 );
stopLength = StopMultiplier * ATR(StopATRrange);
Buy = BuySignal;
Sell = 0;
ApplyStop(stopTypeTrailing, stopModePoint, stopLength, 1, True, 1);
// Plotting the Trailing Stop
Equity(1,0);
inTrade = Flip(Buy, Sell);
SetOption("EveryBarNullCheck", True);
stopline = IIf(inTrade, HighestSince(Buy, H - stopLength), Null);
Plot(stopLine, "ATR Stop Loss", colorLightBlue, styleLine);
PlotShapes(Buy*shapeUpArrow, colorGreen, 0, L);