# Bollinger Bands (BBANDS)

> Checked StratForge C++23 API, calculation and warm-up behavior.

**Route**: `/indicators/bbands/`

Fixed simple moving average and population standard deviation (divide by n, not n - 1). All three bands use the same price units as the input. This constructor has no movav option.

`mid = SMA(period)`

`top = mid + devfactor * sqrt(sum((price - mid)^2) / period)`

`bottom = mid - devfactor * sqrt(sum((price - mid)^2) / period)`

## Parameters

- `period = 20`
- `devfactor = 2.0`

## Worked example

For period = 4, prices [2, 4, 6, 8] give mean 5, squared deviations [9, 1, 1, 9], variance 5 and standard deviation sqrt(5). With devfactor = 2: mid = 5, top = 9.472136, bottom = 0.527864 (six decimals). The first three bars produce NaN; the first valid output is on bar 4. With the default period, 20 bars are required.

Use finite inputs and positive periods/lookback. This revision normalizes zero period/lookback to 1; it does not validate custom safety values or promise meaningful output for non-finite prices. These illustrative calculations are not trading signals or evidence of returns.

## C++23 API — StratForge

```cpp
#include <stratforge/indicators/bollinger.hpp>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>

int main() {
    stratforge::Line<double> close;
    stratforge::BollingerBands bands(close, 4, 2.0);
    assert(bands.minimum_period() == 4);
    for (double price : {2.0, 4.0, 6.0, 8.0}) {
        close.forward(price);
        bands.next();
        if (close.size() < 4) {
            assert(std::isnan(bands.mid()[0]));
            assert(std::isnan(bands.top()[0]));
            assert(std::isnan(bands.bottom()[0]));
        }
    }
    assert(std::abs(bands.mid()[0] - 5.0) < 1e-9);
    assert(std::abs(bands.top()[0] - 9.472135955) < 1e-9);
    assert(std::abs(bands.bottom()[0] - 0.527864045) < 1e-9);
    std::cout << std::fixed << std::setprecision(6)
              << bands.mid()[0] << " " << bands.top()[0] << " "
              << bands.bottom()[0] << "\n";
}
```

## Reproduce

StratForge `d25404dcf2c3842ba304ddc60ac19cfec1f8aa37` (2026-08-28); GCC 14, C++23; scalar build, no xsimd dependency.

```sh
git clone https://github.com/StratCraftsAI/StratForge.git StratForge
git -C StratForge checkout --detach d25404dcf2c3842ba304ddc60ac19cfec1f8aa37
g++-14 -std=c++23 -O2 -Wall -Wextra -pedantic -IStratForge/include example.cpp -o example
./example
```

`example.cpp`: C++23 API. stdout:

```text
5.000000 9.472136 0.527864
```

## Sources

- [StratForge bollinger.hpp](https://github.com/StratCraftsAI/StratForge/blob/d25404dcf2c3842ba304ddc60ac19cfec1f8aa37/include/stratforge/indicators/bollinger.hpp)
- [StratForge Line](https://github.com/StratCraftsAI/StratForge/blob/d25404dcf2c3842ba304ddc60ac19cfec1f8aa37/include/stratforge/core/line.hpp)
- [StratForge population variance](https://github.com/StratCraftsAI/StratForge/blob/d25404dcf2c3842ba304ddc60ac19cfec1f8aa37/include/stratforge/simd/simd_ops.hpp)
