Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (7)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOct$10,481$202,527Equity from $100,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots. 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.
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

Verified

Same 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)

Clean v5

VerifiedDownload the v5 script

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.

SettingDefaultType and rangeFeeds
庫存上限(share)20int (written input.integer)share2 line 113
庫存下限(share)-20int (written input.integer)share2 line 115
KD 週期(KD period)9int (written input.integer) · min 2rsv up bottom k80 k50 k20 k801 k501 k201 line 38
KD 週期(KD period)0.1float · min 0.1line 81 line 84 line 100 line 104
起始年 (begin year)1978int (written input.integer) · min 0condition99
起始月 (begin month)1int (written input.integer) · min 0condition99
起始日 (begin day)1int (written input.integer) · min 0condition99

What it calculates

  • Stoch (stoch, ta.stoch in v5)close, high, low, KD 週期(KD period) (9)rsv · line 35
  • Highest (highest, ta.highest in v5)high, KD 週期(KD period) (9)up · line 36
  • Lowest (lowest, ta.lowest in v5)low, KD 週期(KD period) (9)bottom · line 37
  • Highest / Lowest (highest, ta.highest in v5)high, KD 週期(KD period) (9)k80 · line 62
  • Highest / Lowest (highest, ta.highest in v5)high, KD 週期(KD period) (9)k50 · line 63
  • Highest / Lowest (highest, ta.highest in v5)high, KD 週期(KD period) (9)k20 · line 64
  • Highest / Lowest (highest, ta.highest in v5)high, period-1k801 · line 66
  • Highest / Lowest (highest, ta.highest in v5)high, period-1k501 · line 67
  • Highest / Lowest (highest, ta.highest in 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.

closecloseprice datalowlowprice datahighhighprice dataperiodKD 週期(KD period)setting · 9max庫存上限(share)setting · 20min庫存下限(share)setting · -20yearbegin起始年 (begin year)setting · 1978monthbegin起始月 (begin month)setting · 1dayofmonthbegin起始日 (begin day)setting · 1rsvStoch→ rsvupHighest→ upbottomLowest→ bottomk80Highest / Lowest→ k80k50Highest / Lowest→ k50k20Highest / Lowest→ k20k801Highest / Lowest→ k801k501Highest / Lowest→ k501k201Highest / Lowest→ k201kkvaluedirdirvaluesumsumvalueshare2share2valuerateratevaluelastlastvaluesharesharevaluecondition99condition99conditionk_color, plotColourColourk_color · plotColourclose_all:close_all strategy.close_allorder:buyorder buystrategy.orderorder:sellorder sellstrategy.orderorder:increaseorder increasestrategy.orderorder:decreaseorder decreasestrategy.orderplot:135持股(share)plotplot:137方向(direction)plotplot:138Strong (轉強)plotplot:139Middle (中線)plotplot:140Weak (轉弱)plotplot:144S+1plotplot:148M+1plotplot:152W+1plotplot:153kplotplot:158rateplot

Order rules

OrderFires whenBy defaultLine
Close clean and condition99On130
order buy longshare2 > share and condition99On131
order sell shortshare2 < share and condition99On132
order increase longshare2 != share and inc and condition99On133
order decrease shortshare2 != share and dec and condition99On134

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.

35/35trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D−110.1%+102.5%15358%0.83137.1%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+86.1%+5.5%16553%1.8059.6%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−16.8%+9.8%16940%0.6525.8%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+5.1%+8.4%17666%2.283.4%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−0.6%+16.7%16145%0.741.8%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.2%+21.5%13363%2.110.1%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−2.7%+148.3%16846%0.657.8%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.7%+17.1%14655%1.490.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 barsTradesWonP&L
Rising market6381%$197,964.00
Sideways market7043%−$167,139.00
Falling market2035%−$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.

0%114%
The first 150 of 153 trades
SideEntryExitBarsP&L
long11 Feb 2024 48,300.004 Apr 2024 68,833.8053$20,533.80
long24 Feb 2024 50,744.204 Apr 2024 68,833.8040$18,089.60
long17 Mar 2024 65,300.604 Apr 2024 68,833.8018$3,533.15
long19 Mar 2024 67,610.004 Apr 2024 68,833.8016$1,223.79
long20 Mar 2024 61,937.404 Apr 2024 68,833.8015$6,896.38
long22 Mar 2024 65,501.304 Apr 2024 68,833.8013$3,332.51
long23 Mar 2024 63,796.6023 Apr 2024 66,819.3031$3,022.68
long6 Apr 2024 67,820.6026 Apr 2024 64,498.3020−$3,322.30
long13 Apr 2024 67,116.505 May 2024 63,892.0022−$3,224.49
long14 Apr 2024 63,924.506 May 2024 64,012.0022$87.48
long16 Apr 2024 63,420.0010 May 2024 63,074.0024−$345.99
long18 Apr 2024 61,277.4015 May 2024 63,404.9027$2,127.52
long29 Apr 2024 63,118.6018 May 2024 67,024.0019$3,905.38
long1 May 2024 60,672.0031 May 2024 69,031.8030$8,359.76
long3 May 2024 59,060.604 Jun 2024 69,523.2032$10,462.60
long29 May 2024 68,398.405 Jun 2024 70,537.807$2,139.43
long30 May 2024 67,652.406 Jun 2024 71,108.007$3,455.59
long1 Jun 2024 67,540.003 Jul 2024 62,135.5032−$5,404.55
long3 Jun 2024 67,765.6013 Jul 2024 57,889.1040−$9,876.53
long12 Jun 2024 67,314.2014 Jul 2024 59,204.0032−$8,110.22
long15 Jun 2024 66,044.0015 Jul 2024 60,797.9030−$5,246.08
long18 Jun 2024 66,504.3016 Jul 2024 64,724.1028−$1,780.27
long19 Jun 2024 65,175.3027 Jul 2024 67,908.0038$2,732.68
long20 Jun 2024 64,974.4011 Aug 2024 60,923.5052−$4,050.86
long21 Jun 2024 64,870.0013 Aug 2024 59,346.6053−$5,523.35
long1 Aug 2024 64,628.0014 Aug 2024 60,587.2013−$4,040.85
long3 Aug 2024 61,498.3017 Aug 2024 58,874.6014−$2,623.75
long4 Aug 2024 60,698.0018 Aug 2024 59,492.0014−$1,206.00
long5 Aug 2024 58,161.0020 Aug 2024 59,438.5015$1,277.50
short22 Aug 2024 61,156.0029 Aug 2024 59,034.907$2,121.13
short24 Aug 2024 64,037.2031 Aug 2024 59,124.007$4,913.25
short25 Aug 2024 64,157.001 Sep 2024 58,974.007$5,183.02
long3 Sep 2024 59,132.1013 Sep 2024 58,132.3010−$999.81
short14 Sep 2024 60,498.003 Oct 2024 60,649.3019−$151.28
long3 Oct 2024 60,649.3013 Oct 2024 63,206.2010$2,556.96
long6 Oct 2024 62,058.0015 Oct 2024 66,084.009$4,025.99
long8 Oct 2024 62,224.0016 Oct 2024 67,074.108$4,850.13
long9 Oct 2024 62,160.5017 Oct 2024 67,620.008$5,459.50
long10 Oct 2024 60,636.0027 Oct 2024 67,239.9017$6,603.92
long11 Oct 2024 60,326.4026 Nov 2024 93,010.0046$32,683.60
long26 Oct 2024 66,698.3026 Nov 2024 93,010.0031$26,311.70
long4 Nov 2024 68,776.0026 Nov 2024 93,010.0022$24,234.00
long5 Nov 2024 67,850.004 Jan 2025 98,174.2060$30,324.20
long20 Dec 2024 97,461.905 Jan 2025 98,220.5016$758.65
long22 Dec 2024 97,292.006 Jan 2025 98,363.6015$1,071.61
long23 Dec 2024 95,186.307 Jan 2025 102,236.0015$7,049.32
long24 Dec 2024 94,881.508 Jan 2025 96,954.6015$2,073.13
long27 Dec 2024 95,791.608 Jan 2025 96,954.6012$1,163.00
long28 Dec 2024 94,299.008 Jan 2025 96,954.6011$2,655.57
long30 Dec 2024 93,738.208 Jan 2025 96,954.609$3,216.41
long31 Dec 2024 92,792.1016 Jan 2025 100,497.0016$7,705.30
long9 Jan 2025 95,060.6018 Jan 2025 104,077.009$9,016.86
long10 Jan 2025 92,552.5019 Jan 2025 104,556.009$12,003.70
long12 Jan 2025 94,600.0020 Feb 2025 96,644.4039$2,044.38
long13 Jan 2025 94,545.1021 Feb 2025 98,305.0039$3,759.94
long14 Jan 2025 94,536.106 Mar 2025 90,606.0051−$3,930.11
long28 Jan 2025 102,083.0025 Mar 2025 87,498.2056−$14,584.70
long29 Jan 2025 101,336.0028 Mar 2025 87,232.0058−$14,103.50
long3 Feb 2025 97,700.6019 Apr 2025 84,474.7075−$26,451.80
long5 Feb 2025 97,763.1019 Apr 2025 84,474.7073−$13,288.40
long6 Feb 2025 96,612.4019 Apr 2025 84,474.7072−$12,137.70
long7 Feb 2025 96,554.4019 Apr 2025 84,474.7071−$12,079.70
long8 Feb 2025 96,506.8019 Apr 2025 84,474.7070−$12,032.10
long9 Feb 2025 96,444.8019 Apr 2025 84,474.7069−$11,970.10
long12 Feb 2025 95,778.2019 Apr 2025 84,474.7066−$11,303.50
long14 Feb 2025 96,608.1019 Apr 2025 84,474.7064−$12,133.40
long17 Feb 2025 96,118.1019 Apr 2025 84,474.7061−$11,643.40
long18 Feb 2025 95,780.0019 Apr 2025 84,474.7060−$11,305.30
long19 Feb 2025 95,671.7019 Apr 2025 84,474.7059−$11,197.00
long23 Feb 2025 95,561.0019 Apr 2025 84,474.7055−$11,086.30
long25 Feb 2025 91,552.9019 Apr 2025 84,474.7053−$7,078.18
long26 Feb 2025 88,680.4019 Apr 2025 84,474.7052−$4,205.69
long27 Feb 2025 84,250.1019 Apr 2025 84,474.7051$224.61
long1 Mar 2025 84,349.9019 Apr 2025 84,474.7049$124.75
long10 Mar 2025 80,734.5019 Apr 2025 84,474.7040$3,740.22
long30 Mar 2025 82,648.5019 Apr 2025 84,474.7020$1,826.17
long31 Mar 2025 82,390.0013 Jun 2025 105,672.0074$23,281.70
long30 May 2025 105,590.0013 Jun 2025 105,672.0014$81.99
long31 May 2025 103,985.0013 Jun 2025 105,672.0013$1,686.27
long4 Jun 2025 105,377.0013 Jun 2025 105,672.009$294.84
long5 Jun 2025 104,697.0013 Jun 2025 105,672.008$974.88
long6 Jun 2025 101,509.0030 Jun 2025 108,357.0024$6,848.24
long18 Jun 2025 104,551.003 Jul 2025 108,850.0015$4,298.42
long20 Jun 2025 104,659.003 Jul 2025 108,850.0013$4,191.00
long21 Jun 2025 103,298.003 Jul 2025 108,850.0012$5,551.61
long22 Jun 2025 102,120.003 Jul 2025 108,850.0011$6,729.57
long23 Jun 2025 100,964.003 Jul 2025 108,850.0010$7,885.72
long3 Jul 2025 109,585.0011 Aug 2025 119,294.0039$9,709.49
long21 Jul 2025 117,265.0013 Aug 2025 120,134.0023$2,868.98
long24 Jul 2025 118,736.0014 Aug 2025 123,306.0021$4,570.74
long25 Jul 2025 118,341.0015 Aug 2025 118,295.0021−$45.89
long26 Jul 2025 117,614.0015 Aug 2025 118,295.0020$680.78
long31 Jul 2025 116,466.0015 Aug 2025 118,295.0015$1,828.77
long1 Aug 2025 115,764.0015 Aug 2025 118,295.0014$2,531.02
long3 Aug 2025 112,546.0015 Aug 2025 118,295.0012$11,497.50
long6 Aug 2025 114,130.004 Sep 2025 111,706.0029−$2,424.03
long19 Aug 2025 116,227.008 Sep 2025 111,137.0020−$5,089.70
long20 Aug 2025 112,873.009 Sep 2025 112,065.0020−$807.72
short11 Sep 2025 113,960.0022 Sep 2025 115,232.0011−$1,272.29
short12 Sep 2025 115,483.0023 Sep 2025 112,651.0011$2,831.70
long24 Sep 2025 111,999.003 Oct 2025 120,529.009$8,530.55
long26 Sep 2025 108,994.003 Oct 2025 120,529.007$11,534.90
short4 Oct 2025 122,232.0012 Oct 2025 110,644.008$11,587.80
long15 Oct 2025 112,894.0028 Nov 2025 91,333.9044−$21,560.40
long16 Oct 2025 110,763.003 Dec 2025 91,277.9048−$19,485.40
long17 Oct 2025 108,194.004 Dec 2025 93,429.9048−$14,764.30
long18 Oct 2025 106,432.008 Dec 2025 90,395.3051−$16,036.40
long22 Oct 2025 108,298.009 Dec 2025 90,634.4048−$17,663.30
long23 Oct 2025 107,567.0010 Dec 2025 92,678.8048−$14,888.60
long31 Oct 2025 108,323.0012 Dec 2025 92,513.4042−$15,809.50
long4 Nov 2025 106,583.0025 Dec 2025 87,669.4051−$18,913.60
long5 Nov 2025 101,497.0027 Dec 2025 87,966.4052−$13,530.80
long7 Nov 2025 101,346.0029 Dec 2025 88,901.9052−$12,444.10
long9 Nov 2025 102,313.003 Jan 2026 89,995.1055−$12,317.80
long12 Nov 2025 103,059.004 Jan 2026 90,628.0053−$12,431.00
long13 Nov 2025 101,654.005 Jan 2026 91,529.7053−$10,124.60
long14 Nov 2025 99,692.006 Jan 2026 93,859.7053−$5,832.32
long15 Nov 2025 94,594.0015 Feb 2026 69,822.9092−$24,771.10
long11 Jan 2026 90,504.7017 Feb 2026 68,892.4037−$21,612.30
long20 Jan 2026 92,631.0026 Feb 2026 67,988.0037−$24,643.00
long21 Jan 2026 88,427.701 Mar 2026 66,973.3039−$21,454.40
long25 Jan 2026 89,225.303 Mar 2026 68,830.1037−$20,395.30
long26 Jan 2026 86,670.405 Mar 2026 72,666.8038−$14,003.60
short11 Mar 2026 70,427.1020 Mar 2026 69,930.009$497.05
short12 Mar 2026 70,191.9022 Mar 2026 68,918.1010$1,273.74
short13 Mar 2026 70,541.3023 Mar 2026 67,859.0010$2,682.34
short14 Mar 2026 70,930.0025 Mar 2026 70,556.7011$373.27
short15 Mar 2026 71,211.9027 Mar 2026 68,820.3012$2,391.64
short16 Mar 2026 72,815.3030 Apr 2026 75,780.0045−$2,964.75
short17 Mar 2026 74,884.7014 May 2026 79,313.6058−$4,428.94
short6 Apr 2026 69,034.2016 May 2026 79,113.2040−$10,079.00
short8 Apr 2026 71,924.2017 May 2026 78,148.1039−$6,223.83
short10 Apr 2026 71,788.0018 May 2026 77,457.7038−$5,669.69
short11 Apr 2026 72,962.7019 May 2026 77,001.9038−$4,039.17
long20 May 2026 76,834.4026 May 2026 77,322.006$487.65
short2 Jun 2026 66,760.8023 Jun 2026 64,020.0021$2,740.82
short14 Jun 2026 64,458.0024 Jun 2026 62,734.6010$1,723.44
short15 Jun 2026 65,746.4025 Jun 2026 61,078.0010$4,668.45
short16 Jun 2026 66,328.7028 Jul 2026 63,755.9042$2,572.88
short22 Jun 2026 65,062.901 Aug 2026 62,887.9040$2,174.99
short4 Jul 2026 62,583.303 Aug 2026 63,570.0030−$986.75
short5 Jul 2026 63,144.004 Aug 2026 63,520.0030−$375.99
short6 Jul 2026 63,650.0013 Aug 2026 63,480.0038$170.02
short7 Jul 2026 64,042.9015 Aug 2026 63,043.6039$999.37
long17 Aug 2026 62,900.0019 Sep 2026 80,883.9033$17,983.90
long3 Sep 2026 77,340.0020 Sep 2026 81,250.0017$3,909.98
long3 Sep 2026 77,340.0022 Sep 2026 86,620.0019$9,280.00
long8 Sep 2026 78,623.3025 Sep 2026 84,410.2017$5,786.93
long9 Sep 2026 78,455.8025 Sep 2026 84,410.2016$5,954.44
long10 Sep 2026 78,306.4025 Sep 2026 84,410.2015$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.

ScriptSharesTypeNetTradesMax drawdown
KD compare strategy (交易策略對照組) this page—v4 strategy−110.1%153137.1%
Alex Days HiLo Rajib by 14rajibhighest lowestv4 strategyNo trades on this market
Donchian Strat EXMO by pskoroplashighest lowestv4 strategy+12.1%2268.0%
Average Highest High and Lowest Low Swinger Strategy by exlux99lowest highestv4 strategy+6.4%2049.6%
Highest High and Lowest Low Channel Strategy by ceyhunhighest lowestv5 strategy−3.7%267.9%
Donchian Channels System [racer8] by racer8highest lowestv4 strategy−3.2%3643.6%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 49 msWith a workaround 1,000 bars in 90 msNot measured
PineTSNo 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)