Pine Script v4 · Strategy
KD compare strategy (交易策略對照組)
A Pine Script v4 strategy built on Stoch (close, high), Highest (high, 9) and Lowest (low, 9). It has 5 order rules and no entry of its own. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 7 settings; defaults include 庫存上限(share) 20, 庫存下限(share) -20 and KD 週期(KD period) 9.
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 KD compare strategy (交易策略對照組) repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 35 of 35 trades.
- How did KD compare strategy (交易策略對照組) do on BTCUSDT 1D with default settings?
- −110.08%
- From 26 Jan 2024 to 21 Oct 2026, over 153 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can KD compare strategy (交易策略對照組) 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 KD compare strategy (交易策略對照組) use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does KD compare strategy (交易策略對照組) run in Zeal Terminal?
- Yes
- It compiles in 49 ms and runs 1,000 bars in 90 ms, in the browser.
BTCUSDT · 1D · default settings
- Strong (轉強)
- Middle (中線)
- Weak (轉弱)
- S+1
- M+1
- W+1
- rate
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −110.08%
- −$110,081.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 153
- 88 won · 65 lost
- Profitable
- 57.5%
- of closed trades
- Profit factor
- 0.83
- gross profit ÷ gross loss
- Max drawdown
- 137.10%
- −$462,114.00
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A long position was still open at the last bar. 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.
-
Note
Uses an argument v6 removed
when = on line 130, 131, 132, 133, 134. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 135
transp = on line 135, 137, 144, 148, 152, 153, 158. v5 folds the transparency into the colour with color.new(c, transp).
-
Note
Uses long =, which v5 renamed line 131
long = on line 131, 132, 133, 134. v5 writes it as direction = strategy.long or strategy.short.
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 stoch → ta.stoch, highest → ta.highest, lowest → ta.lowest.
- 5 strategy calls use the when = argument (line 130, 131, 132, 133, 134). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 153 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: input(type = input.integer) → input.int()
- Line 7: input(type = input.integer) → input.int()
- Line 8: input(type = input.integer) → input.int()
- Line 9: input(type = input.float) → input.float()
- Line 35: stoch → ta.stoch
34 more edits
- Line 36: highest → ta.highest
- Line 37: lowest → ta.lowest
- Line 52: input(type = input.integer) → input.int()
- Line 53: input(type = input.integer) → input.int()
- Line 54: input(type = input.integer) → input.int()
- Line 62: highest → ta.highest
- Line 62: lowest → ta.lowest
- Line 62: lowest → ta.lowest
- Line 63: highest → ta.highest
- Line 63: lowest → ta.lowest
- Line 63: lowest → ta.lowest
- Line 64: highest → ta.highest
- Line 64: lowest → ta.lowest
- Line 64: lowest → ta.lowest
- Line 66: highest → ta.highest
- Line 66: lowest → ta.lowest
- Line 66: lowest → ta.lowest
- Line 67: highest → ta.highest
- Line 67: lowest → ta.lowest
- Line 67: lowest → ta.lowest
- Line 68: highest → ta.highest
- Line 68: lowest → ta.lowest
- Line 68: lowest → ta.lowest
- Line 131: long = true → direction = strategy.long
- Line 132: long = false → direction = strategy.short
- Line 133: long = true → direction = strategy.long
- Line 134: long = false → direction = strategy.short
- Line 135: transp = 100 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 137: transp = 100 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 144: transp = 100 → color.new(k_color, 100)
- Line 148: transp = 100 → color.new(k_color, 100)
- Line 152: transp = 100 → color.new(k_color, 100)
- Line 153: transp = 100 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 158: transp = 100 → color.new(plotColour, 100)
⋯// © Tonyder-//@version=4+//@version=5strategy("KD base strategy", overlay=true, pyramiding=1000, process_orders_on_close=true, precision=6, max_bars_back=720)//strategy("KD base strategy", overlay=true, pyramiding=1000, precision=6, max_bars_back=720)-max=input(defval=20, title="庫存上限(share)", type=input.integer)+max=input.int(defval=20, title="庫存上限(share)")-min=input(defval=-20, title="庫存下限(share)", type=input.integer)+min=input.int(defval=-20, title="庫存下限(share)")-period=input(defval=9, title="KD 週期(KD period)", type=input.integer, minval=2)+period=input.int(defval=9, title="KD 週期(KD period)", minval=2)-diff=input(defval=0.1, title="KD 週期(KD period)", type=input.float, minval=0.1)+diff=input.float(defval=0.1, title="KD 週期(KD period)", minval=0.1)k=0.0⋯share=strategy.position_size-rsv:=stoch(close, high, low, period)+rsv:=ta.stoch(close, high, low, period)-up:=highest(high,period)+up:=ta.highest(high,period)-bottom:=lowest(low,period)+bottom:=ta.lowest(low,period)if bar_index <= period⋯// filter-yearbegin=input(defval=1978, title="起始年 (begin year)", type=input.integer, minval=0)+yearbegin=input.int(defval=1978, title="起始年 (begin year)", minval=0)-monthbegin=input(defval=1, title="起始月 (begin month)", type=input.integer, minval=0)+monthbegin=input.int(defval=1, title="起始月 (begin month)", minval=0)-dayofmonthbegin=input(defval=1, title="起始日 (begin day)", type=input.integer, minval=0)+dayofmonthbegin=input.int(defval=1, title="起始日 (begin day)", minval=0)condition99=(year >= yearbegin) and (month >= monthbegin) and (dayofmonth > dayofmonthbegin)⋯// let k = 80, close = (3*80-k[1]*2)/100*(highest(high, period) - lowest(low, period)) + lowest(low, period)-k80:=(3*80-k[1]*2)/100*(highest(high, period) - lowest(low, period)) + lowest(low, period)+k80:=(3*80-k[1]*2)/100*(ta.highest(high, period) - ta.lowest(low, period)) + ta.lowest(low, period)-k50:=(3*50-k[1]*2)/100*(highest(high, period) - lowest(low, period)) + lowest(low, period)+k50:=(3*50-k[1]*2)/100*(ta.highest(high, period) - ta.lowest(low, period)) + ta.lowest(low, period)-k20:=(3*20-k[1]*2)/100*(highest(high, period) - lowest(low, period)) + lowest(low, period)+k20:=(3*20-k[1]*2)/100*(ta.highest(high, period) - ta.lowest(low, period)) + ta.lowest(low, period)// tomorrow k-k801:=(3*80-k*2)/100*(highest(high, period-1) - lowest(low, period-1)) + lowest(low, period-1)+k801:=(3*80-k*2)/100*(ta.highest(high, period-1) - ta.lowest(low, period-1)) + ta.lowest(low, period-1)-k501:=(3*50-k*2)/100*(highest(high, period-1) - lowest(low, period-1)) + lowest(low, period-1)+k501:=(3*50-k*2)/100*(ta.highest(high, period-1) - ta.lowest(low, period-1)) + ta.lowest(low, period-1)-k201:=(3*20-k*2)/100*(highest(high, period-1) - lowest(low, period-1)) + lowest(low, period-1)+k201:=(3*20-k*2)/100*(ta.highest(high, period-1) - ta.lowest(low, period-1)) + ta.lowest(low, period-1)if (dir == 1 and low < k50 and close < close[1] and open > close)⋯strategy.close_all(when=clean and condition99, comment="stop")-strategy.order(id='buy', long=true, qty=share2-share, limit=k501, when=share2 > share and condition99)+strategy.order(id='buy', direction = strategy.long, qty=share2-share, limit=k501, when=share2 > share and condition99)-strategy.order(id="sell", long=false, qty=share-share2, limit=k501, when=share2 < share and condition99)+strategy.order(id="sell", direction = strategy.short, qty=share-share2, limit=k501, when=share2 < share and condition99)-strategy.order(id='increase', long=true, qty=1, when=share2 != share and inc and condition99)+strategy.order(id='increase', direction = strategy.long, qty=1, when=share2 != share and inc and condition99)-strategy.order(id="decrease", long=false, qty=1, when=share2 != share and dec and condition99)+strategy.order(id="decrease", direction = strategy.short, qty=1, when=share2 != share and dec and condition99)-plot(share, "持股(share)",transp=100)+plot(share, "持股(share)")//plot(share2, "持股(share)",transp=100)-plot(dir, "方向(direction)",transp=100)+plot(dir, "方向(direction)")plot(k80, "Strong (轉強)", color.red)⋯ k_color:=color.red-plot(k801, "S+1", k_color, transp=100)+plot(k801, "S+1", color.new(k_color, 100))k_color:=color.black⋯ k_color:=color.white-plot(k501, "M+1", k_color, transp=100)+plot(k501, "M+1", color.new(k_color, 100))k_color:=color.black⋯ k_color:=color.green-plot(k201, title="W+1", color=k_color, transp=100)+plot(k201, title="W+1", color=color.new(k_color, 100))-plot(k, "k",transp=100)+plot(k, "k")// Colour background⋯ color.yellow-plot(series=rate*100, title="rate",color=plotColour, style=plot.style_histogram, linewidth=1, transp=100)+plot(series=rate*100, title="rate",color=color.new(plotColour, 100), style=plot.style_histogram, linewidth=1)//plot(last, "last",transp=100)Same 153 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 130: when = clean and condition99 → if block
- Line 131: when = share2 > share and condition99 → if block
- Line 132: when = share2 < share and condition99 → if block
- Line 133: when = share2 != share and inc and condition99 → if block
- Line 134: when = share2 != share and dec and condition99 → if block
⋯ last:= high-strategy.close_all(when=clean and condition99, comment="stop")+if clean and condition99+ strategy.close_all(comment="stop")-strategy.order(id='buy', direction = strategy.long, qty=share2-share, limit=k501, when=share2 > share and condition99)+if share2 > share and condition99+ strategy.order(id='buy', direction = strategy.long, qty=share2-share, limit=k501)-strategy.order(id="sell", direction = strategy.short, qty=share-share2, limit=k501, when=share2 < share and condition99)+if share2 < share and condition99+ strategy.order(id="sell", direction = strategy.short, qty=share-share2, limit=k501)-strategy.order(id='increase', direction = strategy.long, qty=1, when=share2 != share and inc and condition99)+if share2 != share and inc and condition99+ strategy.order(id='increase', direction = strategy.long, qty=1)-strategy.order(id="decrease", direction = strategy.short, qty=1, when=share2 != share and dec and condition99)+if share2 != share and dec and condition99+ strategy.order(id="decrease", direction = strategy.short, qty=1)plot(share, "持股(share)")⋯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: 153 before, 0 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.
⋯// © Tonyder-//@version=5+//@version=6strategy("KD base strategy", overlay=true, pyramiding=1000, process_orders_on_close=true, precision=6, max_bars_back=720)⋯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 |
|---|---|---|---|
| 庫存上限(share) | 20 | int (written input.integer) | share2 line 113 |
| 庫存下限(share) | -20 | int (written input.integer) | share2 line 115 |
| KD 週期(KD period) | 9 | int (written input.integer) · min 2 | rsv up bottom k80 k50 k20 k801 k501 k201 line 38 |
| KD 週期(KD period) | 0.1 | float · min 0.1 | line 81 line 84 line 100 line 104 |
| 起始年 (begin year) | 1978 | int (written input.integer) · min 0 | condition99 |
| 起始月 (begin month) | 1 | int (written input.integer) · min 0 | condition99 |
| 起始日 (begin day) | 1 | int (written input.integer) · min 0 | condition99 |
What it calculates
- Stoch (
stoch,ta.stochin v5)close, high, low, KD 週期(KD period) (9)rsv · line 35 - Highest (
highest,ta.highestin v5)high, KD 週期(KD period) (9)up · line 36 - Lowest (
lowest,ta.lowestin v5)low, KD 週期(KD period) (9)bottom · line 37 - Highest / Lowest (
highest,ta.highestin v5)high, KD 週期(KD period) (9)k80 · line 62 - Highest / Lowest (
highest,ta.highestin v5)high, KD 週期(KD period) (9)k50 · line 63 - Highest / Lowest (
highest,ta.highestin v5)high, KD 週期(KD period) (9)k20 · line 64 - Highest / Lowest (
highest,ta.highestin v5)high, period-1k801 · line 66 - Highest / Lowest (
highest,ta.highestin v5)high, period-1k501 · line 67 - Highest / Lowest (
highest,ta.highestin v5)high, period-1k201 · line 68
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 |
|---|---|---|---|
| Close | clean and condition99 | On | 130 |
| order buy long | share2 > share and condition99 | On | 131 |
| order sell short | share2 < share and condition99 | On | 132 |
| order increase long | share2 != share and inc and condition99 | On | 133 |
| order decrease short | share2 != share and dec and condition99 | On | 134 |
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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.
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 | −110.1% | +102.5% | 153 | 58% | 0.83 | 137.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +86.1% | +5.5% | 165 | 53% | 1.80 | 59.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −16.8% | +9.8% | 169 | 40% | 0.65 | 25.8% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +5.1% | +8.4% | 176 | 66% | 2.28 | 3.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.6% | +16.7% | 161 | 45% | 0.74 | 1.8% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.2% | +21.5% | 133 | 63% | 2.11 | 0.1% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −2.7% | +148.3% | 168 | 46% | 0.65 | 7.8% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.7% | +17.1% | 146 | 55% | 1.49 | 0.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 | 63 | 81% | $197,964.00 |
| Sideways market | 70 | 43% | −$167,139.00 |
| Falling market | 20 | 35% | −$140,906.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
98.1%
The 95th percentile of the worst drawdown when the same 153 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 95.9% to 98.1%; the tick is what actually happened, 99.2%. Order luck alone moves it this much.
The first 150 of 153 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 11 Feb 2024 48,300.00 | 4 Apr 2024 68,833.80 | 53 | $20,533.80 |
| long | 24 Feb 2024 50,744.20 | 4 Apr 2024 68,833.80 | 40 | $18,089.60 |
| long | 17 Mar 2024 65,300.60 | 4 Apr 2024 68,833.80 | 18 | $3,533.15 |
| long | 19 Mar 2024 67,610.00 | 4 Apr 2024 68,833.80 | 16 | $1,223.79 |
| long | 20 Mar 2024 61,937.40 | 4 Apr 2024 68,833.80 | 15 | $6,896.38 |
| long | 22 Mar 2024 65,501.30 | 4 Apr 2024 68,833.80 | 13 | $3,332.51 |
| long | 23 Mar 2024 63,796.60 | 23 Apr 2024 66,819.30 | 31 | $3,022.68 |
| long | 6 Apr 2024 67,820.60 | 26 Apr 2024 64,498.30 | 20 | −$3,322.30 |
| long | 13 Apr 2024 67,116.50 | 5 May 2024 63,892.00 | 22 | −$3,224.49 |
| long | 14 Apr 2024 63,924.50 | 6 May 2024 64,012.00 | 22 | $87.48 |
| long | 16 Apr 2024 63,420.00 | 10 May 2024 63,074.00 | 24 | −$345.99 |
| long | 18 Apr 2024 61,277.40 | 15 May 2024 63,404.90 | 27 | $2,127.52 |
| long | 29 Apr 2024 63,118.60 | 18 May 2024 67,024.00 | 19 | $3,905.38 |
| long | 1 May 2024 60,672.00 | 31 May 2024 69,031.80 | 30 | $8,359.76 |
| long | 3 May 2024 59,060.60 | 4 Jun 2024 69,523.20 | 32 | $10,462.60 |
| long | 29 May 2024 68,398.40 | 5 Jun 2024 70,537.80 | 7 | $2,139.43 |
| long | 30 May 2024 67,652.40 | 6 Jun 2024 71,108.00 | 7 | $3,455.59 |
| long | 1 Jun 2024 67,540.00 | 3 Jul 2024 62,135.50 | 32 | −$5,404.55 |
| long | 3 Jun 2024 67,765.60 | 13 Jul 2024 57,889.10 | 40 | −$9,876.53 |
| long | 12 Jun 2024 67,314.20 | 14 Jul 2024 59,204.00 | 32 | −$8,110.22 |
| long | 15 Jun 2024 66,044.00 | 15 Jul 2024 60,797.90 | 30 | −$5,246.08 |
| long | 18 Jun 2024 66,504.30 | 16 Jul 2024 64,724.10 | 28 | −$1,780.27 |
| long | 19 Jun 2024 65,175.30 | 27 Jul 2024 67,908.00 | 38 | $2,732.68 |
| long | 20 Jun 2024 64,974.40 | 11 Aug 2024 60,923.50 | 52 | −$4,050.86 |
| long | 21 Jun 2024 64,870.00 | 13 Aug 2024 59,346.60 | 53 | −$5,523.35 |
| long | 1 Aug 2024 64,628.00 | 14 Aug 2024 60,587.20 | 13 | −$4,040.85 |
| long | 3 Aug 2024 61,498.30 | 17 Aug 2024 58,874.60 | 14 | −$2,623.75 |
| long | 4 Aug 2024 60,698.00 | 18 Aug 2024 59,492.00 | 14 | −$1,206.00 |
| long | 5 Aug 2024 58,161.00 | 20 Aug 2024 59,438.50 | 15 | $1,277.50 |
| short | 22 Aug 2024 61,156.00 | 29 Aug 2024 59,034.90 | 7 | $2,121.13 |
| short | 24 Aug 2024 64,037.20 | 31 Aug 2024 59,124.00 | 7 | $4,913.25 |
| short | 25 Aug 2024 64,157.00 | 1 Sep 2024 58,974.00 | 7 | $5,183.02 |
| long | 3 Sep 2024 59,132.10 | 13 Sep 2024 58,132.30 | 10 | −$999.81 |
| short | 14 Sep 2024 60,498.00 | 3 Oct 2024 60,649.30 | 19 | −$151.28 |
| long | 3 Oct 2024 60,649.30 | 13 Oct 2024 63,206.20 | 10 | $2,556.96 |
| long | 6 Oct 2024 62,058.00 | 15 Oct 2024 66,084.00 | 9 | $4,025.99 |
| long | 8 Oct 2024 62,224.00 | 16 Oct 2024 67,074.10 | 8 | $4,850.13 |
| long | 9 Oct 2024 62,160.50 | 17 Oct 2024 67,620.00 | 8 | $5,459.50 |
| long | 10 Oct 2024 60,636.00 | 27 Oct 2024 67,239.90 | 17 | $6,603.92 |
| long | 11 Oct 2024 60,326.40 | 26 Nov 2024 93,010.00 | 46 | $32,683.60 |
| long | 26 Oct 2024 66,698.30 | 26 Nov 2024 93,010.00 | 31 | $26,311.70 |
| long | 4 Nov 2024 68,776.00 | 26 Nov 2024 93,010.00 | 22 | $24,234.00 |
| long | 5 Nov 2024 67,850.00 | 4 Jan 2025 98,174.20 | 60 | $30,324.20 |
| long | 20 Dec 2024 97,461.90 | 5 Jan 2025 98,220.50 | 16 | $758.65 |
| long | 22 Dec 2024 97,292.00 | 6 Jan 2025 98,363.60 | 15 | $1,071.61 |
| long | 23 Dec 2024 95,186.30 | 7 Jan 2025 102,236.00 | 15 | $7,049.32 |
| long | 24 Dec 2024 94,881.50 | 8 Jan 2025 96,954.60 | 15 | $2,073.13 |
| long | 27 Dec 2024 95,791.60 | 8 Jan 2025 96,954.60 | 12 | $1,163.00 |
| long | 28 Dec 2024 94,299.00 | 8 Jan 2025 96,954.60 | 11 | $2,655.57 |
| long | 30 Dec 2024 93,738.20 | 8 Jan 2025 96,954.60 | 9 | $3,216.41 |
| long | 31 Dec 2024 92,792.10 | 16 Jan 2025 100,497.00 | 16 | $7,705.30 |
| long | 9 Jan 2025 95,060.60 | 18 Jan 2025 104,077.00 | 9 | $9,016.86 |
| long | 10 Jan 2025 92,552.50 | 19 Jan 2025 104,556.00 | 9 | $12,003.70 |
| long | 12 Jan 2025 94,600.00 | 20 Feb 2025 96,644.40 | 39 | $2,044.38 |
| long | 13 Jan 2025 94,545.10 | 21 Feb 2025 98,305.00 | 39 | $3,759.94 |
| long | 14 Jan 2025 94,536.10 | 6 Mar 2025 90,606.00 | 51 | −$3,930.11 |
| long | 28 Jan 2025 102,083.00 | 25 Mar 2025 87,498.20 | 56 | −$14,584.70 |
| long | 29 Jan 2025 101,336.00 | 28 Mar 2025 87,232.00 | 58 | −$14,103.50 |
| long | 3 Feb 2025 97,700.60 | 19 Apr 2025 84,474.70 | 75 | −$26,451.80 |
| long | 5 Feb 2025 97,763.10 | 19 Apr 2025 84,474.70 | 73 | −$13,288.40 |
| long | 6 Feb 2025 96,612.40 | 19 Apr 2025 84,474.70 | 72 | −$12,137.70 |
| long | 7 Feb 2025 96,554.40 | 19 Apr 2025 84,474.70 | 71 | −$12,079.70 |
| long | 8 Feb 2025 96,506.80 | 19 Apr 2025 84,474.70 | 70 | −$12,032.10 |
| long | 9 Feb 2025 96,444.80 | 19 Apr 2025 84,474.70 | 69 | −$11,970.10 |
| long | 12 Feb 2025 95,778.20 | 19 Apr 2025 84,474.70 | 66 | −$11,303.50 |
| long | 14 Feb 2025 96,608.10 | 19 Apr 2025 84,474.70 | 64 | −$12,133.40 |
| long | 17 Feb 2025 96,118.10 | 19 Apr 2025 84,474.70 | 61 | −$11,643.40 |
| long | 18 Feb 2025 95,780.00 | 19 Apr 2025 84,474.70 | 60 | −$11,305.30 |
| long | 19 Feb 2025 95,671.70 | 19 Apr 2025 84,474.70 | 59 | −$11,197.00 |
| long | 23 Feb 2025 95,561.00 | 19 Apr 2025 84,474.70 | 55 | −$11,086.30 |
| long | 25 Feb 2025 91,552.90 | 19 Apr 2025 84,474.70 | 53 | −$7,078.18 |
| long | 26 Feb 2025 88,680.40 | 19 Apr 2025 84,474.70 | 52 | −$4,205.69 |
| long | 27 Feb 2025 84,250.10 | 19 Apr 2025 84,474.70 | 51 | $224.61 |
| long | 1 Mar 2025 84,349.90 | 19 Apr 2025 84,474.70 | 49 | $124.75 |
| long | 10 Mar 2025 80,734.50 | 19 Apr 2025 84,474.70 | 40 | $3,740.22 |
| long | 30 Mar 2025 82,648.50 | 19 Apr 2025 84,474.70 | 20 | $1,826.17 |
| long | 31 Mar 2025 82,390.00 | 13 Jun 2025 105,672.00 | 74 | $23,281.70 |
| long | 30 May 2025 105,590.00 | 13 Jun 2025 105,672.00 | 14 | $81.99 |
| long | 31 May 2025 103,985.00 | 13 Jun 2025 105,672.00 | 13 | $1,686.27 |
| long | 4 Jun 2025 105,377.00 | 13 Jun 2025 105,672.00 | 9 | $294.84 |
| long | 5 Jun 2025 104,697.00 | 13 Jun 2025 105,672.00 | 8 | $974.88 |
| long | 6 Jun 2025 101,509.00 | 30 Jun 2025 108,357.00 | 24 | $6,848.24 |
| long | 18 Jun 2025 104,551.00 | 3 Jul 2025 108,850.00 | 15 | $4,298.42 |
| long | 20 Jun 2025 104,659.00 | 3 Jul 2025 108,850.00 | 13 | $4,191.00 |
| long | 21 Jun 2025 103,298.00 | 3 Jul 2025 108,850.00 | 12 | $5,551.61 |
| long | 22 Jun 2025 102,120.00 | 3 Jul 2025 108,850.00 | 11 | $6,729.57 |
| long | 23 Jun 2025 100,964.00 | 3 Jul 2025 108,850.00 | 10 | $7,885.72 |
| long | 3 Jul 2025 109,585.00 | 11 Aug 2025 119,294.00 | 39 | $9,709.49 |
| long | 21 Jul 2025 117,265.00 | 13 Aug 2025 120,134.00 | 23 | $2,868.98 |
| long | 24 Jul 2025 118,736.00 | 14 Aug 2025 123,306.00 | 21 | $4,570.74 |
| long | 25 Jul 2025 118,341.00 | 15 Aug 2025 118,295.00 | 21 | −$45.89 |
| long | 26 Jul 2025 117,614.00 | 15 Aug 2025 118,295.00 | 20 | $680.78 |
| long | 31 Jul 2025 116,466.00 | 15 Aug 2025 118,295.00 | 15 | $1,828.77 |
| long | 1 Aug 2025 115,764.00 | 15 Aug 2025 118,295.00 | 14 | $2,531.02 |
| long | 3 Aug 2025 112,546.00 | 15 Aug 2025 118,295.00 | 12 | $11,497.50 |
| long | 6 Aug 2025 114,130.00 | 4 Sep 2025 111,706.00 | 29 | −$2,424.03 |
| long | 19 Aug 2025 116,227.00 | 8 Sep 2025 111,137.00 | 20 | −$5,089.70 |
| long | 20 Aug 2025 112,873.00 | 9 Sep 2025 112,065.00 | 20 | −$807.72 |
| short | 11 Sep 2025 113,960.00 | 22 Sep 2025 115,232.00 | 11 | −$1,272.29 |
| short | 12 Sep 2025 115,483.00 | 23 Sep 2025 112,651.00 | 11 | $2,831.70 |
| long | 24 Sep 2025 111,999.00 | 3 Oct 2025 120,529.00 | 9 | $8,530.55 |
| long | 26 Sep 2025 108,994.00 | 3 Oct 2025 120,529.00 | 7 | $11,534.90 |
| short | 4 Oct 2025 122,232.00 | 12 Oct 2025 110,644.00 | 8 | $11,587.80 |
| long | 15 Oct 2025 112,894.00 | 28 Nov 2025 91,333.90 | 44 | −$21,560.40 |
| long | 16 Oct 2025 110,763.00 | 3 Dec 2025 91,277.90 | 48 | −$19,485.40 |
| long | 17 Oct 2025 108,194.00 | 4 Dec 2025 93,429.90 | 48 | −$14,764.30 |
| long | 18 Oct 2025 106,432.00 | 8 Dec 2025 90,395.30 | 51 | −$16,036.40 |
| long | 22 Oct 2025 108,298.00 | 9 Dec 2025 90,634.40 | 48 | −$17,663.30 |
| long | 23 Oct 2025 107,567.00 | 10 Dec 2025 92,678.80 | 48 | −$14,888.60 |
| long | 31 Oct 2025 108,323.00 | 12 Dec 2025 92,513.40 | 42 | −$15,809.50 |
| long | 4 Nov 2025 106,583.00 | 25 Dec 2025 87,669.40 | 51 | −$18,913.60 |
| long | 5 Nov 2025 101,497.00 | 27 Dec 2025 87,966.40 | 52 | −$13,530.80 |
| long | 7 Nov 2025 101,346.00 | 29 Dec 2025 88,901.90 | 52 | −$12,444.10 |
| long | 9 Nov 2025 102,313.00 | 3 Jan 2026 89,995.10 | 55 | −$12,317.80 |
| long | 12 Nov 2025 103,059.00 | 4 Jan 2026 90,628.00 | 53 | −$12,431.00 |
| long | 13 Nov 2025 101,654.00 | 5 Jan 2026 91,529.70 | 53 | −$10,124.60 |
| long | 14 Nov 2025 99,692.00 | 6 Jan 2026 93,859.70 | 53 | −$5,832.32 |
| long | 15 Nov 2025 94,594.00 | 15 Feb 2026 69,822.90 | 92 | −$24,771.10 |
| long | 11 Jan 2026 90,504.70 | 17 Feb 2026 68,892.40 | 37 | −$21,612.30 |
| long | 20 Jan 2026 92,631.00 | 26 Feb 2026 67,988.00 | 37 | −$24,643.00 |
| long | 21 Jan 2026 88,427.70 | 1 Mar 2026 66,973.30 | 39 | −$21,454.40 |
| long | 25 Jan 2026 89,225.30 | 3 Mar 2026 68,830.10 | 37 | −$20,395.30 |
| long | 26 Jan 2026 86,670.40 | 5 Mar 2026 72,666.80 | 38 | −$14,003.60 |
| short | 11 Mar 2026 70,427.10 | 20 Mar 2026 69,930.00 | 9 | $497.05 |
| short | 12 Mar 2026 70,191.90 | 22 Mar 2026 68,918.10 | 10 | $1,273.74 |
| short | 13 Mar 2026 70,541.30 | 23 Mar 2026 67,859.00 | 10 | $2,682.34 |
| short | 14 Mar 2026 70,930.00 | 25 Mar 2026 70,556.70 | 11 | $373.27 |
| short | 15 Mar 2026 71,211.90 | 27 Mar 2026 68,820.30 | 12 | $2,391.64 |
| short | 16 Mar 2026 72,815.30 | 30 Apr 2026 75,780.00 | 45 | −$2,964.75 |
| short | 17 Mar 2026 74,884.70 | 14 May 2026 79,313.60 | 58 | −$4,428.94 |
| short | 6 Apr 2026 69,034.20 | 16 May 2026 79,113.20 | 40 | −$10,079.00 |
| short | 8 Apr 2026 71,924.20 | 17 May 2026 78,148.10 | 39 | −$6,223.83 |
| short | 10 Apr 2026 71,788.00 | 18 May 2026 77,457.70 | 38 | −$5,669.69 |
| short | 11 Apr 2026 72,962.70 | 19 May 2026 77,001.90 | 38 | −$4,039.17 |
| long | 20 May 2026 76,834.40 | 26 May 2026 77,322.00 | 6 | $487.65 |
| short | 2 Jun 2026 66,760.80 | 23 Jun 2026 64,020.00 | 21 | $2,740.82 |
| short | 14 Jun 2026 64,458.00 | 24 Jun 2026 62,734.60 | 10 | $1,723.44 |
| short | 15 Jun 2026 65,746.40 | 25 Jun 2026 61,078.00 | 10 | $4,668.45 |
| short | 16 Jun 2026 66,328.70 | 28 Jul 2026 63,755.90 | 42 | $2,572.88 |
| short | 22 Jun 2026 65,062.90 | 1 Aug 2026 62,887.90 | 40 | $2,174.99 |
| short | 4 Jul 2026 62,583.30 | 3 Aug 2026 63,570.00 | 30 | −$986.75 |
| short | 5 Jul 2026 63,144.00 | 4 Aug 2026 63,520.00 | 30 | −$375.99 |
| short | 6 Jul 2026 63,650.00 | 13 Aug 2026 63,480.00 | 38 | $170.02 |
| short | 7 Jul 2026 64,042.90 | 15 Aug 2026 63,043.60 | 39 | $999.37 |
| long | 17 Aug 2026 62,900.00 | 19 Sep 2026 80,883.90 | 33 | $17,983.90 |
| long | 3 Sep 2026 77,340.00 | 20 Sep 2026 81,250.00 | 17 | $3,909.98 |
| long | 3 Sep 2026 77,340.00 | 22 Sep 2026 86,620.00 | 19 | $9,280.00 |
| long | 8 Sep 2026 78,623.30 | 25 Sep 2026 84,410.20 | 17 | $5,786.93 |
| long | 9 Sep 2026 78,455.80 | 25 Sep 2026 84,410.20 | 16 | $5,954.44 |
| long | 10 Sep 2026 78,306.40 | 25 Sep 2026 84,410.20 | 15 | $6,103.81 |
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 |
|---|---|---|---|---|---|
| KD compare strategy (交易策略對照組) this page | — | v4 strategy | −110.1% | 153 | 137.1% |
| 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 49 ms | With a workaround 1,000 bars in 90 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true, long = false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 131, 132, 133, 134) so the engine trades the way the script says. The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“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/// © Tonyder//@version=4strategy("KD base strategy", overlay=true, pyramiding=1000, process_orders_on_close=true, precision=6, max_bars_back=720)//strategy("KD base strategy", overlay=true, pyramiding=1000, precision=6, max_bars_back=720)max=input(defval=20, title="庫存上限(share)", type=input.integer)min=input(defval=-20, title="庫存下限(share)", type=input.integer)period=input(defval=9, title="KD 週期(KD period)", type=input.integer, minval=2)diff=input(defval=0.1, title="KD 週期(KD period)", type=input.float, minval=0.1)k=0.0rsv=0.0dir=0sum=0.0share2=0.0first=0up=0.0bottom=0.0k80=0.0k50=0.0k20=0.0k801=0.0k501=0.0k201=0.0show=0show:=0rate=0.0last=0.0inc=falsedec=falseinc:=falsedec:=falseclean=falseclean:=falseshare=strategy.position_sizersv:=stoch(close, high, low, period)up:=highest(high,period)bottom:=lowest(low,period)if bar_index <= period k:=rsv dir:=0 sum:=0 share2:=0 last:=closeelse k:=k[1]*2/3 + rsv/3 dir := dir[1] sum := sum[1] share2:=share2[1] rate:= rate[1] last:= last[1]// filteryearbegin=input(defval=1978, title="起始年 (begin year)", type=input.integer, minval=0)monthbegin=input(defval=1, title="起始月 (begin month)", type=input.integer, minval=0)dayofmonthbegin=input(defval=1, title="起始日 (begin day)", type=input.integer, minval=0)condition99=(year >= yearbegin) and (month >= monthbegin) and (dayofmonth > dayofmonthbegin)// rsv = 100 * (close - lowest(low, period)) / (highest(high, period) - lowest(low, period))// k=k[1]*2/3 + rsv/3// 3k=k[1]*2 + rsv// 3k-k[1]*2= 100 * (close - lowest(low, period)) / (highest(high, period) - lowest(low, period))// (3k-k[1]*2)/100*(highest(high, period) - lowest(low, period)) + lowest(low, period) = close// let k = 80, close = (3*80-k[1]*2)/100*(highest(high, period) - lowest(low, period)) + lowest(low, period)k80:=(3*80-k[1]*2)/100*(highest(high, period) - lowest(low, period)) + lowest(low, period)k50:=(3*50-k[1]*2)/100*(highest(high, period) - lowest(low, period)) + lowest(low, period)k20:=(3*20-k[1]*2)/100*(highest(high, period) - lowest(low, period)) + lowest(low, period)// tomorrow kk801:=(3*80-k*2)/100*(highest(high, period-1) - lowest(low, period-1)) + lowest(low, period-1)k501:=(3*50-k*2)/100*(highest(high, period-1) - lowest(low, period-1)) + lowest(low, period-1)k201:=(3*20-k*2)/100*(highest(high, period-1) - lowest(low, period-1)) + lowest(low, period-1)if (dir == 1 and low < k50 and close < close[1] and open > close) // rule1, strong target, buy at low price sum := sum + 1else if (dir == -1 and high > k50 and close > close[1] and open < close) // rule2, weak target, sell at high price sum := sum - 1else if (dir == 1 and k[1] > 80 and k < 80 and share > 3) // rule5, end the positive order sum := 1else if (dir == -1 and k[1] < 20 and k > 20 and share < -3) // rule6, end the engitive order sum := -1else if (dir == 1 and high > k50 and close > close[1] and rate < -diff/100 and share > 2) // rule7, reduce the order when the rate is poor sum := sum - 1else if (dir == -1 and low < k50 and close < close[1] and rate > diff/100 and share < -2) // rule8, reduce the order when the rate is poor sum := sum + 1// become to strong if (k >= 80) dir := 1// become to weakif (k <= 20) dir := -1// rule 3, strong become to weak, clean when k < 20if (dir == 1 and share < 0 and close > last and k < 20) clean := true// rule 4, weak become to strong, clean when k > 80if (dir == -1 and share > 0 and close < last and k > 80) clean := true// rule 9, the first negtive order if (dir == 1 and k > 80 and rate > diff/100 and share == 0) inc:= true sum := 1// rule 10, the first negtive orderif (dir == -1 and k < 20 and rate < -diff/100 and share == 0) dec:= true sum := -1if sum > 0 share2 := sumelse if sum < 0 share2 := sumelse share2 := 0if share2 > max share2 := maxif share2 < min share2 := min if (k > 80 and k[1] < 80) rate:= high/last - 1 last:= highif (k < 20 and k[1] > 20) rate:= low/last - 1 last:= lowif (dir > 0 and low < k50 and low[1] > k50) rate:= low/last - 1 last:= lowif (dir < 0 and high > k50 and high[1] <k50) rate:= high/last - 1 last:= highstrategy.close_all(when=clean and condition99, comment="stop")strategy.order(id='buy', long=true, qty=share2-share, limit=k501, when=share2 > share and condition99)strategy.order(id="sell", long=false, qty=share-share2, limit=k501, when=share2 < share and condition99)strategy.order(id='increase', long=true, qty=1, when=share2 != share and inc and condition99)strategy.order(id="decrease", long=false, qty=1, when=share2 != share and dec and condition99)plot(share, "持股(share)",transp=100)//plot(share2, "持股(share)",transp=100)plot(dir, "方向(direction)",transp=100)plot(k80, "Strong (轉強)", color.red)plot(k50, "Middle (中線)", color.white)plot(k20, "Weak (轉弱)", color.green)k_color=color.blackif k80 != k801[1] k_color:=color.redplot(k801, "S+1", k_color, transp=100)k_color:=color.blackif k50 != k501[1] k_color:=color.whiteplot(k501, "M+1", k_color, transp=100)k_color:=color.blackif k20 != k201[1] k_color:=color.greenplot(k201, title="W+1", color=k_color, transp=100)plot(k, "k",transp=100)// Colour backgroundplotColour = rate > 0 ? color.red : rate < 0 ? color.green : color.yellowplot(series=rate*100, title="rate",color=plotColour, style=plot.style_histogram, linewidth=1, transp=100)//plot(last, "last",transp=100)