Pine Script v4 · Strategy
OBV Pyr
A Pine Script v4 strategy built on EMA (source, 250), Change (src) and CUM (nv). It has 14 order rules, 7 entries among them, and trades long only with the default settings. Exits use a stop loss and a profit target. Each trade commits 20% of equity and pays 0.075% commission. 6 settings; defaults include Strategy Direction long, Fast filter length 250 and Slow filter length 500.
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 OBV Pyr repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 7 of 7 trades.
- How did OBV Pyr do on BTCUSDT 1D with default settings?
- −1.22%
- From 26 Jan 2024 to 21 Oct 2026, over 27 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can OBV Pyr 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 OBV Pyr use a stop loss?
- Yes
- strategy.exit sets a stop on line 60, 61, 62, 63, 64, 65, 66.
- Does OBV Pyr run in Zeal Terminal?
- Yes
- It compiles in 19 ms and runs 1,000 bars in 36 ms, in the browser.
BTCUSDT · 1D · default settings
- EMA
- EMA
- Change
- CUM
- SMA
- Crossover
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −1.22%
- −$121.57
- Buy and hold
- +102.5%
- same period
- Closed trades
- 27
- 19 won · 8 lost
- Profitable
- 70.4%
- of closed trades
- Profit factor
- 0.88
- gross profit ÷ gross loss
- Max drawdown
- 9.46%
- −$984.54
One run with the author's default settings, $10,000 starting capital, commission as the script sets it. 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.
-
Note
3 settings have no label
In the settings dialog LengthOBV (line 19), ProfitTarget_Percent (line 32), LossTarget_Percent (line 37) show an empty name. The labels on this page come from the variable names.
-
Note
Uses an argument v6 removed
when = on line 43, 45, 47, 49, 51, 53, 55. 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 ema → ta.ema, change → ta.change, cum → ta.cum, sma → ta.sma, crossover → ta.crossover.
- 7 strategy calls use the when = argument (line 43, 45, 47, 49, 51, 53, 55). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 27 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 12: ema → ta.ema
- Line 13: ema → ta.ema
- Line 20: change → ta.change
- Line 20: change → ta.change
- Line 21: cum → ta.cum
- Line 22: sma → ta.sma
- Line 24: crossover → ta.crossover
⋯// © RafaelZioni-//@version=4+//@version=5strategy(title = " OBV Pyr", overlay = true, pyramiding=5,initial_capital = 10000, default_qty_type= strategy.percent_of_equity, default_qty_value = 20, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.075)⋯source=close-v1=ema(source,fastLength)+v1=ta.ema(source,fastLength)-v2=ema(source,slowLength)+v2=ta.ema(source,slowLength) ⋯LengthOBV = input(20)-nv = change(src) > 0 ? volume : change(src) < 0 ? -volume : 0*volume +nv = ta.change(src) > 0 ? volume : ta.change(src) < 0 ? -volume : 0*volume -c = cum(nv) +c = ta.cum(nv) -c_tb = c - sma(c,LengthOBV) +c_tb = c - ta.sma(c,LengthOBV) // Conditions-longCond = crossover(c_tb,0)+longCond = ta.crossover(c_tb,0)//shortCond =crossunder(cnv_tb,0)⋯Same 27 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: input() → input.string()
- Line 9: input() → input.int()
- Line 10: input() → input.int()
- Line 19: input() → input.int()
- Line 32: input() → input.int()
- Line 37: input() → input.int()
7 more edits
- Line 43: when = strategy.opentrades == 0 and longsignal → if block
- Line 45: when = strategy.opentrades == 1 and longsignal → if block
- Line 47: when = strategy.opentrades == 2 and longsignal → if block
- Line 49: when = strategy.opentrades == 3 and longsignal → if block
- Line 51: when = strategy.opentrades == 4 and longsignal → if block
- Line 53: when = strategy.opentrades == 5 and longsignal → if block
- Line 55: when = strategy.opentrades == 6 and longsignal → if block
⋯strategy(title = " OBV Pyr", overlay = true, pyramiding=5,initial_capital = 10000, default_qty_type= strategy.percent_of_equity, default_qty_value = 20, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.075)-strat_dir_input = input(title="Strategy Direction", defval="long", options=["long", "short", "all"])+strat_dir_input = input.string(title="Strategy Direction", defval="long", options=["long", "short", "all"])strat_dir_value = strat_dir_input == "long" ? strategy.direction.long : strat_dir_input == "short" ? strategy.direction.short : strategy.direction.all⋯//-fastLength = input(250, title="Fast filter length ", minval=1)+fastLength = input.int(250, title="Fast filter length ", minval=1)-slowLength = input(500,title="Slow filter length", minval=1)+slowLength = input.int(500,title="Slow filter length", minval=1)source=close⋯src = close-LengthOBV = input(20)+LengthOBV = input.int(20)nv = ta.change(src) > 0 ? volume : ta.change(src) < 0 ? -volume : 0*volume ⋯ -ProfitTarget_Percent = input(3)+ProfitTarget_Percent = input.int(3)Profit_Ticks = close * (ProfitTarget_Percent / 100) / syminfo.mintick⋯ -LossTarget_Percent = input(10)+LossTarget_Percent = input.int(10)Loss_Ticks = close * (LossTarget_Percent / 100) / syminfo.mintick⋯//-strategy.entry("Entry 1", strategy.long, when=strategy.opentrades == 0 and longsignal)+if strategy.opentrades == 0 and longsignal+ strategy.entry("Entry 1", strategy.long)//-strategy.entry("Entry 2", strategy.long, when=strategy.opentrades == 1 and longsignal)+if strategy.opentrades == 1 and longsignal+ strategy.entry("Entry 2", strategy.long)//-strategy.entry("Entry 3", strategy.long, when=strategy.opentrades == 2 and longsignal)+if strategy.opentrades == 2 and longsignal+ strategy.entry("Entry 3", strategy.long)//-strategy.entry("Entry 4", strategy.long, when=strategy.opentrades == 3 and longsignal)+if strategy.opentrades == 3 and longsignal+ strategy.entry("Entry 4", strategy.long)//-strategy.entry("Entry 5", strategy.long, when=strategy.opentrades == 4 and longsignal)+if strategy.opentrades == 4 and longsignal+ strategy.entry("Entry 5", strategy.long)//-strategy.entry("Entry 6", strategy.long, when=strategy.opentrades == 5 and longsignal)+if strategy.opentrades == 5 and longsignal+ strategy.entry("Entry 6", strategy.long)//-strategy.entry("Entry 7", strategy.long, when=strategy.opentrades == 6 and longsignal)+if strategy.opentrades == 6 and longsignal+ strategy.entry("Entry 7", strategy.long)//⋯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: 27 before, 27 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.
⋯// © RafaelZioni-//@version=5+//@version=6strategy(title = " OBV Pyr", overlay = true, pyramiding=5,initial_capital = 10000, default_qty_type= strategy.percent_of_equity, default_qty_value = 20, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.075)⋯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 |
|---|---|---|---|
| Strategy Direction | long | string · options ["long", "short", "all"] | strat_dir_value |
| Fast filter length | 250 | int · min 1 | v1 |
| Slow filter length | 500 | int · min 1 | v2 |
| Length OBV (no label in the script) | 20 | int | c_tb |
| Profit Target Percent (no label in the script) | 3 | int | Profit_Ticks |
| Loss Target Percent (no label in the script) | 10 | int | Loss_Ticks |
What it calculates
- EMA (
ema,ta.emain v5)source, Fast filter length (250)v1 · line 12 - EMA (
ema,ta.emain v5)source, Slow filter length (500)v2 · line 13 - Change (
change,ta.changein v5)srcnv · line 20 - CUM (
cum,ta.cumin v5)nvc · line 21 - SMA (
sma,ta.smain v5)c, Length OBV (20)c_tb · line 22 - Crossover (
crossover,ta.crossoverin v5)c_tb, 0longCond · line 24
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 Entry 1 long | strategy.opentrades == 0 and longsignal | On | 43 |
| Open Entry 2 long | strategy.opentrades == 1 and longsignal | On | 45 |
| Open Entry 3 long | strategy.opentrades == 2 and longsignal | On | 47 |
| Open Entry 4 long | strategy.opentrades == 3 and longsignal | On | 49 |
| Open Entry 5 long | strategy.opentrades == 4 and longsignal | On | 51 |
| Open Entry 6 long | strategy.opentrades == 5 and longsignal | On | 53 |
| Open Entry 7 long | strategy.opentrades == 6 and longsignal | On | 55 |
| Exit Exit 1 stop and limit | strategy.position_size > 0 | On | 60 |
| Exit Exit 2 stop and limit | strategy.position_size > 0 | On | 61 |
| Exit Exit 3 stop and limit | strategy.position_size > 0 | On | 62 |
| Exit Exit 4 stop and limit | strategy.position_size > 0 | On | 63 |
| Exit Exit 5 stop and limit | strategy.position_size > 0 | On | 64 |
| Exit Exit 6 stop and limit | strategy.position_size > 0 | On | 65 |
| Exit Exit 7 stop and limit | strategy.position_size > 0 | On | 66 |
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 | −1.2% | +102.5% | 27 | 70% | 0.88 | 9.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +5.7% | +5.5% | 17 | 88% | 16.35 | 4.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −3.0% | +9.8% | 9 | 67% | 0.45 | 5.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +4.6% | +8.4% | 23 | 83% | 2.75 | 4.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.7% | +16.7% | 10 | 70% | 1.17 | 4.7% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +6.4% | +21.5% | 22 | 86% | 11.15 | 4.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +1.7% | +148.3% | 21 | 86% | 1.29 | 7.6% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +2.6% | +17.1% | 14 | 86% | 2.24 | 6.6% | 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 | 2 | 100% | $117.47 |
| Sideways market | 12 | 67% | −$299.69 |
| Falling market | 13 | 69% | $60.66 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
7.5%
The 95th percentile of the worst drawdown when the same 27 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 3.5% to 7.5%; the tick is what actually happened, 7.5%. Order luck alone moves it this much.
All 27 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 24 Jun 2025 105,334.00 | 30 Jun 2025 108,585.00 | 6 | $58.63 |
| long | 13 Aug 2025 120,134.00 | 14 Aug 2025 123,833.00 | 1 | $58.84 |
| long | 23 Aug 2025 116,936.00 | 11 Sep 2025 114,556.00 | 19 | −$44.15 |
| long | 3 Sep 2025 111,240.00 | 11 Sep 2025 114,556.00 | 8 | $2.24 |
| long | 3 Sep 2025 111,240.00 | 11 Sep 2025 114,659.00 | 8 | $56.16 |
| long | 8 Sep 2025 111,137.00 | 11 Sep 2025 114,659.00 | 3 | $2.39 |
| long | 8 Sep 2025 111,137.00 | 1 Oct 2025 115,585.00 | 23 | $73.97 |
| long | 25 Sep 2025 113,307.00 | 1 Oct 2025 115,585.00 | 6 | $1.59 |
| long | 25 Sep 2025 113,307.00 | 1 Oct 2025 116,728.00 | 6 | $55.83 |
| long | 29 Sep 2025 112,164.00 | 1 Oct 2025 116,728.00 | 2 | $3.32 |
| long | 29 Sep 2025 112,164.00 | 2 Oct 2025 120,355.00 | 3 | $138.62 |
| long | 13 Oct 2025 114,959.00 | 17 Oct 2025 104,139.00 | 4 | −$198.73 |
| long | 27 Oct 2025 114,559.00 | 4 Nov 2025 103,901.00 | 8 | −$192.73 |
| long | 3 Nov 2025 110,541.00 | 4 Nov 2025 99,882.40 | 1 | −$198.20 |
| long | 11 Nov 2025 106,011.00 | 14 Nov 2025 96,041.90 | 3 | −$187.32 |
| long | 4 Dec 2025 93,429.90 | 9 Dec 2025 93,114.40 | 5 | −$9.38 |
| long | 8 Dec 2025 90,395.30 | 9 Dec 2025 93,114.40 | 1 | $1.42 |
| long | 8 Dec 2025 90,395.30 | 22 Dec 2025 90,523.20 | 14 | −$0.16 |
| long | 17 Dec 2025 87,863.40 | 22 Dec 2025 90,523.20 | 5 | $2.94 |
| long | 17 Dec 2025 87,863.40 | 29 Dec 2025 90,008.10 | 12 | $41.43 |
| long | 27 Dec 2025 87,369.60 | 29 Dec 2025 90,008.10 | 2 | $3.48 |
| long | 27 Dec 2025 87,369.60 | 13 Jan 2026 96,149.00 | 17 | $178.02 |
| long | 23 Jan 2026 89,559.70 | 31 Jan 2026 81,133.60 | 8 | −$187.99 |
| long | 21 Feb 2026 68,020.00 | 25 Feb 2026 69,941.80 | 4 | $51.61 |
| long | 1 Mar 2026 66,973.30 | 2 Mar 2026 68,946.60 | 1 | $54.23 |
| long | 3 Mar 2026 68,830.10 | 4 Mar 2026 70,880.20 | 1 | $55.16 |
| long | 10 Mar 2026 68,432.20 | 11 Mar 2026 70,530.60 | 1 | $57.20 |
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 |
|---|---|---|---|---|---|
| OBV Pyr this page | — | v4 strategy | −1.2% | 27 | 9.5% |
| MACD Strategy with trailing ATR stop by Wunderbit | cum ema sma | v4 strategy | +72.7% | 89 | 53.7% |
| Full CRYPTO pack macd, rsi, obv, ema strategy by SoftKill21 | ema sma cum | v4 strategy | No trades on this market | ||
| meta-capitulation by serafimovkliment | sma cum | v5 strategy | No trades on this market | ||
| MACD Long Strat by TheGrindToday | ema sma | v4 strategy | +24.0% | 79 | 20.1% |
| Stock trending strategy by exlux99 | ema sma | v4 strategy | +35.3% | 21 | 14.7% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 19 ms | Yes 1,000 bars in 36 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/// © RafaelZioni//@version=4strategy(title = " OBV Pyr", overlay = true, pyramiding=5,initial_capital = 10000, default_qty_type= strategy.percent_of_equity, default_qty_value = 20, calc_on_order_fills=false, slippage=0,commission_type=strategy.commission.percent,commission_value=0.075)strat_dir_input = input(title="Strategy Direction", defval="long", options=["long", "short", "all"])strat_dir_value = strat_dir_input == "long" ? strategy.direction.long : strat_dir_input == "short" ? strategy.direction.short : strategy.direction.allstrategy.risk.allow_entry_in(strat_dir_value)//fastLength = input(250, title="Fast filter length ", minval=1)slowLength = input(500,title="Slow filter length", minval=1)source=closev1=ema(source,fastLength)v2=ema(source,slowLength) // filter=true src = closeLengthOBV = input(20)nv = change(src) > 0 ? volume : change(src) < 0 ? -volume : 0*volume c = cum(nv) c_tb = c - sma(c,LengthOBV) // ConditionslongCond = crossover(c_tb,0)//shortCond =crossunder(cnv_tb,0)//longsignal = (v1 > v2 or filter == false ) and longCond//shortsignal = (v1 < v2 or filter == false ) and shortCond //set take profit ProfitTarget_Percent = input(3)Profit_Ticks = close * (ProfitTarget_Percent / 100) / syminfo.mintick //set take profit LossTarget_Percent = input(10)Loss_Ticks = close * (LossTarget_Percent / 100) / syminfo.mintick ////Order Placing//strategy.entry("Entry 1", strategy.long, when=strategy.opentrades == 0 and longsignal)//strategy.entry("Entry 2", strategy.long, when=strategy.opentrades == 1 and longsignal)//strategy.entry("Entry 3", strategy.long, when=strategy.opentrades == 2 and longsignal)//strategy.entry("Entry 4", strategy.long, when=strategy.opentrades == 3 and longsignal)//strategy.entry("Entry 5", strategy.long, when=strategy.opentrades == 4 and longsignal)//strategy.entry("Entry 6", strategy.long, when=strategy.opentrades == 5 and longsignal)//strategy.entry("Entry 7", strategy.long, when=strategy.opentrades == 6 and longsignal)//////if strategy.position_size > 0 strategy.exit(id="Exit 1", from_entry="Entry 1", profit=Profit_Ticks, loss=Loss_Ticks) strategy.exit(id="Exit 2", from_entry="Entry 2", profit=Profit_Ticks, loss=Loss_Ticks) strategy.exit(id="Exit 3", from_entry="Entry 3", profit=Profit_Ticks, loss=Loss_Ticks) strategy.exit(id="Exit 4", from_entry="Entry 4", profit=Profit_Ticks, loss=Loss_Ticks) strategy.exit(id="Exit 5", from_entry="Entry 5", profit=Profit_Ticks, loss=Loss_Ticks) strategy.exit(id="Exit 6", from_entry="Entry 6", profit=Profit_Ticks, loss=Loss_Ticks) strategy.exit(id="Exit 7", from_entry="Entry 7", profit=Profit_Ticks, loss=Loss_Ticks)