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.
Информация о релизе: Added consistent description.
Added an alert when price crosses the trailing ATR.
Информация о релизе: 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.
Информация о релизе: 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.
Информация о релизе: Adopted @Pinecoders pine coding conventions (definitely check those out if you are writing your own indicators).
Made all variable names unique to this script.
Replaced the trailing calculations with functions.
Removed global variables from functions.
Fixed the alert conditions to take the flip input into account.
Made the stop lines prettier.
Информация о релизе: Made the flipping logic more robust.
Информация о релизе: Adding the new resolution parameter to the study options as requested by Caesar.
This allows you to run the script on a different timeframe to the chart.
By default, it takes the same timeframe as the chart.
Скрипт с открытым кодом

В истинном духе TradingView автор этого скрипта опубликовал его с открытым исходным кодом, чтобы трейдеры могли понять, как он работает, и проверить на практике. Вы можете воспользоваться им бесплатно, но повторное использование этого кода в публикации регулируется Правилами поведения. Вы можете добавить этот скрипт в избранное и использовать его на графике.

Хотите использовать этот скрипт на графике?


Can you help me finishing up my pinescript indicator?
SimpleCryptoLife vickyakhan555
@vickyakhan555, Will DM you.
+1 Ответить
Hello, great indicator. I was wondering if you could add an alert for if daily close is higher than 0.75ATR from yesterday's close and also within the top 1/3 of the daily candles range?
@ChrisGDXB, Thank you. I might add a feature like that. Let's chat first so I can understand it.
Whoa, thanks for taking the time to code this so neatly and well-explained!
And yes, we need more cake!
@Bart_, Hahaha yes indeed we do. Thank you for the kind words, sir.
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.