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

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
struct BollingerBand;

// -------------------------------------

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using bband_v = BollingerBand<T, I, A>;
        
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. A Bollinger Band® is a technical analysis tool defined by a set of trendlines. They are plotted as two standard deviations, both positively and negatively, away from a simple moving average (SMA) of a security's price and can be adjusted to user preferences.
Bollinger Bands® was developed by technical trader John Bollinger and designed to give investors a higher probability of identifying when an asset is oversold or overbought.
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. get_upper_band_to_raw() – Returns a vector of upper band minus data column.
  2. get_raw_to_lower_band() – Returns a vector of data column minus lower band.
T: Column data type
I: Index type
A: Memory alignment boundary for vectors. Default is system default alignment
#include <DataFrame/DataFrameFinancialVisitors.h>

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
struct AccelerationBandsVisitor;

// -------------------------------------

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using aband_v = AccelerationBandsVisitor<T, I, A>;
        
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 visitor calculates the Acceleration Bands indicators. It requires 3 input columns in the order of close, high, low.
The Acceleration Bands System was introduced in 2002 by Price Headley. The concept is based on the idea of getting into a trade just as the security is trending but before its price moves heavily in one direction or another.
The Acceleration Bands measure volatility over a user-defined number of bars (default is often the past 20 bars). They are plotted using a simple moving average as the midpoint, with the upper and lower bands being of equal distance from the midpoint, similar to Bollinger Bands.
    explicit
    AccelerationBandsVisitor(size_t roll_period = 20, double multiplier = 4);

    roll_period The averaging period
    multiplier Applied to high/low ratio
        
get_upper_band() returns the upper band vector
get_result() returns the mid band vector
get_lower_band() returns the lower band vector
T: Column data type
I: Index type
A: Memory alignment boundary for vectors. Default is system default alignment
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);
}
// -----------------------------------------------------------------------------

static void test_AccelerationBandsVisitor()  {

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

    StrDataFrame    df;

    try  {
        df.read("data/SHORT_IBM.csv", io_format::csv2);

        aband_v<double, std::string>    aband;

        df.single_act_visit<double, double, double>("IBM_Close", "IBM_High", "IBM_Low", aband);

        // Upper-band
        //
        assert(aband.get_upper_band().size() == 1721);
        assert(std::isnan(aband.get_upper_band()[0]));
        assert(std::isnan(aband.get_upper_band()[18]));
        assert(std::abs(aband.get_upper_band()[19] - 191.2407) < 0.0001);
        assert(std::abs(aband.get_upper_band()[25] - 187.2326) < 0.0001);
        assert(std::abs(aband.get_upper_band()[30] - 185.7256) < 0.0001);
        assert(std::abs(aband.get_upper_band()[35] - 185.0129) < 0.0001);
        assert(std::abs(aband.get_upper_band()[1720] - 127.0993) < 0.0001);
        assert(std::abs(aband.get_upper_band()[1712] - 130.0339) < 0.0001);
        assert(std::abs(aband.get_upper_band()[1707] - 129.903) < 0.0001);

        // Mid-band
        //
        assert(aband.get_result().size() == 1721);
        assert(std::isnan(aband.get_result()[0]));
        assert(std::isnan(aband.get_result()[18]));
        assert(std::abs(aband.get_result()[19] - 184.436) < 0.0001);
        assert(std::abs(aband.get_result()[25] - 180.7035) < 0.0001);
        assert(std::abs(aband.get_result()[30] - 179.142) < 0.0001);
        assert(std::abs(aband.get_result()[35] - 178.817) < 0.0001);
        assert(std::abs(aband.get_result()[1720] - 119.8055) < 0.0001);
        assert(std::abs(aband.get_result()[1712] - 123.058) < 0.0001);
        assert(std::abs(aband.get_result()[1707] - 122.7085) < 0.0001);

        // Lower-band
        //
        assert(aband.get_lower_band().size() == 1721);
        assert(std::isnan(aband.get_lower_band()[0]));
        assert(std::isnan(aband.get_lower_band()[18]));
        assert(std::abs(aband.get_lower_band()[19] - 177.8282) < 0.0001);
        assert(std::abs(aband.get_lower_band()[25] - 174.2877) < 0.0001);
        assert(std::abs(aband.get_lower_band()[30] - 172.3706) < 0.0001);
        assert(std::abs(aband.get_lower_band()[35] - 172.6929) < 0.0001);
        assert(std::abs(aband.get_lower_band()[1720] - 113.2393) < 0.0001);
        assert(std::abs(aband.get_lower_band()[1712] - 116.8539) < 0.0001);
        assert(std::abs(aband.get_lower_band()[1707] - 116.1055) < 0.0001);
    }
    catch (const DataFrameError &ex)  {
        std::cout << ex.what() << std::endl;
    }
}
C++ DataFrame