Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

Stoch cross

A Pine Script v4 strategy built on SMA or Stoch (21) and SMA (k, 21). It has 2 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 9 settings; defaults include K 35, D 21 and Smooth 21.

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 Stoch cross repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 10 of 10 trades.
How did Stoch cross do on BTCUSDT 1D with default settings?
+80.41%
From 26 Jan 2024 to 21 Oct 2026, over 30 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can Stoch cross 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 Stoch cross use a stop loss?
No
Every exit waits for a signal; nothing limits a losing trade before it fires.
Does Stoch cross run in Zeal Terminal?
Yes
It compiles in 7 ms and runs 1,000 bars in 23 ms, in the browser.

BTCUSDT · 1D · default settings

  • SMA
  • SMA
  • 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 (9)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOct$178,333$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. The script plots nothing itself; the lines and the lower pane are its own calculations, drawn for reference.
Net profit
+80.41%
$80,406.40
Buy and hold
+102.5%
same period
Closed trades
30
16 won · 14 lost
Profitable
53.3%
of closed trades
Profit factor
2.17
gross profit ÷ gross loss
Max drawdown
19.45%
−$19,938.60

One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A short 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.

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 sma → ta.sma, stoch → ta.stoch.

v4 → v5

Verified

Same 30 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 3: //@version=4 → //@version=5
  • Line 8: input(type = input.integer) → input.int()
  • Line 9: input(type = input.integer) → input.int()
  • Line 10: input(type = input.integer) → input.int()
  • Line 11: input(type = input.integer) → input.int()
  • Line 12: input(type = input.integer) → input.int()
4 more edits
  • Line 13: input(type = input.integer) → input.int()
  • Line 20: sma → ta.sma
  • Line 20: stoch → ta.stoch
  • Line 21: sma → ta.sma
⋯// © utanico-//@version=4+//@version=5strategy(title="Stochastic", overlay=true, shorttitle="Stoch")⋯smoothK = input(21, title="Smooth", minval=1)-startYear = input(type=input.integer, title = "開始年", defval = 2020)+startYear = input.int(title = "開始年", defval = 2020)-startMonth = input(type=input.integer, title = "開始月", defval = 1)+startMonth = input.int(title = "開始月", defval = 1)-startDay = input(type=input.integer, title = "開始日", defval = 1)+startDay = input.int(title = "開始日", defval = 1)-endYear = input(type=input.integer, title = "終了年", defval = 2030)+endYear = input.int(title = "終了年", defval = 2030)-endMonth = input(type=input.integer, title = "終了月", defval = 12)+endMonth = input.int(title = "終了月", defval = 12)-endDay = input(type=input.integer, title = "終了日", defval = 31)+endDay = input.int(title = "終了日", defval = 31)//開始日時⋯is_test = test_start <= time and  time <= test_end-k = sma(stoch(close, high, low, periodK), smoothK)+k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)-d = sma(k, periodD)+d = ta.sma(k, periodD)if (is_test)⋯

Clean v5

VerifiedDownload the v5 script

Same 30 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 5: input() → input.int()
  • Line 6: input() → input.int()
  • Line 7: input() → input.int()
⋯strategy(title="Stochastic", overlay=true, shorttitle="Stoch")-periodK = input(35, title="K", minval=1)+periodK = input.int(35, title="K", minval=1)-periodD = input(21, title="D", minval=1)+periodD = input.int(21, title="D", minval=1)-smoothK = input(21, title="Smooth", minval=1)+smoothK = input.int(21, title="Smooth", minval=1)startYear = input.int(title = "開始年", defval = 2020)⋯

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: 30 before, 46 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.

⋯// © utanico-//@version=5+//@version=6strategy(title="Stochastic", overlay=true, shorttitle="Stoch")⋯

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
K35int · min 1k
D21int · min 1d
Smooth21int · min 1k
開始年2020int (written input.integer)test_start
開始月1int (written input.integer)test_start
開始日1int (written input.integer)test_start
終了年2030int (written input.integer)test_end
終了月12int (written input.integer)test_end
終了日31int (written input.integer)test_end

What it calculates

  • SMA / Stoch (sma, ta.sma in v5)stoch(close, high, low, periodK), Smooth (21)k · line 20
  • SMA (sma, ta.sma in v5)k, D (21)d · 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.

lowlowprice datahighhighprice dataclosecloseprice dataperiodKKsetting · 35smoothKSmoothsetting · 21periodDDsetting · 21startYear開始年setting · 2020startMonth開始月setting · 1startDay開始日setting · 1endYear終了年setting · 2030endMonth終了月setting · 12endDay終了日setting · 31timetimeprice datakSMA / Stoch→ kdSMA→ dtest_starttest_startvaluetest_endtest_endvalueis_testis_testconditionentry:Stoch_LEOpen Stoch_LEstrategy.entryentry:Stoch_SEOpen Stoch_SEstrategy.entry

Order rules

OrderFires whenBy defaultLine
Open Stoch_LE long(k > d)On24
Open Stoch_SE short(k < d)On28

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.

10/10trades 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 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+80.4%+102.5%3053%2.1719.5%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+10.2%+5.5%3333%1.298.6%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−0.7%+9.8%3842%0.904.3%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.5%+8.4%3639%1.500.5%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.3%+16.7%2665%3.330.1%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−0.0%+21.5%3447%0.980.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.3%+148.3%4434%0.751.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−0.0%+17.1%3540%0.920.3%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 market933%−$3,836.42
Sideways market1385%$101,694.00
Falling market825%−$17,450.80

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

26.2%

The 95th percentile of the worst drawdown when the same 30 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 11.3% to 26.2%; the tick is what actually happened, 15.3%. Order luck alone moves it this much.

0%30%
All 30 trades
SideEntryExitBarsP&L
short10 Apr 2024 69,146.0018 May 2024 67,024.0038$2,122.00
long18 May 2024 67,024.0016 Jun 2024 66,228.3029−$795.75
short16 Jun 2024 66,228.3017 Jul 2024 65,044.0031$1,184.25
long17 Jul 2024 65,044.0012 Aug 2024 58,712.6026−$6,331.41
short12 Aug 2024 58,712.602 Sep 2024 57,301.8021$1,410.82
long2 Sep 2024 57,301.809 Sep 2024 54,869.907−$2,431.82
short9 Sep 2024 54,869.9021 Sep 2024 63,201.1012−$8,331.10
long21 Sep 2024 63,201.1019 Oct 2024 68,428.0028$5,226.94
short19 Oct 2024 68,428.0023 Oct 2024 67,426.004$1,001.98
long23 Oct 2024 67,426.0010 Dec 2024 97,276.5048$29,850.50
short10 Dec 2024 97,276.5020 Jan 2025 101,332.0041−$4,055.09
long20 Jan 2025 101,332.0011 Feb 2025 97,430.8022−$3,900.75
short11 Feb 2025 97,430.8019 Mar 2025 82,715.0036$14,715.80
long19 Mar 2025 82,715.0028 May 2025 108,938.0070$26,223.10
short28 May 2025 108,938.006 Jul 2025 108,198.0039$740.05
long6 Jul 2025 108,198.001 Aug 2025 115,764.0026$7,565.95
short1 Aug 2025 115,764.0017 Sep 2025 116,789.0047−$1,024.89
long17 Sep 2025 116,789.0020 Oct 2025 108,643.0033−$8,146.19
short20 Oct 2025 108,643.004 Dec 2025 93,429.9045$15,212.80
long4 Dec 2025 93,429.9027 Jan 2026 88,347.1054−$5,082.87
short27 Jan 2026 88,347.1026 Feb 2026 67,988.0030$20,359.00
long26 Feb 2026 67,988.004 Apr 2026 66,964.3037−$1,023.75
short4 Apr 2026 66,964.3017 Apr 2026 75,154.3013−$8,189.99
long17 Apr 2026 75,154.3018 May 2026 77,457.7031$2,303.39
short18 May 2026 77,457.7024 Jun 2026 62,734.6037$14,723.10
long24 Jun 2026 62,734.6011 Aug 2026 63,970.0048$1,235.44
short11 Aug 2026 63,970.0027 Aug 2026 79,023.8016−$15,053.70
long27 Aug 2026 79,023.8025 Sep 2026 84,410.2029$5,386.49
short25 Sep 2026 84,410.206 Oct 2026 85,766.9011−$1,356.63
long6 Oct 2026 85,766.9010 Oct 2026 82,635.604−$3,131.31

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
Stoch cross this page—v4 strategy+80.4%3019.5%
(IK) Stoch-60-15 by tapRoot_codingstoch smav4 strategyNo trades on this market
Stochastic Oscillator by Zeal Terminalstoch smav5 indicatorindicator
CCI Level Zone by Naruephatsma stochv5 strategyNo trades on this market
Stochastic Moving Average by LucasVivienstoch smav5 strategy+2.8%74.4%
OTT-Stoch-TP-SL by BigCoinHuntersma stochv5 strategyNo trades on this market

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 7 msYes 1,000 bars in 23 msNot measured
PineTSNo 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/// © utanico//@version=4strategy(title="Stochastic", overlay=true, shorttitle="Stoch")periodK = input(35, title="K", minval=1)periodD = input(21, title="D", minval=1)smoothK = input(21, title="Smooth", minval=1)startYear = input(type=input.integer, title = "開始年", defval = 2020)startMonth = input(type=input.integer, title = "開始月", defval = 1)startDay = input(type=input.integer, title = "開始日", defval = 1)endYear = input(type=input.integer, title = "終了年", defval = 2030)endMonth = input(type=input.integer, title = "終了月", defval = 12)endDay = input(type=input.integer, title = "終了日", defval = 31)//開始日時test_start = timestamp(startYear, startMonth, startDay, 00, 00)//終了日時test_end   = timestamp(endYear, endMonth, endDay, 00, 00)//テスト期間の指定is_test = test_start <= time and  time <= test_endk = sma(stoch(close, high, low, periodK), smoothK)d = sma(k, periodD)if (is_test)    if (k > d)        strategy.entry("Stoch_LE", strategy.long, comment="Stoch_LE")    //if (strategy.opentrades > 0 and k < d)        //strategy.close("Stoch_LE",comment="CloseLONG")    if (k < d)        strategy.entry("Stoch_SE", strategy.short, comment="Stoch_SE")    //if (strategy.opentrades < 0 and k > d)        //strategy.close("Stoch_SE",comment="CloseShort")