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.


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 Pine Script Mastery Course

Source Code

// ATR+ Stop Loss Indicator v2.0
// Created by Matthew J. Slabosz
// © ZenAndTheArtOfTrading /
// @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,, 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,, 0), style=plot.style_linebr, title="Long Trailing Stop")
plot(shortStop,, 0), style=plot.style_linebr, title="Short Trailing Stop")

// Create an output table to display average pip change
var table resultTable = == "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,, 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 2 votes
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
Edwin Goh
Edwin Goh
3 years ago

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

3 years ago

Hi Matthew, I’ve built a basic SPY crash warning indicator, but built it as a strategy for visualisation purposes and I’m happy with where/when it throws out a crash signal. But given it’s a strategy instead of study I thought I’d try optimise it for profit as well by using a trailing stop. I tried with an oversold RSI along with a look back but thought the ATR would work better (in theory it should) however when I applied this to my indicator it threw out the warning/Shorting signals that it is supposed to generate. I’ve played with ATR lookback… Read more »

Tayfun Ata Delice
Tayfun Ata Delice
3 years 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.


2 years 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?

2 years 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

Reply to  niko
2 years 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
2 years ago

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

Last edited 2 years ago by Derek Hilton
2 years ago

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

2 years 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,

2 years ago

hi, please add the click and the closing of the “Structure”

11 months ago

Hi, can you show how to use trail_price and trail_offset

2 months ago

hey Matt, what would you charge/cost to write a script/algo/code, Thanks Rob