Site iconZen & The Art of Trading

ATR Trailing Stop Indicator




I rely on this script for both my live trading and my backtesting process. I couldn’t live without it. It’s extremely simple – all it does is calculate your trailing stop price.

How It Works

The first number in blue is the current ATR (pips). The second number in green is your trailing stop loss price for Long trades, and the third number in red is your trailing stop loss price for Short trades.

For short trades, the stop loss price is calculated by adding the current ATR value to the highest-high of the given lookback period.

For long trades, the stop loss price is calculated by subtracting the current ATR value from the lowest-low of the given lookback period.

Settings

ATR Length:
ATR period (how many candles to include in the calculation).

Bars To Look Back For Highs/Lows:
Candle lookback period for swing high/lows.

ATR Multiplier:
This controls your ATR multiplier. For example, if you want to use a 2x ATR stop, set this to 2.

Trailing Stop Type:
If set to High/Low, the script will use swing lows and highs in its calculation. If set to close, the script will ignore swing lows and highs and give you the distance of the ATR from the current candle close instead. And if set to open, will use the open price.

Show ATR?
This turns on the ATR display.

ATR Color:
You can change the ATR color with this setting.

ATR Location:
This sets where to place the ATR display (bottom right, bottom center, top right, top center).



Source Code

// ATR+ Stop Loss Indicator v2.0
// Created by Matthew J. Slabosz
// © ZenAndTheArtOfTrading / www.PineScriptMastery.com
// @version=4
study("ATR Trailing Stop Indicator [Chart]", "S/L+", overlay=true)

// Get ATR inputs
atrLength  = input(title="ATR Length", type=input.integer, defval=14, minval=1, group="ATR Settings")
lookback   = input(title="Bars To Look Back For Highs/Lows", type=input.integer, defval=7, minval=1, group="ATR Settings")
multiplier = input(title="ATR Multiplier", type=input.float, defval=1.0, minval=0.1, group="ATR Settings")
trailType  = input(title="Trailing Stop Type", type=input.string, defval="High/Low", options=["High/Low", "Close", "Open"], group="ATR Settings")
// Get display table inputs
showATR     = input(title="Show ATR?", type=input.bool, defval=true, group="Display Settings")
atrColor    = input(title="ATR Color", type=input.color, defval=color.new(color.blue, 100), group="Display Settings")
atrLocation = input(title="ATR Location", type=input.string, defval="BR", options=["TR", "TC", "BR", "BC"], group="Display Settings")

// Calculate data
atr = atr(atrLength)
longStop = (trailType == "High/Low" ? lowest(low, lookback) : trailType == "Close" ? close : open) - atr * multiplier
shortStop = (trailType == "High/Low" ? highest(high, lookback) : trailType == "Close" ? close : open) + atr * multiplier

// Plot data
plot(longStop, color=color.new(color.green, 0), style=plot.style_linebr, title="Long Trailing Stop")
plot(shortStop, color=color.new(color.red, 0), style=plot.style_linebr, title="Short Trailing Stop")

// Create an output table to display average pip change
var table resultTable = table.new(atrLocation == "BR" ? position.bottom_right : atrLocation == "TR" ? position.top_right : atrLocation == "TC" ? position.top_center : position.bottom_center, 1, 1, border_width=3)
f_fillCell(_table, _column, _row, _value, _text) =>
    _cellText = tostring(_value, "#.#####") + "\n" + _text
    table.cell(_table, _column, _row, _cellText, bgcolor=atrColor, text_color=color.new(atrColor, 0))

// Only fill the cell data on the last bar on the chart (for optimization)
if barstate.islast and showATR
    f_fillCell(resultTable, 0, 0, atr, "ATR")

Last Updated: 8th June, 2021


Exit mobile version