What it measures
A Donchian channel draws the highest high and the lowest low of the last N bars, with a middle line halfway between them. It is about as simple as an indicator gets: the channel shows the recent range, and price closing beyond it marks a new N-bar high or low.
Richard Donchian, often called one of the fathers of trend following, used channel breakouts in the mid-20th century. The idea became widely known through the "Turtle" trading experiment of the 1980s, which is often cited as using 20- and 55-day breakouts for entries and shorter channels for exits.
The formula in words
- Upper band = the highest high of the last N bars, including the current one.
- Lower band = the lowest low of the last N bars.
- Middle line = (upper band + lower band) ÷ 2.
- A breakout is a close above the previous bar's upper band (or below the previous bar's lower band).
The "previous bar" detail in step 4 matters. Because the current bar's high is part of the current upper band, a close can never be above the current band, only above the band as it stood one bar earlier.
How traders read it
- Breakout entries. A close above the upper band is a new N-bar high, which trend followers read as a possible start or continuation of a trend.
- Channel exits. A shorter channel is often used for exits: for example, enter on a 20-bar breakout and exit when price closes below the 10-bar low.
- Stops and risk. The opposite band, or the middle line, gives a natural stop. Because channels can be wide, risk per trade is usually controlled with position size rather than a tight stop.
- Range and volatility. A narrowing channel means the market has been quiet; a widening one means it is travelling.
Common settings
| Setting | Typical value | Notes |
|---|---|---|
| Entry channel | 20 bars | 55 for slower, longer-term breakouts. |
| Exit channel | 10 bars | Roughly half the entry length is a common pairing. |
| Timeframe | Daily for classic trend following | Shorter timeframes produce many more false breakouts. |
Some platforms exclude the current bar from the channel; this script includes it and compares breakouts with the previous bar's band, which gives the same signals.
Pine Script v6 code
The complete, self-contained script is below. Copy it into a new indicator in TradingView's Pine Editor, or download the .pine file.
//@version=6
indicator("Donchian channel breakouts", shorttitle = "Donchian", overlay = true)
len = input.int(20, "Channel length", minval = 1)
exitLen = input.int(10, "Exit channel length", minval = 1)
showExit = input.bool(false, "Show the exit channel")
upper = ta.highest(high, len)
lower = ta.lowest(low, len)
mid = math.avg(upper, lower)
exitUpper = ta.highest(high, exitLen)
exitLower = ta.lowest(low, exitLen)
// Breakouts compare the close with the previous bar's channel: the current high is always inside its own channel.
breakUp = close > upper[1]
breakDown = close < lower[1]
newBreakUp = breakUp and not breakUp[1]
newBreakDown = breakDown and not breakDown[1]
upperPlot = plot(upper, "Upper", color = color.new(color.blue, 0))
lowerPlot = plot(lower, "Lower", color = color.new(color.blue, 0))
plot(mid, "Middle", color = color.new(color.gray, 40))
fill(upperPlot, lowerPlot, color = color.new(color.blue, 94), title = "Channel")
plot(showExit ? exitUpper : na, "Exit upper", color = color.new(color.orange, 30))
plot(showExit ? exitLower : na, "Exit lower", color = color.new(color.orange, 30))
plotshape(newBreakUp, "Breakout up", shape.triangleup, location.belowbar, color.new(color.teal, 0), size = size.small)
plotshape(newBreakDown, "Breakout down", shape.triangledown, location.abovebar, color.new(color.red, 0), size = size.small)
alertcondition(newBreakUp, "Donchian breakout up", "{{ticker}} closed above the previous upper Donchian band ({{interval}})")
alertcondition(newBreakDown, "Donchian breakout down", "{{ticker}} closed below the previous lower Donchian band ({{interval}})")The channel and its middle line are drawn on the price chart, with an optional shorter exit channel. Triangles mark the first close beyond the previous bar's band, so a strong trend doesn't produce a signal on every bar, and the same events are available as alert conditions.
Add it to your chart
- Open any chart on TradingView and open the Pine Editor.
- Create a new indicator, delete the template code and paste the script.
- Click Save, then Add to chart. Change lengths and levels from the indicator's settings.
Stuck on an error? See how to add a Pine Script indicator to TradingView.
Pitfalls
- False breakouts. Many breakouts fail and reverse back into the range, especially on intraday charts. Breakout systems typically have low win rates and depend on a few large trends; test with costs before drawing conclusions.
- Comparing with the current band.
close > ta.highest(high, 20)is never true. Use the previous bar's band, as the script does. - Wide stops. Using the opposite band as a stop can mean a large distance. Control risk with position size, not by moving the stop closer.
- Optimising the length. It's tempting to search for the "best" length on past data. Stick to round numbers and check neighbours.
Related reading
- 8 backtesting mistakes that make a strategy look better than it is
- Pine Script strategy.exit(): stop loss and take profit that behave as you expect
- ATR Stop-Loss Calculator (free tool)
- Drawdown Recovery Calculator (free tool)
The script is an educational example: test it on your own markets before relying on it. Indicators describe the past; they don't predict the future. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.