Skip to main content

Bollinger Bands (BB)

Warmup Window

Minimum bars needed: period bars (default params: 20 bars (period=20))

Both the SMA middle line and the standard deviation need period bars of data. After warmup, all three bands update in real-time.

After the warmup window is filled, this indicator produces a new value on every incoming bar in real-time.

Lagging Indicator

The bands and middle line lag behind both price and volatility changes.

EventLagDetail
Middle band (SMA) reacts to price reversalperiod / 2 barsSMA has lag ≈ period/2
Bands widen/narrow after volatility changeperiod / 2 barsStd dev computed over same rolling window
Price touches upper/lower bandperiod / 2 barsBands trail the actual volatility shift

Formula for custom params: lag ≈ period / 2

Bollinger Bands are a volatility indicator that consists of a middle band (SMA) and two outer bands (standard deviations). They help traders identify overbought and oversold conditions.

def bollinger_bands(
data: Union[PdDataFrame, PlDataFrame],
source_column='Close',
period=20,
std_dev=2,
middle_band_column_result_column='bollinger_middle',
upper_band_column_result_column='bollinger_upper',
lower_band_column_result_column='bollinger_lower'
) -> Union[PdDataFrame, PlDataFrame]:

Example

from investing_algorithm_framework import download

from pyindicators import bollinger_bands

pl_df = download(
symbol="btc/eur",
market="binance",
time_frame="1d",
start_date="2023-12-01",
end_date="2023-12-25",
save=True,
storage_path="./data"
)
pd_df = download(
symbol="btc/eur",
market="binance",
time_frame="1d",
start_date="2023-12-01",
end_date="2023-12-25",
pandas=True,
save=True,
storage_path="./data"
)

# Calculate bollinger bands for Polars DataFrame
pl_df = bollinger_bands(pl_df, source_column="Close")
pl_df.show(10)

# Calculate bollinger bands for Pandas DataFrame
pd_df = bollinger_bands(pd_df, source_column="Close")
pd_df.tail(10)

BOLLINGER_BANDS

Chart Parameters

The image above uses the following parameters:

ParameterValue
source_columnClose
period20
std_dev2