Zeal Terminal Open in Zeal Terminal

Pine Script v5 · Strategy

The Z-score

A Pine Script v5 strategy built on SMA (_series, _length), Stdev (_series, _length) and EMA (haClose, 10). It has 6 order rules, 3 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. It has no settings.

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 The Z-score repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 12 of 12 trades.
How did The Z-score do on BTCUSDT 1D with default settings?
+2.35%
From 26 Jan 2024 to 21 Oct 2026, over 52 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Does The Z-score 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: 52 before, 52 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
Does The Z-score use a stop loss?
No
Every exit waits for a signal; nothing limits a losing trade before it fires.
Does The Z-score run in Zeal Terminal?
Yes
It compiles in 19 ms and runs 1,000 bars in 50 ms, in the browser.

BTCUSDT · 1D · default settings

  • The White Line
  • Lower Blue Line
  • Upper Blue Line
  • Middle Yellow Line
  • The Z-Score Mixed With EMA 10
  • Highest of emaScore
  • Lowest of emaScore
  • 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 (0)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOct$1,022,450$2,025,273Equity from $1,000,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. 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
+2.35%
$23,528.10
Buy and hold
+102.5%
same period
Closed trades
52
21 won · 31 lost
Profitable
40.4%
of closed trades
Profit factor
1.17
gross profit ÷ gross loss
Max drawdown
5.48%
−$54,777.70

One run with the author's default settings, $1,000,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.

Settings

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

The script declares no settings.

What it calculates

  • SMA_series, _length_mean · line 19
  • Stdev_series, _length_stddev · line 20
  • EMAhaClose, 10score · line 23
  • EMAscore, 20emaScore · line 24
  • Percentile_linear_interpolationemaScore, 50, 10lowerBlue · line 26
  • Percentile_linear_interpolationemaScore, 50, 90upperBlue · line 27
  • Percentile_linear_interpolationemaScore, 50, 50middleLine · line 29
  • HighestemaScore, 100highest · line 37
  • LowestemaScore, 100lowest · line 38
  • Crossoverscore, highestaddOn · line 44

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.

ohlc4ohlc4price dataclosecloseprice dataopenopenprice datahighhighprice datalowlowprice data_meanSMA→ _mean_stddevStdev→ _stddevscoreEMA→ scoreemaScoreEMA→ emaScorelowerBluePercentile_linear_inter…→ lowerBlueupperBluePercentile_linear_inter…→ upperBluemiddleLinePercentile_linear_inter…→ middleLinehighestHighest→ highestlowestLowest→ lowestaddOnCrossover→ addOnhaClosehaClosevaluehaOpenhaOpenvaluehaHighhaHighvaluehaLowhaLowvaluelongConlongConconditionshortConshortConconditionentry:LongOpen Longstrategy.entryclose:LongClose Longstrategy.closeentry:LongNR2Open LongNR2strategy.entryclose:LongNR2Close LongNR2strategy.closeentry:ShortOpen Shortstrategy.entryclose:ShortClose Shortstrategy.closeplot:31The White Lineplotplot:32Lower Blue Lineplotplot:33Upper Blue Lineplotplot:34Middle Yellow Lineplotplot:35The Z-Score Mixed With …plotplot:40Highest of emaScoreplotplot:41Lowest of emaScoreplot

Order rules

OrderFires whenBy defaultLine
Open Long longlongConOn49
Close LongshortConOn51
Open LongNR2 longaddOnOn53
Close LongNR2shortConOn55
Open Short shortshortConOn58
Close ShortlongConOn60

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.

12/12trades 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+2.4%+102.5%5240%1.175.5%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+0.4%+5.5%5336%1.091.5%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−0.6%+9.8%5433%0.540.7%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.0%+8.4%4931%0.860.1%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.0%+16.7%4269%2.400.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−0.0%+21.5%4540%0.720.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+0.0%+148.3%4637%1.430.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.0%+17.1%4340%1.090.0%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 market2326%−$37,028.10
Sideways market2245%$36,931.60
Falling market771%$23,624.60

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

How bad could the drawdown be?

6.9%

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

0%8%
All 52 trades
SideEntryExitBarsP&L
short20 Mar 2024 61,937.401 Apr 2024 71,280.0012−$9,342.59
long1 Apr 2024 71,280.004 Apr 2024 65,963.303−$5,316.73
short4 Apr 2024 65,963.309 Apr 2024 71,620.005−$5,656.73
long9 Apr 2024 71,620.0015 Apr 2024 65,661.906−$5,958.15
short15 Apr 2024 65,661.907 May 2024 63,165.2022$2,496.67
long7 May 2024 63,165.2013 May 2024 61,484.006−$1,681.18
short13 May 2024 61,484.0015 May 2024 61,577.502−$93.49
long15 May 2024 61,577.505 Jun 2024 70,537.8021$8,960.34
short5 Jun 2024 70,537.8013 Jul 2024 57,889.1038$12,648.70
long13 Jul 2024 57,889.105 Aug 2024 58,161.0023$271.91
short5 Aug 2024 58,161.0011 Aug 2024 60,923.506−$2,762.51
long11 Aug 2024 60,923.509 Sep 2024 54,869.9029−$6,053.56
short9 Sep 2024 54,869.9015 Sep 2024 59,993.006−$5,123.07
long15 Sep 2024 59,993.004 Oct 2024 60,752.7019$759.70
short4 Oct 2024 60,752.7017 Oct 2024 67,620.0013−$6,867.28
long17 Oct 2024 67,620.0027 Oct 2024 67,092.8010−$527.24
short27 Oct 2024 67,092.8030 Oct 2024 72,736.403−$5,643.65
long30 Oct 2024 72,736.403 Nov 2024 69,374.704−$3,361.67
short3 Nov 2024 69,374.708 Nov 2024 75,857.905−$6,483.15
long8 Nov 2024 75,857.9019 Nov 2024 90,464.1011$14,606.20
short19 Nov 2024 90,464.1014 Dec 2024 101,424.0025−$10,960.20
long14 Dec 2024 101,424.0024 Dec 2024 94,881.5010−$6,542.77
short24 Dec 2024 94,881.508 Jan 2025 96,954.6015−$2,073.13
long8 Jan 2025 96,954.6017 Feb 2025 96,118.1040−$836.48
short17 Feb 2025 96,118.107 Mar 2025 89,931.9018$6,186.24
long7 Mar 2025 89,931.9026 May 2025 109,004.0080$19,072.30
short26 May 2025 109,004.0012 Jun 2025 108,645.0017$359.07
long12 Jun 2025 108,645.0014 Jun 2025 106,067.002−$2,578.54
short14 Jun 2025 106,067.0028 Jun 2025 107,048.0014−$980.99
long28 Jun 2025 107,048.0021 Jul 2025 117,265.0023$10,217.50
short21 Jul 2025 117,265.0012 Aug 2025 118,686.0022−$1,420.89
long12 Aug 2025 118,686.0023 Aug 2025 116,936.0011−$1,750.00
short23 Aug 2025 116,936.008 Sep 2025 111,137.0016$5,798.65
long8 Sep 2025 111,137.0027 Sep 2025 109,643.0019−$1,493.89
short27 Sep 2025 109,643.005 Oct 2025 122,391.008−$12,747.50
long5 Oct 2025 122,391.0014 Oct 2025 115,166.009−$7,224.99
short14 Oct 2025 115,166.0028 Oct 2025 114,108.0014$1,058.35
long28 Oct 2025 114,108.0031 Oct 2025 108,323.003−$5,784.78
short31 Oct 2025 108,323.0026 Nov 2025 87,370.0026$20,952.90
long26 Nov 2025 87,370.0021 Jan 2026 88,427.7056$1,057.69
short21 Jan 2026 88,427.7015 Feb 2026 69,822.9025$18,604.70
long15 Feb 2026 69,822.9026 Mar 2026 71,336.5039$1,513.59
short26 Mar 2026 71,336.507 Apr 2026 68,853.7012$2,482.87
long7 Apr 2026 68,853.7030 Apr 2026 75,780.0023$6,926.34
short30 Apr 2026 75,780.006 May 2026 80,905.506−$5,125.53
long6 May 2026 80,905.5015 May 2026 81,090.009$184.47
short15 May 2026 81,090.0014 Jun 2026 64,458.0030$16,632.00
long14 Jun 2026 64,458.001 Aug 2026 62,887.9048−$1,570.13
short1 Aug 2026 62,887.9019 Aug 2026 64,725.4018−$1,837.54
long19 Aug 2026 64,725.402 Sep 2026 77,439.0014$12,713.60
short2 Sep 2026 77,439.0022 Sep 2026 86,620.0020−$9,181.00
long22 Sep 2026 86,620.001 Oct 2026 83,623.609−$2,996.42

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
The Z-score this page—v5 strategy+2.4%525.5%
Crunchster's Turtle and Trend System by Crunchster1lowest highest ema stdev smav5 strategy+18.0%3116.7%
MZ SRSI Strategy V1.0 by MightyZingerema sma highest lowest stdevv4 strategyNo trades on this market
BBPBΔ(OBV-PVT)BB - Time Series Decomposition & Volume Weighted by tathalema stdev sma highest lowestv4 strategyNo trades on this market
WOMBO COMBO- EMA & VWAP & MACD & BB & STC by MakeMoneyCoESTB2020ema sma stdev highest lowestv4 strategy−0.5%62.2%
Lawyers Trend Pro Strategy by ErdemDemirsma stdev lowest highestv4 strategy+12.1%10241.7%

Upgrade and export

Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.

  • No v6-only features. v6 is not needed to run this script.

Clean v5

Verified

Nothing to change.

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

⋯// how it goes in comment section. -//@version=5+//@version=6// Define the strategy settings⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 19 msWith a workaround 1,000 bars in 50 msNot measured
PineTSNot testedNot testedNot measured

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.

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/// © jensenvilhelm// Here is an attempt to create a robust strategy for BTCUSD on a 5 minute chart// I can't seem to get this code to work the way i want.... if you want to give it a try, please let me know -// how it goes in comment section. //@version=5// Define the strategy settingsstrategy("The Z-score", shorttitle="TZS", overlay=true)// User can set the start date for the strategystartDate = timestamp("2023 06 01")// Heikin-Ashi Open, Close, High and Low calculationhaClose = ohlc4var float haOpen = nahaOpen := na(haOpen[1]) ? (open + close) / 2 : (haOpen[1] + haClose[1]) / 2haHigh = math.max(nz(haOpen, high), nz(haClose, high), high)haLow = math.min(nz(haOpen, low), nz(haClose, low), low)// Function to calculate the Z-Scorez_score(_series, _length) =>    _mean = ta.sma(_series, _length)    _stddev = ta.stdev(_series, _length)    (_series - _mean) / _stddev// Compute the score and its EMAscore = z_score(ta.ema(haClose, 10), 25)emaScore = ta.ema(score, 20)// Calculate lower and upper thresholds using percentiles of EMAlowerBlue = ta.percentile_linear_interpolation(emaScore, 50, 10)upperBlue = ta.percentile_linear_interpolation(emaScore, 50, 90)// Calculate the middle line as 50th percentilemiddleLine = ta.percentile_linear_interpolation(emaScore, 50, 50) // Plot the EMA of the score and the thresholdsplot(emaScore,"The White Line", color=color.white, linewidth=2)plot(lowerBlue,"Lower Blue Line", linewidth=2)plot(upperBlue, "Upper Blue Line", linewidth=2)plot(middleLine, "Middle Yellow Line", linewidth=2, color=color.yellow)plot(score,"The Z-Score Mixed With EMA 10", color=color.green)// Calculate highest and lowest EMA score over 100 bars periodhighest = ta.highest(emaScore, 100)lowest = ta.lowest(emaScore, 100)// Plot highest and lowest EMA score lines plot(highest, "Highest of emaScore", color=color.red, linewidth=2)plot(lowest, "Lowest of emaScore", color=color.red, linewidth=2)// Define entry and exit conditions for long and short positionslongCon = ta.crossover(score, lowerBlue) or ta.crossover(emaScore, middleLine)addOn = ta.crossover(score, highest)shortCon = ta.crossunder(emaScore, upperBlue) or ta.crossunder(emaScore, lowerBlue) or ta.crossunder(emaScore, highest)// Execute trading logic based on conditions and after the start dateif (time >= startDate)    if longCon        strategy.entry("Long", strategy.long)        if shortCon            strategy.close("Long")    if addOn        strategy.entry("LongNR2", strategy.long)        if shortCon            strategy.close("LongNR2")        if shortCon        strategy.entry("Short", strategy.short)        if longCon            strategy.close("Short")