Pine Script v4 · Strategy
Donchian Channels Strategy by KrisWaters
A Pine Script v4 strategy built on Highest (high, 20), Lowest (low, 10) and ATR (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 100% of equity and pays 0.075% commission. 15 settings; defaults include From Day 1, From Month 1 and From Year 2017.
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 Strategy by KrisWaters 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 Channels Strategy by KrisWaters do on BTCUSDT 1D with default settings?
- +55.76%
- From 26 Jan 2024 to 21 Oct 2026, over 21 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Donchian Channels Strategy by KrisWaters 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 Strategy by KrisWaters use a stop loss?
- Yes
- strategy.exit sets a stop on line 52, 56.
- Does Donchian Channels Strategy by KrisWaters run in Zeal Terminal?
- Yes
- It compiles in 15 ms and runs 1,000 bars in 88 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +55.76%
- $557.56
- Buy and hold
- +102.5%
- same period
- Closed trades
- 21
- 9 won · 12 lost
- Profitable
- 42.9%
- of closed trades
- Profit factor
- 1.74
- gross profit ÷ gross loss
- Max drawdown
- 23.31%
- −$368.63
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
4 settings have no label
In the settings dialog longUpperLength (line 24), longLowerLength (line 25), shortUpperLength (line 26), shortLowerLength (line 27) show an empty name. The labels on this page come from the variable names.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses transp =, which v5 removed line 39
transp = on line 39, 40. 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, atr → ta.atr, max → math.max, min → math.min.
v4 → v5
VerifiedSame 21 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 29: highest → ta.highest
- Line 30: lowest → ta.lowest
- Line 31: highest → ta.highest
- Line 32: lowest → ta.lowest
- Line 39: transp = 95 → color.new(color.green, 95)
6 more edits
- Line 40: transp = 95 → color.new(color.red, 95)
- Line 43: atr → ta.atr
- Line 51: strategy.entry(…, true) → strategy.entry(…, strategy.long)
- Line 52: max → math.max
- Line 55: strategy.entry(…, false) → strategy.entry(…, strategy.short)
- Line 56: min → math.min
⋯// © kriswaters-//@version=4+//@version=5strategy("Donchian Channels Strategy by KrisWaters", overlay=true,⋯// Donchian indicator calculations-longUpperValue = highest(high,longUpperLength)+longUpperValue = ta.highest(high,longUpperLength)-longLowerValue = lowest(low,longLowerLength)+longLowerValue = ta.lowest(low,longLowerLength)-shortUpperValue = highest(high,shortUpperLength)+shortUpperValue = ta.highest(high,shortUpperLength)-shortLowerValue = lowest(low,shortLowerLength)+shortLowerValue = ta.lowest(low,shortLowerLength)// Plot Donchian Channels⋯// Styling-fill(uLong,lLong, color=color.green, transp=95, title="Long Arkaplan")+fill(uLong,lLong, color=color.new(color.green, 95), title="Long Arkaplan")-fill(uShort,lShort, color=color.red, transp=95, title="Short Arkaplan")+fill(uShort,lShort, color=color.new(color.red, 95), title="Short Arkaplan")// Stop-loss value calculationsatrMultiplier = 2.0-atrValue = atr(20)+atrValue = ta.atr(20)longStopValue = open - (atrMultiplier*atrValue)⋯if canEnterLong and na(longUpperValue) != true and na(longLowerValue) != true and window()- strategy.entry("Long", true, stop=longUpperValue)+ strategy.entry("Long", strategy.long, stop=longUpperValue)- strategy.exit("Long Exit", "Long", stop=useAtrAsStopRule ? max(longLowerValue,longStopValue) : longLowerValue)+ strategy.exit("Long Exit", "Long", stop=useAtrAsStopRule ? math.max(longLowerValue,longStopValue) : longLowerValue) if canEnterShort and na(shortUpperValue) != true and na(shortLowerValue) != true and window()- strategy.entry("Short", false, stop=shortLowerValue)+ strategy.entry("Short", strategy.short, stop=shortLowerValue)- strategy.exit("Short Exit", "Short", stop=useAtrAsStopRule ? min(shortUpperValue,shortStopValue) : shortUpperValue)+ strategy.exit("Short Exit", "Short", stop=useAtrAsStopRule ? math.min(shortUpperValue,shortStopValue) : shortUpperValue)Same 21 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 8: input() → input.int()
- Line 9: input() → input.int()
- Line 10: input() → input.int()
- Line 11: input() → input.int()
- Line 12: input() → input.int()
- Line 13: input() → input.int()
9 more edits
- Line 18: input() → input.bool()
- Line 19: input() → input.bool()
- Line 20: input() → input.bool()
- Line 21: input() → input.bool()
- Line 22: input() → input.bool()
- Line 24: input() → input.int()
- Line 25: input() → input.int()
- Line 26: input() → input.int()
- Line 27: input() → input.int()
⋯// Date filter-FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)+FromDay = input.int(defval = 1, title = "From Day", minval = 1, maxval = 31)-FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)+FromMonth = input.int(defval = 1, title = "From Month", minval = 1, maxval = 12)-FromYear = input(defval = 2017, title = "From Year", minval = 1900)+FromYear = input.int(defval = 2017, title = "From Year", minval = 1900)-ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)+ToDay = input.int(defval = 1, title = "To Day", minval = 1, maxval = 31)-ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)+ToMonth = input.int(defval = 1, title = "To Month", minval = 1, maxval = 12)-ToYear = input(defval = 9999, title = "To Year", minval = 2017)+ToYear = input.int(defval = 9999, title = "To Year", minval = 2017)start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window⋯// Strategy Settings-canEnterLong = input(true, title="Can Enter Long Position")+canEnterLong = input.bool(true, title="Can Enter Long Position")-canEnterShort = input(false, title="Can Enter Short Position")+canEnterShort = input.bool(false, title="Can Enter Short Position")-showLongChannel = input(true, title="Show Donchian Long Channels")+showLongChannel = input.bool(true, title="Show Donchian Long Channels")-showShortChannel = input(false , title="Show Donchian Short Channels")+showShortChannel = input.bool(false , title="Show Donchian Short Channels")-useAtrAsStopRule = input(false, title="Enable ATR Stop Rule") +useAtrAsStopRule = input.bool(false, title="Enable ATR Stop Rule") // DonCcian Channel Lengths-longUpperLength = input(20, minval=1)+longUpperLength = input.int(20, minval=1)-longLowerLength = input(10, minval=1)+longLowerLength = input.int(10, minval=1)-shortUpperLength = input(10, minval=1)+shortUpperLength = input.int(10, minval=1)-shortLowerLength = input(20, minval=1)+shortLowerLength = input.int(20, minval=1)// Donchian indicator calculations⋯Does it run in Pine Script v6?
Yes
Same 21 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.
⋯// © kriswaters-//@version=5+//@version=6strategy("Donchian Channels Strategy by KrisWaters", 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 |
|---|---|---|---|
| From Day | 1 | int · min 1 · max 31 | start |
| From Month | 1 | int · min 1 · max 12 | start |
| From Year | 2017 | int · min 1900 | start |
| To Day | 1 | int · min 1 · max 31 | finish |
| To Month | 1 | int · min 1 · max 12 | finish |
| To Year | 9999 | int · min 2017 | finish |
| Can Enter Long Position | true | bool | entry Long exit Long Exit line 50 |
| Can Enter Short Position | false | bool | entry Short exit Short Exit line 54 |
| Show Donchian Long Channels | true | bool | uLong lLong plot |
| Show Donchian Short Channels | false | bool | uShort lShort plot |
| Enable ATR Stop Rule | false | bool | plot exit Long Exit exit Short Exit |
| Long Upper Length (no label in the script) | 20 | int · min 1 | longUpperValue |
| Long Lower Length (no label in the script) | 10 | int · min 1 | longLowerValue |
| Short Upper Length (no label in the script) | 10 | int · min 1 | shortUpperValue |
| Short Lower Length (no label in the script) | 20 | int · min 1 | shortLowerValue |
What it calculates
- Highest (
highest,ta.highestin v5)high, Long Upper Length (20)longUpperValue · line 29 - Lowest (
lowest,ta.lowestin v5)low, Long Lower Length (10)longLowerValue · line 30 - Highest (
highest,ta.highestin v5)high, Short Upper Length (10)shortUpperValue · line 31 - Lowest (
lowest,ta.lowestin v5)low, Short Lower Length (20)shortLowerValue · line 32 - ATR (
atr,ta.atrin v5)20atrValue · line 43
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 | canEnterLong and na(longUpperValue) != true and na(longLowerValue) != true and window() | On | 51 |
| Exit Long Exit stop | canEnterLong and na(longUpperValue) != true and na(longLowerValue) != true and window() | On | 52 |
| Open Short short | canEnterShort and na(shortUpperValue) != true and na(shortLowerValue) != true and window() | On | 55 |
| Exit Short Exit stop | canEnterShort and na(shortUpperValue) != true and na(shortLowerValue) != true and window() | On | 56 |
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 | +55.8% | +102.5% | 21 | 43% | 1.74 | 23.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +5.2% | +5.5% | 20 | 35% | 1.19 | 11.2% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +65.2% | +9.8% | 20 | 40% | 1.48 | 37.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +22.8% | +8.4% | 17 | 41% | 1.89 | 7.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +148.3% | +16.7% | 19 | 53% | 2.49 | 32.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +10.2% | +21.5% | 20 | 30% | 1.26 | 12.6% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −32.5% | +148.3% | 26 | 31% | 0.70 | 60.8% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +7.1% | +17.1% | 22 | 32% | 1.32 | 11.6% | 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 | 7 | 57% | $55.18 |
| Sideways market | 10 | 40% | $550.60 |
| Falling market | 4 | 25% | −$48.23 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
34.0%
The 95th percentile of the worst drawdown when the same 21 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 15.2% to 34.0%; the tick is what actually happened, 19.7%. Order luck alone moves it this much.
All 21 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 15 Feb 2024 52,043.70 | 17 Mar 2024 64,780.00 | 31 | $242.86 |
| long | 8 Apr 2024 71,769.50 | 13 Apr 2024 64,493.10 | 5 | −$127.68 |
| long | 15 May 2024 65,500.00 | 11 Jun 2024 67,257.50 | 27 | $28.21 |
| long | 15 Jul 2024 63,861.80 | 1 Aug 2024 63,456.70 | 17 | −$8.96 |
| long | 23 Aug 2024 62,745.10 | 30 Aug 2024 57,860.00 | 7 | −$89.89 |
| long | 17 Sep 2024 61,167.00 | 1 Oct 2024 62,357.90 | 14 | $18.74 |
| long | 14 Oct 2024 66,498.00 | 5 Dec 2024 90,791.10 | 52 | $386.26 |
| long | 15 Dec 2024 104,088.00 | 20 Dec 2024 94,256.50 | 5 | −$138.88 |
| long | 17 Jan 2025 102,724.00 | 27 Jan 2025 99,550.00 | 10 | −$42.41 |
| long | 22 Apr 2025 88,500.00 | 30 May 2025 104,185.00 | 38 | $222.53 |
| long | 2 Jul 2025 108,952.00 | 25 Jul 2025 115,737.00 | 23 | $90.46 |
| long | 11 Aug 2025 120,248.00 | 18 Aug 2025 115,879.00 | 7 | −$59.74 |
| long | 15 Sep 2025 116,666.00 | 22 Sep 2025 114,384.00 | 7 | −$31.99 |
| long | 1 Oct 2025 117,900.00 | 10 Oct 2025 112,656.00 | 9 | −$68.38 |
| long | 9 Dec 2025 94,150.00 | 14 Dec 2025 87,719.30 | 5 | −$99.05 |
| long | 2 Jan 2026 90,634.60 | 20 Jan 2026 90,128.40 | 18 | −$9.35 |
| long | 4 Mar 2026 70,983.00 | 21 Mar 2026 68,793.40 | 17 | −$42.40 |
| long | 8 Apr 2026 72,761.00 | 15 May 2026 78,754.60 | 37 | $102.58 |
| long | 14 Jul 2026 64,700.00 | 28 Jul 2026 63,100.00 | 14 | −$35.96 |
| long | 19 Aug 2026 65,474.50 | 11 Sep 2026 76,264.00 | 23 | $217.98 |
| long | 21 Sep 2026 82,300.00 | 8 Oct 2026 82,563.00 | 17 | $2.63 |
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 Strategy by KrisWaters this page | — | v4 strategy | +55.8% | 21 | 23.3% |
| Aggresive Scalper-Swing Crypto Strategy by exlux99 | lowest highest atr | v4 strategy | −7.4% | 3 | 16.0% |
| Chandelier Exit - Strategy by melihtuna | atr highest lowest | v4 strategy | −34.8% | 11 | 43.2% |
| ETF trader by FX_minds | atr lowest highest | v4 strategy | +78.3% | 17 | 32.7% |
| Chandelier Exit by Zeal Terminal | atr highest lowest | v5 indicator | indicator | ||
| DC Breakout Strategy - This is simplicity at its finest. by Robrecht99 | highest lowest atr | v5 strategy | +10.8% | 57 | 8.3% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 15 ms | With a workaround 1,000 bars in 88 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as true, false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 51, 55) so the engine trades the way the script says.
“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/// © kriswaters//@version=4strategy("Donchian Channels Strategy by KrisWaters", overlay=true, initial_capital=1000, currency='USD', default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_type= strategy.commission.percent, commission_value=0.075) // Date filterFromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)FromYear = input(defval = 2017, title = "From Year", minval = 1900)ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)ToYear = input(defval = 9999, title = "To Year", minval = 2017)start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start windowfinish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish windowwindow() => time >= start and time <= finish ? true : false // create function "within window of time"// Strategy SettingscanEnterLong = input(true, title="Can Enter Long Position")canEnterShort = input(false, title="Can Enter Short Position")showLongChannel = input(true, title="Show Donchian Long Channels")showShortChannel = input(false , title="Show Donchian Short Channels")useAtrAsStopRule = input(false, title="Enable ATR Stop Rule") // DonCcian Channel LengthslongUpperLength = input(20, minval=1)longLowerLength = input(10, minval=1)shortUpperLength = input(10, minval=1)shortLowerLength = input(20, minval=1)// Donchian indicator calculationslongUpperValue = highest(high,longUpperLength)longLowerValue = lowest(low,longLowerLength)shortUpperValue = highest(high,shortUpperLength)shortLowerValue = lowest(low,shortLowerLength)// Plot Donchian ChannelsuLong = plot(showLongChannel ? longUpperValue : na, color=color.green, offset=1)lLong = plot(showLongChannel ? longLowerValue : na, color=color.green, offset=1)uShort = plot(showShortChannel ? shortUpperValue : na, color=color.red, offset=1)lShort = plot(showShortChannel ? shortLowerValue : na, color=color.red, offset=1)// Stylingfill(uLong,lLong, color=color.green, transp=95, title="Long Arkaplan")fill(uShort,lShort, color=color.red, transp=95, title="Short Arkaplan")// Stop-loss value calculationsatrMultiplier = 2.0atrValue = atr(20)longStopValue = open - (atrMultiplier*atrValue)shortStopValue = open + (atrMultiplier*atrValue)// Plot stop-loss lineplot(useAtrAsStopRule ? longStopValue : na, color=color.red, linewidth=2, offset=1)plot(useAtrAsStopRule ? shortStopValue : na, color=color.red, linewidth=2, offset=1)// Long and Short Position Rulesif canEnterLong and na(longUpperValue) != true and na(longLowerValue) != true and window() strategy.entry("Long", true, stop=longUpperValue) strategy.exit("Long Exit", "Long", stop=useAtrAsStopRule ? max(longLowerValue,longStopValue) : longLowerValue) if canEnterShort and na(shortUpperValue) != true and na(shortLowerValue) != true and window() strategy.entry("Short", false, stop=shortLowerValue) strategy.exit("Short Exit", "Short", stop=useAtrAsStopRule ? min(shortUpperValue,shortStopValue) : shortUpperValue)