DocsExamples

Bollinger Breakout Strategy

A volatility breakout strategy that enters when price expands beyond the upper Bollinger Band and exits on mean reversion back to the middle band.

Concept

Bollinger Bands measure volatility. When price pushes through the upper band, it signals strong momentum. The strategy rides this momentum until price reverts to the moving average (middle band).

SignalConditionLogic
BuyClose > Upper BandVolatility breakout
SellClose < Middle BandMean reversion

Code

/** * Bollinger Breakout Strategy * * Buy: Price closes above upper band (breakout) * Sell: Price closes below middle band (mean reversion) */ function define(ctx) { ctx.param('period', { type: 'int', label: 'BB Period', default: 20, min: 10, max: 50, step: 5, optimize: true, }); ctx.param('stdDev', { type: 'float', label: 'Std Dev Multiplier', default: 2.0, min: 1.0, max: 3.0, step: 0.5, optimize: true, }); } function init(ctx) { ctx.addIndicator('bb', 'BB', { period: ctx.p.period, stdDev: ctx.p.stdDev, source: 'close', }); } function onBar(ctx, i) { const upper = ctx.ind.bb_upper[i]; const middle = ctx.ind.bb_middle[i]; const close = ctx.series.close[i]; // Skip if indicators not ready if (q.isNaN(upper) || q.isNaN(middle)) return; const pos = ctx.position('ASSET'); // Buy: breakout above upper band if (close > upper && pos.qty === 0) { ctx.order.market('ASSET', 1, { signal: 'buy', reason: 'upper_band_breakout' }); } // Sell: mean reversion back to middle band if (close < middle && pos.qty > 0) { ctx.order.close('ASSET', { signal: 'sell', reason: 'mean_reversion' }); } }

How It Works

Bollinger Bands Output

The BB indicator produces three series:

SeriesAccessDescription
Upperctx.ind.bb_upper[i]Middle + (stdDev * standard deviation)
Middlectx.ind.bb_middle[i]Simple moving average of close
Lowerctx.ind.bb_lower[i]Middle - (stdDev * standard deviation)

Entry Logic

When the close price exceeds the upper band, it means price has moved more than N standard deviations above the mean — a statistically significant move that often continues.

Exit Logic

The middle band (SMA) acts as a "fair value" reference. When price drops back below it, the breakout momentum has faded.

Parameters

ParameterDefaultRangePurpose
period2010-50Lookback for SMA and standard deviation
stdDev2.01.0-3.0Band width in standard deviations

period: Shorter periods (10-15) create tighter, more reactive bands. Longer periods (30-50) create wider bands that filter out noise.

stdDev: Higher values (2.5-3.0) require stronger breakouts, producing fewer but higher-quality signals. Lower values (1.0-1.5) generate more signals with more false breakouts.

Best Markets

  • Works well in markets that alternate between consolidation and expansion
  • Performs in crypto due to frequent volatility cycles
  • Struggles in steadily trending markets with no clear breakout points

Recommended Timeframes

TimeframeCharacter
4hGood balance of signal frequency and reliability
1dFewer signals, higher quality breakouts
1hMore signals, more false breakouts — consider adding volume filters

Enhancement Ideas

  • Add volume confirmation (only enter breakouts with above-average volume)
  • Detect band squeeze (narrowing bands) before entry for higher probability setups
  • Use a trailing stop instead of mean reversion exit for stronger trends
  • Add RSI filter to avoid buying into already-extended momentum

Related

examplebollingerbandsbreakoutvolatilitymean reversionqsl