Pine Script v4 · Strategy
Strategy Oil Z Score
A Pine Script v4 strategy built on BB (close, 20), EMA (close, 100) and SMA (divO, 252). It has 8 order rules, 4 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. 8 settings; defaults include Length 252, Oil Z Std Dev 2.0 and Use underlying Oil.
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 Strategy Oil Z Score repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Strategy Oil Z Score do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings; it opened a long position that was still open on the last bar, so there is no result to report. Its code limits trading to a date window, which can leave this period out. It closed no trades on the other markets either. A backtest, not investment advice.
- Can Strategy Oil Z Score be converted to Pine Script v5?
- Not tested
- The steps are shown below, but neither version placed a trade on any market tested, so there was nothing to compare and no download is offered.
- Does Strategy Oil Z Score use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Strategy Oil Z Score run in Zeal Terminal?
- Yes
- It compiles in 19 ms and runs 1,000 bars in 48 ms, in the browser.
BTCUSDT · 1D · default settings
- HLine
- HLine
- Winning trade
- Losing trade
- Equity
- Buy and hold
Needs TVC:MOVE D data, not available: not a Binance spot market Needs VIX D data, not available: not a Binance spot market Needs CBOE:OVX D data, not available: not a Binance spot market Needs TVC:USOIL D data, not available: not a Binance spot market Values that depend on it are empty on this chart.
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 42.92%
- −$42,917.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
1 setting has no label
In the settings dialog Length (line 12) shows an empty name. The labels on this page come from the variable names.
-
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 55, 56, 58, 59, 63, 64, 66, 67. The upgraded version below moves each call inside an if block.
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 highest → ta.highest, security → request.security, bb → ta.bb, ema → ta.ema, sma → ta.sma, stdev → ta.stdev.
- 8 strategy calls use the when = argument (line 55, 56, 58, 59, 63, 64, 66, 67). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 3: //@version=4 → //@version=5
- Line 9: highest → ta.highest
- Line 15: input(type = input.float) → input.float()
- Line 16: input(type = input.integer) → input.int()
- Line 18: security → request.security
- Line 19: security → request.security
8 more edits
- Line 20: security → request.security
- Line 21: security → request.security
- Line 22: input(type = input.bool) → input.bool()
- Line 24: input(type = input.time) → input.time()
- Line 28: bb → ta.bb
- Line 30: ema → ta.ema
- Line 40: sma → ta.sma
- Line 41: stdev → ta.stdev
⋯// © jroche1973-//@version=4+//@version=5strategy("Strategy Oil Z Score", precision=6, overlay=false, pyramiding=100, calc_on_order_fills=true)⋯ // On next bars, update the label's x and y position, and the text it displays.- label.set_xy(_label, bar_index, highest(10)[1])+ label.set_xy(_label, bar_index, ta.highest(10)[1]) label.set_text(_label, _text)⋯use_what = input(title="Use underlying", defval="Oil", options=["Oil", "Move", "Vix"])-_bb_mult = input(title="BB mult", type=input.float, defval=2)+_bb_mult = input.float(title="BB mult", defval=2)-_bb_range = input(title="BB mean range", type=input.integer, defval=20)+_bb_range = input.int(title="BB mean range", defval=20)//src = input(close)-MOVE = security("TVC:MOVE", timeframe.period, close)+MOVE = request.security("TVC:MOVE", timeframe.period, close)-VIX = security("VIX", timeframe.period, close)+VIX = request.security("VIX", timeframe.period, close)-OILVIX = security("CBOE:OVX", timeframe.period, close)+OILVIX = request.security("CBOE:OVX", timeframe.period, close)-USO = security("TVC:USOIL", timeframe.period, close)+USO = request.security("TVC:USOIL", timeframe.period, close)-show_ema = input(type=input.bool, title="Apply EMA", defval=true)+show_ema = input.bool(title="Apply EMA", defval=true)l_ema = input( title="EMA Length", defval=100)-startDateTime = input(type=input.time, defval=timestamp("15 July 2019 09:30"), +startDateTime = input.time(defval=timestamp("15 July 2019 09:30"), title="Start Date", group="Strategy Date Range", ⋯ "the strategy simulates buy and sell trades.") -[_bb_mid, _bb_hig, _bb_low] = bb(close, _bb_range, _bb_mult)+[_bb_mid, _bb_hig, _bb_low] = ta.bb(close, _bb_range, _bb_mult)_bb_percent = (close - _bb_low) / (_bb_hig - _bb_low)-s_ema = ema(close, l_ema)+s_ema = ta.ema(close, l_ema)iOILVIX = 1/OILVIX⋯ divO := USO/iOILVIX-m_basis = sma(divO, Length)+m_basis = ta.sma(divO, Length)-m_zscore = (divO - m_basis) / stdev(divO, Length)+m_zscore = (divO - m_basis) / ta.stdev(divO, Length)show_m = true⋯Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 12: input() → input.int()
- Line 13: input() → input.float()
- Line 14: input() → input.string()
- Line 23: input() → input.int()
- Line 55: when = strategy.position_size > 0 → if block
- Line 56: when = long_move → if block
6 more edits
- Line 58: when = strategy.position_size <= 0 → if block
- Line 59: when = short_move → if block
- Line 63: when = strategy.position_size > 0 → if block
- Line 64: when = long_move → if block
- Line 66: when = strategy.position_size <= 0 → if block
- Line 67: when = short_move → if block
⋯ -Length = input(252) //252+Length = input.int(252) //252-o_StdDevs = input(defval = 2.0, title="Oil Z Std Dev")+o_StdDevs = input.float(defval = 2.0, title="Oil Z Std Dev")-use_what = input(title="Use underlying", defval="Oil", options=["Oil", "Move", "Vix"])+use_what = input.string(title="Use underlying", defval="Oil", options=["Oil", "Move", "Vix"])_bb_mult = input.float(title="BB mult", defval=2)⋯show_ema = input.bool(title="Apply EMA", defval=true)-l_ema = input( title="EMA Length", defval=100)+l_ema = input.int( title="EMA Length", defval=100)startDateTime = input.time(defval=timestamp("15 July 2019 09:30"), ⋯ if m_zscore > 0 and _bb_percent < 0.5 and close < s_ema //short- strategy.entry("sell", strategy.short, 100, when=strategy.position_size > 0)+ if strategy.position_size > 0+ strategy.entry("sell", strategy.short, 100)- strategy.close("close long", when=long_move)+ if long_move+ strategy.close("close long") else- strategy.entry("buy", strategy.long, 100, when=strategy.position_size <= 0)+ if strategy.position_size <= 0+ strategy.entry("buy", strategy.long, 100)- strategy.close("close short", when=short_move)+ if short_move+ strategy.close("close short")else⋯ if m_zscore > 0 and _bb_percent < 0.5//short- strategy.entry("sell", strategy.short, 100, when=strategy.position_size > 0)+ if strategy.position_size > 0+ strategy.entry("sell", strategy.short, 100)- strategy.close("close long", when=long_move)+ if long_move+ strategy.close("close long") else- strategy.entry("buy", strategy.long, 100, when=strategy.position_size <= 0)+ if strategy.position_size <= 0+ strategy.entry("buy", strategy.long, 100)- strategy.close("close short", when=short_move)+ if short_move+ strategy.close("close short")Does it run in Pine Script v6?
Not tested
Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © jroche1973-//@version=5+//@version=6strategy("Strategy Oil Z Score", precision=6, overlay=false, pyramiding=100, calc_on_order_fills=true)⋯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 |
|---|---|---|---|
| Length (no label in the script) | 252 | int | m_basis m_zscore |
| Oil Z Std Dev | 2.0 | float | hline |
| Use underlying | Oil | string · options ["Oil", "Move", "Vix"] | line 34 line 36 |
| BB mult | 2 | float | _bb_mid _bb_hig _bb_low |
| BB mean range | 20 | int (written input.integer) | _bb_mid _bb_hig _bb_low |
| Apply EMA | true | bool | line 52 |
| EMA Length | 100 | int | s_ema |
| Strategy Date Range | |||
| Start DateSpecifies the start date and time from which " + "the strategy simulates buy and sell trades. | timestamp("15 July 2019 09:30") | time | line 53 line 61 |
What it calculates
- BB (
bb,ta.bbin v5)close, BB mean range (20), BB mult (2)_bb_mid · line 28 - BB (
bb,ta.bbin v5)close, BB mean range (20), BB mult (2)_bb_hig · line 28 - BB (
bb,ta.bbin v5)close, BB mean range (20), BB mult (2)_bb_low · line 28 - EMA (
ema,ta.emain v5)close, EMA Length (100)s_ema · line 30 - SMA (
sma,ta.smain v5)divO, Length (252)m_basis · line 40 - Stdev (
stdev,ta.stdevin v5)divO, Length (252)m_zscore · line 41
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 sell short | strategy.position_size > 0 | On | 55 |
| Close close long | long_movem_zscore < 0 and _bb_percent > 0.5 | On | 56 |
| Open buy long | strategy.position_size <= 0 | On | 58 |
| Close close short | short_movem_zscore > 0 and _bb_percent < 0.5 | On | 59 |
| Open sell short | strategy.position_size > 0 | On | 63 |
| Close close long | long_movem_zscore < 0 and _bb_percent > 0.5 | On | 64 |
| Open buy long | strategy.position_size <= 0 | On | 66 |
| Close close short | short_movem_zscore > 0 and _bb_percent < 0.5 | On | 67 |
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.
- ClearHigher-timeframe data4 security call(s), none with lookahead on.
- 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
Not run on other markets: the script requests TVC:MOVE D, VIX D, CBOE:OVX D, TVC:USOIL D, and that data is not available here (not a Binance spot market; not a Binance spot market; not a Binance spot market; not a Binance spot market). Its results would rest on empty values.
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 |
|---|---|---|---|---|---|
| Strategy Oil Z Score this page | — | v4 strategy | 0.0% | 0 | 42.9% |
| Strategy- Combo Z Score by jroche1973 | bb ema sma stdev | v4 strategy | No trades on this market | ||
| Volume Weighted Bollinger Bands Strategy by Ankit_1618 | ema sma stdev | v4 strategy | +10.0% | 15 | 27.5% |
| BB21_MA200_Strategy by mohanee | ema sma stdev | v4 strategy | No trades on this market | ||
| Super Breakout day trading by beststockalert | stdev sma ema | v4 strategy | No trades on this market | ||
| Super Auto Breakout Day Trade Volatile stocks by beststockalert | stdev sma ema | v4 strategy | −6.1% | 51 | 29.1% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 19 ms | With a workaround 1,000 bars in 48 ms | Not measured |
| PineTS | No v5 and v6 only | — | 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. 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/// © jroche1973//@version=4strategy("Strategy Oil Z Score", precision=6, overlay=false, pyramiding=100, calc_on_order_fills=true)f_print(_text) => // Create label on the first bar. var _label = label.new(bar_index, na, _text, xloc.bar_index, yloc.price, color(na), label.style_none, color.gray, size.large, text.align_left) // On next bars, update the label's x and y position, and the text it displays. label.set_xy(_label, bar_index, highest(10)[1]) label.set_text(_label, _text) Length = input(252) //252o_StdDevs = input(defval = 2.0, title="Oil Z Std Dev")use_what = input(title="Use underlying", defval="Oil", options=["Oil", "Move", "Vix"])_bb_mult = input(title="BB mult", type=input.float, defval=2)_bb_range = input(title="BB mean range", type=input.integer, defval=20)//src = input(close)MOVE = security("TVC:MOVE", timeframe.period, close)VIX = security("VIX", timeframe.period, close)OILVIX = security("CBOE:OVX", timeframe.period, close)USO = security("TVC:USOIL", timeframe.period, close)show_ema = input(type=input.bool, title="Apply EMA", defval=true)l_ema = input( title="EMA Length", defval=100)startDateTime = input(type=input.time, defval=timestamp("15 July 2019 09:30"), title="Start Date", group="Strategy Date Range", tooltip="Specifies the start date and time from which " + "the strategy simulates buy and sell trades.") [_bb_mid, _bb_hig, _bb_low] = bb(close, _bb_range, _bb_mult)_bb_percent = (close - _bb_low) / (_bb_hig - _bb_low)s_ema = ema(close, l_ema)iOILVIX = 1/OILVIXdivO = USO/iOILVIX//Proxy of M of Move ie alt VVIXif use_what == "Move" divO := MOVE/iOILVIXelse if use_what == "Vix" divO := VIX/iOILVIXelse divO := USO/iOILVIXm_basis = sma(divO, Length)m_zscore = (divO - m_basis) / stdev(divO, Length)show_m = truehline(o_StdDevs, color=color.white, linestyle=hline.style_dotted)hline(-1 * o_StdDevs, color=color.white, linestyle=hline.style_dotted)color m_color = m_zscore < 0 ? color.new(color.white, 70): color.new(color.blue, 70) //we want white if not activecolor mb_color = m_zscore < 0 and _bb_percent > 0.5 ? color.new(color.white, 70): color.new(color.blue, 70) //we want white if not activeuse_b = _bb_percent < 0.5 ? true: falsebgcolor(show_m and use_b ? color.new(m_color,70) : color.new(color.white, 70)) // we want whatever color our status stipulates if 'on' or white if 'off'//Strategy Testinglong_move = m_zscore < 0 and _bb_percent > 0.5short_move = m_zscore > 0 and _bb_percent < 0.5if show_ema if time >= startDateTime and time <= timenow if m_zscore > 0 and _bb_percent < 0.5 and close < s_ema //short strategy.entry("sell", strategy.short, 100, when=strategy.position_size > 0) strategy.close("close long", when=long_move) else strategy.entry("buy", strategy.long, 100, when=strategy.position_size <= 0) strategy.close("close short", when=short_move)else if time >= startDateTime and time <= timenow if m_zscore > 0 and _bb_percent < 0.5//short strategy.entry("sell", strategy.short, 100, when=strategy.position_size > 0) strategy.close("close long", when=long_move) else strategy.entry("buy", strategy.long, 100, when=strategy.position_size <= 0) strategy.close("close short", when=short_move)