Pine Script v5 · Strategy
Donchian Breakout Strategy
A Pine Script v5 strategy built on Highest (high, 20) and Lowest (low, 20). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade commits 10% of equity and pays 0.05% commission. 3 settings; defaults include Entry channel length 20 and Exit channel length 10.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does Donchian Breakout Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 6 of 6 trades.
- How did Donchian Breakout Strategy do on BTCUSDT 1D with default settings?
- +2.59%
- From 26 Jan 2024 to 21 Oct 2026, over 19 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- What alerts does Donchian Breakout Strategy have?
- 2
- Breakout up; Breakout down.
- Does Donchian Breakout Strategy run in Pine Script v6?
- Yes
- Same 19 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. The v6 script can be downloaded below.
- Does Donchian Breakout Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 26, 28.
BTCUSDT · 1D · default settings
- Entry high
- Entry low
- Exit low
- Exit high
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +2.59%
- $258.92
- Buy and hold
- +102.5%
- same period
- Closed trades
- 19
- 8 won · 11 lost
- Profitable
- 42.1%
- of closed trades
- Profit factor
- 1.43
- gross profit ÷ gross loss
- Max drawdown
- 2.78%
- −$289.96
One run with the author's default settings, $10,000 starting capital, commission as the script sets it. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
Nothing to point out.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Entry channel length | 20 | int · min 2 | entryHigh entryLow |
| Exit channel length | 10 | int · min 1 | exitHigh exitLow |
| Trade the short side | false | bool | entry Short line 22 |
What it calculates
- Highesthigh, Entry channel length (20)
entryHigh · line 12 - Lowestlow, Entry channel length (20)
entryLow · line 13 - Highesthigh, Exit channel length (10)
exitHigh · line 14 - Lowestlow, Exit channel length (10)
exitLow · line 15
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | goLong | On | 21 |
| Open Short short | goShort and allowShorts | On | 23 |
| Exit Long exit stop | strategy.position_size > 0 | On | 26 |
| Exit Short exit stop | strategy.position_size < 0 | On | 28 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| Breakout up | alertcondition | Close broke above the entry channel | 35 |
| Breakout down | alertcondition | Close broke below the entry channel | 36 |
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | +2.6% | +102.5% | 19 | 42% | 1.43 | 2.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +1.0% | +5.5% | 14 | 29% | 1.58 | 0.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.2% | +9.8% | 18 | 39% | 0.98 | 5.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +2.0% | +8.4% | 13 | 38% | 2.06 | 0.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +6.6% | +16.7% | 17 | 53% | 2.17 | 3.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +3.8% | +21.5% | 12 | 50% | 3.27 | 1.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.8% | +148.3% | 18 | 33% | 1.11 | 5.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.3% | +17.1% | 18 | 22% | 1.15 | 1.8% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 6 | 50% | $13.78 |
| Sideways market | 10 | 40% | $264.00 |
| Falling market | 3 | 33% | −$18.86 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
4.3%
The 95th percentile of the worst drawdown when the same 19 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.8% to 4.3%; the tick is what actually happened, 2.4%. Order luck alone moves it this much.
All 19 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 27 Feb 2024 54,476.50 | 19 Mar 2024 64,533.00 | 21 | $183.42 |
| long | 16 May 2024 66,206.50 | 11 Jun 2024 66,670.00 | 26 | $6.10 |
| long | 16 Jul 2024 64,724.10 | 1 Aug 2024 63,456.70 | 16 | −$20.95 |
| long | 24 Aug 2024 64,037.30 | 30 Aug 2024 57,787.30 | 6 | −$100.16 |
| long | 19 Sep 2024 61,760.00 | 1 Oct 2024 62,350.00 | 12 | $8.60 |
| long | 16 Oct 2024 67,074.10 | 5 Dec 2024 90,791.10 | 50 | $354.95 |
| long | 16 Dec 2024 104,464.00 | 20 Dec 2024 94,150.00 | 4 | −$103.94 |
| long | 18 Jan 2025 104,077.00 | 2 Feb 2025 97,777.80 | 15 | −$63.48 |
| long | 23 Apr 2025 93,443.00 | 31 May 2025 103,985.00 | 38 | $114.67 |
| long | 10 Jul 2025 111,234.00 | 25 Jul 2025 115,737.00 | 15 | $40.94 |
| long | 14 Aug 2025 123,306.00 | 19 Aug 2025 115,879.00 | 5 | −$63.75 |
| long | 17 Sep 2025 116,789.00 | 22 Sep 2025 113,430.00 | 5 | −$30.79 |
| long | 2 Oct 2025 118,595.00 | 10 Oct 2025 111,561.00 | 8 | −$62.22 |
| long | 5 Jan 2026 91,529.80 | 20 Jan 2026 89,694.60 | 15 | −$21.58 |
| long | 5 Mar 2026 72,666.80 | 22 Mar 2026 68,793.30 | 17 | −$55.56 |
| long | 14 Apr 2026 74,418.00 | 16 May 2026 78,754.60 | 32 | $58.28 |
| long | 15 Jul 2026 65,044.00 | 31 Jul 2026 62,742.50 | 16 | −$37.24 |
| long | 20 Aug 2026 69,334.80 | 11 Sep 2026 76,264.00 | 22 | $100.89 |
| long | 22 Sep 2026 86,620.00 | 8 Oct 2026 82,563.00 | 16 | −$49.26 |
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Donchian Breakout Strategy this page | — | v5 strategy | +2.6% | 19 | 2.8% |
| Alex Days HiLo Rajib by 14rajib | highest lowest | v4 strategy | No trades on this market | ||
| Donchian Strat EXMO by pskoroplas | highest lowest | v4 strategy | +12.1% | 22 | 68.0% |
| Average Highest High and Lowest Low Swinger Strategy by exlux99 | lowest highest | v4 strategy | +6.4% | 20 | 49.6% |
| Highest High and Lowest Low Channel Strategy by ceyhun | highest lowest | v5 strategy | −3.7% | 26 | 7.9% |
| Donchian Channels System [racer8] by racer8 | highest lowest | v4 strategy | −3.2% | 36 | 43.6% |
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- No v6-only features. v6 is not needed to run this script.
Clean v5
VerifiedNothing to change.
Does it run in Pine Script v6?
Yes
Same 19 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © Zeal Terminal-//@version=5+//@version=6strategy("Donchian Breakout Strategy", shorttitle = "Donchian Breakout", 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 = 1)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 28 ms | Yes 1,000 bars in 52 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report.
Source
Written by Zeal Terminal and published under the Mozilla Public License 2.0.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © Zeal Terminal//@version=5strategy("Donchian Breakout Strategy", shorttitle = "Donchian Breakout", 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 = 1) // Not investment advice. Enters on a close beyond the entry channel and exits// on a close beyond the shorter exit channel on the other side.entryLen = input.int(20, "Entry channel length", minval = 2)exitLen = input.int(10, "Exit channel length", minval = 1)allowShorts = input.bool(false, "Trade the short side") entryHigh = ta.highest(high, entryLen)[1]entryLow = ta.lowest(low, entryLen)[1]exitHigh = ta.highest(high, exitLen)[1]exitLow = ta.lowest(low, exitLen)[1] goLong = close > entryHighgoShort = close < entryLow if goLong strategy.entry("Long", strategy.long)if goShort and allowShorts strategy.entry("Short", strategy.short) if strategy.position_size > 0 strategy.exit("Long exit", "Long", stop = exitLow)if strategy.position_size < 0 strategy.exit("Short exit", "Short", stop = exitHigh) plot(entryHigh, "Entry high", color = #26a69a)plot(entryLow, "Entry low", color = #ef5350)plot(exitLow, "Exit low", color = color.new(#26a69a, 60), style = plot.style_stepline)plot(exitHigh, "Exit high", color = color.new(#ef5350, 60), style = plot.style_stepline) alertcondition(goLong, "Breakout up", "Close broke above the entry channel")alertcondition(goShort, "Breakout down", "Close broke below the entry channel")