Bollinger %B and BandWidth

Calculate the Bollinger %B and BandWidth for a given price series.

Bollinger (2001, “Bollinger on Bollinger Bands”) derived two indicators from his bands. %B places the close within the bands: 0 is on the lower band, 0.5 on the middle band and 1 on the upper band, with values below 0 or above 1 for a close outside the bands. BandWidth is the distance between the bands relative to the middle band, a measure of volatility that can be compared across securities and over time.

The formula is as follows:

\[\text{Middle Band} = \operatorname{SMA}(\text{Close},\; \text{window})\] \[\text{Upper Band} = \text{Middle Band} + (\text{num\_std\_dev} \cdot \text{Std Dev}),\;\; \text{Lower Band} = \text{Middle Band} - (\text{num\_std\_dev} \cdot \text{Std Dev})\] \[\%B = \frac{\text{Close} - \text{Lower Band}}{\text{Upper Band} - \text{Lower Band}}\] \[\text{BandWidth} = \frac{\text{Upper Band} - \text{Lower Band}}{\text{Middle Band}}\]

The bands are those of get_bollinger_bands, with the population standard deviation (dividing by n) that Bollinger specifies and TA-Lib and StockCharts implement, and the defaults are Bollinger’s own 20 periods and 2 standard deviations. Both results are decimals: a BandWidth of 0.10 means bands 10% of the middle band apart (StockCharts multiplies it by 100). A BandWidth at a six-month low marks what Bollinger calls the Squeeze, which often precedes a larger move without saying in which direction; %B above 1 or below 0 shows a close outside the bands, which in a trend can last for weeks.

For more information, see the following sources:

Also known as: Percent B, %b, Bollinger Band Width, BBW, Bollinger Squeeze.

Bollinger %B and BandWidth in Python or via the MCP

Install the Finance Toolkit with:

pip install financetoolkit -U

Then call get_bollinger_percent_b_and_bandwidth as shown below.

from financetoolkit import Toolkit

toolkit = Toolkit(
    tickers=["AAPL"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2024-01-01",
    end_date="2025-12-31",
)

toolkit.technicals.get_bollinger_percent_b_and_bandwidth().xs("AAPL", level=1, axis="columns").tail(5)

Or, after setting up the Finance Toolkit MCP, ask:

Where did Apple trade within its Bollinger Bands (%B), and how wide were the bands, from 24 to 31 December 2025?

Which returns:

Date %B BandWidth
2025-12-24 0.2981 0.0597
2025-12-26 0.2899 0.0609
2025-12-29 0.3276 0.0614
2025-12-30 0.308 0.0586
2025-12-31 0.2372 0.0499

Parameters

get_bollinger_percent_b_and_bandwidth accepts the following parameters.

ParameterTypeDescriptionExample
periodstrThe time period to consider for historical data. Can be “intraday”, “daily”, “weekly”, “monthly”, “quarterly” or “yearly”. Defaults to “daily”."weekly"
close_columnstrThe column name for closing prices in the historical data. Defaults to “Adj Close”."Close"
windowintNumber of periods of the moving average and standard deviation. Defaults to 20.20
num_std_devfloatNumber of standard deviations for the bands. Defaults to 2.3
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise).2
growthboolWhether to calculate the growth of %B and BandWidth. Defaults to False.True
lagint | list[int]The lag to use for the growth calculation. Defaults to 1.4
standardizeboolWhether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False.True
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column
Share