ATR + Trailing Stops

Simple visualisation of Average True Range in Pinescript V4.

The script has two modes: Running and Trailing.
In Running mode, it continuously displays the ATR above and below the price. Specifically, it displays the High and Low price plus and minus the ATR times a user-supplied multiplier. This can be helpful for visualising volatility .
In Trailing mode, it displays the same ATR line, except the line trails until price crosses it. When price crosses it, it flips from long to short or vice-versa.

You could use trailing ATR as a stop loss. Adjust the multiplier and lookback period for your asset and preference.
Dec 29
Note di rilascio: Added consistent description.
Added an alert when price crosses the trailing ATR.
Jan 15
Note di rilascio: Added better description of the settings.
Added the option to choose which moving average is used for smoothing.
Added options to weight the candle wicks vs the candle bodies in the ATR calculation. I haven't seen this anywhere else, but I think it's interesting, especially for spiky assets.
These new options are advanced and will probably not be touched by most users. If you do your own Pinescripting, though, the code for the calculation of wick ATR might be interesting. And the concept that you can apply different smoothing mechanisms is an important one for deconstructing "ATR" as a thing.
Jan 18
Note di rilascio: Bug fix. Rewrote the flipping logic so that we cannot be both long and short (after the initial period where we don't have an ATR flip yet).
The code is clearer but you should notice no other difference.
Rimuovi dagli script preferiti Aggiungi agli script preferiti


Firs, some good stuff :)
Awesome script, I like the option to flip on wick.
There is a "but" of course :) In some situations, script stops working and plots a long horizontal line.

Settings are 11 and 1.8

here is a closer look

BTW, if you compare the stop line to other ATR scripts using identical parameters, lines wont match. Your are a lot further away. I am not sure, is your correct and the other one wrong or the other way around.

Both use 19 and 3.6

@alskdj, thanks for the feedback buddy; I'll take a look
@alskdj, I can reproduce the problem. Seems the flip on wick isn't working properly, at least in the latest version. I'll take a look and hopefully I can fix it.
@alskdj, As for the difference between my script and other scripts, that's an interesting question. Because atr() is a built-in function in TradingView we tend to see it as like, a unit, but it's not, it's just a function. So there is no one correct ATR. I know that's not the answer you were looking for so let me explain.

There is a formula for the range of a candle, which is just the difference between High and Low. Then a slightly more complex one for True Range, which additionally accounts for inter-day gaps that you get not so much in crypto but in stocks. So True Range you can say is calculated correctly or incorrectly. Then you apply a moving average of some kind to get Average True Range. Classically, it's a Rolling Moving Average (RMA) of 14 periods. In the latest version of my script, you can choose other moving average calculations.

Finally, there is the trailing part. Mine trails very simply and intuitively. Looking at the screenshot you posted, I would have to guess that the green line there is using a different trailing calculation. See how it trails strongly up when price is falling? It's doing some extra calculations to adapt a bit more dynamically. I might look at doing something like that in the future, but then you're moving away from just ATR and into your own proprietary ideas about how trailing stops could be set.
alskdj SimpleCryptoLife
@SimpleCryptoLife, I think I understand where the difference comes from. You are using high/low price but other, I was looking at, used only the close price.

Green is (it has some other problems and he is working on it)

Purple and blue are your script
@alskdj, Ah ok, interesting
JoshuaMcGowan SimpleCryptoLife
@SimpleCryptoLife, excellent details/explanation.
SimpleCryptoLife JoshuaMcGowan
@JoshuaMcGowan, thank you sir
@alskdj, I think I've fixed the bug you found. If you remove the indicator from your chart, refresh the chart, and add it again to get the latest version, it should work. I tested it on the same time and settings from your example. Let me know if it works for you. Thanks again for helping me improve the indicator.
Home Screener azioni Screener forex Screener cripto Calendario economico Come funziona Caratteristiche Grafico Costi Invita un amico Regolamento Centro di supporto Soluzioni per broker & siti web Widget Librerie grafiche Libreria grafica leggera Blog & Novità Twitter
Profilo Impostazioni profilo Account e fatturazione Invita un amico I miei ticket di supporto Centro di supporto Idee pubblicate Follower Seguiti Messaggi Privati Chat Esci