Pine Script v4 · Strategy
Donchian Channels System [racer8]
A Pine Script v4 strategy built on Highest (20) and Lowest (20). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 1 setting; defaults include Period 20.
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 Channels System [racer8] repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 11 of 11 trades.
- How did Donchian Channels System [racer8] do on BTCUSDT 1D with default settings?
- −3.21%
- From 26 Jan 2024 to 21 Oct 2026, over 36 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Donchian Channels System [racer8] be converted to Pine Script v5?
- Yes
- The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
- Does Donchian Channels System [racer8] use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Donchian Channels System [racer8] run in Zeal Terminal?
- Yes
- It compiles in 9 ms and runs 1,000 bars in 17 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −3.21%
- −$3,208.23
- Buy and hold
- +102.5%
- same period
- Closed trades
- 36
- 12 won · 24 lost
- Profitable
- 33.3%
- of closed trades
- Profit factor
- 0.97
- gross profit ÷ gross loss
- Max drawdown
- 43.59%
- −$50,319.60
One run with the author's default settings, $100,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.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames highest → ta.highest, lowest → ta.lowest.
v4 → v5
VerifiedSame 36 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 6: highest → ta.highest
- Line 7: lowest → ta.lowest
⋯// © racer8-//@version=4+//@version=5strategy("Donchian Channels System", overlay=true)n = input(20,"Period")-hi = highest(n)+hi = ta.highest(n)-lo = lowest(n)+lo = ta.lowest(n)cl = close⋯Same 36 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.int()
⋯strategy("Donchian Channels System", overlay=true)-n = input(20,"Period")+n = input.int(20,"Period")hi = ta.highest(n)⋯Does it run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 36 before, 47 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © racer8-//@version=5+//@version=6strategy("Donchian Channels System", overlay=true)⋯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 |
|---|---|---|---|
| Period | 20 | int | hi lo |
What it calculates
- Highest (
highest,ta.highestin v5)Period (20)hi · line 6 - Lowest (
lowest,ta.lowestin v5)Period (20)lo · line 7
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 Buy long | cl>hi[1] | On | 11 |
| Open Sell short | cl<lo[1] | On | 13 |
| Close Buy | close<mid | On | 16 |
| Close Sell | close>mid | On | 18 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
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 | −3.2% | +102.5% | 36 | 33% | 0.97 | 43.6% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +5.3% | +5.5% | 32 | 31% | 1.19 | 9.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.3% | +9.8% | 35 | 37% | 1.05 | 1.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.2% | +8.4% | 32 | 31% | 1.14 | 0.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.1% | +16.7% | 36 | 28% | 0.81 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 31 | 35% | 1.31 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.1% | +148.3% | 36 | 33% | 0.83 | 0.7% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 36 | 28% | 1.02 | 0.1% | 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 | 11 | 36% | −$4,049.98 |
| Sideways market | 19 | 32% | −$5,272.59 |
| Falling market | 6 | 33% | $6,114.34 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
49.1%
The 95th percentile of the worst drawdown when the same 36 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 25.2% to 49.1%; the tick is what actually happened, 34.3%. Order luck alone moves it this much.
All 36 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 27 Feb 2024 54,476.50 | 20 Mar 2024 61,937.40 | 22 | $7,460.93 |
| short | 2 May 2024 58,365.00 | 4 May 2024 62,882.00 | 2 | −$4,517.04 |
| long | 16 May 2024 66,206.50 | 12 Jun 2024 67,314.20 | 27 | $1,107.72 |
| short | 15 Jun 2024 66,044.00 | 14 Jul 2024 59,204.00 | 29 | $6,839.98 |
| long | 16 Jul 2024 64,724.10 | 3 Aug 2024 61,498.30 | 18 | −$3,225.72 |
| short | 5 Aug 2024 58,161.00 | 9 Aug 2024 61,686.00 | 4 | −$3,525.00 |
| long | 24 Aug 2024 64,037.20 | 28 Aug 2024 59,415.00 | 4 | −$4,622.24 |
| short | 7 Sep 2024 53,963.00 | 14 Sep 2024 60,498.00 | 7 | −$6,535.02 |
| long | 19 Sep 2024 61,760.00 | 2 Oct 2024 60,804.90 | 13 | −$955.06 |
| long | 16 Oct 2024 67,074.10 | 4 Nov 2024 68,776.00 | 19 | $1,701.85 |
| long | 7 Nov 2024 75,572.00 | 10 Dec 2024 97,276.50 | 33 | $21,704.50 |
| long | 16 Dec 2024 104,464.00 | 20 Dec 2024 97,461.90 | 4 | −$7,002.13 |
| long | 18 Jan 2025 104,077.00 | 3 Feb 2025 97,700.60 | 16 | −$6,376.88 |
| short | 25 Feb 2025 91,552.90 | 3 Mar 2025 94,270.00 | 6 | −$2,717.11 |
| short | 7 Apr 2025 78,430.00 | 10 Apr 2025 82,615.20 | 3 | −$4,185.22 |
| long | 23 Apr 2025 93,443.00 | 30 May 2025 105,590.00 | 37 | $12,146.80 |
| short | 6 Jun 2025 101,509.00 | 10 Jun 2025 110,263.00 | 4 | −$8,754.33 |
| long | 10 Jul 2025 111,234.00 | 31 Jul 2025 117,840.00 | 21 | $6,606.29 |
| short | 2 Aug 2025 113,298.00 | 8 Aug 2025 117,472.00 | 6 | −$4,174.10 |
| long | 14 Aug 2025 123,306.00 | 16 Aug 2025 117,342.00 | 2 | −$5,964.40 |
| short | 26 Aug 2025 110,112.00 | 11 Sep 2025 113,960.00 | 16 | −$3,848.02 |
| long | 17 Sep 2025 116,789.00 | 23 Sep 2025 112,651.00 | 6 | −$4,137.97 |
| short | 26 Sep 2025 108,994.00 | 30 Sep 2025 114,312.00 | 4 | −$5,317.48 |
| long | 2 Oct 2025 118,595.00 | 11 Oct 2025 112,774.00 | 9 | −$5,820.50 |
| short | 5 Nov 2025 101,497.00 | 4 Dec 2025 93,429.90 | 29 | $8,067.28 |
| long | 5 Jan 2026 91,529.70 | 11 Jan 2026 90,504.70 | 6 | −$1,025.04 |
| long | 14 Jan 2026 95,414.00 | 21 Jan 2026 88,427.70 | 7 | −$6,986.33 |
| short | 26 Jan 2026 86,670.40 | 26 Feb 2026 67,988.00 | 31 | $18,682.30 |
| long | 5 Mar 2026 72,666.80 | 7 Mar 2026 68,114.00 | 2 | −$4,552.75 |
| long | 17 Mar 2026 74,884.70 | 21 Mar 2026 70,510.70 | 4 | −$4,373.94 |
| long | 14 Apr 2026 74,418.00 | 17 May 2026 78,148.10 | 33 | $3,730.05 |
| short | 23 May 2026 75,539.50 | 23 Jun 2026 64,020.00 | 31 | $11,519.50 |
| long | 15 Jul 2026 65,044.00 | 28 Jul 2026 63,755.90 | 13 | −$1,288.13 |
| long | 20 Aug 2026 69,334.80 | 11 Sep 2026 76,568.70 | 22 | $7,233.95 |
| short | 16 Sep 2026 75,644.50 | 19 Sep 2026 80,883.90 | 3 | −$5,239.38 |
| long | 22 Sep 2026 86,620.00 | 9 Oct 2026 81,754.50 | 17 | −$4,865.55 |
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 Channels System [racer8] this page | — | v4 strategy | −3.2% | 36 | 43.6% |
| 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% |
| eswaran ab by ESWARMONK2515 | lowest highest | v4 strategy | +8.4% | 13 | 39.6% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 9 ms | Yes 1,000 bars in 17 ms | Not measured |
| PineTS | No v5 and v6 only | — | 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. PineTS runs Pine Script v5 and v6 only.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © racer8//@version=4strategy("Donchian Channels System", overlay=true)n = input(20,"Period")hi = highest(n)lo = lowest(n)cl = closemid = 0.5*(hi + lo)if cl>hi[1] strategy.entry("Buy", strategy.long)if cl<lo[1] strategy.entry("Sell", strategy.short) if close<mid strategy.close("Buy")if close>mid strategy.close("Sell")plot(hi,color=color.blue)plot(lo,color=color.red)plot(mid,color=color.gray)