Signature | Description | Parameters |
---|---|---|
#include <DataFrame/DataFrameFinancialVisitors.h> template<typename T, typename I = unsigned long> struct RateOfChangeVisitor; // ------------------------------------- template<typename T, typename I = unsigned long> using roc_v = RateOfChangeVisitor<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 visitor calculates the rate of change based on the period. It requires 1 input column. The result is a vector of values with same number of items as the given column. The first period items, in the result, will be NAN. Rate of change is calculated as the ratio of difference between periods over the beginning of the period explicit RateOfChangeVisitor(size_t period) period: Number of periods |
T: Column data type I: Index type |
static void test_RateOfChangeVisitor() { std::cout << "\nTesting RateOfChangeVisitor{ } ..." << std::endl; typedef StdDataFrame<std::string> StrDataFrame; StrDataFrame df; try { df.read("data/SHORT_IBM.csv", io_format::csv2); roc_v<double, std::string> roc (10); // 10 period rate of change df.single_act_visit<double>("IBM_Close", roc); assert(roc.get_result().size() == 1721); assert(std::isnan(roc.get_result()[0])); assert(std::isnan(roc.get_result()[9])); assert(std::abs(roc.get_result()[10] - 0.0174096) < 0.000001); assert(std::abs(roc.get_result()[14] - -0.0278768) < 0.000001); assert(std::abs(roc.get_result()[25] - -0.0133044) < 0.000001); assert(std::abs(roc.get_result()[1720] - -0.113317) < 0.00001); assert(std::abs(roc.get_result()[1712] - -0.0377142) < 0.000001); assert(std::abs(roc.get_result()[1707] - 0.0343972) < 0.000001); } catch (const DataFrameError &ex) { std::cout << ex.what() << std::endl; } }