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.
Phát hành các Ghi chú: Added consistent description.
Added an alert when price crosses the trailing ATR.
Phát hành các Ghi chú: 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.
Phát hành các Ghi chú: 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.
Phát hành các Ghi chú: 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.
Phát hành các Ghi chú: Made the flipping logic more robust.
Gỡ bỏ khỏi Script Ưa thích Thêm vào Script Ưa thích

Bình luận

Whoa, thanks for taking the time to code this so neatly and well-explained!
And yes, we need more cake!
Phản hồi
@Bart_, Hahaha yes indeed we do. Thank you for the kind words, sir.
Phản hồi
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

Phản hồi
@alskdj, thanks for the feedback buddy; I'll take a look
Phản hồi
@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.
Phản hồi
@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.
Phản hồi
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
Phản hồi
@alskdj, Ah ok, interesting
Phản hồi
JoshuaMcGowan SimpleCryptoLife
@SimpleCryptoLife, excellent details/explanation.
Phản hồi
SimpleCryptoLife JoshuaMcGowan
@JoshuaMcGowan, thank you sir
Phản hồi
Trang chủ Bộ lọc cổ phiếu Bộ lọc Forex Bộ lọc Tiền điện tử Lịch kinh tế Cách thức hoạt động Tính năng Biểu đồ Trả phí Giới thiệu bạn Quy tắc Áp dụng Trung tâm Trợ giúp Giải pháp cho Website & Nhà môi giới Widget Giải pháp biểu đồ Thư viện Biểu đồ Lightweight Blog & Tin tức Twitter
Hồ sơ Tùy chỉnh Hồ sơ Tài khoản và Thanh toán Giới thiệu bạn Ticket Hỗ trợ của tôi Trung tâm Trợ giúp Các ý tưởng đã đăng Người theo dõi Đang theo dõi Tin nhắn riêng Trò chuyện Đăng xuất