Signature Description Parameters
#include <DataFrame/DataFrameFinancialVisitors.h>

template<typename T, typename I = unsigned long>
struct BollingerBand;
        
This is a “single action visitor”, meaning it is passed the whole data vector in one call and you must use the single_act_visit() interface.

This functor class calculates Bollinger bands and generates the spread between the given column and lower/upper bands. It could be used to generate signals within financial applications.
The constructor takes:
  • Upper band multiplier to be multiplied by standard-deviation and added to the moving average
  • Lower band multiplier to be multiplied by standard-deviation and subtracted from the moving average
  • Number of periods for a simple moving mean and std.
  • Biased; whether the moving std is biased. The default is false meaning the denominator is “n – 1”.
    BollingerBand(double upper_band_multiplier,
                  double lower_band_multiplier,
                  std::size_t moving_mean_period,
                  bool biased = false)
        
There are 2 methods that give you the results:
  1. const result_type &get_upper_band_to_raw() const – Returns a vector of upper band minus data column.
  2. const result_type &get_raw_to_lower_band() const – Returns a vector of data column minus lower band.
T: Column data type
I: Index type
static void test_BollingerBand()  {

    std::cout << "\nTesting BollingerBand{ } ..." << std::endl;

    MyDataFrame::set_thread_level(10);

    std::vector<unsigned long>  idx =
        { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
          21, 22, 23, 24, 25, 26, 27, 28, 29, 31, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
    std::vector<double> d1 =
        { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
          19, 18, 17, 17, 16, 15, 14, 13, 14, 13, 12, 11, 12, 10, 9, 8, 7, 6, 7, 5 };
    MyDataFrame         df;

    df.load_data(std::move(idx), std::make_pair("col_1", d1));

    using bollinger_band_t = BollingerBand<double>;

    bollinger_band_t  visitor(2.0, 2.0, 5);

    df.single_act_visit<double>("col_1", visitor);

    auto    &upper_to_raw = visitor.get_upper_band_to_raw();
    auto    &raw_to_lower = visitor.get_raw_to_lower_band();

    assert(upper_to_raw.size() == 40);
    assert(std::isnan(upper_to_raw[3]));
    assert(fabs(upper_to_raw[8] - 1.16228) < 0.00001);
    assert(fabs(upper_to_raw[12] - 1.16228) < 0.00001);
    assert(fabs(upper_to_raw[38] - 2.68035) < 0.00001);
    assert(fabs(upper_to_raw[39] - 3.88035) < 0.00001);

    assert(raw_to_lower.size() == 40);
    assert(std::isnan(raw_to_lower[1]));
    assert(fabs(raw_to_lower[8] - 5.16228) < 0.00001);
    assert(fabs(raw_to_lower[12] - 5.16228) < 0.00001);
    assert(fabs(raw_to_lower[38] - 1.88035) < 0.00001);
    assert(fabs(raw_to_lower[39] - 0.680351) < 0.00001);

    MyDataFrame::set_thread_level(0);
}