Pine Script ATR Stop Loss
Sets the stop a multiple of ATR away from the entry price, so stop distance tracks current volatility instead of a fixed tick count.
Pine Script
//@version=6
strategy("Target Filled - ATR Stop Loss Example", overlay=true, calc_on_every_tick=false)
// --- Inputs ---------------------------------------------------
lenATR = input.int(14, "ATR Length", minval=1)
multATR = input.float(1.5, "Stop Multiple", minval=0.1, step=0.1,
tooltip="Stop distance = ATR x this multiple")
// --- Volatility measure -----------------------------------------
atrValue = ta.atr(lenATR)
// --- Sample signal — replace with your own entry logic ---------
fastMA = ta.ema(close, 9)
slowMA = ta.ema(close, 21)
goLong = ta.crossover(fastMA, slowMA)
goShort = ta.crossunder(fastMA, slowMA)
if goLong
strategy.entry("Long", strategy.long)
if goShort
strategy.entry("Short", strategy.short)
// --- ATR stop, anchored to the average fill price ---------------
longStop = strategy.position_avg_price - atrValue * multATR
shortStop = strategy.position_avg_price + atrValue * multATR
if strategy.position_size > 0
strategy.exit("Long Stop", "Long", stop=longStop)
if strategy.position_size < 0
strategy.exit("Short Stop", "Short", stop=shortStop)
// Visual: plot whichever stop is currently live
activeStop = strategy.position_size > 0 ? longStop : strategy.position_size < 0 ? shortStop : na
plot(activeStop, "ATR Stop", color=color.new(color.red, 0), style=plot.style_linebr, linewidth=2) Settings
| Input | Default | Purpose |
|---|---|---|
| lenATR | 14 | ATR lookback length. |
| multATR | 1.5 | Stop distance as a multiple of the ATR reading. |
How to use
- Paste into a blank Pine Editor tab, or lift just the ATR and
strategy.exitblock into a strategy you already have. - Swap the EMA-crossover placeholder for your own entry conditions — the stop logic doesn't care how the trade was opened.
- The stop recalculates every bar from
strategy.position_avg_price, so it stays anchored to your actual fill rather than the signal bar. - The plotted line is the live stop level — check that it clears normal bar-to-bar noise on the timeframe you trade.
Frequently Asked Questions
How do you calculate a stop loss with ATR?
Take the current ATR reading, multiply it by a fixed factor, and subtract that from your entry price on a long (add it on a short). At ATR(14) of 10 points with a 1.5 multiplier, a long filled at 5000 gets a stop at 4985. Because ATR moves with recent range, the stop automatically widens when the market is choppy and tightens when it is calm.
What ATR multiplier should I use for futures?
1.5x is a sensible starting point for intraday index futures like MES and MNQ on 5-15 minute charts. Go much below 1x and normal noise starts tagging the stop; push past roughly 2.5x and the dollar risk per trade usually grows too large for a prop firm drawdown budget. Step through 1.0-2.5 in 0.25 increments in a backtest and judge by max drawdown, not win rate alone.