ATR Exit

I want to play around with some trend following in the backtester with using an ATR for the stop loss: exit on open the following bar when the previous bar closes below xATR(y).

When I did a show bar with the following script I got 1 arrow at the start of the chart and then never again. Its not showing me every time the close crosses below.


Similarly in the backtester, you can choose an ATR Long Trailing Stop but I don't think its working correctly because I only get a few trades (ie with an entry of a close above a moving average over 10 years of daily data I get 43 trades....).

I have watched the 3 scripting courses and searched the knowledge base but can't find anything that explains how to set up these functions? Do I somehow have to link the stop loss with each entry trigger?

Hi Jane,

The ATRTSCAN is a special function that is used in the stop section of the back tester. I’m not 100% sure why, but it does not work in a script (something we’ll check out).

In the mean time, this is a script that will work for you.

a1 = ATR(BARS=15, MULT=2.00);

CLOSE() CrossesBelow (HIGH()[1] - a1[1])

we calculate the ATR and store it in “a1”. Then we see if today’s close crosses below yesterday’s value of high minus the atr by using the “[1]” offset.

Hope that helps


Thanks Mathew

Its not working in the backtester either -the built-in ATR Long Trailing Stop was what I tried first but it is not working correctly?

Is the [1] the same as using the offset function? I did wonder if that was why the ATR Long Trailing Stop was not functioning correctly but I didn't know if the offset was perhaps 'baked-in' to the ATRSCAN function already?



Hi Jane,

I’ve tested the script and it appears to be working ok here.

Where have you added the criteria, to the Stops section or to the Exit? As it’s a custom criteria it should be under the Exit section (with an OR separating it from other exit criteria), the Stops section works with preset options only.

Hi Matthew

I have it added as the exit criteria and I have no other exit criteria (so no OR required?).

Can I just check that the script is correct? I have CLOSE() <= ATRTSCAN(CALCOFF=HIGH) but I think I may have changed it from the low which was the default?

Either way, I just ran the back on a single code (BXB) and I only get 1 trade in 10 years. It seems to be taking the first entry in the test period of the last 10 years on 2/9/08 and then the exit is on the last bar of the chart (30/8/18). In fact I suspect this is only because I have 'close all positions' ticked in the backtester.

In the attached chart of BXB, there are 3 show bars overlaid on the chart as well as the strategy trades arrows. Green arrows are possible entries (criteria is just a close above 18 period WMA), red arrow is the ATR Long Trailing Stop script above - you get 3 red arrows at the beginning of the chart in 1985 and then nothing and then the pink arrows are Mathew's ATR exit script from this thread.

I have NO idea what I'm doing wrong?!! I've uploaded screenshots of the backtest as well as the workbook with the BXB chart...

LearntoScript.owb (39.7 KB)

Hi Jane,

Use this as the exit, not the ATRTSCAN function, and see how you go:

a1 = ATR(BARS=15, MULT=2.00);

CLOSE() CrossesBelow (HIGH()[1] - a1[1])

Hi Darren,

I have copied and pasted this exit criteria and it states (please enter a valid script to add to this criteria). I would like to add as exit criteria + ATR14 multi 3.

I have tried CLOSE + ATR14 MULTI3, it comes up as a valid script but does not work as required. I have tried many variables but as soon as I add the + ATR it

still states that it is a valid script but does not work, any help would be appreciated.

Hi Bill,

Can you please put in a screen shot of exactly what you have in the script editor. I think some characters did not come across in your post and it is a little hard to see what is going on.



Hi Mathew, all I would like to do is add an exit strategy on a back test ,

entry criteria + (when close, closes above ATR14*3).

Also on a chart with show bar with the entry one colour and the exit another colour.

entry could be (RSI14 crosses above 50) and (close > ma50)

Thank you.

Hi Bill,

The script is fine - I just typed it in and it works. I suspect that there is a hidden character in there somewhere. Usually this can happen if a part of the script has been pasted in. I know it is annoying, but can you please try creating a new script and typing that out again?

Another solution is to use Notepad as a go-between. Paste the script into Notepad, then copy it again. The reason for this is that Notepad strips out all hidden characters.

All the best


Hi Mathew,

I retyped the criteria and it was valid but it is not giving me what I am after.

I thought that it would be a simple code for (entry criteria + (when close, closes above ATR14*3).

CLOSE Crossabove ATR? I have attached the 2 out comes.

The ATR price on the first buy is .32. (buy@ 8.75 +.96=9.71) exit 9.71 about 8 bars after buy.