Lesson 2 of 15

Bollinger Bands

Bollinger Bands

Bollinger Bands are a volatility indicator consisting of three lines:

  • Middle band: Simple Moving Average (SMA) over a rolling window
  • Upper band: Middle + n_std standard deviations
  • Lower band: Middle − n_std standard deviations

They help identify overbought or oversold conditions. When prices touch the upper band, the asset may be overbought; when they touch the lower band, it may be oversold.

Standard Deviation (Population)

For a window w ending at index t:

mean = sum(prices[t-w+1:t+1]) / w
std  = sqrt(sum((p - mean)^2 for p in window) / w)

Task

Implement bollinger_bands(prices, window, n_std=2.0) that returns a tuple (upper, middle, lower), each a list of the same length as prices. The first window - 1 elements of each list are None.

Python runtime loading...
Loading...
Click "Run" to execute your code.