Skip to content

Bollinger Bands

Nalin Angrish edited this page Jul 18, 2026 · 1 revision

Bollinger Bands

Overview

Bollinger Bands are a volatility-based envelope plotted around a simple moving average (SMA). The middle band is the SMA; the upper and lower bands are placed $k$ standard deviations above and below it.

In Navier-Stocks, Bollinger Bands provide the structural price boundaries used by the breakout detector to identify statistically significant price extensions. When price breaks decisively outside the bands with high volume, it signals a potential continuation or reversal.

Mathematical Definition

Given a price series $p_1, p_2, \dots, p_n$ and a window $\text{period} = N$:

The middle band at index $i$ is the SMA of the trailing $N$ observations:

$$ \text{Middle}_i = \frac{1}{N} \sum_{j=i-N+1}^{i} p_j $$

The population standard deviation of the same window:

$$ \sigma_i = \sqrt{\frac{1}{N} \sum_{j=i-N+1}^{i} (p_j - \text{Middle}_i)^2} $$

The upper and lower bands:

$$ \text{Upper}_i = \text{Middle}_i + k \cdot \sigma_i $$

$$ \text{Lower}_i = \text{Middle}_i - k \cdot \sigma_i $$

Where the standard multiplier is $k = 2$.

Derived Metrics

BandWidth

The relative width of the bands, expressed as a percentage of the middle band:

$$ \text{BandWidth} = \frac{\text{Upper} - \text{Lower}}{\text{Middle}} \times 100 $$

Narrow bands indicate low volatility (squeeze); wide bands indicate high volatility (expansion).

%B

The position of a price relative to the bands, normalized to [0, 1]:

$$ %b = \frac{\text{price} - \text{Lower}}{\text{Upper} - \text{Lower}} $$

%b Value Meaning
$> 1.0$ Price above upper band (overextended bullish)
$1.0$ Price at upper band
$0.5$ Price at middle band (SMA)
$0.0$ Price at lower band
$< 0.0$ Price below lower band (overextended bearish)

Interpretation in the Pipeline

Candles from TickStore
         │
         ▼
  BollingerBands(prices, period=20, k=2)
         │
         ├──► Bands{Upper, Middle, Lower}
         │
         ▼
  Breakout Detector (1.6)
  ── Checks if close is outside bands
  ── Confirms with volume breakout
  ── Publishes signal.intent.*

The breakout detector uses Bollinger Bands in two ways:

  1. Breakout confirmation: A close outside the bands ($%b < 0$ or $%b > 1$) with concurrent volume surge indicates a statistically significant move.
  2. Mean reversion context: When $%b$ is extreme but volume is normal, price may revert to the middle band.

Implementation

File: src/analysts/quantitative/indicators/bollinger.go

Function Input Returns
BollingerBands prices, period, multiplier (Bands, ok)
BollingerBandsSeries prices, period, multiplier []Bands
BandWidth b Bands float64
PercentB price, b Bands float64

Underlying math primitives: src/pkg/math/math.go — Mean() and StdDev()

Constants

const DefaultBollingerPeriod     = 20
const DefaultBollingerMultiplier = 2.0

Edge Cases

Condition Behaviour
len(prices) < period ok = false
period < 2 ok = false (variance undefined for n < 2)
Constant prices ($\sigma = 0$) All three bands collapse to the same value
BandWidth with Middle = 0 Returns 0 (avoids division by zero)
PercentB with collapsed bands Returns 0

Example

Given $N = 3$, $k = 2$, prices [10, 12, 11]:

$$ \text{Middle} = \frac{10 + 12 + 11}{3} = 11 $$

$$ \sigma = \sqrt{\frac{(10-11)^2 + (12-11)^2 + (11-11)^2}{3}} = \sqrt{\frac{2}{3}} \approx 0.8165 $$

$$ \text{Upper} = 11 + 2 \times 0.8165 \approx 12.633 $$

$$ \text{Lower} = 11 - 2 \times 0.8165 \approx 9.367 $$

References

  • VWAP Indicator — primary trend anchor
  • Volume Breakout Indicator — volume confirmation layer
  • src/analysts/quantitative/indicators/bollinger_test.go — comprehensive test suite
  • wiki/Risk-Guardrails.md — operational integration details

🌊 Navier-Stocks Documentation


Clone this wiki locally