Zeal Terminal Open Zeal Terminal

Pine Script v5 · Strategy

Volatility Stop with Vwap Strategy

A Pine Script v5 strategy built on VWAP and ATR (atrLength). It has 4 order rules, 2 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. 3 settings; defaults include Source close, Length 20 and ATR Factor 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 Volatility Stop with Vwap Strategy repaint?
No
No repainting patterns were found in the source.
Does Volatility Stop with Vwap Strategy run in Pine Script v6?
Not tested
The v5 script does not run in Zeal Terminal's engine (Pine Script v5 Error at @L10:C0: no source was supplied for the library 'PineCoders/Time/1'. Libraries cannot be fetched from TradingView; supply the source in the 'libraries' option. No libraries were supplied.), so this step could not be checked. The v6 version is shown for reference and is not offered as a download.
Does Volatility Stop with Vwap Strategy use a stop loss?
No
Every exit waits for a signal; nothing limits a losing trade before it fires.
Does Volatility Stop with Vwap Strategy run in Zeal Terminal?
No
Not yet: it imports the library PineCoders/Time/1, whose source is not available here, so it does not run here
Does not run in Zeal Terminal yetIt imports the library PineCoders/Time/1, whose source is not available here, so it does not run here. The settings, logic and review below are read from the source, which does not need a run.

Review

What a careful reader of this script would point out, found by checking the source.

  • 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 96, 97, 98, 99. The upgraded version below moves each call inside an if block.

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
Stop Calculations
SourceclosesourcestopChartTf trendUpChartTf
Length20int · min 2stopChartTf trendUpChartTf
ATR Factor2.0float · min 0.25 · step 0.25atrM stopChartTf trendUpChartTf

What it calculates

  • VWAPvwap_weekly · line 15
  • ATRatrLengthatrM · line 29

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.

hlc3 · volumehlc3 · volumeprice datahigh · low · closehigh · low · closeprice dataatrInputATR Factorsetting · 2.0closecloseprice datalenInputLengthsetting · 20timetimeprice datahighhighprice datalowlowprice datavwap_weeklyVWAP→ vwap_weeklyatrMATR→ atrMsrcsrcvaluetrendUptrendUpvaluemaxmaxvalueminminvaluestopstopvaluestopChartTf, trendUpChartTfstopChartTf · trendUpCh…2 valuesdistance_long, distance_shortdistance_long · distanc…2 valuestpLong_long, sl_shorttpLong_long · sl_short2 valuesslLong_long, tp_shortslLong_long · tp_short2 valuestimeOfBuyShorttimeOfBuyShortvaluelongEntrylongEntryconditionlongExitlongExitconditionshortEntryshortEntryconditionshortExitshortExitconditioninLimboinLimboconditiontrendReversal, trendChangeToUp, trendChangeToDntrendReversal · trendCh…3 conditionslong, shortlong · short2 conditionstpLong_trigger, slShort_TriggertpLong_trigger · slShor…2 conditionsslLong_Trigger, tpShort_triggerslLong_Trigger · tpShor…2 conditionslongExitSignallongExitSignalconditionshortExitSignalshortExitSignalconditionentry:longOpen longstrategy.entryclose:longClose longstrategy.closeentry:shortOpen shortstrategy.entryclose:shortClose shortstrategy.closeplot:49plotplot

Order rules

OrderFires whenBy defaultLine
Open long longlongEntrylong and not longOpened and not long[1]On96
Close longlongExitlongOpened[1] and longExitSignalOn97
Open short shortshortEntryshort and not shortOpened and not short[1]On98
Close shortshortExitshortOpened[1] and shortExitSignalOn99

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 data1 request.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.

Similar scripts

No other indicator or strategy here shares enough of its calculations.

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.

  • 4 strategy calls use the when = argument (line 96, 97, 98, 99). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
  • No v6-only features. v6 is not needed to run this script.

Clean v5

Not verified

The v5 script does not run in Zeal Terminal's engine (Pine Script v5 Error at @L10:C0: no source was supplied for the library 'PineCoders/Time/1'. Libraries cannot be fetched from TradingView; supply the source in the 'libraries' option. No libraries were supplied.), so this step could not be checked.

  • Line 96: when = longEntry → if block
  • Line 97: when = longExit → if block
  • Line 98: when = shortEntry → if block
  • Line 99: when = shortExit → if block
⋯    sl_short := na-strategy.entry("long",strategy.long,when=longEntry)+if longEntry+    strategy.entry("long",strategy.long)-strategy.close("long",when=longExit)+if longExit+    strategy.close("long")-strategy.entry("short",strategy.short,when=shortEntry)+if shortEntry+    strategy.entry("short",strategy.short)-strategy.close('short',when=shortExit)+if shortExit+    strategy.close('short')

Does it run in Pine Script v6?

Not tested

The v5 script does not run in Zeal Terminal's engine (Pine Script v5 Error at @L10:C0: no source was supplied for the library 'PineCoders/Time/1'. Libraries cannot be fetched from TradingView; supply the source in the 'libraries' option. No libraries were supplied.), so this step could not be checked. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.

⋯// @ exlux99-//@version=5+//@version=6// Volatility Stop MTF⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2No It imports the library PineCoders/Time/1, whose source is not available here, so it does not run hereNot measured
PineTSNot testedNot testedNot 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.

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/// © TradingView// @ exlux99//@version=5// Volatility Stop MTF// v3, 2022.05.29// This code was written using the recommendations from the Pine Script™ User Manual's styleInput Guide://   https://www.tradingview.com/pine-script-docs/en/v5/writing/Style_guide.htmlstrategy("Volatility Stop with Vwap Strategy", "VStop  Vwap", true)import PineCoders/Time/1 as pcTimeLibstring GRP1           = "═════════ Stop Calculations ═════════"float  srcInput       = input.source(close, "Source",     group = GRP1)int    lenInput       = input.int(20,       "Length",     group = GRP1, minval = 2)float  atrInput       = input.float(2.0,    "ATR Factor", group = GRP1, minval = 0.25, step = 0.25)vwap_weekly = request.security(syminfo.tickerid, 'W', ta.vwap)// ———————————————————— Functions {// @function        Calculates a value from the ATR, which is offset from price action to use as a stop loss or trend detection. // @param source    (series int/float) The source to calculate the stop value from.  // @param atrLength (simple int) The length over which to calcualte the RMA of the true range. (number of bars back).// @param atrInput (series int/float) A multiplier to allow adjustment of the stop as a function of the ATR value. Example: 1 is 100% of the calculated ATR value. // @param offset    (series int) The first half of the offset calculations required to achieve repainting/non-repainting data when used in `request.security()`.// @returns         ([float, bool]) A tuple of the volatility stop value and the trend direction as a bool. vStop(series float source, simple int atrLength, series float atrInput, series int offset = 0) =>    float src = nz(source, close)    var bool  trendUp = true    var float max = src    var float min = src    var float stop = 0.0    float atrM  = nz(ta.atr(atrLength) * atrInput, ta.tr)    max  := math.max(max, src)    min  := math.min(min, src)    stop := nz(trendUp ? math.max(stop, max - atrM) : math.min(stop, min + atrM), src)    trendUp := src - stop >= 0.0    if trendUp != nz(trendUp[1], true)        max  := src        min  := src        stop := trendUp ? max - atrM : min + atrM    [stop[offset], trendUp[offset]]// ————— Stop calcs[stopChartTf, trendUpChartTf] = vStop(srcInput, lenInput, atrInput, 0)float stop       = stopChartTfbool  trendUp    = trendUpChartTfvar bool inLimbo = false// Get conditions for colors, plotting, and alerts.bool trendReversal   = trendUp != trendUp[1]bool trendChangeToUp = trendUp and not trendUp[1]bool trendChangeToDn = not trendUp and trendUp[1]inLimbo := inLimbo and trendUp == trendUp[1]plot(stop)long = trendUp and close > stop and close > vwap_weeklyshort = not trendUp and close < stop and close < vwap_weeklydistance_long =  close + (close - stop )   distance_short = close - (stop - close)var longOpened = falsevar int timeOfBuyLong = navar float tpLong_long = navar float slLong_long = navar bool longEntry = nalongEntry := long and not longOpened and not long[1]if longEntry    longOpened := true    tpLong_long := distance_long    slLong_long := distance_short    tpLong_trigger = longOpened[1] and (ta.crossover(close, tpLong_long) or ta.crossover(high, tpLong_long)) //or high > lowest_low_var_tpslLong_Trigger = longOpened[1] and (ta.crossunder(close, slLong_long) or ta.crossover(low, slLong_long)) //or low < lowest_low_var_sllongExitSignal = tpLong_trigger or slLong_Trigger or short longExit = longOpened[1] and longExitSignalif longExit     longOpened := false    timeOfBuyLong := na    tpLong_long := na    slLong_long := na///////////////////////////////////////////////////////////////////////////////////////////////////////////////////    var shortOpened = falsevar int timeOfBuyShort = navar float tp_short = navar float sl_short = navar bool shortEntry = nashortEntry := short and not shortOpened  and not short[1]if shortEntry    shortOpened := true    timeOfBuyShort := time    tp_short := distance_short    sl_short := distance_longtpShort_trigger = shortOpened[1] and (ta.crossunder(close, tp_short) or ta.crossunder(low, tp_short)) //or low < highest_high_var_tpslShort_Trigger = shortOpened[1] and (ta.crossover(close, sl_short) or ta.crossover(high, sl_short)) //or high > highest_high_var_slshortExitSignal = long or tpShort_trigger or slShort_TriggershortExit = shortOpened[1] and shortExitSignal if shortExit     shortOpened := false    timeOfBuyShort := na    timeOfBuyShort    tp_short := na    sl_short := nastrategy.entry("long",strategy.long,when=longEntry)strategy.close("long",when=longExit)strategy.entry("short",strategy.short,when=shortEntry)strategy.close('short',when=shortExit)