strategy · on price · open source
Bollinger Bands mean reversion strategy (20, 2)
Bollinger Bands (20, 2) mean reversion: buy a close at or below the lower band, exit back at the middle band. Rules, code and a backtest with costs included. Deepwick Script runs a subset of Pine Script.
What the Bollinger Bands Mean Reversion strategy does
Bollinger Band mean reversion: long at the lower band, exit on the move back to the basis. The bands themselves are plotted for visual confirmation.
- Lower
Multfor tighter bands (more signals, smaller wins); raise it for stretchier extremes. - Pair with a higher-timeframe trend filter to avoid mean-reverting into a crash.
- Best on range-bound assets and short timeframes.
Entry and exit rules
Language reference: Deepwick Script.
Default inputs
| Input | Default | Min | Max |
|---|---|---|---|
Length | 20 | 5 | 100 |
StdDev Mult | 2 | 0.5 | 5 |
Stop Loss % | 1.5 | 0.5 | 10 |
Take Profit % | 4 | 0 | 20 |
How Deepwick fills this strategy
- Market orders fill at the next bar's open.
- Stops and limits follow the bar's path: open → nearer extreme → farther extreme → close.
- Gaps fill at the open.
- Commission is charged on every fill, and slippage applies to market and stop fills, as set in
strategy().
Source code
Deepwick Script, a subset of Pine Script · read-only
//@version=6
strategy("Bollinger Mean Reversion", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05)
len = input.int(20, "Length", minval=5, maxval=100, step=1)
mult = input.float(2, "StdDev Mult", minval=0.5, maxval=5, step=0.1)
stopPct = input.float(1.5, "Stop Loss %", minval=0.5, maxval=10, step=0.1)
tpPct = input.float(4, "Take Profit %", minval=0, maxval=20, step=0.1)
colorNeutral = input.color("color.gray", "Color Neutral")
colorLine = input.color("color.blue", "Color Line")
colorUp = input.color("color.green", "Color Up")
colorDown = input.color("color.red", "Color Down")
[basis, upper, lower] = ta.bb(close, len, mult)
longSignal = strategy.position_size == 0 and close <= lower
exitSignal = strategy.position_size > 0 and close >= basis
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)
if exitSignal
strategy.close("Long", comment="Back to basis")
p_Basis = plot(basis, "Basis", color=colorNeutral, linewidth=1)
p_Upper = plot(upper, "Upper", color=colorLine, linewidth=1)
p_Lower = plot(lower, "Lower", color=colorLine, linewidth=1)
fill(p_Upper, p_Lower, color=color.new(colorLine, 92))
plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=colorUp, text="L")
plotshape(exitSignal, "Exit", style=shape.triangledown, location=location.abovebar, color=colorDown, text="X")
alertcondition(longSignal, "BB long entry", "Price touched lower Bollinger Band")
alertcondition(exitSignal, "BB exit", "Price returned to basis")
Run it in Deepwick
- Open a chart of any crypto pair or CFD.
- Click “Scripts” and add this strategy.
- Press Run and read the trades in the Strategy Tester.
FAQ
Buy the lower band touch or wait for a close back inside?
This script buys the close at or below the band. Waiting for a close back inside (ta.crossover(close, lower)) usually gives fewer trades; the Bollinger example on the Deepwick home page uses that variant.
What settings work best?
20 periods and 2 standard deviations are the defaults. A higher multiplier means fewer, more extreme entries. Validate any change out of sample.
When does Bollinger mean reversion fail?
When price walks the band: in a strong trend it can ride the lower band for many bars. The 1.5% stop limits each loss, but the state-based entry can re-enter repeatedly.
No comments yet.
Sign in to comment.