SimpleCryptoLife

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.
הסר מסקריפטים מועדפים הוסף לסקריפטים מועדפים

תגובות

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.
השב
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
השב
בית סורק מניות סורק מט"ח סורק מטבעות-קריפטו יומן כלכלי איך זה עובד‏ תכונות גרף מחירון הפנה חבר חוקי הבית מרכז תמיכה כלים לאתרים וברוקרים יישומונים פתרונות גרפיים ספריית גרפים קלי משקל בלוג וחדשות טוויטר‏
פרופיל הגדרות פרופיל חשבון וחיוב הפנה חבר לשוניות התמיכה שלי מרכז תמיכה רעיונות שפורסמו עוקבים עוקב הודעות פרטיות צ'אט התנתק