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).


Pine Script Basics Course
 

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


Free Premium Charts!

5 1 vote
Article Rating
Subscribe
Notify of
guest
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Edwin Goh
Edwin Goh
1 year 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
10 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

Mister
Mister
6 months ago

fucking circular bullshit – click for source takes to a page with click to download – which takes to click for source – which takes to click to download….is this just a poorly written clickbait attempt?

niko
niko
4 months ago

Hi Mathew,

Many thanks for the script. Really appreciate it.

I have one question tho, how do you trail the stop loss? From the chart it can be seen that the price has never touched the stop loss (gree/red line).

Once again thanks man

Matt
Reply to  niko
4 months ago

Hey Niko! The way I use it is once I’ve entered my position and it goes in my favour, I set my stoploss to the highest line. If I’m long and the line goes down for example, I don’t lower my stop loss. I only raise it.

Derek Hilton
Derek Hilton
Reply to  Matt
4 months ago

Found the answer I actually installed ATR+Stop Loss not Trailing Stop….

Last edited 4 months ago by Derek Hilton
casanova1012
casanova1012
3 months ago

your awesome, thanks for being such a value to us SERIOUS traders

Steve
Steve
25 days ago

Matthew, Thanks for the excellent script. It’s displaying the above/below price SL line & the ATR value in the table. However the table is not displaying the long SL & short SL text/values. Thanks,