ATR Breakout — Pine Script v5 strategy
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
| Parameter | Value | Role |
|---|---|---|
ATR length | 14 | ta.atr(14) sets the Average True Range period used for the volatility filter and for the stop distance. |
Breakout lookback | 20 | ta.highest(high, 20) builds the upper level from the last 20 highs. |
Breakout offset | upper[1] | The entry compares close to the previous bar value of the level, not to the current one. |
ATR filter length | 20 | ta.sma(atr, 20) averages ATR over 20 bars; ATR must be above that average. |
Stop multiplier | 2 | strategy.exit uses stop = close - atr * 2. |
Position side | strategy.long | strategy.entry only sends long orders; no short branch exists in the code. |
Overlay | true | strategy("ATR Breakout", overlay=true) plots the strategy on the price pane. |
Exit order type | stop | strategy.exit is called with the stop argument only, with no limit and no trail. |
Known limits of this rule
- Breakout entries react to price crossing a fixed high, so a ranging market produces repeated signals near the same level and the stop can be hit before any follow-through.
- The strategy() call sets no commission, no slippage and no funding cost, so the code itself ignores trading costs.
- The exit call sits inside the if block, so the stop is only refreshed on bars where the entry condition is true; without a take profit or trailing stop, a trade can stay open for a long time.
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.
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
More TradingView paths
Educational code on past data. No investment advice; no result is promised.