Signature Description Parameters
template<typename T, typename I = unsigned long, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
struct MACDVisitor;
This is a “single action visitor”, meaning it is passed the whole data vector in one call and you must use the single_action_visit() interface.
This functor class calculates the Moving Average Convergence/Divergence oscillator (MACD) which is one of the simplest and most effective momentum indicators available. It could be used to generate signals within financial applications.
The constructor takes:
  • Number of periods for the short-term exponential moving average.
  • Number of periods for the long-term exponential moving average. (short-term EMA – long-term EMA) = MACD Line
  • Number of periods for the signal line. EMA(MACD Line) = Signal Line
  • Decay type for the exponential moving averages.
  • Decay value for the exponential moving averages (See DataFrame Types and Exponential Roll Adopter).
        MACDVisitor(size_type short_mean_period,  // e.g. 12-day
                    size_type long_mean_period,   // e.g. 26-day
                    size_type signal_line_period, // e.g.  9-day
                    exponential_decay_spec ed_spec = exponential_decay_spec::span,
                    double expo_decay_value = 0.2)
        
    There are 3 methods that give you the results:
  1. const result_type &get_macd_line() const – Returns vector of MACD Line (See above).
  2. const result_type &get_signal_line() const – Returns vector of Signal Line (See above).
  3. const result_type &get_macd_histogram() const – Returns vector of MACD Histogram. (MACD Line – Signal Line) = MACD Histogram
T: Column data type
I: Index type
static void test_MACDVisitor()  {

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

    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 macd_t = MACDVisitor<double>;

    macd_t  visitor(2, 5, 6);

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

    auto    &macd_result = visitor.get_macd_line();
    auto    &signal_line = visitor.get_signal_line();
    auto    &macd_histo = visitor.get_macd_histogram();

    assert(macd_result.size() == 40);
    assert(std::isnan(macd_result[3]));
    assert(fabs(macd_result[8] - 0.526749) < 0.000001);
    assert(fabs(macd_result[12] - 0.104049) < 0.000001);
    assert(fabs(macd_result[38] - 2.74705e-06) < 0.000001);
    assert(fabs(macd_result[39] - -1.83136e-06) < 0.000001);

    assert(signal_line.size() == 40);
    assert(std::isnan(signal_line[2]));
    assert(std::isnan(signal_line[4]));
    assert(fabs(signal_line[8] - 2.50206) < 0.00001);
    assert(fabs(signal_line[12] - 1.18789) < 0.00001);
    assert(fabs(signal_line[38] - 0.000150401) < 0.000001);
    assert(fabs(signal_line[39] - -0.000103319) < 0.000001);

    assert(macd_histo.size() == 40);
    assert(std::isnan(macd_histo[0]));
    assert(std::isnan(macd_histo[4]));
    assert(fabs(macd_histo[8] - -1.97531) < 0.00001);
    assert(fabs(macd_histo[12] - -1.08385) < 0.00001);
    assert(fabs(macd_histo[38] - -0.000147654) < 0.000001);
    assert(fabs(macd_histo[39] - 0.000101488) < 0.000001);
}