Strategy · Pine Script v6 · On the chart

ATR Breakout Channel [QuantForge]

by QuantForgeHQ

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.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code78
Settings8inputs
LicenceMIT
Last update3 months ago6 Jul 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 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.

Details

Published by

QuantForgeHQ on GitHub

Draws

On the price chart

Markets

Crypto, Stocks

Timeframes

H4 and above

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Breakout channel lengthint20donLen
Trade directionstringBothtradeDir
Use EMA trend filterbooltrueuseTrend
Trend EMA lengthint200emaLen
ATR lengthint14atrLen
Trailing stop (ATR mult)float3.0trailMult
Start datetimetimestamp("1 Jan 2023 00:00 +0000")startDate
End datetimetimestamp("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.

Similar Pine Scripts

106 stars

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

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 9:22 5 MIN 15 MIN BANKNIFTY

均线突破交易系统

by tangly1024
76 stars

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

Strategy v5 TrendMoving averages
Updated 5 months ago · None stated GitHub repository of 均线突破交易系统
16 stars

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

Strategy v6 TrendBreakout
Updated 12 months ago · None stated GitHub repository of Seykota Alt 10: Profit Targets
4 stars

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

Strategy v5 Volatility & bandsTrend
Updated 2 years ago · None stated GitHub repository of Bollinger Trend - Strategy V1
4 stars

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

Strategy v5 TrendVolatility & bands
Updated 5 months ago · None stated GitHub repository of Keltner Channel Strategy

More Breakout scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers