Pine Script v4 · Strategy
VCP pivot buy
A Pine Script v4 strategy built on SMA (close, 50), EMA (s, p) and RMA (s, p). It has 4 order rules, 1 entry among them, and trades long only with the default settings. Exits use a stop loss and a profit target. Each trade is one contract and pays no commission. 23 settings; defaults include Length 50, Source close and Offset 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 VCP pivot buy repaint?
- No
- No repainting patterns were found in the source.
- Can VCP pivot buy be converted to Pine Script v5?
- Not tested
- The steps are shown below, but the original does not run in Zeal Terminal's engine, so they could not be checked.
- Does VCP pivot buy use a stop loss?
- Yes
- strategy.exit sets a stop on line 105, 108, 111.
- Does VCP pivot buy run in Zeal Terminal?
- No
- Not yet: [Context.call] Execution error for ID "offset@L36:C15": The provided reference is not a function. Check if the function exists in the stdlib.
Review
What a careful reader of this script would point out, found by checking the source.
-
Likely bug
sma(volume, slow) ignores the settings line 22
Line 22 hard-codes volume where line 11 uses “Source”. Changing those settings moves one band but not this one.
sma(volume, slow)sma(src, slow) -
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 offset(), which v5 removed line 36
offset() on line 36. v5 writes offset(x, n) as x[n].
-
Note
Uses transp =, which v5 removed line 48
transp = on line 48, 49, 89, 90, 91. v5 folds the transparency into the colour with color.new(c, transp).
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 sma → ta.sma, ema → ta.ema, rma → ta.rma, vwma → ta.vwma, wma → ta.wma, highest → ta.highest, lowest → ta.lowest, percentrank → ta.percentrank.
v4 → v5
Not verifiedThe v4 script does not run in Zeal Terminal's engine ([Context.call] Execution error for ID "offset@L36:C15": The provided reference is not a function. Check if the function exists in the stdlib.), so this step could not be checked.
- Line 3: //@version=4 → //@version=5
- Line 10: input(type = input.integer) → input.int()
- Line 11: sma → ta.sma
- Line 14: sma → ta.sma
- Line 17: sma → ta.sma
- Line 20: input(type = input.integer) → input.int()
33 more edits
- Line 22: sma → ta.sma
- Line 23: sma → ta.sma
- Line 25: ema → ta.ema
- Line 25: ema → ta.ema
- Line 25: ema → ta.ema
- Line 25: ema → ta.ema
- Line 25: ema → ta.ema
- Line 25: ema → ta.ema
- Line 27: ema → ta.ema
- Line 28: rma → ta.rma
- Line 29: vwma → ta.vwma
- Line 30: wma → ta.wma
- Line 32: sma → ta.sma
- Line 36: offset(x, n) → (x)[n]
- Line 48: transp = 0 → color.new(#1155CC, 0)
- Line 49: transp = 50 → color.new(#FF0000, 50)
- Line 53: input(type = input.integer) → input.int()
- Line 54: input(type = input.integer) → input.int()
- Line 55: input(type = input.integer) → input.int()
- Line 56: input(type = input.integer) → input.int()
- Line 57: input(type = input.integer) → input.int()
- Line 65: highest → ta.highest
- Line 66: lowest → ta.lowest
- Line 76: highest → ta.highest
- Line 77: lowest → ta.lowest
- Line 80: percentrank → ta.percentrank
- Line 81: percentrank → ta.percentrank
- Line 89: transp = 70 → color.new(sessioncolor2, 70)
- Line 90: transp = 70 → color.new(sessioncolor3, 70)
- Line 91: transp = 70 → color.new(sessioncolor4, 70)
- Line 93: highest → ta.highest
- Line 94: input(type = input.float) → input.float()
- Line 95: input(type = input.float) → input.float()
⋯// © stockone1231-//@version=4+//@version=5// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯src = input(close, title="Source")-offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)+offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)-out50 = sma(src, len50)+out50 = ta.sma(src, len50)plot(out50, color=color.blue, title="MA", offset=offset)len200 = input(200, minval=1, title="Length")-out200 = sma(src, len200)+out200 = ta.sma(src, len200)plot(out200, color=color.red, title="MA", offset=offset)len150 = input(150, minval=1, title="Length")-out150 = sma(src, len150)+out150 = ta.sma(src, len150)plot(out150, color=color.green, title="MA", offset=offset)slow = input(50, minval=1, title="TREND") // 10 days a-signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 500, defval = 10)+signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 500, defval = 10)// Calculations-slowline = sma(volume, slow)+slowline = ta.sma(volume, slow)-trend = sma(slowline, signal_length)+trend = ta.sma(slowline, signal_length)tema(s, p) =>- 3 * ema(s, p) - 3 * ema(ema(s, p), p) + ema(ema(ema(s, p), p), p)+ 3 * ta.ema(s, p) - 3 * ta.ema(ta.ema(s, p), p) + ta.ema(ta.ema(ta.ema(s, p), p), p)ma(t, s, p) =>- ema_1 = ema(s, p)+ ema_1 = ta.ema(s, p)- rma_1 = rma(s, p)+ rma_1 = ta.rma(s, p)- vwma_1 = vwma(s, p)+ vwma_1 = ta.vwma(s, p)- wma_1 = wma(s, p)+ wma_1 = ta.wma(s, p) tema_1 = tema(s, p)- sma_1 = sma(s, p)+ sma_1 = ta.sma(s, p) t == "ema" ? ema_1 : t == "rma" ? rma_1 : ⋯off(s, o) =>- offset_1 = offset(s, o == 0 ? 1 : o)+ offset_1 = (s)[o == 0 ? 1 : o] o > 0 ? offset_1 : s⋯out22 = ma(mat22, off(src22, off22), len22)-plot(out11, color=#1155CC, transp=0, title="MA 1")+plot(out11, color=color.new(#1155CC, 0), title="MA 1")-plot(out22, color=#FF0000, transp=50, linewidth=2, title="MA 2")+plot(out22, color=color.new(#FF0000, 50), linewidth=2, title="MA 2")//trade to hold trend indicator//⋯//pivot-atr_length = input(defval=3, title="ATR Length", type=input.integer)+atr_length = input.int(defval=3, title="ATR Length")-lookback = input(defval=150, title="Look Back Period", type=input.integer)+lookback = input.int(defval=150, title="Look Back Period")-closerange = input(defval=4, title="Close range", type=input.integer)+closerange = input.int(defval=4, title="Close range")-percentile1 = input(defval=30, title="Smaller Percentile", type=input.integer)+percentile1 = input.int(defval=30, title="Smaller Percentile")-percentile2 = input(defval=40, title="Largerer Percentile", type=input.integer)+percentile2 = input.int(defval=40, title="Largerer Percentile")//range2⋯for i = 1 to atr_length- h1:=highest(high,atr_length)+ h1:=ta.highest(high,atr_length)- l1:=lowest(low,atr_length)+ l1:=ta.lowest(low,atr_length) range1 := h1-l1⋯for i = 1 to atr_length- h2:=highest(close,atr_length)+ h2:=ta.highest(close,atr_length)- l2:=lowest(close,atr_length)+ l2:=ta.lowest(close,atr_length) range2 := h2-l2atrp2 = (range2 / h2) * 100-per1 = percentrank( atrp1, lookback)+per1 = ta.percentrank( atrp1, lookback)-per2 = percentrank( atrp2, lookback)+per2 = ta.percentrank( atrp2, lookback)setting1 = (per1 <= percentile2) and (per1 > percentile1)⋯sessioncolor4 = closepivot and bullishtrenddef? color.red :na-bgcolor(sessioncolor2 , transp=70)+bgcolor(color.new(sessioncolor2, 70) )-bgcolor(sessioncolor3 , transp=70)+bgcolor(color.new(sessioncolor3, 70) )-bgcolor(sessioncolor4 , transp=70)+bgcolor(color.new(sessioncolor4, 70) )high_loopback = input(3, "High Lookback Length")-h3 = float (highest(high, high_loopback))+h3 = float (ta.highest(high, high_loopback))-takePer = input(10.0, title='Take Profit %', type=input.float) / 100+takePer = input.float(10.0, title='Take Profit %') / 100-shortPer = input(10.0, title='Stoploss %', type=input.float) / 100+shortPer = input.float(10.0, title='Stoploss %') / 100stoploss = strategy.position_avg_price * (1 - shortPer)⋯Clean v5
VerifiedSame 12 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 8: input() → input.int()
- Line 9: input() → input.source()
- Line 13: input() → input.int()
- Line 16: input() → input.int()
- Line 19: input() → input.int()
- Line 38: input() → input.string()
8 more edits
- Line 39: input() → input.int()
- Line 40: input() → input.source()
- Line 41: input() → input.int()
- Line 42: input() → input.string()
- Line 43: input() → input.int()
- Line 44: input() → input.source()
- Line 45: input() → input.int()
- Line 92: input() → input.int()
⋯strategy("My Strategy", overlay=true)-len50 = input(50, minval=1, title="Length")+len50 = input.int(50, minval=1, title="Length")-src = input(close, title="Source")+src = input.source(close, title="Source")offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)⋯plot(out50, color=color.blue, title="MA", offset=offset)-len200 = input(200, minval=1, title="Length")+len200 = input.int(200, minval=1, title="Length")out200 = ta.sma(src, len200)plot(out200, color=color.red, title="MA", offset=offset)-len150 = input(150, minval=1, title="Length")+len150 = input.int(150, minval=1, title="Length")out150 = ta.sma(src, len150)plot(out150, color=color.green, title="MA", offset=offset)-slow = input(50, minval=1, title="TREND") // 10 days a+slow = input.int(50, minval=1, title="TREND") // 10 days asignal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 500, defval = 10)⋯ o > 0 ? offset_1 : s-mat11 = input(defval="ema", options=["ema", "sma", "rma", "vwma", "wma", "tema"], title="Moving Average 1 Type")+mat11 = input.string(defval="ema", options=["ema", "sma", "rma", "vwma", "wma", "tema"], title="Moving Average 1 Type")-len11 = input(10, minval=1, title="Moving Average 1 Length")+len11 = input.int(10, minval=1, title="Moving Average 1 Length")-src11 = input(close, title="Moving Average 1 Source")+src11 = input.source(close, title="Moving Average 1 Source")-off11 = input(0, minval=0, title="Moving Average 1 Offset")+off11 = input.int(0, minval=0, title="Moving Average 1 Offset")-mat22 = input(defval="ema", options=["ema", "sma", "rma", "vwma", "wma", "tema"], title="Moving Average 2 Type")+mat22 = input.string(defval="ema", options=["ema", "sma", "rma", "vwma", "wma", "tema"], title="Moving Average 2 Type")-len22 = input(20, minval=1, title="Moving Average 2 Length")+len22 = input.int(20, minval=1, title="Moving Average 2 Length")-src22 = input(close, title="Moving Average 2 Source")+src22 = input.source(close, title="Moving Average 2 Source")-off22 = input(0, minval=0, title="Moving Average 2 Offset")+off22 = input.int(0, minval=0, title="Moving Average 2 Offset")out11 = ma(mat11, off(src11, off11), len11)⋯bgcolor(color.new(sessioncolor4, 70) )-high_loopback = input(3, "High Lookback Length")+high_loopback = input.int(3, "High Lookback Length")h3 = float (ta.highest(high, high_loopback))⋯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: 12 before, 12 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.
⋯// © stockone1231-//@version=5+//@version=6// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯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 | 50 | int · min 1 | out50 |
| Source | close | source | out50 out200 out150 |
| Offset | 0 | int (written input.integer) · min -500 · max 500 | MA |
| Length | 200 | int · min 1 | out200 |
| Length | 150 | int · min 1 | out150 |
| TREND | 50 | int · min 1 | slowline |
| Signal Smoothing | 10 | int (written input.integer) · min 1 · max 500 | trend |
| Moving Average 1 Type | ema | string · options ["ema", "sma", "rma", "vwma", "wma", "tema"] | out11 |
| Moving Average 1 Length | 10 | int · min 1 | out11 |
| Moving Average 1 Source | close | source | out11 |
| Moving Average 1 Offset | 0 | int · min 0 | out11 |
| Moving Average 2 Type | ema | string · options ["ema", "sma", "rma", "vwma", "wma", "tema"] | out22 |
| Moving Average 2 Length | 20 | int · min 1 | out22 |
| Moving Average 2 Source | close | source | out22 |
| Moving Average 2 Offset | 0 | int · min 0 | out22 |
| ATR Length | 3 | int (written input.integer) | l1 h1 l2 h2 line 64 line 75 |
| Look Back Period | 150 | int (written input.integer) | per1 per2 |
| Close range | 4 | int (written input.integer) | closepivot |
| Smaller Percentile | 30 | int (written input.integer) | setting1 rangepivot |
| Largerer Percentile | 40 | int (written input.integer) | setting1 |
| High Lookback Length | 3 | int | h3 |
| Take Profit % | 10.0 | float | longTake1 longTake2 longTake3 |
| Stoploss % | 10.0 | float | stoploss |
What it calculates
- SMA (
sma,ta.smain v5)Source (close), Length (50)out50 · line 11 - SMA (
sma,ta.smain v5)Source (close), Length (200)out200 · line 14 - SMA (
sma,ta.smain v5)Source (close), Length (150)out150 · line 17 - SMA (
sma,ta.smain v5)volume, TREND (50)slowline · line 22 - SMA (
sma,ta.smain v5)slowline, Signal Smoothing (10)trend · line 23 - EMA (
ema,ta.emain v5)s, pema_1 · line 27 - RMA (
rma,ta.rmain v5)s, prma_1 · line 28 - VWMA (
vwma,ta.vwmain v5)s, pvwma_1 · line 29 - WMA (
wma,ta.wmain v5)s, pwma_1 · line 30 - SMA (
sma,ta.smain v5)s, psma_1 · line 32 - Lowest (
lowest,ta.lowestin v5)low, ATR Length (3)l1 · line 66 - Highest (
highest,ta.highestin v5)high, ATR Length (3)h1 · line 65 - Lowest (
lowest,ta.lowestin v5)close, ATR Length (3)l2 · line 77 - Highest (
highest,ta.highestin v5)close, ATR Length (3)h2 · line 76 - Percentrank (
percentrank,ta.percentrankin v5)atrp1, Look Back Period (150)per1 · line 80 - Percentrank (
percentrank,ta.percentrankin v5)atrp2, Look Back Period (150)per2 · line 81 - Highest (
highest,ta.highestin v5)high, High Lookback Length (3)h3 · line 93
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.
The script has 68 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | (long1) | On | 102 |
| Exit Close Long1 stop and limit | strategy.position_size > 0 | On | 105 |
| Exit Close Long2 stop and limit | strategy.position_size > 0 | On | 108 |
| Exit Close Long3 stop and limit | strategy.position_size > 0 | On | 111 |
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.
The live replay needs a run, and this script does not run in Zeal Terminal yet; the answer comes from the checks on the source alone.
- 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.
Similar scripts
Indicators and strategies built from the same calculations.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| MACD Willy Strategy by PtGambler | rma sma ema highest lowest wma | v5 strategy | No trades on this market | ||
| Ultra Moving Average Rating Trend Strategy by exlux99 | ema wma highest lowest sma rma vwma | v5 strategy | +2.8% | 5 | 2.4% |
| Bagheri IG Ether by Bagheri_IG | ema highest lowest rma sma | v4 strategy | +0.0% | 3 | 0.0% |
| Bagheri IG Ether v2 by Bagheri_IG | ema highest lowest rma sma | v4 strategy | −0.0% | 2 | 0.0% |
| Swing Multi Moving Averages Crypto and Stocks Strategy by exlux99 | ema wma highest lowest sma | v5 strategy | +60.2% | 22 | 13.1% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | No [Context.call] Execution error for ID "offset@L36:C15": The provided reference is not a function. Check if the function exists in the stdlib. | 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/// © stockone1231//@version=4// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © perrycc007//@version=4strategy("My Strategy", overlay=true)len50 = input(50, minval=1, title="Length")src = input(close, title="Source")offset = input(title="Offset", type=input.integer, defval=0, minval=-500, maxval=500)out50 = sma(src, len50)plot(out50, color=color.blue, title="MA", offset=offset)len200 = input(200, minval=1, title="Length")out200 = sma(src, len200)plot(out200, color=color.red, title="MA", offset=offset)len150 = input(150, minval=1, title="Length")out150 = sma(src, len150)plot(out150, color=color.green, title="MA", offset=offset)slow = input(50, minval=1, title="TREND") // 10 days asignal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 500, defval = 10)// Calculationsslowline = sma(volume, slow)trend = sma(slowline, signal_length)tema(s, p) => 3 * ema(s, p) - 3 * ema(ema(s, p), p) + ema(ema(ema(s, p), p), p)ma(t, s, p) => ema_1 = ema(s, p) rma_1 = rma(s, p) vwma_1 = vwma(s, p) wma_1 = wma(s, p) tema_1 = tema(s, p) sma_1 = sma(s, p) t == "ema" ? ema_1 : t == "rma" ? rma_1 : t == "vwma" ? vwma_1 : t == "wma" ? wma_1 : t == "tema" ? tema_1 : sma_1off(s, o) => offset_1 = offset(s, o == 0 ? 1 : o) o > 0 ? offset_1 : smat11 = input(defval="ema", options=["ema", "sma", "rma", "vwma", "wma", "tema"], title="Moving Average 1 Type")len11 = input(10, minval=1, title="Moving Average 1 Length")src11 = input(close, title="Moving Average 1 Source")off11 = input(0, minval=0, title="Moving Average 1 Offset")mat22 = input(defval="ema", options=["ema", "sma", "rma", "vwma", "wma", "tema"], title="Moving Average 2 Type")len22 = input(20, minval=1, title="Moving Average 2 Length")src22 = input(close, title="Moving Average 2 Source")off22 = input(0, minval=0, title="Moving Average 2 Offset")out11 = ma(mat11, off(src11, off11), len11)out22 = ma(mat22, off(src22, off22), len22)plot(out11, color=#1155CC, transp=0, title="MA 1")plot(out22, color=#FF0000, transp=50, linewidth=2, title="MA 2")//trade to hold trend indicator//bullishtrenddef = (close>out50) and (out50>out150) and (out150>out200) and (out200>out200[1]) and (out200>out200[20]) and (out50>=out50[1])//pivotatr_length = input(defval=3, title="ATR Length", type=input.integer)lookback = input(defval=150, title="Look Back Period", type=input.integer)closerange = input(defval=4, title="Close range", type=input.integer)percentile1 = input(defval=30, title="Smaller Percentile", type=input.integer)percentile2 = input(defval=40, title="Largerer Percentile", type=input.integer)//range2atr_value1 = 0.0stan1 = 0.0range1 = 0.0l1=0.0h1=0.0for i = 1 to atr_length h1:=highest(high,atr_length) l1:=lowest(low,atr_length) range1 := h1-l1atrp1 = (range1 / h1) * 100//range2atr_value2 = 0.0stan2 = 0.0range2 = 0.0l2=0.0h2=0.0for i = 1 to atr_length h2:=highest(close,atr_length) l2:=lowest(close,atr_length) range2 := h2-l2atrp2 = (range2 / h2) * 100per1 = percentrank( atrp1, lookback)per2 = percentrank( atrp2, lookback)setting1 = (per1 <= percentile2) and (per1 > percentile1)rangepivot = (per1 <= percentile1) and (atrp1<=10)closepivot = (atrp2<=closerange)pivot = rangepivot or closepivotsessioncolor2 = setting1 and bullishtrenddef? color.yellow :nasessioncolor3 = rangepivot and bullishtrenddef? color.blue :nasessioncolor4 = closepivot and bullishtrenddef? color.red :nabgcolor(sessioncolor2 , transp=70)bgcolor(sessioncolor3 , transp=70)bgcolor(sessioncolor4 , transp=70)high_loopback = input(3, "High Lookback Length")h3 = float (highest(high, high_loopback))takePer = input(10.0, title='Take Profit %', type=input.float) / 100shortPer = input(10.0, title='Stoploss %', type=input.float) / 100stoploss = strategy.position_avg_price * (1 - shortPer)longTake1 = strategy.position_avg_price * (1 + takePer)longTake2 = strategy.position_avg_price * (1 + takePer*2)longTake3 = strategy.position_avg_price * (1 + takePer*3)long1 = bullishtrenddef and pivotif (long1) strategy.entry("Long", strategy.long, stop = h3[1] ) if strategy.position_size > 0 strategy.exit(id="Close Long1", qty_percent = 25, stop=stoploss, limit=longTake1) if strategy.position_size > 0 strategy.exit(id="Close Long2", qty_percent = 50, stop=stoploss, limit=longTake2) if strategy.position_size > 0 strategy.exit(id="Close Long3", qty_percent = 100, stop=stoploss, limit=longTake3)