Bollinger Bänder (BBANDS)
Geprüfte StratForge-C++23-API mit Berechnung und Anlaufphase.
Route: /indicators/bbands/
Fester einfacher gleitender Mittelwert und Populationsstandardabweichung (Division durch n, nicht n - 1). Alle drei Bänder haben die Einheit der Eingabepreise. Dieser Konstruktor bietet keine movav-Option.
mid = SMA(period)
top = mid + devfactor * sqrt(sum((price - mid)^2) / period)
bottom = mid - devfactor * sqrt(sum((price - mid)^2) / period)
Parameter
period = 20devfactor = 2.0
Rechenbeispiel
Bei period = 4 und Preisen [2, 4, 6, 8]: Mittelwert 5, quadrierte Abweichungen [9, 1, 1, 9], Varianz 5, Standardabweichung sqrt(5). Mit devfactor = 2: mid = 5, top = 9.472136, bottom = 0.527864 (sechs Dezimalstellen). Die ersten drei Balken ergeben NaN; der vierte ist gültig. Standardmäßig sind 20 Balken nötig.
Endliche Preise und positive period/lookback verwenden. Diese Version normalisiert null auf 1, prüft eigene Sicherheitswerte nicht und garantiert keine sinnvollen Ergebnisse für nicht endliche Preise. Die Beispiele sind keine Handelssignale oder Renditenachweise.
C++23 API — StratForge
#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";
}
Reproduzieren
StratForge d25404dcf2c3842ba304ddc60ac19cfec1f8aa37 (2026-08-28); GCC 14, C++23; scalar build, no xsimd dependency.
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:
5.000000 9.472136 0.527864
