strategy · own pane · open source
Volatility Regime Filter strategy
Volatility Regime Filter is a free open-source strategy by @deepwick. The best-validated configuration in the research corpus, not a hunch. Deepwick Script runs a subset of Pine Script.
Running…
Loading chart…
What Volatility Regime Filter does
The best-validated configuration in the research corpus, not a hunch. Trades only when realised volatility sits inside a normal band, and only in the direction of the long-term trend.
- The filter: annualised realised volatility (40-bar stdev × √252) ranked against its own recent history; trade only when that percentile is between 10 % and 70 %.
- The trend gate: only long when the close is above its level 252 bars ago.
- Walk-forward validated on XAUUSD H1 (Dukascopy tick, 60,829 bars, 2016→2026) with the config frozen — no per-window optimisation.
- 🚨 The percentile window is the parameter that matters, and it has to be long. The corpus ranked the volatility against the whole 10-year history; a short rolling percentile is a different filter, not a smaller version of the same one. Measured on the same walk-forward (median OOS Sharpe): rolling 160 bars → 2.57 (7 of 11 windows profitable) · rolling 500 → 2.76 (7 of 11) · rolling 1000 → 4.14 (8 of 11) · rolling 5000 → 3.89 (9 of 11). The default here is 1000 bars (25× the volatility lookback); anything under ~500 costs roughly half the edge.
- The published Sharpe was not inflated by look-ahead — it was slightly diluted by it. The corpus computed the percentile with pandas
rank(pct=True)over the whole series, so each bar's percentile used bars that come after it. Recomputing it causally (same window) makes it better, not worse: median OOS Sharpe 4.20 instead of 3.83, mean CAGR 17.4 % instead of 14.8 %, same 10 of 11 windows profitable. So the edge is real and the look-ahead was not what was propping it up. - 🚨 It does not generalise. The same filter on EURUSD peaks at Sharpe 1.23 (CAGR 1.8 %): it needs an asset whose volatility regime shifts line up with directional opportunity. Use it on gold-like instruments, not on range-bound FX.
- 🚨 Sparse and not set-and-forget: ~11 trades a year, average max drawdown -11.4 %, and it struggles in chop (2021-2022 gold) while thriving in trends (2024-2026). It is a low-frequency system that can lose 10 %+ in a bad year.
- No leverage assumed: the Sharpe above is unleveraged. Size it down — 0.5-1 % risk per trade.
Source code
Deepwick Script, a subset of Pine Script · read-only
//@version=6
strategy("Volatility Regime Filter", overlay=false, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05)
lookback = input.int(40, "Vol lookback", minval=10, maxval=200, step=5)
rankWindow = input.int(1000, "Percentile window (bars)", minval=100, maxval=5000, step=100)
lowerPct = input.float(0.1, "Lower vol percentile", minval=0, maxval=0.5, step=0.05)
upperPct = input.float(0.7, "Upper vol percentile", minval=0.5, maxval=1, step=0.05)
momLen = input.int(252, "Trend lookback (bars)", minval=20, maxval=500, step=10)
stopPct = input.float(2, "Stop Loss %", minval=0.25, maxval=10, step=0.1)
tpPct = input.float(6, "Take Profit %", minval=0, maxval=20, step=0.1)
colorLine = input.color("color.blue", "Color Line")
colorNeutral = input.color("color.gray", "Color Neutral")
colorUp = input.color("color.green", "Color Up")
realised = ta.stdev(ta.change(close), lookback) * math.sqrt(252)
volRank = ta.percentrank(realised, rankWindow)
volOk = volRank >= lowerPct * 100 and volRank <= upperPct * 100
trendUp = close > close[momLen]
longSignal = strategy.position_size == 0 and volOk and trendUp
if longSignal
strategy.entry("Long", strategy.long)
if strategy.position_size > 0
strategy.exit("Risk", stop=stopPct > 0 ? strategy.position_avg_price * (1 - stopPct / 100) : na, limit=tpPct > 0 ? strategy.position_avg_price * (1 + tpPct / 100) : na)
hline(70, "Upper band", color=colorNeutral, linestyle=hline.style_dashed)
hline(10, "Lower band", color=colorNeutral, linestyle=hline.style_dashed)
p_Vol_percentile = plot(volRank, "Vol percentile", color=colorLine, linewidth=2)
p_Realised_vol = plot(realised, "Realised vol", color=colorNeutral)
plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=colorUp, text="L")
alertcondition(longSignal, "Vol regime long", "Normal volatility regime and the long-term trend is up")
No comments yet.
Sign in to comment.