← Back to Documentations

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

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
struct YangZhangVolVisitor;

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

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using yz_vol_v = YangZhangVolVisitor<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 Yang Zhang volatility. It requires 4 input columns in the order of low, high, open, close.
The result is a vector of values with same number of items as the given columns. The first roll_count items, in the result, will be NAN. The values are annulaized by trading_periods

NOTE: Roll count must be greater than 1

Yang Zhang is a historical volatility estimator that handles both opening jumps and the drift and has a minimum estimation error.
We can think of the Yang-Zhang volatility as the combination of the overnight (close-to-open volatility) and a weighted average of the Rogers-Satchell volatility and the day's open-to-close volatility. It is considered to be 14 times more efficient than the close-to-close estimator.
    explicit
    YangZhangVolVisitor(std::size_t roll_count = 30,
                        std::size_t trading_periods = 252);
        
T: Column data type
I: Index type
A: Memory alignment boundary for vectors. Default is system default alignment
static void test_YangZhangVolVisitor()  {

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

    MyDataFrame df;

    try  {
        std::ifstream   stream;

        stream.open("FORD.csv");
        df.read(stream, io_format::csv2);

        YangZhangVolVisitor<double, unsigned long, 64> yz_v;

        df.single_act_visit<double, double, double, double>("FORD_Low", "FORD_High", "FORD_Open", "FORD_Close", yz_v);

        assert(yz_v.get_result().size() == 12265);
        std::cout << std::endl;
        assert(std::isnan(yz_v.get_result()[0]));
        assert(std::isnan(yz_v.get_result()[29]));
        assert(std::isnan(yz_v.get_result()[30]));
        assert(std::abs(yz_v.get_result()[31] - 0.170786) < 0.000001);
        assert(std::abs(yz_v.get_result()[36] - 0.182502) < 0.000001);
        assert(std::abs(yz_v.get_result()[12264] - 0.278925) < 0.000001);
        assert(std::abs(yz_v.get_result()[12261] - 0.277669) < 0.000001);
        assert(std::abs(yz_v.get_result()[12256] - 0.291269) < 0.000001);
    }
    catch (const DataFrameError &ex)  {
        std::cout << ex.what() << std::endl;
        ::exit(-1);
    }
}

C++ DataFrame