Pine Script v4 · Strategy
ZVWAP strategy
A Pine Script v4 strategy built on SMA (source1-mean, 2, pds), EMA (close, 55) and VWAP (hlc3). It has 4 order rules, 2 entries among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade is 3 contracts and pays no commission. 6 settings; defaults include length 13, OverSold Line -0.5 and OverBought Line 2.0.
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 ZVWAP strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 1 of 1 trades.
- How did ZVWAP strategy do on BTCUSDT 1D with default settings?
- +614.37%
- From 26 Jan 2024 to 21 Oct 2026, over 13 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can ZVWAP strategy 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 ZVWAP strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does ZVWAP strategy run in Zeal Terminal?
- Yes
- It compiles in 16 ms and runs 1,000 bars in 59 ms, in the browser.
BTCUSDT · 1D · default settings
- Middle Line
- OB High
- OB Low
- OS High
- OS Low
- ZVWAP
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +614.37%
- $61,437.40
- Buy and hold
- +102.5%
- same period
- Closed trades
- 13
- 7 won · 6 lost
- Profitable
- 53.8%
- of closed trades
- Profit factor
- 1.68
- gross profit ÷ gross loss
- Max drawdown
- 748.49%
- −$74,849.00
One run with the author's default settings, $10,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
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 37, 39, 42, 43. The upgraded version below moves each call inside an if block.
-
Note
Uses long =, which v5 renamed line 37
long = on line 37, 39. v5 writes it as direction = strategy.long or strategy.short.
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 sum → math.sum, sqrt → math.sqrt, sma → ta.sma, pow → math.pow, ema → ta.ema, vwap → ta.vwap, crossover → ta.crossover, crossunder → ta.crossunder.
- 4 strategy calls use the when = argument (line 37, 39, 42, 43). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 13 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: sum → math.sum
- Line 8: sum → math.sum
- Line 9: sqrt → math.sqrt
- Line 9: sma → ta.sma
- Line 9: pow → math.pow
8 more edits
- Line 29: ema → ta.ema
- Line 30: ema → ta.ema
- Line 31: vwap → ta.vwap
- Line 35: crossover → ta.crossover
- Line 37: long = true → direction = strategy.long
- Line 39: long = true → direction = strategy.long
- Line 39: crossover → ta.crossover
- Line 43: crossunder → ta.crossunder
⋯// © mohanee-//@version=4+//@version=5//This is based on Z distance from VWAP by Lazybear⋯calc_zvwap(pds, source1) =>- mean = sum(volume*source1,pds)/sum(volume,pds)+ mean = math.sum(volume*source1,pds)/math.sum(volume,pds)- vwapsd = sqrt(sma(pow(source1-mean, 2), pds) )+ vwapsd = math.sqrt(ta.sma(math.pow(source1-mean, 2), pds) ) (close-mean)/vwapsd⋯plot(zvwapVal,title="ZVWAP",color=color.purple, linewidth=2)-longEmaVal=ema(close,slowEma)+longEmaVal=ta.ema(close,slowEma)-shortEmaVal=ema(close,fastEma) +shortEmaVal=ta.ema(close,fastEma) -vwapVal=vwap(hlc3)+vwapVal=ta.vwap(hlc3)zvwapDipped=false⋯ zvwapDipped := zvwapDipped or zvwapVal[i]<=buyLine-longCondition= shortEmaVal > longEmaVal and zvwapDipped and crossover(zvwapVal,0)+longCondition= shortEmaVal > longEmaVal and zvwapDipped and ta.crossover(zvwapVal,0)barcolor(longCondition ? color.yellow: na)-strategy.entry(id="ZVWAPLE", long=true, when= longCondition and strategy.position_size<1) +strategy.entry(id="ZVWAPLE", direction = strategy.long, when= longCondition and strategy.position_size<1) //Add-strategy.entry(id="ZVWAPLE", comment="Add", long=true, when= strategy.position_size>1 and close<strategy.position_avg_price and crossover(zvwapVal,0)) +strategy.entry(id="ZVWAPLE", comment="Add", direction = strategy.long, when= strategy.position_size>1 and close<strategy.position_avg_price and ta.crossover(zvwapVal,0)) //calculate stop Loss⋯strategy.close(id="ZVWAPLE",comment="SL Exit", when=close<stopLossVal) //close all on stop loss-strategy.close(id="ZVWAPLE",comment="TPExitAll", qty=strategy.position_size , when= crossunder(zvwapVal,sellLine)) //close all zvwapVal>sellLine+strategy.close(id="ZVWAPLE",comment="TPExitAll", qty=strategy.position_size , when= ta.crossunder(zvwapVal,sellLine)) //close all zvwapVal>sellLineSame 13 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.int()
- Line 15: input() → input.float()
- Line 16: input() → input.float()
- Line 17: input() → input.int()
- Line 18: input() → input.int()
- Line 19: input() → input.int()
4 more edits
- Line 37: when = longCondition and strategy.position_size<1 → if block
- Line 39: when = strategy.position_size>1 and close<strategy.position_avg_price and ta.crossover(zvwapVal,0) → if block
- Line 42: when = close<stopLossVal → if block
- Line 43: when = ta.crossunder(zvwapVal,sellLine) → if block
⋯strategy(title="ZVWAP[LB] strategy", overlay=false,pyramiding=2, default_qty_type=strategy.fixed, default_qty_value=3, initial_capital=10000, currency=currency.USD)-length=input(13,"length")+length=input.int(13,"length")calc_zvwap(pds, source1) =>⋯lowerBottom=-2.0 //input(-2.0)-buyLine=input(-0.5, title="OverSold Line",minval=-2, maxval=3)+buyLine=input.float(-0.5, title="OverSold Line",minval=-2, maxval=3)-sellLine=input(2.0, title="OverBought Line",minval=-2, maxval=3)+sellLine=input.float(2.0, title="OverBought Line",minval=-2, maxval=3)-fastEma=input(13, title="Fast EMA",minval=1, maxval=50)+fastEma=input.int(13, title="Fast EMA",minval=1, maxval=50)-slowEma=input(55, title="Slow EMA",minval=10, maxval=200)+slowEma=input.int(55, title="Slow EMA",minval=10, maxval=200)-stopLoss =input(5, title="Stop Loss",minval=1) +stopLoss =input.int(5, title="Stop Loss",minval=1) hline(0, title="Middle Line", linestyle=hline.style_dotted, color=color.green)⋯barcolor(longCondition ? color.yellow: na)-strategy.entry(id="ZVWAPLE", direction = strategy.long, when= longCondition and strategy.position_size<1) +if longCondition and strategy.position_size<1+ strategy.entry(id="ZVWAPLE", direction = strategy.long) //Add-strategy.entry(id="ZVWAPLE", comment="Add", direction = strategy.long, when= strategy.position_size>1 and close<strategy.position_avg_price and ta.crossover(zvwapVal,0)) +if strategy.position_size>1 and close<strategy.position_avg_price and ta.crossover(zvwapVal,0)+ strategy.entry(id="ZVWAPLE", comment="Add", direction = strategy.long) //calculate stop LossstopLossVal = strategy.position_avg_price - (strategy.position_avg_price*stopLoss*0.01)-strategy.close(id="ZVWAPLE",comment="SL Exit", when=close<stopLossVal) //close all on stop loss+if close<stopLossVal+ strategy.close(id="ZVWAPLE",comment="SL Exit") //close all on stop loss-strategy.close(id="ZVWAPLE",comment="TPExitAll", qty=strategy.position_size , when= ta.crossunder(zvwapVal,sellLine)) //close all zvwapVal>sellLine+if ta.crossunder(zvwapVal,sellLine)+ strategy.close(id="ZVWAPLE",comment="TPExitAll", qty=strategy.position_size ) //close all zvwapVal>sellLineDoes 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: 13 before, 0 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.
⋯// © mohanee-//@version=5+//@version=6//This is based on Z distance from VWAP by Lazybear⋯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 | 13 | int | zvwapVal |
| OverSold Line | -0.5 | float · min -2 · max 3 | zvwapDipped |
| OverBought Line | 2.0 | float · min -2 · max 3 | close ZVWAPLE |
| Fast EMA | 13 | int · min 1 · max 50 | shortEmaVal |
| Slow EMA | 55 | int · min 10 · max 200 | longEmaVal |
| Stop Loss | 5 | int · min 1 | stopLossVal |
What it calculates
- SMA (
sma,ta.smain v5)pow(source1-mean, 2), pdsvwapsd · line 9 - EMA (
ema,ta.emain v5)close, Slow EMA (55)longEmaVal · line 29 - EMA (
ema,ta.emain v5)close, Fast EMA (13)shortEmaVal · line 30 - VWAP (
vwap,ta.vwapin v5)hlc3vwapVal · line 31
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 ZVWAPLE long | longCondition and strategy.position_size<1 | On | 37 |
| Open ZVWAPLE long | strategy.position_size>1 and close<strategy.position_avg_price and crossover(zvwapVal,0) | On | 39 |
| Close ZVWAPLE | close<stopLossVal | On | 42 |
| Close ZVWAPLE | crossunder(zvwapVal,sellLine) | On | 43 |
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 | +614.4% | +102.5% | 13 | 54% | 1.68 | 748.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +145.4% | +5.5% | 13 | 54% | 1.51 | 115.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.9% | +9.8% | 17 | 41% | 1.01 | 35.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +19.8% | +8.4% | 17 | 59% | 3.49 | 7.5% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +3.1% | +16.7% | 7 | 57% | 3.11 | 1.5% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.1% | +21.5% | 14 | 64% | 1.09 | 0.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +3.7% | +148.3% | 19 | 58% | 1.22 | 11.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +3.9% | +17.1% | 17 | 82% | 2.45 | 2.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 | 10 | 60% | $79,181.20 |
| Sideways market | 3 | 33% | −$17,743.80 |
| Falling market | 0 | — | $0.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
11,858.8%
The 95th percentile of the worst drawdown when the same 13 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 116.8% to 11,858.8%; the tick is what actually happened, 7,209.6%. Order luck alone moves it this much.
All 13 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 26 Mar 2024 69,880.00 | 3 Apr 2024 65,464.00 | 8 | −$13,248.00 |
| long | 5 Apr 2024 68,487.80 | 14 Apr 2024 63,924.50 | 9 | −$13,689.80 |
| long | 23 Apr 2024 66,819.30 | 1 May 2024 60,672.00 | 8 | −$18,441.90 |
| long | 26 Apr 2024 64,498.30 | 1 May 2024 60,672.00 | 5 | −$11,479.00 |
| long | 4 Jun 2024 68,809.90 | 19 Jun 2024 65,175.30 | 15 | −$10,903.70 |
| long | 12 Oct 2024 62,540.00 | 17 Oct 2024 67,620.00 | 5 | $15,240.00 |
| long | 7 Nov 2024 75,572.00 | 8 Nov 2024 75,857.90 | 1 | $857.70 |
| long | 3 Jan 2025 96,984.80 | 19 Jan 2025 104,556.00 | 16 | $22,714.30 |
| long | 15 Jan 2025 96,560.90 | 19 Jan 2025 104,556.00 | 4 | $23,986.10 |
| long | 8 Jun 2025 105,552.00 | 12 Jul 2025 117,528.00 | 34 | $35,926.50 |
| long | 24 Jun 2025 105,334.00 | 12 Jul 2025 117,528.00 | 18 | $36,581.20 |
| long | 8 Aug 2025 117,472.00 | 26 Aug 2025 110,112.00 | 18 | −$22,080.10 |
| long | 19 Sep 2026 80,883.90 | 23 Sep 2026 86,208.60 | 4 | $15,974.10 |
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 |
|---|---|---|---|---|---|
| ZVWAP strategy this page | — | v4 strategy | +614.4% | 13 | 748.5% |
| Ultimate Oscillator [Long] Strategy by mohanee | sma vwap ema | v4 strategy | No trades on this market | ||
| JBravo Swing with GoGo Juice by bradvaughn | sma ema vwap | v4 strategy | +68.7% | 93 | 22.3% |
| VWAP and BB strategy [EEMANI] by mohanee | ema vwap sma | v4 strategy | No trades on this market | ||
| Buying Selling Volume Strategy by exlux99 | vwap ema sma | v5 strategy | No trades on this market | ||
| Long only strategy VWAP with BB and Golden Cross EMA50-200 by SoftKill21 | ema vwap sma | v4 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 16 ms | With a workaround 1,000 bars in 59 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 37, 39) so the engine trades the way the script says.
“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/// © mohanee//@version=4//This is based on Z distance from VWAP by Lazybearstrategy(title="ZVWAP[LB] strategy", overlay=false,pyramiding=2, default_qty_type=strategy.fixed, default_qty_value=3, initial_capital=10000, currency=currency.USD)length=input(13,"length")calc_zvwap(pds, source1) => mean = sum(volume*source1,pds)/sum(volume,pds) vwapsd = sqrt(sma(pow(source1-mean, 2), pds) ) (close-mean)/vwapsdupperTop=2.5 //input(2.5)upperBottom=2.0 //input(2.0)lowerTop=-0.5 //input(-0.5)lowerBottom=-2.0 //input(-2.0)buyLine=input(-0.5, title="OverSold Line",minval=-2, maxval=3)sellLine=input(2.0, title="OverBought Line",minval=-2, maxval=3)fastEma=input(13, title="Fast EMA",minval=1, maxval=50)slowEma=input(55, title="Slow EMA",minval=10, maxval=200)stopLoss =input(5, title="Stop Loss",minval=1) hline(0, title="Middle Line", linestyle=hline.style_dotted, color=color.green)ul1=plot(upperTop, "OB High")ul2=plot(upperBottom, "OB Low")fill(ul1,ul2, color=color.red)ll1=plot(lowerTop, "OS High")ll2=plot(lowerBottom, "OS Low")fill(ll1,ll2, color=color.green)zvwapVal=calc_zvwap(length,close)plot(zvwapVal,title="ZVWAP",color=color.purple, linewidth=2)longEmaVal=ema(close,slowEma)shortEmaVal=ema(close,fastEma) vwapVal=vwap(hlc3)zvwapDipped=falsefor i = 1 to 10 zvwapDipped := zvwapDipped or zvwapVal[i]<=buyLinelongCondition= shortEmaVal > longEmaVal and zvwapDipped and crossover(zvwapVal,0)barcolor(longCondition ? color.yellow: na)strategy.entry(id="ZVWAPLE", long=true, when= longCondition and strategy.position_size<1) //Addstrategy.entry(id="ZVWAPLE", comment="Add", long=true, when= strategy.position_size>1 and close<strategy.position_avg_price and crossover(zvwapVal,0)) //calculate stop LossstopLossVal = strategy.position_avg_price - (strategy.position_avg_price*stopLoss*0.01)strategy.close(id="ZVWAPLE",comment="SL Exit", when=close<stopLossVal) //close all on stop lossstrategy.close(id="ZVWAPLE",comment="TPExitAll", qty=strategy.position_size , when= crossunder(zvwapVal,sellLine)) //close all zvwapVal>sellLine