Concept 12 / 52·Volatility·Bands and channels
‹ DivergenceEngulfing Candle ›

Donchian Channels in Pine Script

Donchian channel · price channel · N-day high and low · 20-day breakout channel · turtle channel

The highest high and lowest low of the last N bars drawn as a channel, so a close outside the prior channel is a new N-bar extreme and the classic breakout trigger.

Reads best on: Daily and weekly charts of futures, commodities, forex and crypto, where trends run for months; intraday it works on liquid index futures with a session or higher-timeframe filter.
On this page: the Pine v6 script, checked, drawn on real NVDA bars. Backtest it free, costs charged, every trade on the chart.
Backtest results: Donchian breakout strategy: SPY, QQQ and more since 2016, costs charged, every trade listed.
01

Definition

Donchian Channels are the highest high and the lowest low of the last N bars, drawn as two lines with a mid line halfway between them. The default length is 20, a trading month on daily bars. They belong to the bands and channels family but are built from extremes rather than from an average, so the lines are levels price actually traded at. The upper line is the price at which the market would make a new N-bar high, the lower line the price of a new N-bar low, and the width is the range of the window.

A new 20-bar high is a specific fact: every trader who went short in the window is under water and there is no supply from that window overhead. Trend following rests on the observation that large moves begin with new highs and new lows, so the channel edges are entry triggers rather than fade levels. The channel also carries volatility, because its width is the range of the window, and a long flat stretch of both lines is a compression that ends in a break. The channel does not predict; it defines what a breakout is and removes the argument about it.

Richard Donchian, a pioneer of trend following, traded a 4-week rule: long when price exceeds the 4-week high, short when it falls under the 4-week low. The Turtle traders of the 1980s used the same idea on daily bars, entering on a 20-day breakout and exiting on a 10-day reversal, with a slower 55-day entry and 20-day exit as a second system. The library indicator draws the 20-bar channel and marks closes beyond it; the library strategy trades a 20-bar entry channel with a 10-bar exit channel and an ATR stop, which is the Turtle structure.

On the chart the lines move in stair steps. The upper line only changes when a new high is made or when the old high leaves the window, so it steps up in a rising market and stays flat during a pullback. In a range both lines are flat and the channel is a box. The library scripts compare the close with the channel of the previous bar rather than the current one, because a channel that includes the current bar can never be exceeded: the bar's own high is already in it. Expect frequent small breakouts that fail and a few that run for weeks.

02

Reading the chart

The channel is defined by the window, so set the length for the move you want to catch and then read the steps.

  1. Set the length to the horizon. 20 bars catches monthly moves on a daily chart, 55 catches the slow trends, and on intraday charts 20 bars is 20 candles, not 20 days, so choose by the timeframe.
  2. Read the shape of the lines. Both lines flat means a range and a box to trade the edges of; one line stepping while the other lags means a trend in the stepping direction.
  3. Note where price sits. In an uptrend the close hugs the upper line and pullbacks stay above the mid line; a close under the mid line is the first sign the trend is tiring.
  4. Watch for a close beyond the previous bar's channel. That is a new N-bar extreme confirmed on a closed bar, the mark the library indicator draws, and the turtle strategy's entry.
  5. Track the exit channel. The strategy leaves a long on a close below the 10-bar low, so on the chart the shorter channel is the trailing stop and its distance from price is the open risk.
  6. Judge the width. A narrow channel that has been narrow for many bars is a compression, and a breakout from it has more room than one from an already wide channel.
03

The maths

The channel lines are the extremes of the previous N bars, the mid line their average, and the width their difference.

Uppert=max⁡i=t−nt−1Hi\mathrm{Upper}_t = \max_{i=t-n}^{t-1} H_i
Lowert=min⁡i=t−nt−1Li\mathrm{Lower}_t = \min_{i=t-n}^{t-1} L_i
Midt=Uppert+Lowert2\mathrm{Mid}_t = \frac{\mathrm{Upper}_t + \mathrm{Lower}_t}{2}
Widtht=Uppert−Lowert\mathrm{Width}_t = \mathrm{Upper}_t - \mathrm{Lower}_t
HiH_ithe high of bar i
LiL_ithe low of bar i
nnthe channel length in bars, 20 by default
ttthe current bar, which is excluded from the window
Uppert\mathrm{Upper}_tthe channel high, the highest high of the previous n bars
Lowert\mathrm{Lower}_tthe channel low, the lowest low of the previous n bars
Midt\mathrm{Mid}_tthe mid line
Widtht\mathrm{Width}_tthe width of the channel, the range of the window

Including the current bar in the window makes the upper line equal the current high whenever the bar is the highest, so price can touch it but never close above it. The library scripts use the previous n bars and compare the close with that.

The Turtles entered on a trade above the prior high during the bar, not on the close. Entering on the confirmed close is later and cleaner, and it is what the library does; an intrabar version fills more often and earlier.

The mid line is the same midpoint Ichimoku uses for its lines, so a 26-bar Donchian mid line is the Ichimoku base line.

The strategy's exit channel is shorter than its entry channel, 10 against 20, so the exit tightens faster than a mirror of the entry would.

04

Inputs that matter

InputTypicalWhat changing it does
Channel length20Shorter gives more breakouts and more false ones; longer waits for a bigger extreme, enters later and whipsaws less.
Entry channel length (strategy)20The breakout horizon: 20 for the classic system, 55 for the slow one; longer entries are rarer and stronger.
Exit channel length (strategy)10Shorter exits give back less at the turn but are shaken out by pullbacks; longer holds through pullbacks and returns more at the end.
Stop (ATR multiples, strategy)2The initial stop from the entry price; tighter takes small losses often, wider survives the first pullback but loses more when the breakout fails.
05

In practice

  • As a breakout entry: a confirmed close above the prior 20-bar high is the long and a close below the prior 20-bar low the short, the marks the library indicator draws and alerts on.
  • As a complete trend system: the library strategy enters on the 20-bar channel, exits on the 10-bar channel, protects the entry with a 2 ATR stop, and can trade longs, shorts or both.
  • As a range map: when both lines are flat the channel is the box, and range traders fade the edges while breakout traders wait for the close outside.
  • As a trailing stop: the lower line of a shorter channel under a long trade steps up behind price and never falls, which is the turtle exit and a stop many trend followers use.
  • As a volatility read: the width is the range of the window, and a channel that has narrowed for a long time is a compression that tends to break hard.
  • As a filter: the filter keeps only breakouts in the direction of a higher-timeframe trend, or only from a narrow channel, which removes many of the failures a raw channel produces.
06

Where it goes wrong

  • Building the channel from the current bar. A line that includes the bar's own high cannot be broken on the close; use the previous bars, as the library scripts do.
  • Fading every breakout because most fail. Trend systems accept a low win rate and pay for it with the few breakouts that run; fading them takes the small wins and eats the big losses.
  • Trading raw breakouts in chop. In a flat market the channel is a box and closes outside it snap back; add a trend or compression filter before trusting a break.
  • Expecting a high win rate from the turtle strategy. Systems like it lose more often than they win and rely on the size of the few winners; judge it on expectancy across years, not on the last month.
07

Compared with

Breakout. Breakout is the general idea of price leaving a range. The Donchian channel is one precise definition of the range, the N-bar high and low, so it turns the idea into a rule you can test and alert on.

Bollinger Bands. Bollinger Bands are a statistical corridor around an average, and a close outside them is an extreme. A Donchian line is a price the market actually traded at, and a close outside it is a new extreme, so the two read the same close differently.

Keltner Channels. A Keltner channel is a smoothed average with an ATR corridor, so its lines never equal a real high or low. Donchian lines are the actual extremes, which makes them cleaner for breakouts and useless as a measure of ordinary movement.

Ichimoku Cloud. Ichimoku takes the midpoints of 9, 26 and 52-bar Donchian channels and projects two of them ahead as a cloud. Donchian channels keep the edges and trade the breakout; Ichimoku keeps the middle and trades the balance around it.

08

Build it in Quant

Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.

Backtest Donchian Channels as a strategy
Costs and slippage included; every version stays selectable.
09

Questions

  • Why do the library scripts use the previous bar's channel?

    Because a channel that includes the current bar contains the bar's own high and low. Price can reach the upper line but never close above it, so the breakout condition would never fire. Comparing the close with the channel of the previous N bars asks the real question: did this bar close above every high in the window before it?

  • What did the Turtles actually trade?

    Two systems on daily bars. System 1 entered on a 20-day breakout and exited on a 10-day reversal; System 2 entered on 55 days and exited on 20. Both used an ATR-based stop of 2 units and sized positions by ATR so that each trade risked a fixed share of equity. The library strategy is the same structure with a close-based entry.

  • Why do so many breakouts fail?

    Because a new 20-bar high happens often in a range and most of them are not the start of a trend. Trend following accepts this: the losses are small, cut by the exit channel or the stop, and the rare breakouts that run are meant to cover them. Filters help, such as keeping only breaks from a narrow channel or in the direction of the higher-timeframe trend.

  • What channel length should I use?

    20 is the reference on daily bars and catches monthly moves; 55 catches the slower trends with fewer signals; 10 is fast and noisy. On intraday charts the number is bars, not days, so a 20-bar channel on a 5-minute chart covers under 2 hours. Choose the length by the move you want to hold, then test it.

  • How does a Donchian system exit?

    The classic exit is the opposite shorter channel: the rule leaves a long when the close falls below the 10-bar low, which trails the trend and never retreats. Add an initial ATR stop for the first days, when the exit channel can be far below. Fixed targets fit the method badly, because the method depends on the few trades that run much further than any target.

  • Does it work on intraday charts?

    Yes, with a filter. Liquid index futures and major pairs trend intraday, and a 20-bar channel on 5 or 15-minute bars catches those runs, but the session open and close produce breakouts of their own. Combine it with a session window or a higher-timeframe trend, and expect the same low win rate as on daily bars.