Indicator: Title Shorttitle Overlay Linktoseries Timeframe
Indicator: Title Shorttitle Overlay Linktoseries Timeframe
Indicator: Title Shorttitle Overlay Linktoseries Timeframe
// © SimpleCryptoLife
indicator(title='ATR + Trailing Stops', shorttitle='ATR Trail', overlay=true, linktoseries=true,
timeframe='')
// == Description ==
// Simple visualisation of Average True Range in Pinescript V4, displayed over the main chart. The script
has two modes: Running and Trailing.
// In Trailing mode, it displays the ATR line above or below the price until price crosses it. When price
crosses it, it flips from long to short or vice-versa.
// 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.
// == Usage ==
// Running ATR can be helpful for visualising volatility. Trailing ATR can be used as a stop loss.
// The different weightings for wicks and bodies might be interesting if you have overly spiky assets, but
you want to exit only on close
// Works on any market on any timeframe. Obviously, the lower the timeframe, the more chop.
// Use with market structure, support & resistance, and your other favourite indicators.
// == Settings ==
// ATR Lookback Period - The period over which the average is calculated. Higher = smoother; lower =
more reactive.
// ATR Multiplier - ATR is usually used with a multiplier of 2 or 3. I like 2.4 for BTC. Experiment for
your asset.
// Trail Mode - Whether the ATR is recalcualted every bar or allowed to trail.
// Flip Trail on - What counts as crossing the ATR - a close beyond, or a wick into it.
// The following settings are advanced and probably don't need to be touched for most applications.
// Percentage of Body to Include - To emulate the standard ATR, leave this and the wick percentage both
at 100. To give greater weight to candle bodies, increase this and decrease the wick percentage.
// Percentage of Wick to Include - To emulate the standard ATR, leave this and the body percentage both
at 100. To give greater weight to wicks, increase this and decrease the body percentage.
// Using wick-based ATR tends to keep it closer to price when there's a large move. Experiment for your
trading style.
// ATR Smoothing Mode - The built-in atr function uses RMA as a smoothing method. This gives you the
option to choose RMA, SMA, EMA, or WMA.
// == Alerts==
// One alert is preconfigured for price crossing the trailing ATR. You can set this to trigger Once, or Once
per bar close, in the
// TradingView alert configuration screen. You can use this for soft stops or to look for entries.
// == No Repainting ==
// This indicator does not repaint, meaning, it doesn’t go back and alter any bars in the past.
// However, until a candle closes, the indicator can only use the current price, and so the drawings,
colours and warnings for the current candle
// (including any lines that join the current candle to the previous one) will change as the price changes.
Once the candle closes, they are fixed.
// == Credits ==
// Original concept for plotting running ATR on a chart from a script by Bluephantom, modified by
RickSanchex
// Completely rewritten and extended by @SimpleCryptoLife to include trailing option and made
compatible with PineScript v4
// The function for applying different moving averages based on user input is from the TradingView ATR
template
// == Disclaimer ==
// You retain full responsibility for your trading at all times. Before trading with actual money, first make
sure your risk management is professional-level.
// Need to declare these variables here, in the global scope, so we can use them in other functions later
var float trailAtrLong = atrLow
var float trailAtrShort = atrHigh
// Trail the high (short) stop down and the low (long) stop up
trailAtrLong := f_trail(atrLow, trailAtrLong, 'up')
trailAtrShort := f_trail(atrHigh, trailAtrShort, 'down')
// // DEBUG
// plot(atrTrailMode == "Trailing" ? trailAtrLong : na, "Trailing ATR Long Stop Highlight",
color=color.teal, style=plot.style_linebr, linewidth=1, transp=0)
// plot(atrTrailMode == "Trailing" ? trailAtrShort : na, "Trailing ATR Short Stop Highlight",
color=color.red, style=plot.style_linebr, linewidth=1, transp=0)
// Get the plots for the trails, to show only long stop when long and short stop when short.
[trailAtrLongTemp, trailAtrShortTemp, trailAtrLongPlot, trailAtrShortPlot] = f_flip(atrFlipInput,
trailAtrLong, trailAtrShort, atrLow, atrHigh)
// Put these variables back in the global scope so we can persist them and use them as inputs to the
function next bar.
trailAtrLong := trailAtrLongTemp
trailAtrShort := trailAtrShortTemp
// If we are in Running mode, plot the price plus and minus the ATR
plot(atrTrailMode == 'Running' ? atrLow : na, 'Running ATR Low', color=color.new(color.white, 60),
linewidth=1)
plot(atrTrailMode == 'Running' ? atrHigh : na, 'Running ATR High', color=color.new(color.white, 60),
linewidth=1)
// Alert for crossing the trailing ATR. You can set this to trigger Once, or Once per bar close, in the
// TradingView alert configuration screen. You can use this for soft stops or to look for entries.
// ====================================================== //
// //
// (╯°□°)╯︵ ˙ƃᴉq ooʇ sᴉ ǝʞɐɔ oʇ ǝldoǝd ɟo oᴉʇɐɹ ǝɥ┴ //
// //
// ====================================================== //