Pine Script v4 · Strategy
Volatility Stop Strategy (by Coinrule)
A Pine Script v4 strategy built on ATR (atrlen). It has 2 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 100% of equity and pays 0.1% commission. 10 settings; defaults include From Month 1, From Day 1 and From Year 2021.
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 Strategy (by Coinrule) repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 3 of 3 trades.
- How did Volatility Stop Strategy (by Coinrule) do on BTCUSDT 1D with default settings?
- +11.02%
- From 26 Jan 2024 to 21 Oct 2026, over 16 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Volatility Stop Strategy (by Coinrule) 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 Volatility Stop Strategy (by Coinrule) use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Volatility Stop Strategy (by Coinrule) run in Zeal Terminal?
- Yes
- It compiles in 10 ms and runs 1,000 bars in 41 ms, in the browser.
BTCUSDT · 1D · default settings
- Vstop
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +11.02%
- $11.02
- Buy and hold
- +102.5%
- same period
- Closed trades
- 16
- 6 won · 10 lost
- Profitable
- 37.5%
- of closed trades
- Profit factor
- 1.10
- gross profit ÷ gross loss
- Max drawdown
- 35.45%
- −$49.86
One run with the author's default settings, $100 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
1 setting does nothing
“Show Date Range” is declared but never read.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
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. The upgraded version below moves each call inside an if block.
-
Note
Uses long =, which v5 renamed line 37
long = on line 37. 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 atr → ta.atr, max → math.max, min → math.min, crossover → ta.crossover, crossunder → ta.crossunder.
- 2 strategy calls use the when = argument (line 37, 39). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 16 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 2: //@version=4 → //@version=5
- Line 7: input(type = input.integer) → input.int()
- Line 8: input(type = input.integer) → input.int()
- Line 9: input(type = input.integer) → input.int()
- Line 10: input(type = input.integer) → input.int()
- Line 11: input(type = input.integer) → input.int()
11 more edits
- Line 12: input(type = input.integer) → input.int()
- Line 13: input(type = input.bool) → input.bool()
- Line 25: atr → ta.atr
- Line 25: tr → ta.tr
- Line 26: max → math.max
- Line 27: min → math.min
- Line 28: max → math.max
- Line 28: min → math.min
- Line 37: long = true → direction = strategy.long
- Line 37: crossover → ta.crossover
- Line 39: crossunder → ta.crossunder
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-//@version=4+//@version=5strategy(shorttitle='Volatility Stop Strategy',title='Volatility Stop Strategy (by Coinrule)', overlay=true, initial_capital = 100, process_orders_on_close=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type=strategy.commission.percent, commission_value=0.1)⋯//Backtest dates-fromMonth = input(defval = 1, title = "From Month", type = input.integer, minval = 1, maxval = 12)+fromMonth = input.int(defval = 1, title = "From Month", minval = 1, maxval = 12)-fromDay = input(defval = 1, title = "From Day", type = input.integer, minval = 1, maxval = 31)+fromDay = input.int(defval = 1, title = "From Day", minval = 1, maxval = 31)-fromYear = input(defval = 2021, title = "From Year", type = input.integer, minval = 1970)+fromYear = input.int(defval = 2021, title = "From Year", minval = 1970)-thruMonth = input(defval = 1, title = "Thru Month", type = input.integer, minval = 1, maxval = 12)+thruMonth = input.int(defval = 1, title = "Thru Month", minval = 1, maxval = 12)-thruDay = input(defval = 1, title = "Thru Day", type = input.integer, minval = 1, maxval = 31)+thruDay = input.int(defval = 1, title = "Thru Day", minval = 1, maxval = 31)-thruYear = input(defval = 2112, title = "Thru Year", type = input.integer, minval = 1970)+thruYear = input.int(defval = 2112, title = "Thru Year", minval = 1970)-showDate = input(defval = true, title = "Show Date Range", type = input.bool)+showDate = input.bool(defval = true, title = "Show Date Range")start = timestamp(fromYear, fromMonth, fromDay, 00, 00) // backtest start window⋯ var stop = 0.0- atrM = nz(atr(atrlen) * atrfactor, tr)+ atrM = nz(ta.atr(atrlen) * atrfactor, ta.tr)- max := max(max, src)+ max := math.max(max, src)- min := min(min, src)+ min := math.min(min, src)- stop := nz(uptrend ? max(stop, max - atrM) : min(stop, min + atrM), src)+ stop := nz(uptrend ? math.max(stop, max - atrM) : math.min(stop, min + atrM), src) uptrend := src - stop >= 0.0⋯//Entry -strategy.entry(id="long", long = true, when = crossover(close, vStop) and window())+strategy.entry(id="long", direction = strategy.long, when = ta.crossover(close, vStop) and window())//Exit-strategy.close("long", when = crossunder(close, vStop))+strategy.close("long", when = ta.crossunder(close, vStop))plot(vStop,"Vstop", color.black, linewidth=2)Same 16 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 17: input() → input.int()
- Line 18: input() → input.source()
- Line 19: input() → input.float()
- Line 37: when = ta.crossover(close, vStop) and window() → if block
- Line 39: when = ta.crossunder(close, vStop) → if block
⋯window() => time >= start and time <= finish ? true : false // create function "within window of time"-length = input(20, "Length", minval = 2)+length = input.int(20, "Length", minval = 2)-src = input(close, "Source")+src = input.source(close, "Source")-factor = input(3.0, "vStop Multiplier", minval = 0.25, step = 0.25)+factor = input.float(3.0, "vStop Multiplier", minval = 0.25, step = 0.25)volStop(src, atrlen, atrfactor) =>⋯//Entry -strategy.entry(id="long", direction = strategy.long, when = ta.crossover(close, vStop) and window())+if ta.crossover(close, vStop) and window()+ strategy.entry(id="long", direction = strategy.long)//Exit-strategy.close("long", when = ta.crossunder(close, vStop))+if ta.crossunder(close, vStop)+ strategy.close("long")plot(vStop,"Vstop", color.black, linewidth=2)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: 16 before, 16 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.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-//@version=5+//@version=6strategy(shorttitle='Volatility Stop Strategy',title='Volatility Stop Strategy (by Coinrule)', overlay=true, initial_capital = 100, process_orders_on_close=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type=strategy.commission.percent, commission_value=0.1)⋯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 |
|---|---|---|---|
| From Month | 1 | int (written input.integer) · min 1 · max 12 | start |
| From Day | 1 | int (written input.integer) · min 1 · max 31 | start |
| From Year | 2021 | int (written input.integer) · min 1970 | start |
| Thru Month | 1 | int (written input.integer) · min 1 · max 12 | finish |
| Thru Day | 1 | int (written input.integer) · min 1 · max 31 | finish |
| Thru Year | 2112 | int (written input.integer) · min 1970 | finish |
| Show Date Range | true | bool | unused |
| Length | 20 | int · min 2 | uptrend vStop |
| Source | close | source | max min uptrend stop vStop |
| vStop Multiplier | 3.0 | float · min 0.25 · step 0.25 | uptrend vStop |
What it calculates
- ATR (
atr,ta.atrin v5)atrlenatrM · line 25
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 long long | crossover(close, vStop) and window() | On | 37 |
| Close long | crossunder(close, vStop) | On | 39 |
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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.
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 | +11.0% | +102.5% | 16 | 38% | 1.10 | 35.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −1.6% | +5.5% | 18 | 28% | 0.94 | 14.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +64.2% | +9.8% | 16 | 31% | 1.38 | 50.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +3.3% | +8.4% | 16 | 31% | 1.08 | 14.5% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +54.3% | +16.7% | 16 | 50% | 1.45 | 51.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +21.4% | +21.5% | 14 | 57% | 1.76 | 16.7% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +66.6% | +148.3% | 16 | 56% | 1.68 | 31.8% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +2.9% | +17.1% | 18 | 50% | 1.13 | 12.0% | 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 | 5 | 20% | −$45.02 |
| Sideways market | 7 | 43% | $52.61 |
| Falling market | 4 | 50% | $3.54 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
51.2%
The 95th percentile of the worst drawdown when the same 16 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 26.3% to 51.2%; the tick is what actually happened, 32.3%. Order luck alone moves it this much.
All 16 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 27 Jan 2024 42,120.60 | 4 Feb 2024 42,582.90 | 8 | $0.90 |
| long | 7 Feb 2024 44,349.60 | 19 Mar 2024 61,937.40 | 41 | $39.73 |
| long | 8 Apr 2024 71,620.00 | 17 Apr 2024 61,277.40 | 9 | −$20.55 |
| long | 17 May 2024 67,024.00 | 19 Jun 2024 64,974.40 | 33 | −$3.90 |
| long | 15 Jul 2024 64,724.10 | 3 Aug 2024 60,698.00 | 19 | −$7.44 |
| long | 23 Aug 2024 64,037.20 | 5 Sep 2024 56,180.00 | 13 | −$13.53 |
| long | 18 Sep 2024 61,760.00 | 27 Dec 2024 94,299.00 | 100 | $49.87 |
| long | 17 Jan 2025 104,077.00 | 23 Feb 2025 96,258.00 | 37 | −$11.17 |
| long | 21 Apr 2025 87,516.20 | 5 Jun 2025 101,509.00 | 45 | $21.10 |
| long | 9 Jun 2025 110,263.00 | 22 Jun 2025 100,964.00 | 13 | −$13.36 |
| long | 3 Jul 2025 109,585.00 | 19 Aug 2025 112,873.00 | 47 | $3.96 |
| long | 16 Sep 2025 116,789.00 | 25 Sep 2025 108,994.00 | 9 | −$9.99 |
| long | 1 Oct 2025 118,595.00 | 10 Oct 2025 112,775.00 | 9 | −$6.91 |
| long | 5 Jan 2026 93,859.70 | 20 Jan 2026 88,427.70 | 15 | −$7.69 |
| long | 4 Mar 2026 72,666.80 | 29 Mar 2026 66,010.90 | 25 | −$11.30 |
| long | 13 Apr 2026 74,418.00 | 22 May 2026 75,539.50 | 39 | $1.43 |
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 |
|---|---|---|---|---|---|
| Volatility Stop Strategy (by Coinrule) this page | — | v4 strategy | +11.0% | 16 | 35.5% |
| SOJA PIVOT by ParaBellum68 | atr | v4 strategy | +3.3% | 9 | 18.2% |
| Volatility System by EduardoMattje | atr | v5 strategy | +150.2% | 2 | 21.5% |
| ATR Stop Buy Strategy by phobo3s | atr | v4 strategy | No trades on this market | ||
| VBand Strategy by Saravanan_Ragavan | atr | v4 strategy | No trades on this market | ||
| Volatility Breakout Strategy [Angel Algo] by AngelAlgo | atr | v5 strategy | +3.1% | 18 | 3.6% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 10 ms | With a workaround 1,000 bars in 41 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 (line 37) 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///@version=4strategy(shorttitle='Volatility Stop Strategy',title='Volatility Stop Strategy (by Coinrule)', overlay=true, initial_capital = 100, process_orders_on_close=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type=strategy.commission.percent, commission_value=0.1)// Works better on 3h, 1h, 2h, 4h// Best time frame 2H//Backtest datesfromMonth = input(defval = 1, title = "From Month", type = input.integer, minval = 1, maxval = 12)fromDay = input(defval = 1, title = "From Day", type = input.integer, minval = 1, maxval = 31)fromYear = input(defval = 2021, title = "From Year", type = input.integer, minval = 1970)thruMonth = input(defval = 1, title = "Thru Month", type = input.integer, minval = 1, maxval = 12)thruDay = input(defval = 1, title = "Thru Day", type = input.integer, minval = 1, maxval = 31)thruYear = input(defval = 2112, title = "Thru Year", type = input.integer, minval = 1970)showDate = input(defval = true, title = "Show Date Range", type = input.bool)start = timestamp(fromYear, fromMonth, fromDay, 00, 00) // backtest start windowfinish = timestamp(thruYear, thruMonth, thruDay, 23, 59) // backtest finish windowwindow() => time >= start and time <= finish ? true : false // create function "within window of time"length = input(20, "Length", minval = 2)src = input(close, "Source")factor = input(3.0, "vStop Multiplier", minval = 0.25, step = 0.25)volStop(src, atrlen, atrfactor) => var max = src var min = src var uptrend = true var stop = 0.0 atrM = nz(atr(atrlen) * atrfactor, tr) max := max(max, src) min := min(min, src) stop := nz(uptrend ? max(stop, max - atrM) : min(stop, min + atrM), src) uptrend := src - stop >= 0.0 if uptrend != nz(uptrend[1], true) max := src min := src stop := uptrend ? max - atrM : min + atrM [stop, uptrend][vStop, uptrend] = volStop(src, length, factor)//Entry strategy.entry(id="long", long = true, when = crossover(close, vStop) and window())//Exitstrategy.close("long", when = crossunder(close, vStop))plot(vStop,"Vstop", color.black, linewidth=2)