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
- 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, 10
score · line 23 - EMAscore, 20
emaScore · line 24 - Percentile_linear_interpolationemaScore, 50, 10
lowerBlue · line 26 - Percentile_linear_interpolationemaScore, 50, 90
upperBlue · line 27 - Percentile_linear_interpolationemaScore, 50, 50
middleLine · line 29 - HighestemaScore, 100
highest · line 37 - LowestemaScore, 100
lowest · line 38 - Crossoverscore, highest
addOn · 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | longCon | On | 49 |
| Close Long | shortCon | On | 51 |
| Open LongNR2 long | addOn | On | 53 |
| Close LongNR2 | shortCon | On | 55 |
| Open Short short | shortCon | On | 58 |
| Close Short | longCon | On | 60 |
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 | +2.4% | +102.5% | 52 | 40% | 1.17 | 5.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.4% | +5.5% | 53 | 36% | 1.09 | 1.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −0.6% | +9.8% | 54 | 33% | 0.54 | 0.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.0% | +8.4% | 49 | 31% | 0.86 | 0.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 42 | 69% | 2.40 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 45 | 40% | 0.72 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.0% | +148.3% | 46 | 37% | 1.43 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 43 | 40% | 1.09 | 0.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 23 | 26% | −$37,028.10 |
| Sideways market | 22 | 45% | $36,931.60 |
| Falling market | 7 | 71% | $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.
All 52 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 20 Mar 2024 61,937.40 | 1 Apr 2024 71,280.00 | 12 | −$9,342.59 |
| long | 1 Apr 2024 71,280.00 | 4 Apr 2024 65,963.30 | 3 | −$5,316.73 |
| short | 4 Apr 2024 65,963.30 | 9 Apr 2024 71,620.00 | 5 | −$5,656.73 |
| long | 9 Apr 2024 71,620.00 | 15 Apr 2024 65,661.90 | 6 | −$5,958.15 |
| short | 15 Apr 2024 65,661.90 | 7 May 2024 63,165.20 | 22 | $2,496.67 |
| long | 7 May 2024 63,165.20 | 13 May 2024 61,484.00 | 6 | −$1,681.18 |
| short | 13 May 2024 61,484.00 | 15 May 2024 61,577.50 | 2 | −$93.49 |
| long | 15 May 2024 61,577.50 | 5 Jun 2024 70,537.80 | 21 | $8,960.34 |
| short | 5 Jun 2024 70,537.80 | 13 Jul 2024 57,889.10 | 38 | $12,648.70 |
| long | 13 Jul 2024 57,889.10 | 5 Aug 2024 58,161.00 | 23 | $271.91 |
| short | 5 Aug 2024 58,161.00 | 11 Aug 2024 60,923.50 | 6 | −$2,762.51 |
| long | 11 Aug 2024 60,923.50 | 9 Sep 2024 54,869.90 | 29 | −$6,053.56 |
| short | 9 Sep 2024 54,869.90 | 15 Sep 2024 59,993.00 | 6 | −$5,123.07 |
| long | 15 Sep 2024 59,993.00 | 4 Oct 2024 60,752.70 | 19 | $759.70 |
| short | 4 Oct 2024 60,752.70 | 17 Oct 2024 67,620.00 | 13 | −$6,867.28 |
| long | 17 Oct 2024 67,620.00 | 27 Oct 2024 67,092.80 | 10 | −$527.24 |
| short | 27 Oct 2024 67,092.80 | 30 Oct 2024 72,736.40 | 3 | −$5,643.65 |
| long | 30 Oct 2024 72,736.40 | 3 Nov 2024 69,374.70 | 4 | −$3,361.67 |
| short | 3 Nov 2024 69,374.70 | 8 Nov 2024 75,857.90 | 5 | −$6,483.15 |
| long | 8 Nov 2024 75,857.90 | 19 Nov 2024 90,464.10 | 11 | $14,606.20 |
| short | 19 Nov 2024 90,464.10 | 14 Dec 2024 101,424.00 | 25 | −$10,960.20 |
| long | 14 Dec 2024 101,424.00 | 24 Dec 2024 94,881.50 | 10 | −$6,542.77 |
| short | 24 Dec 2024 94,881.50 | 8 Jan 2025 96,954.60 | 15 | −$2,073.13 |
| long | 8 Jan 2025 96,954.60 | 17 Feb 2025 96,118.10 | 40 | −$836.48 |
| short | 17 Feb 2025 96,118.10 | 7 Mar 2025 89,931.90 | 18 | $6,186.24 |
| long | 7 Mar 2025 89,931.90 | 26 May 2025 109,004.00 | 80 | $19,072.30 |
| short | 26 May 2025 109,004.00 | 12 Jun 2025 108,645.00 | 17 | $359.07 |
| long | 12 Jun 2025 108,645.00 | 14 Jun 2025 106,067.00 | 2 | −$2,578.54 |
| short | 14 Jun 2025 106,067.00 | 28 Jun 2025 107,048.00 | 14 | −$980.99 |
| long | 28 Jun 2025 107,048.00 | 21 Jul 2025 117,265.00 | 23 | $10,217.50 |
| short | 21 Jul 2025 117,265.00 | 12 Aug 2025 118,686.00 | 22 | −$1,420.89 |
| long | 12 Aug 2025 118,686.00 | 23 Aug 2025 116,936.00 | 11 | −$1,750.00 |
| short | 23 Aug 2025 116,936.00 | 8 Sep 2025 111,137.00 | 16 | $5,798.65 |
| long | 8 Sep 2025 111,137.00 | 27 Sep 2025 109,643.00 | 19 | −$1,493.89 |
| short | 27 Sep 2025 109,643.00 | 5 Oct 2025 122,391.00 | 8 | −$12,747.50 |
| long | 5 Oct 2025 122,391.00 | 14 Oct 2025 115,166.00 | 9 | −$7,224.99 |
| short | 14 Oct 2025 115,166.00 | 28 Oct 2025 114,108.00 | 14 | $1,058.35 |
| long | 28 Oct 2025 114,108.00 | 31 Oct 2025 108,323.00 | 3 | −$5,784.78 |
| short | 31 Oct 2025 108,323.00 | 26 Nov 2025 87,370.00 | 26 | $20,952.90 |
| long | 26 Nov 2025 87,370.00 | 21 Jan 2026 88,427.70 | 56 | $1,057.69 |
| short | 21 Jan 2026 88,427.70 | 15 Feb 2026 69,822.90 | 25 | $18,604.70 |
| long | 15 Feb 2026 69,822.90 | 26 Mar 2026 71,336.50 | 39 | $1,513.59 |
| short | 26 Mar 2026 71,336.50 | 7 Apr 2026 68,853.70 | 12 | $2,482.87 |
| long | 7 Apr 2026 68,853.70 | 30 Apr 2026 75,780.00 | 23 | $6,926.34 |
| short | 30 Apr 2026 75,780.00 | 6 May 2026 80,905.50 | 6 | −$5,125.53 |
| long | 6 May 2026 80,905.50 | 15 May 2026 81,090.00 | 9 | $184.47 |
| short | 15 May 2026 81,090.00 | 14 Jun 2026 64,458.00 | 30 | $16,632.00 |
| long | 14 Jun 2026 64,458.00 | 1 Aug 2026 62,887.90 | 48 | −$1,570.13 |
| short | 1 Aug 2026 62,887.90 | 19 Aug 2026 64,725.40 | 18 | −$1,837.54 |
| long | 19 Aug 2026 64,725.40 | 2 Sep 2026 77,439.00 | 14 | $12,713.60 |
| short | 2 Sep 2026 77,439.00 | 22 Sep 2026 86,620.00 | 20 | −$9,181.00 |
| long | 22 Sep 2026 86,620.00 | 1 Oct 2026 83,623.60 | 9 | −$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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| The Z-score this page | — | v5 strategy | +2.4% | 52 | 5.5% |
| Crunchster's Turtle and Trend System by Crunchster1 | lowest highest ema stdev sma | v5 strategy | +18.0% | 31 | 16.7% |
| MZ SRSI Strategy V1.0 by MightyZinger | ema sma highest lowest stdev | v4 strategy | No trades on this market | ||
| BBPBΔ(OBV-PVT)BB - Time Series Decomposition & Volume Weighted by tathal | ema stdev sma highest lowest | v4 strategy | No trades on this market | ||
| WOMBO COMBO- EMA & VWAP & MACD & BB & STC by MakeMoneyCoESTB2020 | ema sma stdev highest lowest | v4 strategy | −0.5% | 6 | 2.2% |
| Lawyers Trend Pro Strategy by ErdemDemir | sma stdev lowest highest | v4 strategy | +12.1% | 102 | 41.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
VerifiedNothing 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
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 19 ms | With a workaround 1,000 bars in 50 ms | Not measured |
| PineTS | Not tested | Not tested | Not 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")