ATR Trailing Stop loss not working as expected

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.

atrWeekly

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

You are using "volatile" option that works differently than your formula. You should disable "volatile" option and use the code from KB:

Hi Tomasz

Recently purchased the software and working superb smooth and as intended

let me tell you about me before jumping in to question !!! i am a no coder nor from any technical background .i am a medical professional ..

here is my query : is it possible to code the Trailing stoploss as " Percentage of profit". i did read few threads and info on KB ..i understand few bits of coding for Tailing stoploss using a function called APPLYStop .. but still i am not getting it !!

though u can direct me to the a correct thread or just let me know how to code it

thanks in Advance , please again iam telling i am not a coder , may be u can consider it as basic or small question .. but i will appreciate ur response

Thanks

Ravi

1 Like

Tomasz

any help or right direction will be helpful ..

thanks

1 Like

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