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 Trailing Stop Loss Indicator

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

Use Structure?
If set to true, the script will use swing lows and highs in its calculation. If set to false, the script will ignore swing lows and highs and give you the distance of the ATR from the current candle close instead.

How Far To Look Back For High/Lows:
Candle lookback period for swing high/lows.

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


Pine Script Basics Course

Source Code

// @version=4
// Created by Matthew J. Slabosz
// www.zenandtheartoftrading.com
study("[ZEN] ATR Trailing Stop Indicator", "S/L", overlay=false)

// Get inputs
atrLength = input(title="ATR Length:", type=input.integer, defval=14, minval=1)
useStructure = input(title="Use Structure?", type=input.bool, defval=true)
lookback = input(title="How Far To Look Back For High/Lows:", type=input.integer, defval=7, minval=1)
atrstopmultiplier = input(title="ATR x ?", type=input.float, defval=1.0, minval=0.1)

// Calculate data
atr = atr(atrLength)
longStop = (useStructure ? lowest(low, lookback) : close) - atr * atrstopmultiplier
shortStop = (useStructure ? highest(high, lookback) : close) + atr * atrstopmultiplier

// Plot data
plot(atr, color=color.blue, title="ATR", transp=100)
plot(longStop, color=color.green, style=plot.style_circles, title="Long Trailing Stop", transp=100)
plot(shortStop, color=color.red, style=plot.style_circles, title="Short Trailing Stop", transp=100)

CHART COMPANION

// @version=4
// Created by Matthew J. Slabosz
// www.zenandtheartoftrading.com
study("[ZEN] ATR Trailing Stop Indicator [Chart]", "S/L+", overlay=true)

// Get inputs
atrLength = input(title="ATR Length:", type=input.integer, defval=14, minval=1)
useStructure = input(title="Use Structure?", type=input.bool, defval=true)
lookback = input(title="How Far To Look Back For High/Lows:", type=input.integer, defval=7, minval=1)
atrstopmultiplier = input(title="ATR x ?", type=input.float, defval=1.0, minval=0.1)

// Calculate data
atr = atr(atrLength)
longStop = (useStructure ? lowest(low, lookback) : close) - atr * atrstopmultiplier
shortStop = (useStructure ? highest(high, lookback) : close) + atr * atrstopmultiplier

// Plot data
plot(atr, color=color.blue, title="ATR", transp=100)
plot(longStop, color=color.green, style=plot.style_linebr, title="Long Trailing Stop", transp=0)
plot(shortStop, color=color.red, style=plot.style_linebr, title="Short Trailing Stop", transp=0)

Last Updated: 10th September, 2019


Free Premium Charts!

5 1 vote
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Edwin Goh
Edwin Goh
10 months ago

the ATR trailing stop indicator loads on my chart, but do you know how to set ATR trailing stop on my trade order

Tayfun Ata Delice
Tayfun Ata Delice
2 months ago

Hello Mathew, thanks for the scripts. I just pasted and saved it in pine script. But it flattens the chart like sma/chandliers…

How can I fix it?

Thank you.

Tayfun