布林線 (BBANDS)

已核驗的 StratForge C++23 API、計算與預熱行為。

Route: /indicators/bbands/

固定使用簡單移動平均與母體標準差(除以 n,而不是 n - 1)。三條軌道與輸入價格單位相同。此建構函式沒有 movav 選項。

mid = SMA(period)

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

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

參數

  • period = 20
  • devfactor = 2.0

計算範例

period = 4,價格 [2, 4, 6, 8] 的平均為 5,偏差平方為 [9, 1, 1, 9],變異數為 5,標準差為 sqrt(5)。devfactor = 2 時:mid = 5,top = 9.472136,bottom = 0.527864(六位小數)。前三根柱輸出 NaN,第 4 根首次有效;預設週期需要 20 根柱。

使用有限價格與正數週期及回看長度。此版本把零週期或零回看長度正規化為 1,不驗證自訂安全值,也不保證非有限價格的輸出有意義。範例計算不是交易訊號或報酬證據。

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";
}

重現

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

來源