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
- 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
VerifiedSame 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)⋯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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| K | 35 | int · min 1 | k |
| D | 21 | int · min 1 | d |
| Smooth | 21 | int · min 1 | k |
| 開始年 | 2020 | int (written input.integer) | test_start |
| 開始月 | 1 | int (written input.integer) | test_start |
| 開始日 | 1 | int (written input.integer) | test_start |
| 終了年 | 2030 | int (written input.integer) | test_end |
| 終了月 | 12 | int (written input.integer) | test_end |
| 終了日 | 31 | int (written input.integer) | test_end |
What it calculates
- SMA / Stoch (
sma,ta.smain v5)stoch(close, high, low, periodK), Smooth (21)k · line 20 - SMA (
sma,ta.smain 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Stoch_LE long | (k > d) | On | 24 |
| Open Stoch_SE short | (k < d) | On | 28 |
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 | +80.4% | +102.5% | 30 | 53% | 2.17 | 19.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +10.2% | +5.5% | 33 | 33% | 1.29 | 8.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.7% | +9.8% | 38 | 42% | 0.90 | 4.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.5% | +8.4% | 36 | 39% | 1.50 | 0.5% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.3% | +16.7% | 26 | 65% | 3.33 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 34 | 47% | 0.98 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.3% | +148.3% | 44 | 34% | 0.75 | 1.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.0% | +17.1% | 35 | 40% | 0.92 | 0.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 9 | 33% | −$3,836.42 |
| Sideways market | 13 | 85% | $101,694.00 |
| Falling market | 8 | 25% | −$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.
All 30 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 10 Apr 2024 69,146.00 | 18 May 2024 67,024.00 | 38 | $2,122.00 |
| long | 18 May 2024 67,024.00 | 16 Jun 2024 66,228.30 | 29 | −$795.75 |
| short | 16 Jun 2024 66,228.30 | 17 Jul 2024 65,044.00 | 31 | $1,184.25 |
| long | 17 Jul 2024 65,044.00 | 12 Aug 2024 58,712.60 | 26 | −$6,331.41 |
| short | 12 Aug 2024 58,712.60 | 2 Sep 2024 57,301.80 | 21 | $1,410.82 |
| long | 2 Sep 2024 57,301.80 | 9 Sep 2024 54,869.90 | 7 | −$2,431.82 |
| short | 9 Sep 2024 54,869.90 | 21 Sep 2024 63,201.10 | 12 | −$8,331.10 |
| long | 21 Sep 2024 63,201.10 | 19 Oct 2024 68,428.00 | 28 | $5,226.94 |
| short | 19 Oct 2024 68,428.00 | 23 Oct 2024 67,426.00 | 4 | $1,001.98 |
| long | 23 Oct 2024 67,426.00 | 10 Dec 2024 97,276.50 | 48 | $29,850.50 |
| short | 10 Dec 2024 97,276.50 | 20 Jan 2025 101,332.00 | 41 | −$4,055.09 |
| long | 20 Jan 2025 101,332.00 | 11 Feb 2025 97,430.80 | 22 | −$3,900.75 |
| short | 11 Feb 2025 97,430.80 | 19 Mar 2025 82,715.00 | 36 | $14,715.80 |
| long | 19 Mar 2025 82,715.00 | 28 May 2025 108,938.00 | 70 | $26,223.10 |
| short | 28 May 2025 108,938.00 | 6 Jul 2025 108,198.00 | 39 | $740.05 |
| long | 6 Jul 2025 108,198.00 | 1 Aug 2025 115,764.00 | 26 | $7,565.95 |
| short | 1 Aug 2025 115,764.00 | 17 Sep 2025 116,789.00 | 47 | −$1,024.89 |
| long | 17 Sep 2025 116,789.00 | 20 Oct 2025 108,643.00 | 33 | −$8,146.19 |
| short | 20 Oct 2025 108,643.00 | 4 Dec 2025 93,429.90 | 45 | $15,212.80 |
| long | 4 Dec 2025 93,429.90 | 27 Jan 2026 88,347.10 | 54 | −$5,082.87 |
| short | 27 Jan 2026 88,347.10 | 26 Feb 2026 67,988.00 | 30 | $20,359.00 |
| long | 26 Feb 2026 67,988.00 | 4 Apr 2026 66,964.30 | 37 | −$1,023.75 |
| short | 4 Apr 2026 66,964.30 | 17 Apr 2026 75,154.30 | 13 | −$8,189.99 |
| long | 17 Apr 2026 75,154.30 | 18 May 2026 77,457.70 | 31 | $2,303.39 |
| short | 18 May 2026 77,457.70 | 24 Jun 2026 62,734.60 | 37 | $14,723.10 |
| long | 24 Jun 2026 62,734.60 | 11 Aug 2026 63,970.00 | 48 | $1,235.44 |
| short | 11 Aug 2026 63,970.00 | 27 Aug 2026 79,023.80 | 16 | −$15,053.70 |
| long | 27 Aug 2026 79,023.80 | 25 Sep 2026 84,410.20 | 29 | $5,386.49 |
| short | 25 Sep 2026 84,410.20 | 6 Oct 2026 85,766.90 | 11 | −$1,356.63 |
| long | 6 Oct 2026 85,766.90 | 10 Oct 2026 82,635.60 | 4 | −$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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Stoch cross this page | — | v4 strategy | +80.4% | 30 | 19.5% |
| (IK) Stoch-60-15 by tapRoot_coding | stoch sma | v4 strategy | No trades on this market | ||
| Stochastic Oscillator by Zeal Terminal | stoch sma | v5 indicator | indicator | ||
| CCI Level Zone by Naruephat | sma stoch | v5 strategy | No trades on this market | ||
| Stochastic Moving Average by LucasVivien | stoch sma | v5 strategy | +2.8% | 7 | 4.4% |
| OTT-Stoch-TP-SL by BigCoinHunter | sma stoch | v5 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 7 ms | Yes 1,000 bars in 23 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report. PineTS runs Pine Script v5 and v6 only.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © 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")