The ATR Breakout Channel strategy detects breakouts by comparing the current close price to the high or low of the previous 20 bars, forming a channel.
1Copy the scriptPress Copy script above, or the copy button on the code below.
2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.
Source Code
78 lines of Pine Script v6 from atr_breakout_channel_v1.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.
atr_breakout_channel_v1.pine
//@version=6
strategy("ATR Breakout Channel [QuantForge]",
overlay = true,
initial_capital = 10000,
default_qty_type = strategy.percent_of_equity,
default_qty_value = 10,
commission_type = strategy.commission.percent,
commission_value = 0.05,
slippage = 2)
// ============================================================
// ATR Breakout Channel - QuantForge portfolio demo
// Trend-following engine:
// Entry: close breaks the prior N-bar extreme (confirmed bar),
// in the direction of the long-term EMA trend.
// Exit: ratcheting ATR trailing stop (chandelier style).
// Initial stop frozen at entry, then trails, never loosens.
// No repainting: channel uses prior-bar extremes only.
// ============================================================
// ---------- Inputs ----------
donLen = input.int(20, "Breakout channel length", minval = 5, group = "Entry")
tradeDir = input.string("Both", "Trade direction", options = ["Long only", "Short only", "Both"], group = "Entry")
useTrend = input.bool(true, "Use EMA trend filter", group = "Entry")
emaLen = input.int(200, "Trend EMA length", minval = 10, group = "Entry")
atrLen = input.int(14, "ATR length", minval = 1, group = "Risk")
trailMult = input.float(3.0, "Trailing stop (ATR mult)", step = 0.25, minval = 0.5, group = "Risk")
startDate = input.time(timestamp("1 Jan 2023 00:00 +0000"), "Start date", group = "Test window")
endDate = input.time(timestamp("1 Jan 2030 00:00 +0000"), "End date", group = "Test window")
// ---------- Channel and filters ----------
chanHigh = ta.highest(high, donLen)[1]
chanLow = ta.lowest(low, donLen)[1]
atrVal = ta.atr(atrLen)
emaVal = ta.ema(close, emaLen)
trendLongOK = not useTrend or close > emaVal
trendShortOK = not useTrend or close < emaVal
inWindow = time >= startDate and time <= endDate
allowLong = tradeDir != "Short only"
allowShort = tradeDir != "Long only"
longSignal = close > chanHigh and trendLongOK
shortSignal = close < chanLow and trendShortOK
// ---------- Entries ----------
if longSignal and allowLong and inWindow and strategy.position_size == 0
strategy.entry("Long", strategy.long)
if shortSignal and allowShort and inWindow and strategy.position_size == 0
strategy.entry("Short", strategy.short)
// ---------- Ratcheting ATR trailing stop ----------
var float trailStop = na
if strategy.position_size > 0
candidate = close - trailMult * atrVal
trailStop := na(trailStop) ? candidate : math.max(trailStop, candidate)
strategy.exit("Long trail", from_entry = "Long", stop = trailStop)
else if strategy.position_size < 0
candidate = close + trailMult * atrVal
trailStop := na(trailStop) ? candidate : math.min(trailStop, candidate)
strategy.exit("Short trail", from_entry = "Short", stop = trailStop)
else
trailStop := na
// ---------- Visuals ----------
pH = plot(chanHigh, "Channel high", color = color.new(color.teal, 30))
pL = plot(chanLow, "Channel low", color = color.new(color.teal, 30))
fill(pH, pL, color = color.new(color.teal, 92), title = "Channel")
plot(emaVal, "Trend EMA", color = color.new(color.purple, 50))
plot(strategy.position_size != 0 ? trailStop : na, "Trailing stop",
color = color.new(color.red, 0), style = plot.style_linebr, linewidth = 2)
// ---------- Alerts ----------
alertcondition(longSignal, "Long breakout", "Close broke above the N-bar channel high with trend filter passed")
alertcondition(shortSignal, "Short breakout", "Close broke below the N-bar channel low with trend filter passed")
Overview
Entries occur when the price breaks through the channel in the trend direction of a 200-period EMA if the trend filter is enabled. Exits are managed by a ratcheting ATR-based trailing stop that starts at entry and progressively tightens without loosening. The strategy plots the channel lines, EMA trend line, and trailing stop on the price chart. Main settings include channel length, trade direction, EMA parameters, ATR length, trailing multiplier, and date range for the test window.
The inputs you can change from the gear icon on the chart. Defaults are the author's.
Setting
Type
Default
Variable
Breakout channel length
int
20
donLen
Trade direction
string
Both
tradeDir
Use EMA trend filter
bool
true
useTrend
Trend EMA length
int
200
emaLen
ATR length
int
14
atrLen
Trailing stop (ATR mult)
float
3.0
trailMult
Start date
time
timestamp("1 Jan 2023 00:00 +0000")
startDate
End date
time
timestamp("1 Jan 2030 00:00 +0000")
endDate
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.
This strategy runs on the price chart and enters long when a fast simple moving average crosses above a slow one, and short on the opposite cross, while closing the other side. Entries also need ATR above a filter, a pullback check, a minimum candle-body chang
This strategy trades long breakouts of a fast and a slow moving average on the price chart. It opens a long position when price crosses above either average, using a percentage of equity as size. It reduces the position by a set share when the high reaches the
This strategy plots Donchian channels, trailing stops, and profit targets directly on the price chart. It enters breakout trades using a 55-bar Donchian channel and sizes positions according to an ATR-based risk percentage per unit. The system adds units as pr
This strategy combines channel breakout concepts with an ATR-ADX volatility trailing framework and multi-timeframe moving averages. It evaluates trend conditions using configurable moving average types, including SMA, EMA, Hull MA, and Tilson T3, paired with d
This strategy executes trend-following trades based on Bollinger Band breakouts confirmed by a long-term moving average filter. A long position opens when the price crosses above the upper Bollinger Band, provided the band spread meets a minimum threshold and
The Keltner Channel Strategy is a breakout system plotted directly on the main price chart. It calculates upper and lower channel bands around an exponential or simple moving average using ATR, True Range, or bar range multiplied by a specified factor. A long