ATR + Trailing Stops

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 an alert when price crosses the trailing ATR.
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.
The code is clearer but you should notice no other difference.
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.
This allows you to run the script on a different timeframe to the chart.
By default, it takes the same timeframe as the chart.
Left the debug for these in - you can uncomment it if you want to see where an alert would have fired on historical bars.
Script open-source
In pieno spirito TradingView, il creatore di questo script lo ha reso open-source, in modo che i trader possano esaminarlo e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricorda che la ripubblicazione del codice è soggetta al nostro Regolamento.
Per un accesso rapido a un grafico, aggiungi questo script ai tuoi preferiti: per saperne di più clicca qui.
💰 Trade with me: simplecrypto.life/trade-with-me/
🙋 Free help with Pinescript: simplecrypto.life/get-help-with-pinescript/
Declinazione di responsabilità
Script open-source
In pieno spirito TradingView, il creatore di questo script lo ha reso open-source, in modo che i trader possano esaminarlo e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricorda che la ripubblicazione del codice è soggetta al nostro Regolamento.
Per un accesso rapido a un grafico, aggiungi questo script ai tuoi preferiti: per saperne di più clicca qui.
💰 Trade with me: simplecrypto.life/trade-with-me/
🙋 Free help with Pinescript: simplecrypto.life/get-help-with-pinescript/