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

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

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

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using vwap_v = VWAPVisitor<T, I, A>;
        
This functor class calculates VWAP – and more - between the two column values. The first column is the index (assumed to represent time). The second column is assumed to be trade price. The third column is assumed to be trade size The constructor takes:
  • The interval value for the bucket. VWAP is calculated for buckets of interval time. 0 means everything is in one bucket.
  • Max Volume: Excludes trades whose size is equal or greater than Max Volume. 0 means include everything.
  • Total Volume Limit: Stops calculations when the cumulative volume exceeds Total Volume Limit. 0 means there is no limit.
  • A function to calculates the difference between two index values. The default is a simple subtraction.
    explicit
    VWAPVisitor(double interval,
                double max_volume = 0,
                double total_volume_limit = 0,
                distance_func f =
                    [](const I &idx1, const I &idx2) -> double {
                        return (static_cast(idx2 - idx1));
                });
The result is a vector of following structs:
    struct  VWAP  {
        T            vwap;
        I            index_value;
        std::size_t  event_count;
        T            total_volume;
        T            high_price;
        T            low_price;
        T            cumulative_vwap;
        std::size_t  cumulative_event_count;
        T            cumulative_total_volume;
        T            cumulative_high_price;
        T            comulative_low_price;
    };
T: Column data type.
I: Index type.
A: Memory alignment boundary for vectors. Default is system default alignment
static void test_VWAP()  {

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

    RandGenParams<double>   price_p;

    price_p.mean = 1.0;
    price_p.std = 0.005;
    price_p.seed = 10;
    price_p.min_value = 500.0;
    price_p.max_value = 580.0;

    RandGenParams<double>   size_p = price_p;

    size_p.std = 1;
    size_p.min_value = 50.0;
    size_p.max_value = 2000.0;

    MyDataFrame df;

    df.load_data(
        MyDataFrame::gen_sequence_index(100, 1124, 1),
        std::make_pair("price", gen_uniform_real_dist<double>(1024, price_p)),
        std::make_pair("size", gen_uniform_real_dist<double>(1024, size_p)));

    VWAPVisitor<double> v1(100);
    auto                result = df.visit<double, double>("price", "size", v1).get_result();

    assert(result.size() == 11);
    assert(result[0].event_count == 100);
    assert(result[0].index_value == 100);
    assert(result[1].event_count == 100);
    assert(result[1].index_value == 200);
    assert(result[10].event_count == 24);
    assert(result[10].index_value == 1100);
    assert(fabs(result[0].vwap - 548.091) < 0.001);
    assert(fabs(result[0].average_price - 535.331) < 0.001);
    assert(fabs(result[0].cumulative_vwap - 548.091) < 0.001);
    assert(fabs(result[4].vwap - 551.923) < 0.001);
    assert(fabs(result[4].average_price - 537.798) < 0.001);
    assert(fabs(result[4].cumulative_vwap - 550.347) < 0.001);
    assert(fabs(result[10].vwap - 553.196) < 0.001);
    assert(fabs(result[10].average_price - 539.629) < 0.001);
    assert(fabs(result[10].cumulative_vwap - 552.067) < 0.001);
}
C++ DataFrame