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

template<typename T, typename I = unsigned long>
struct MACDVisitor;

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

template<typename T, typename I = unsigned long>
using macd_v = MACDVisitor<T, I>;
        
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 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(std::size_t short_mean_period,   // e.g. 12-day
                std::size_t long_mean_period,    // e.g. 26-day
                std::size_t signal_line_period); // e.g.  9-day
        
    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] - 1.5) < 0.000001);
    assert(fabs(macd_result[12] - 1.5) < 0.000001);
    assert(fabs(macd_result[38] - -0.777175) < 0.000001);
    assert(fabs(macd_result[39] - -1.12938) < 0.00001);

    assert(signal_line.size() == 40);
    assert(std::isnan(signal_line[2]));
    assert(std::isnan(signal_line[4]));
    assert(fabs(signal_line[8] - 1.5) < 0.00001);
    assert(fabs(signal_line[12] - 1.5) < 0.00001);
    assert(fabs(signal_line[38] - -1.08524) < 0.00001);
    assert(fabs(signal_line[39] - -1.09785) < 0.00001);

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