Concept 07 / 52·Structure·Range expansion
‹ Break of Structure (BOS)Chandelier Exit ›

Breakout in Pine Script

range breakout · channel breakout · breakout trading · false breakout · fakeout

A close beyond a defined range, whether the highest high of the last N bars, the opening range or a consolidation, taken as the start of a move rather than the end of one.

Reads best on: Index futures, commodities, liquid stocks and major crypto on daily charts for channel breakouts; 1-minute to 15-minute charts for the opening range on stocks and index futures.
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, Keltner Channel strategy: SPY, QQQ and more since 2016, costs charged, every trade listed.
01

Definition

A breakout is a move that closes beyond a range that had contained price: the highest high or lowest low of the last N bars, the high and low of the opening minutes, or the edges of a consolidation. It belongs to the range-expansion family, which trades the moment a market leaves balance rather than the reversals inside it. The breakout trader accepts long entries at highs and short entries at lows on the premise that a range that has just broken is more likely to extend than to return.

Breakouts work because of what a range does to positions. While price is contained, both sides build positions and set stops just outside the edges. When an edge breaks the stops fire, the traders on the wrong side chase, and the traders who were waiting for confirmation enter, all in the same direction. The move feeds on itself for a while. The failure mode is the same mechanism in reverse: if the break attracts no follow-through, the early entrants are trapped and the return into the range is fast.

The systematic version is the Donchian channel breakout: long on a close above the highest high of the last 20 bars, short on a close below the lowest low, which Richard Donchian pioneered and which the Turtle traders of the 1980s ran with a 20-bar entry and a 10-bar exit. The intraday version is the opening range breakout, trading the first close beyond the high or low of the first 15 or 30 minutes. Both are in the library, as an indicator, an opening range tool and a strategy with an ATR stop.

On the chart, expect a low hit rate and a skewed payoff: many breakouts fail within a few bars and give back a little, and a minority run for a long time. Breakouts from long, tight consolidations on rising volume travel furthest. Breakouts inside a wide, choppy range fail most often. The retest of the broken edge is the second chance, and whether it holds is the clearest early evidence. The implementations use closes on confirmed bars, so a wick through the level is not a signal.

02

Reading the chart

Reading a breakout is reading the range first, then the bar that leaves it, then the bars that follow.

  1. Define the range explicitly. The highest high and lowest low of the last N bars, the opening window's high and low, or a consolidation with clear edges; a breakout of an undefined range is a guess.
  2. Judge the range's quality. Long and tight, with volatility contracting, is the best setup; wide and choppy, with several failed breaks already, is the worst.
  3. Require a close beyond the edge, not a wick. The implementations mark a long on a close crossing above the channel high and a short below the channel low, on confirmed bars only.
  4. Check the volume on the breakout bar. Above-average volume is sponsorship from larger participants; a break on thin volume is treated as unconfirmed until the retest holds or volume expands.
  5. Watch the next few bars. Follow-through and a retest of the broken edge that holds confirm the breakout; a close back inside the range within a few bars is a failure and often a reversal signal.
  6. Set the invalidation before entering. The stop goes back inside the range or an ATR multiple away, as the strategy template does, and the exit channel or a trailing stop manages the position if the move runs.
03

The maths

A channel breakout compares the close with the highest high and lowest low of the previous N bars, excluding the current bar so the close can exceed the channel.

Uppert=max⁡(Ht−n,…,Ht−1)\mathrm{Upper}_t = \max(H_{t-n}, \ldots, H_{t-1})
Lowert=min⁡(Lt−n,…,Lt−1)\mathrm{Lower}_t = \min(L_{t-n}, \ldots, L_{t-1})
Longt={1Ct>Uppert∧Ct−1≤Uppert−10otherwise\mathrm{Long}_t = \begin{cases} 1 & C_t > \mathrm{Upper}_t \land C_{t-1} \le \mathrm{Upper}_{t-1} \\ 0 & \text{otherwise} \end{cases}
Shortt={1Ct<Lowert∧Ct−1≥Lowert−10otherwise\mathrm{Short}_t = \begin{cases} 1 & C_t < \mathrm{Lower}_t \land C_{t-1} \ge \mathrm{Lower}_{t-1} \\ 0 & \text{otherwise} \end{cases}
Stoplong=Ct−s ATR14\mathrm{Stop}_{\text{long}} = C_t - s \, \mathrm{ATR}_{14}
Uppert,Lowert\mathrm{Upper}_t, \mathrm{Lower}_tthe channel high and low on bar t, built from the n bars before it
Ht−ntoHt−1H_{t-n} \mathrm{to} H_{t-1}the highs of the previous n bars
Lt−ntoLt−1L_{t-n} \mathrm{to} L_{t-1}the lows of the previous n bars
nnthe channel length, 20 by default
CtC_tthe close of the current bar
Longt,Shortt\mathrm{Long}_t, \mathrm{Short}_tthe breakout marks, a close crossing the channel edge
ssthe stop distance in ATR multiples, 2 by default in the strategy
ATR14\mathrm{ATR}_{14}the 14-bar average true range
Stoplong\mathrm{Stop}_{long}the strategy's initial stop on a long entry; the short stop mirrors it above the close

The channel is shifted back one bar. Without the shift the current bar's own high would be in the channel and the close could never exceed it, so a breakout would never register.

The indicator draws the channel and its mid line and marks longs and shorts on crosses; the strategy adds a shorter exit channel, 10 bars by default, and a stop at 2 ATR from the entry price, with switches to trade longs only or shorts only.

The opening range tool builds the range from a session window on the exchange clock, 09:30 to 10:00 by default, and marks one long on the first close above it and one short on the first close below it per day.

All three fire on confirmed bars, so the mark appears when the bar closes and does not flicker inside the bar.

04

Inputs that matter

InputTypicalWhat changing it does
Channel length20A longer channel breaks less often and catches only the larger moves; a shorter one gives earlier, more frequent breaks with more failures.
Exit channel10 (strategy)A shorter exit hands back less on a reversal but gets shaken out of trends; a longer one holds through pullbacks at the cost of giving back more.
Stop (ATR multiples)2 (strategy)A wider stop survives the retest of the range; a tighter one cuts failed breakouts quickly but is hit by ordinary noise.
Opening window09:30 to 10:00A shorter window gives earlier breakouts with more fakes; a longer one waits for a settled range and trades later in the session.
05

In practice

  • As a trend-following system: the strategy goes long on a close above the 20-bar high, short on a close below the 20-bar low, exits on the opposite 10-bar channel and stops at 2 ATR, which is the turtle rule set.
  • As a consolidation trade: after a long, tight range with contracting volatility, the first close beyond the edge on rising volume is entered long or short with the stop back inside the range.
  • As an intraday routine: the opening range tool marks the first close above or below the first 30 minutes' range, once per day in each direction.
  • As a retest entry: rather than the breakout bar, the first pullback to the broken edge that holds is taken, with a tighter stop and the same target.
  • As a failed-breakout reversal: a close back inside the range within a few bars of the break traps the breakout traders, and the trade against them targets the other side of the range.
  • As a filter for other tools: a moving-average cross or a momentum signal that occurs as price clears the channel edge is stronger than one inside the range.
06

Where it goes wrong

  • Entering on the wick. A spike through the level that closes back inside is a sweep, not a breakout; the implementations use the close for that reason.
  • Ignoring the range's quality. Breaks of wide, choppy ranges fail most of the time; look for long, tight consolidations with contracting volatility.
  • Trading without an exit plan. Breakouts have a low hit rate, so the stop and the trailing exit decide the outcome; define both before the entry.
  • Giving up after a few failures. The results come from the minority of breakouts that run; cutting the losers small and holding the winners is the whole method.
07

Compared with

Donchian Channels. The Donchian channel is the specific tool, the highest high and lowest low of N bars; breakout is the concept it implements, which also covers opening ranges and drawn consolidations. Every Donchian signal is a breakout, not every breakout is a Donchian signal.

Opening Range Breakout. The opening range breakout is a breakout of a range defined by time, the first minutes of the session, rather than by a number of bars. It resets daily and fires once per direction; a channel breakout runs on any timeframe and fires whenever the channel is crossed.

Break of Structure. A break of structure is a close beyond the last confirmed swing high or low, a single pivot; a channel breakout is a close beyond the extreme of a whole window of bars. Structure breaks fire more often and earlier, channel breaks require a larger move.

Gaps. A gap jumps over the range edge without trading through it; a breakout trades through. A gap out of a base is a breakout that happened overnight, and it is judged by the same volume and follow-through rules.

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 Breakout as a strategy
Costs and slippage included; every version stays selectable.
09

Questions

  • What makes a breakout more likely to succeed?

    A long, tight range with contracting volatility, a close beyond the edge on volume well above average, a retest that holds, and a higher-timeframe trend in the same direction. The weakest breakouts come from wide, choppy ranges that have already failed to break several times, and from bars that only wick through the edge.

  • How are false breakouts reduced?

    They cannot be avoided, only reduced and managed. Rules require a close rather than a wick, check the volume, prefer tight ranges, and use the retest entry where they can. The rest is accepted: a stop back inside the range keeps a false break small, and a close back inside the range is itself a setup in the other direction.

  • Why does the strategy use a 10-bar exit and a 20-bar entry?

    That is the classic turtle configuration. The 20-bar entry waits for a meaningful break; the 10-bar exit lets the position ride while the trend makes new highs and closes it on the first serious pullback. The 2 ATR stop caps the loss on breaks that fail at once, before the exit channel has had time to move.

  • Breakout bar or retest: which entry?

    Both are valid. The breakout bar catches every move including the ones that never retest, at the cost of a wider stop and more failures. The retest gives a tighter stop and skips the breaks that reverse at once, at the cost of missing the strongest moves. Many traders take a part on each.

  • Does breakout trading work on all timeframes?

    Yes, with different hit rates. Daily and weekly breakouts of long bases have the best follow-through, because the participants who built the base are many. Intraday breakouts, including the opening range, are faster and fail more often, so the stop and the session context matter more. Very short timeframes produce mostly noise.

  • What does the channel's one-bar shift mean for the marks?

    The channel on any bar is built from the bars before it, not including the current one. That is what allows a close to exceed the channel high; otherwise the current high would always be at least the close. It also means the trigger is a close above the previous N-bar high, which is the standard breakout definition.