Pine Script v4 · Strategy
Donchian Channel Strategy Idea
A Pine Script v4 strategy built on Highest (high, 10) and Lowest (low, 10). It has 6 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade commits 100% of equity and pays 0.075% commission. 5 settings; defaults include High Period 10, Low Period 10 and Stop-loss (%) 5.0.
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 Channel Strategy Idea repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 19 of 19 trades.
- How did Donchian Channel Strategy Idea do on BTCUSDT 1D with default settings?
- −1.16%
- From 26 Jan 2024 to 21 Oct 2026, over 62 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Donchian Channel Strategy Idea 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 Channel Strategy Idea use a stop loss?
- Yes
- strategy.exit sets a stop on line 38, 39.
- Does Donchian Channel Strategy Idea run in Zeal Terminal?
- Yes
- It compiles in 11 ms and runs 1,000 bars in 29 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −1.16%
- −$11.56
- Buy and hold
- +102.5%
- same period
- Closed trades
- 62
- 21 won · 41 lost
- Profitable
- 33.9%
- of closed trades
- Profit factor
- 0.99
- gross profit ÷ gross loss
- Max drawdown
- 38.95%
- −$535.00
One run with the author's default settings, $1,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
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 28, 29, 31, 32. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 49
transp = on line 49. v5 folds the transparency into the colour with color.new(c, transp).
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.
- 4 strategy calls use the when = argument (line 28, 29, 31, 32). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 62 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 20: highest → ta.highest
- Line 21: lowest → ta.lowest
- Line 49: transp = 90 → color.new(colors, 90)
⋯// © QuantCT-//@version=4+//@version=5strategy("Donchian Channel Strategy Idea",⋯// ____ Logic-highest_high = highest(high, high_period)+highest_high = ta.highest(high, high_period)-lowest_low = lowest(low, low_period)+lowest_low = ta.lowest(low, low_period)base_line = (highest_high + lowest_low) / 2⋯plot(base_line, color=color.silver)-fill(highest_high_plot, lowest_low_plot, color=colors, transp=90)+fill(highest_high_plot, lowest_low_plot, color=color.new(colors, 90))Same 62 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 14: input() → input.int()
- Line 15: input() → input.int()
- Line 16: input() → input.bool()
- Line 17: input() → input.float()
- Line 18: input() → input.bool()
- Line 28: when = enter_long → if block
3 more edits
- Line 29: when = exit_long → if block
- Line 31: when = enter_short → if block
- Line 32: when = exit_short → if block
⋯// ____ Inputs-high_period = input(title="High Period", defval=10) +high_period = input.int(title="High Period", defval=10) -low_period = input(title="Low Period", defval=10)+low_period = input.int(title="Low Period", defval=10)-long_only = input(title="Long Only", defval=false)+long_only = input.bool(title="Long Only", defval=false)-slp = input(title="Stop-loss (%)", minval=1.0, maxval=25.0, defval=5.0)+slp = input.float(title="Stop-loss (%)", minval=1.0, maxval=25.0, defval=5.0)-use_sl = input(title="Use Stop-Loss", defval=false)+use_sl = input.bool(title="Use Stop-Loss", defval=false)// ____ Logic⋯exit_short = (close > base_line)-strategy.entry("Long", strategy.long, when=enter_long)+if enter_long+ strategy.entry("Long", strategy.long)-strategy.close("Long", when=exit_long) +if exit_long+ strategy.close("Long") if (not long_only)- strategy.entry("Short", strategy.short, when=enter_short)+ if enter_short+ strategy.entry("Short", strategy.short)- strategy.close("Short", when=exit_short) + if exit_short+ strategy.close("Short") ⋯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: 62 before, 98 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.
⋯// © QuantCT-//@version=5+//@version=6strategy("Donchian Channel Strategy Idea",⋯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 |
|---|---|---|---|
| High Period | 10 | int | highest_high |
| Low Period | 10 | int | lowest_low |
| Long Only | false | bool | entry Short close Short line 30 |
| Stop-loss (%) | 5.0 | float · min 1.0 · max 25.0 | sl_long sl_short |
| Use Stop-Loss | false | bool | exit SL exit SL line 37 |
What it calculates
- Highest (
highest,ta.highestin v5)high, High Period (10)highest_high · line 20 - Lowest (
lowest,ta.lowestin v5)low, Low Period (10)lowest_low · line 21
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 | enter_long(close > highest_high[1]) | On | 28 |
| Close Long | exit_long(close < base_line) | On | 29 |
| Open Short short | enter_short(close < lowest_low[1]) | On | 31 |
| Close Short | exit_short(close > base_line) | On | 32 |
| Exit SL stop | (use_sl) | On | 38 |
| Exit SL stop | (use_sl) | On | 39 |
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 | −1.2% | +102.5% | 62 | 34% | 0.99 | 39.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −13.2% | +5.5% | 65 | 26% | 0.77 | 19.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +16.3% | +9.8% | 60 | 35% | 1.05 | 42.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +4.7% | +8.4% | 58 | 29% | 1.06 | 26.7% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +26.3% | +16.7% | 59 | 39% | 1.12 | 46.6% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.5% | +21.5% | 64 | 33% | 1.01 | 15.9% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −22.3% | +148.3% | 63 | 27% | 0.87 | 57.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −7.0% | +17.1% | 62 | 31% | 0.89 | 19.9% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Settings heatmap
Two settings that feed the entry signal, moved around their defaults. The best in-sample pair (High Period 6, Low Period 17) made +19.3% before 26 Dec 2025 and +32.0% after it, against +7.3% for the defaults. 27% of the 64 combinations made money in-sample and 98% out-of-sample.
| Low Period ↓ · High Period → | 5 | 6 | 7 | 9 | 10 | 12 | 14 | 17 |
|---|---|---|---|---|---|---|---|---|
| 5 | −28% | −21% | −23% | −24% | −27% | −23% | −20% | −27% |
| 6 | −20% | −6% | −7% | −9% | −13% | −5% | −3% | −21% |
| 7 | −6% | +2% | +6% | −2% | −6% | −0% | +13% | −0% |
| 9 | −15% | −8% | −3% | −7% | −12% | −8% | −6% | −15% |
| 10 | −14% | −4% | +0% | −3% | −9% | −5% | −4% | −12% |
| 12 | −14% | −12% | −7% | −7% | −13% | −9% | −8% | −16% |
| 14 | −0% | +8% | +13% | +5% | −2% | +3% | +6% | +0% |
| 17 | +11% | +19% | +11% | +1% | −5% | +1% | +6% | +5% |
| Low Period ↓ · High Period → | 5 | 6 | 7 | 9 | 10 | 12 | 14 | 17 |
|---|---|---|---|---|---|---|---|---|
| 5 | +2% | +12% | +8% | +9% | +9% | +17% | +15% | +16% |
| 6 | +5% | +12% | +8% | +10% | +10% | +20% | +17% | +22% |
| 7 | +0% | +7% | +6% | +8% | +7% | +18% | +16% | +24% |
| 9 | +6% | +12% | +13% | +12% | +11% | +12% | +4% | +19% |
| 10 | +5% | +10% | +9% | +8% | +7% | +9% | +1% | +16% |
| 12 | +8% | +14% | +13% | +11% | +10% | +12% | −0% | +10% |
| 14 | +20% | +24% | +25% | +16% | +15% | +19% | +5% | +18% |
| 17 | +30% | +32% | +30% | +20% | +16% | +22% | +8% | +14% |
Net P&L of trades opened in each period, as a share of starting capital. marks the defaults and the best in-sample cell. Hover a cell for its trade count. 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 | 18 | 28% | −$81.81 |
| Sideways market | 32 | 38% | $100.17 |
| Falling market | 12 | 33% | −$29.91 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
53.2%
The 95th percentile of the worst drawdown when the same 62 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 27.7% to 53.2%; the tick is what actually happened, 36.1%. Order luck alone moves it this much.
All 62 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 8 Feb 2024 44,349.60 | 24 Feb 2024 50,744.20 | 16 | $142.47 |
| long | 27 Feb 2024 54,476.50 | 17 Mar 2024 65,300.60 | 19 | $224.95 |
| short | 20 Mar 2024 61,937.40 | 21 Mar 2024 67,840.50 | 1 | −$132.38 |
| long | 9 Apr 2024 71,620.00 | 13 Apr 2024 67,116.50 | 4 | −$79.39 |
| short | 14 Apr 2024 63,924.50 | 23 Apr 2024 66,819.30 | 9 | −$54.07 |
| short | 1 May 2024 60,672.00 | 4 May 2024 62,882.00 | 3 | −$41.78 |
| long | 16 May 2024 66,206.50 | 27 May 2024 68,507.70 | 11 | $35.19 |
| long | 6 Jun 2024 71,108.00 | 9 Jun 2024 69,310.50 | 3 | −$29.28 |
| short | 15 Jun 2024 66,044.00 | 1 Jul 2024 62,772.00 | 16 | $51.20 |
| short | 5 Jul 2024 57,050.00 | 13 Jul 2024 57,889.10 | 8 | −$18.10 |
| long | 15 Jul 2024 60,797.90 | 31 Jul 2024 66,188.00 | 16 | $95.62 |
| short | 3 Aug 2024 61,498.30 | 9 Aug 2024 61,686.00 | 6 | −$5.44 |
| long | 24 Aug 2024 64,037.20 | 28 Aug 2024 59,415.00 | 4 | −$87.48 |
| short | 2 Sep 2024 57,301.80 | 10 Sep 2024 57,042.00 | 8 | $3.34 |
| long | 14 Sep 2024 60,498.00 | 1 Oct 2024 63,327.60 | 17 | $49.94 |
| short | 2 Oct 2024 60,804.90 | 12 Oct 2024 62,540.00 | 10 | −$34.68 |
| long | 15 Oct 2024 66,084.00 | 26 Oct 2024 66,698.30 | 11 | $8.72 |
| long | 29 Oct 2024 69,962.20 | 3 Nov 2024 69,374.70 | 5 | −$11.16 |
| long | 7 Nov 2024 75,572.00 | 26 Nov 2024 93,010.00 | 19 | $255.84 |
| long | 16 Dec 2024 104,464.00 | 19 Dec 2024 100,204.00 | 3 | −$57.99 |
| long | 7 Jan 2025 102,236.00 | 8 Jan 2025 96,954.60 | 1 | −$69.83 |
| long | 18 Jan 2025 104,077.00 | 27 Jan 2025 102,620.00 | 9 | −$19.29 |
| short | 3 Feb 2025 97,700.60 | 4 Feb 2025 101,329.00 | 1 | −$47.38 |
| short | 25 Feb 2025 91,552.90 | 3 Mar 2025 94,270.00 | 6 | −$36.76 |
| long | 20 Mar 2025 86,845.90 | 29 Mar 2025 84,424.40 | 9 | −$33.52 |
| short | 7 Apr 2025 78,430.00 | 10 Apr 2025 82,615.20 | 3 | −$60.83 |
| long | 22 Apr 2025 87,516.20 | 5 May 2025 94,277.60 | 13 | $79.27 |
| long | 9 May 2025 103,262.00 | 30 May 2025 105,590.00 | 21 | $23.69 |
| short | 31 May 2025 103,985.00 | 8 Jun 2025 105,552.00 | 8 | −$19.07 |
| long | 10 Jun 2025 110,263.00 | 15 Jun 2025 105,415.00 | 5 | −$51.39 |
| short | 22 Jun 2025 102,120.00 | 24 Jun 2025 105,334.00 | 2 | −$35.62 |
| long | 30 Jun 2025 108,357.00 | 21 Jul 2025 117,265.00 | 21 | $84.20 |
| short | 2 Aug 2025 113,298.00 | 8 Aug 2025 117,472.00 | 6 | −$43.29 |
| long | 11 Aug 2025 119,294.00 | 15 Aug 2025 118,295.00 | 4 | −$10.71 |
| short | 20 Aug 2025 112,873.00 | 4 Sep 2025 111,706.00 | 15 | $9.51 |
| long | 11 Sep 2025 113,960.00 | 22 Sep 2025 115,232.00 | 11 | $10.46 |
| short | 23 Sep 2025 112,651.00 | 30 Sep 2025 114,312.00 | 7 | −$17.79 |
| long | 2 Oct 2025 118,595.00 | 11 Oct 2025 112,774.00 | 9 | −$54.40 |
| long | 27 Oct 2025 114,559.00 | 30 Oct 2025 110,021.00 | 3 | −$41.99 |
| short | 5 Nov 2025 101,497.00 | 11 Nov 2025 106,011.00 | 6 | −$45.09 |
| short | 15 Nov 2025 94,594.00 | 27 Nov 2025 90,484.00 | 12 | $39.25 |
| long | 4 Dec 2025 93,429.90 | 13 Dec 2025 90,268.40 | 9 | −$34.40 |
| short | 16 Dec 2025 86,432.10 | 22 Dec 2025 88,658.90 | 6 | −$25.64 |
| long | 5 Jan 2026 91,529.70 | 10 Jan 2026 90,641.30 | 5 | −$10.24 |
| long | 14 Jan 2026 95,414.00 | 19 Jan 2026 93,673.10 | 5 | −$17.84 |
| short | 21 Jan 2026 88,427.70 | 14 Feb 2026 68,854.00 | 24 | $194.98 |
| short | 24 Feb 2026 64,656.00 | 26 Feb 2026 67,988.00 | 2 | −$57.37 |
| long | 5 Mar 2026 72,666.80 | 7 Mar 2026 68,114.00 | 2 | −$65.63 |
| long | 17 Mar 2026 74,884.70 | 20 Mar 2026 69,930.00 | 3 | −$64.78 |
| short | 23 Mar 2026 67,859.00 | 6 Apr 2026 69,034.20 | 14 | −$16.82 |
| long | 8 Apr 2026 71,924.20 | 20 Apr 2026 73,801.80 | 12 | $21.55 |
| long | 5 May 2026 79,861.00 | 14 May 2026 79,313.60 | 9 | −$7.50 |
| short | 17 May 2026 78,148.10 | 26 May 2026 77,322.00 | 9 | $8.09 |
| short | 29 May 2026 73,617.50 | 13 Jun 2026 63,580.00 | 15 | $121.28 |
| long | 15 Jun 2026 65,746.40 | 19 Jun 2026 62,958.00 | 4 | −$44.75 |
| short | 25 Jun 2026 61,078.00 | 3 Jul 2026 61,560.00 | 8 | −$9.16 |
| long | 6 Jul 2026 63,650.00 | 14 Jul 2026 62,334.50 | 8 | −$21.40 |
| long | 15 Jul 2026 65,044.00 | 25 Jul 2026 64,140.00 | 10 | −$14.54 |
| long | 20 Aug 2026 69,334.80 | 2 Sep 2026 77,439.00 | 13 | $107.24 |
| short | 16 Sep 2026 75,644.50 | 19 Sep 2026 80,883.90 | 3 | −$73.45 |
| long | 19 Sep 2026 80,883.90 | 29 Sep 2026 83,500.00 | 10 | $29.73 |
| short | 9 Oct 2026 81,754.50 | 19 Oct 2026 82,116.00 | 10 | −$5.89 |
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 Channel Strategy Idea this page | — | v4 strategy | −1.2% | 62 | 39.0% |
| 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% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 11 ms | Yes 1,000 bars in 29 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/// © QuantCT//@version=4strategy("Donchian Channel Strategy Idea", shorttitle="Donchian", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=1000, commission_type=strategy.commission.percent, commission_value=0.075)// ____ Inputshigh_period = input(title="High Period", defval=10) low_period = input(title="Low Period", defval=10)long_only = input(title="Long Only", defval=false)slp = input(title="Stop-loss (%)", minval=1.0, maxval=25.0, defval=5.0)use_sl = input(title="Use Stop-Loss", defval=false)// ____ Logichighest_high = highest(high, high_period)lowest_low = lowest(low, low_period)base_line = (highest_high + lowest_low) / 2 enter_long = (close > highest_high[1])exit_long = (close < base_line)enter_short = (close < lowest_low[1])exit_short = (close > base_line)strategy.entry("Long", strategy.long, when=enter_long)strategy.close("Long", when=exit_long) if (not long_only) strategy.entry("Short", strategy.short, when=enter_short) strategy.close("Short", when=exit_short) // ____ SLsl_long = strategy.position_avg_price * (1- (slp/100))sl_short = strategy.position_avg_price * (1 + (slp/100))if (use_sl) strategy.exit(id="SL", from_entry="Long", stop=sl_long) strategy.exit(id="SL", from_entry="Short", stop=sl_short) // ____ Plotscolors = strategy.position_size > 0 ? #27D600 : strategy.position_size < 0 ? #E30202 : color.orangehighest_high_plot = plot(highest_high, color=colors)lowest_low_plot = plot(lowest_low, color=colors)plot(base_line, color=color.silver)fill(highest_high_plot, lowest_low_plot, color=colors, transp=90)