Skip to main content

ATR Breakout — Pine Script v5 strategy

VOLATILITYATRPine Script v5

This strategy buys when the close rises above the highest high of the previous 20 bars while ATR is above its own 20-bar average. Once in the trade it places a stop order at 2 ATR below the close of the signal bar.

Full code (free to copy)

//@version=5
strategy("ATR Breakout", overlay=true)
atr = ta.atr(14)
upper = ta.highest(high, 20)
if (close > upper[1] and atr > ta.sma(atr, 20))
    strategy.entry("Long", strategy.long)
    strategy.exit("Exit", "Long", stop=close - atr * 2)

How the script works

The script opens with version 5 and strategy() with overlay=true, so it is drawn on the price chart. ta.atr(14) computes the Average True Range over 14 bars. ta.highest(high, 20) returns the highest high of the last 20 bars, current bar included; upper[1] is that same series read one bar back, so the breakout level never uses the bar being tested.

The entry test runs every bar: close must be greater than upper[1], and the 14-bar ATR must be greater than its own 20-bar simple moving average. When both are true, strategy.entry("Long", strategy.long) sends a long market order. Only one side exists; the code never opens a short.

Inside the same block, strategy.exit("Exit", "Long", stop=close - atr * 2) attaches a stop order to the long position, priced two ATR below the close of that bar. Because the call sits inside the if block, the stop level is only submitted on bars where the entry condition is also true; there is no take profit, no trailing stop and no time-based exit.

Parameters in the code

ParameterValueRole
ATR length14ta.atr(14) sets the Average True Range period used for the volatility filter and for the stop distance.
Breakout lookback20ta.highest(high, 20) builds the upper level from the last 20 highs.
Breakout offsetupper[1]The entry compares close to the previous bar value of the level, not to the current one.
ATR filter length20ta.sma(atr, 20) averages ATR over 20 bars; ATR must be above that average.
Stop multiplier2strategy.exit uses stop = close - atr * 2.
Position sidestrategy.longstrategy.entry only sends long orders; no short branch exists in the code.
Overlaytruestrategy("ATR Breakout", overlay=true) plots the strategy on the price pane.
Exit order typestopstrategy.exit is called with the stop argument only, with no limit and no trail.

Known limits of this rule

Before you trust it: test it on data it never saw

A backtest in TradingView's Strategy Tester shows one past path with the settings you picked. Strategy Arena Lab (free, Windows and Linux) replays this Pine on your PC, compares its trades one by one with TradingView's for supported Pine, counts the variants you try, then judges the rule on a period kept sealed during the search: holds, mirage or inconclusive.

Get the free Lab →

FAQ

Does it work on crypto?

The code assumes nothing about the instrument; it only needs candles with high, low and close. On a 24-hour market the 14-bar and 20-bar windows cover a different span of time than on a stock session. You can replay it on a period you have never seen in Strategy Arena Lab, which is free.

How do I add a take profit?

A stop already exists, set by stop=close - atr * 2 in strategy.exit. To add a target, pass a limit argument to the same strategy.exit call, and to change the stop distance edit the 2 or the ATR length of 14. You can replay the edited version on a period you have never seen in Strategy Arena Lab, which is free.

Other free Pine Script v5 strategies

RSI Mean ReversionEMA Crossover 9/21EMA Ribbon 8/21/55Bollinger Band BounceMACD MomentumSuperTrendStochastic RSIVWAP BounceRSI + EMA Combo
TradingView Starter
Choose the account path. Prove the strategy separately.
Your first chart and price alert do not require the Lab. Strategy Arena proof remains required before trusting or exporting a strategy.
Partner attribution is for first-time users and web purchases only. Strategy Arena may earn a commission on eligible purchases; starting with a free account needs no payment.
I have never had an account · Start free I already have an account Check a strategy
More TradingView paths
Evidence-gated Pine review

Educational code on past data. No investment advice; no result is promised.