HomePine Script Snippets › ATR Stop Loss

Pine Script ATR Stop Loss

Sets your stop a multiple of ATR from the entry price so stop distance adapts to current volatility instead of a fixed tick count.

//@version=6
strategy("ATR Stop Loss", overlay=true, calc_on_every_tick=false)

// ── Inputs ──────────────────────────────────────────────
atrLen  = input.int(14, "ATR Length", minval=1)
atrMult = input.float(1.5, "ATR Multiplier", step=0.1, minval=0.1)

// ── ATR ──────────────────────────────────────────────────
atr = ta.atr(atrLen)

// ── Entry logic (replace with your own conditions) ───────
longCondition  = ta.crossover(ta.ema(close, 9), ta.ema(close, 21))
shortCondition = ta.crossunder(ta.ema(close, 9), ta.ema(close, 21))

if longCondition
    strategy.entry("Long", strategy.long)

if shortCondition
    strategy.entry("Short", strategy.short)

// ── ATR stop, anchored to average entry price ────────────
if strategy.position_size > 0
    strategy.exit("Long Exit", "Long", stop=strategy.position_avg_price - atr * atrMult)

if strategy.position_size < 0
    strategy.exit("Short Exit", "Short", stop=strategy.position_avg_price + atr * atrMult)

// Visual: plot the active stop level
plot(strategy.position_size > 0 ? strategy.position_avg_price - atr * atrMult :
     strategy.position_size < 0 ? strategy.position_avg_price + atr * atrMult : na,
     "ATR Stop", color=color.new(color.red, 0), style=plot.style_linebr)

Settings

How to use

Frequently Asked Questions

How do you calculate a stop loss with ATR?

Multiply the current ATR by a fixed factor and subtract it from your entry price for longs (add for shorts). With ATR(14) at 10 points and a 1.5 multiplier, a long entered at 5000 gets a stop at 4985. The stop widens in volatile conditions and tightens in quiet ones, keeping risk proportional to what the market is actually doing.

What ATR multiplier should I use for futures?

A 1.5x multiplier is a reasonable default for intraday index futures like MES and MNQ on 5-15 minute charts. Below 1x you get stopped out by normal noise; above 2.5x the dollar risk per trade usually gets too large for prop firm drawdown limits. Backtest 1.0-2.5 in 0.25 steps and judge by max drawdown, not just win rate.

Related

Want a complete eval-ready strategy with ATR stops built in?

This snippet is a building block. Our paid scripts are complete strategies — entries, ATR-based stops, kill switch, session filter, and EOD flatten pre-wired and tested on 3 years of MES/NQ data. Instant delivery from $50.

View Plans — From $50