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

template<typename T, typename I = unsigned long>
struct TrueRangeVisitor;
        
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 price range based on low, high, and close. It requires 3 input columns in order of low, highme, close.
The result is a vector of values with same number of items as the given columns. The first roll_peiord - 1 items, in the result will be NAN, if rolling_avg is true.

The true range indicator is taken as the greatest of the following: current high less the current low; the absolute value of the current high less the previous close; and the absolute value of the current low less the previous close.
    explicit
    TrueRangeVisitor(bool rolling_avg = false,
                     size_t rolling_period = 14,
                     normalize = false);

    rolling_avg: Apply rolling average to the result vector
    rolling_period: Rolling average period
    normalize: If rolling_avg and normalize are true, the ATR is normalized by 100/ClosePrice
        
T: Column data type
I: Index type
static void test_TrueRangeVisitor()  {

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

    typedef StdDataFrame<std::string> StrDataFrame;

    StrDataFrame    df;

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

        TrueRangeVisitor<double, std::string>   tr;

        std::future<TrueRangeVisitor<double, std::string> &>    fut =
            df.single_act_visit_async<double, double, double>("IBM_Low", "IBM_High", "IBM_Close", tr);

        fut.get();

        assert(tr.get_result().size() == 1721);
        assert(std::abs(tr.get_result()[0] - 2.2) < 0.0001);
        assert(std::abs(tr.get_result()[12] - 2.8906) < 0.0001);
        assert(std::abs(tr.get_result()[19] - 2.8988) < 0.0001);
        assert(std::abs(tr.get_result()[20] - 2.8429) < 0.0001);
        assert(std::abs(tr.get_result()[24] - 2.5409) < 0.0001);
        assert(std::abs(tr.get_result()[25] - 2.5886) < 0.0001);
        assert(std::abs(tr.get_result()[1720] - 3.4109) < 0.0001);
        assert(std::abs(tr.get_result()[1712] - 3.6414) < 0.0001);
        assert(std::abs(tr.get_result()[1707] - 3.5273) < 0.0001);

        TrueRangeVisitor<double, std::string>   tr2 (true, 14, true);

        std::future<TrueRangeVisitor<double, std::string> &>    fut2 =
            df.single_act_visit_async<double, double, double>("IBM_Low", "IBM_High", "IBM_Close", tr2);

        fut2.get();

        assert(tr2.get_result().size() == 1721);
        assert(std::abs(tr2.get_result()[0] - 1.1858) < 0.0001);
        assert(std::abs(tr2.get_result()[12] - 1.534) < 0.0001);
        assert(std::abs(tr2.get_result()[19] - 1.6344) < 0.0001);
        assert(std::abs(tr2.get_result()[20] - 1.609) < 0.0001);
        assert(std::abs(tr2.get_result()[24] - 1.4547) < 0.0001);
        assert(std::abs(tr2.get_result()[25] - 1.4604) < 0.0001);
        assert(std::abs(tr2.get_result()[1720] - 3.0547) < 0.0001);
        assert(std::abs(tr2.get_result()[1712] - 3.1025) < 0.0001);
        assert(std::abs(tr2.get_result()[1707] - 2.8196) < 0.0001);
    }
    catch (const DataFrameError &ex)  {
        std::cout << ex.what() << std::endl;
    }
}
C++ DataFrame