Signature | Description | Parameters |
---|---|---|
#include <DataFrame/DataFrameFinancialVisitors.h> template<typename T, typename I = unsigned long, std::size_t A = 0> struct CenterOfGravityVisitor; // ------------------------------------- template<typename T, typename I = unsigned long, std::size_t A = 0> using cog_v = CenterOfGravityVisitor<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 rolling values of Ehlers' center of gravity indicator. It requires 1 input column. The result is a vector of values with same number of items as the given column. The first roll_count - 1 items, in the result, will be NAN. CG shows the position of the balance point of the Time Series values within the lookback window. For a constant time series (for example, sideways markets) the center of gravity is located around the middle point of the window, so CG generates values around -(Period/2+1). If time series rises within the window, the CG shifts from the middle point to the right in the window (to lower negative values). If time series drops within the window, the CG shifts from the middle point to the left (to higher negative values). The sharper the rise (drop) of the time series, the stronger the CG shifts from its middle point. Trending markets can then be identified using CG. For example, given Period=10, a constant time series (sideway markets) will produce CG output equal to -5.5. Rising time series then produces CG values in the range [-5.5, 0], and dropping time series produces CG values in the range [-11, -5.5]. explicit CenterOfGravityVisitor(size_t roll_count = 10); |
T: Column data type I: Index type A: Memory alignment boundary for vectors. Default is system default alignment |
static void test_CenterOfGravityVisitor() { std::cout << "\nTesting CenterOfGravityVisitor{ } ..." << std::endl; typedef StdDataFrame<std::string> StrDataFrame; StrDataFrame df; try { df.read("data/SHORT_IBM.csv", io_format::csv2); cog_v<double, std::string> cog; df.single_act_visit<double>("IBM_Close", cog); assert(cog.get_result().size() == 1721); assert(std::isnan(cog.get_result()[0])); assert(std::isnan(cog.get_result()[8])); assert(std::abs(cog.get_result()[10] - -5.4998) < 0.0001); assert(std::abs(cog.get_result()[14] - -5.51127) < 0.0001); assert(std::abs(cog.get_result()[25] - -5.51401) < 0.0001); assert(std::abs(cog.get_result()[1720] - -5.60765) < 0.0001); assert(std::abs(cog.get_result()[1712] - -5.54681) < 0.0001); assert(std::abs(cog.get_result()[1707] - -5.44354) < 0.0001); } catch (const DataFrameError &ex) { std::cout << ex.what() << std::endl; } }