Backtest a Bollinger Bands Mean Reversion Strategy
Backtest a Bollinger Bands mean-reversion strategy with trailing protection on real candles, and check regime risk, fill timing, and drawdown.
Before you start
Bollinger Bands mean reversion enters after price stretches beyond a volatility band (20 bars, 2.5 standard deviations) and closes back inside, pairing mean reversion entries with a 3% trailing stop to cap runaway trend risk. QTrader provides this strategy as a ready-to-run Python example in the indicators workbench.
Follow the workflow
- Launch the Python Bollinger mean-reversion example on 4h candles and inspect how
indicator.bb(close, length, mult)unpacksmiddle, upper, lower. - Inspect the entry trigger (
indicator.crossover(close, lower)) and the exit rule (indicator.crossunder(close, upper)) alongside the built-instrategy(trailing_stop_percent=3)protection. - Check same-candle stop assumptions and transaction costs, because mean-reversion trades frequently fill near volatility spikes.
- Evaluate separate trending and ranging date windows to see how the 3% trailing stop behaves during strong breakouts.
Worked example
Run QTrader's bundled Python Bollinger mean-reversion template and inspect both trailing-stop exits and upper-band exits in the trade table.
strategy(trailing_stop_percent=3)
length = input.int(20, "Length", minval=2)
mult = input.float(2.5, "Multiplier")
def enter_long():
middle, upper, lower = indicator.bb(close, length, mult)
return indicator.crossover(close, lower)
def exit_long():
middle, upper, lower = indicator.bb(close, length, mult)
return indicator.crossunder(close, upper)
Limits and interpretation
Mean-reversion systems are vulnerable to structural breakouts ("catching falling knives"), and a fixed 3% trailing stop can be stopped out repeatedly in high-volatility crypto regimes. Historical candle backtests simulate trailing exits using OHLC path assumptions rather than live tick ticks.