Volatility Range Breakout strategy in Pine Script
Bands around the price, as wide as its recent range, are averaged; a break of a band opens the trade.
Volatility bands · WMA · long and short · by wbburgin · Mozilla Public License 2.0
What is its backtest worth?
A parameter sweep finds the best setting of the Volatility Range Breakout strategy. PineTester tells you if it holds up: whether the best result was only picked after the fact, what it makes with fees, what you could have known at the time (a walk-forward on data the pick did not see) and how deep it fell along the way. On the free start, your first own backtest gets the full evaluation; its trade list and result file come with the full report or a plan.
Creates a draft on Binance BTC/USDT 4h over about six and a half years with 0.1% commission per order and 2 ticks slippage; you start it yourself. A backtest uses credits; new accounts receive free credits.
The rules
- Builds a high and a low band from the close plus and minus the recent volatility (the high–low range of the source), averaged over the set length.
- Enters long when the high crosses above the high band, short when the low crosses below the low band.
- Closes when the close crosses back through the middle of the two bands; allows up to 20 entries in the same direction.
What to watch for
- With up to 20 entries the position can grow far beyond a single trade; check the size and the largest drop.
- Only two inputs: the full grid is small, so the result of one setting can be read against its neighbours.
How PineTester judges a backtest, with its sources: how we judge results.
The Pine Script code
The author's code with its licence notice (Mozilla Public License 2.0), with one change marked in the code: the same test conditions for every library strategy in its strategy() line – the whole account per trade (split over the entries where it adds to a position), 0.1% commission per order and 2 ticks slippage; the trading rules are unchanged. Pine Script v5; PineTester converts it to v6 automatically, so results can differ slightly from TradingView.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © wbburgin
// Modified by PineTester (2026-11-14): the same test conditions for every library strategy in strategy(): 5% of equity per entry (pyramiding 20),
// 0.1% commission per order, 2 ticks slippage, initial capital 10,000. The trading rules are unchanged.
//@version=5
strategy("Volatility Range Breakout Strategy [wbburgin]", shorttitle = "VRB Strategy [wbburgin]", overlay=true,
pyramiding=20,max_bars_back=2000, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=5, commission_type=strategy.commission.percent, commission_value=0.1, slippage=2)
wma(float priceType,int length,float weight) =>
norm = 0.0
sum = 0.0
for i = 0 to length - 1
norm := norm + weight
sum := sum + priceType[i] * weight
sum / norm
// This definition of volatility uses the high-low range divided by the average of that range.
volatility(source,length) =>
h = ta.highest(source,length)
l = ta.lowest(source,length)
vx = 2 * (h - l) / (h + l)
vx
vm1 = input.int(100,"Average Length")
volLen = input.int(100,"Volatility Length")
vsrc = input.source(close,"Volatility Source")
cross_type = input.source(close,"Exit Source")
exit_type = input.string("Volatility MA",options=["Volatility MA","Range Crossover"],title="Exit Type")
volatility = volatility(vsrc,volLen)
highband1 = close + (close * volatility)
lowband1 = close - (close * volatility)
hb1 = wma(highband1,vm1,volatility)
lb1 = wma(lowband1,vm1,volatility)
hlavg = math.avg(hb1,lb1)
upcross = ta.crossover(high,hb1) //Crossing over the high band of historical volatility signifies a bullish breakout
dncross = ta.crossunder(low,lb1) //Crossing under the low band of historical volatility signifies a bearish breakout
vlong = upcross
vshort = dncross
vlong_exit = switch
exit_type == "Volatility MA" => ta.crossunder(cross_type,hlavg)
exit_type == "Range Crossover" => ta.crossunder(cross_type,hb1)
vshort_exit = switch
exit_type == "Volatility MA" => ta.crossover(cross_type,hlavg)
exit_type == "Range Crossover" => ta.crossover(cross_type,lb1)
if vlong
strategy.entry("Long",strategy.long)
if vlong_exit
strategy.close("Long")
if vshort
strategy.entry("Short",strategy.short)
if vshort_exit
strategy.close("Short")
plot(hlavg,color=color.white,title="Weighted Volatility Moving Average")
t = plot(hb1,color=color.new(color.red,50),title="Volatility Reversal Band - Top")
b = plot(lb1,color=color.new(color.green,50),title="Volatility Reversal Band - Bottom")
alertcondition(vlong,"Volatility Long Entry Signal")
alertcondition(vlong_exit,"Volatility Long Exit Signal")
alertcondition(vshort,"Volatility Short Entry Signal")
alertcondition(vshort_exit,"Volatility Short Exit Signal")
fill(t,b,color=color.new(color.aqua,90))
Related strategies
- Darvas Box – A break out of the Darvas box in the direction of a slow weighted average, with the other side of the box as stop.
- Vix Fix + Stochastic – Buys dips after a spike of the Williams Vix Fix and sells when the Stochastic turns down from overbought.
- Fractal Breakout – Buys above the last fractal support when a test says the short ATR is clearly below the long one.
Open-source script by wbburgin, shown under the Mozilla Public License 2.0. Not a recommendation; neither the author nor TradingView endorses PineTester. Backtests are hypothetical results on past data, not a forecast.