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

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

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

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
using vwbas_v = VWBASVisitor<T, I, A>;
        
This functor class calculates VWBAS (Volume Weighted Bid-Ask Spread) – and more - between the four column values. The first column is the index (assumed to represent time). The second column is assumed to be bid price. The third column is assumed to be ask price. The fourth column is assumed to be bid size. The fifth column is assumed to be ask size.
The constructor takes:
  • The interval value for the bucket. VWBAS 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.
  • A function to calculates the difference between two index values. The default is a simple subtraction.
    VWBASVisitor(double interval,
                 double max_volume = 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  VWBAS  {
        T            spread;
        T            percent_spread; // with respect to bid side
        T            vwbas;
        T            percent_vwbas;  // with respect to bid side
        I            index_value;
        std::size_t  event_count;
        T            total_ask_volume;
        T            total_bid_volume;
        T            high_ask_price;
        T            low_ask_price;
        T            high_bid_price;
        T            low_bid_price;
        T            cumulative_vwbas;
        std::size_t  cumulative_event_count;
        T            cumulative_total_ask_volume;
        T            cumulative_total_bid_volume;
        T            cumulative_high_ask_price;
        T            comulative_low_ask_price;
        T            cumulative_high_bid_price;
        T            comulative_low_bid_price;
    };
T: Column data type.
I: Index type.
A: Memory alignment boundary for vectors. Default is system default alignment
static void test_VWBAS()  {

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

    RandGenParams<double>   bprice_p;

    bprice_p.mean = 1.0;
    bprice_p.std = 0.005;
    bprice_p.seed = 10;
    bprice_p.min_value = 100.0;
    bprice_p.max_value = 102.0;

    RandGenParams<double>   aprice_p = bprice_p;

    aprice_p.seed = 200;
    aprice_p.min_value = 102.0;
    aprice_p.max_value = 104.0;

    RandGenParams<double>   asize_p = bprice_p;

    asize_p.std = 1;
    asize_p.seed = 500;
    asize_p.min_value = 50.0;
    asize_p.max_value = 2000.0;

    RandGenParams<double>   bsize_p = asize_p;

    asize_p.std = 1;
    asize_p.seed = 123456;
    asize_p.min_value = 50.0;
    asize_p.max_value = 2000.0;
    MyDataFrame df;

    df.load_data(
        MyDataFrame::gen_sequence_index(100, 1124, 1),
        std::make_pair("bid_price", gen_uniform_real_dist<double>(1024, bprice_p)),
        std::make_pair("ask_price", gen_uniform_real_dist<double>(1024, aprice_p)),
        std::make_pair("bid_size", gen_uniform_real_dist<double>(1024, bsize_p)),
        std::make_pair("ask_size", gen_uniform_real_dist<double>(1024, asize_p)));

    VWBASVisitor<double>    v1(100);
    const MyDataFrame       const_df = df;
    auto                    fut =
        const_df.visit_async<double, double, double, double>("bid_price", "ask_price", "bid_size", "ask_size", v1);
    auto                    result = fut.get().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].cumulative_event_count == 200);
    assert(result[1].index_value == 200);
    assert(result[10].event_count == 24);
    assert(result[10].index_value == 1100);

    assert(fabs(result[0].spread - 2.11835) < 0.00001);
    assert(fabs(result[0].percent_spread - 2.0998) < 0.0001);
    assert(fabs(result[0].vwbas - 2.15156) < 0.00001);
    assert(fabs(result[0].percent_vwbas - 2.13298) < 0.00001);
    assert(fabs(result[0].high_bid_price - 101.966) < 0.001);
    assert(fabs(result[0].low_ask_price - 102.012) < 0.001);
    assert(fabs(result[0].cumulative_vwbas - 2.15156) < 0.00001);

    assert(fabs(result[5].spread - 1.92471) < 0.00001);
    assert(fabs(result[5].percent_spread - 1.90509) < 0.0001);
    assert(fabs(result[5].vwbas - 1.9199) < 0.0001);
    assert(fabs(result[5].percent_vwbas - 1.90052) < 0.00001);
    assert(fabs(result[5].high_bid_price - 101.987) < 0.001);
    assert(fabs(result[5].low_ask_price - 102.04) < 0.01);
    assert(fabs(result[5].cumulative_vwbas - 2.07029) < 0.00001);

    assert(fabs(result[10].spread - 1.98223) < 0.00001);
    assert(fabs(result[10].percent_spread - 1.96279) < 0.0001);
    assert(fabs(result[10].vwbas - 2.05129) < 0.0001);
    assert(fabs(result[10].percent_vwbas - 2.03336) < 0.00001);
    assert(fabs(result[10].high_bid_price - 101.997) < 0.001);
    assert(fabs(result[10].low_ask_price - 102.12) < 0.01);
    assert(fabs(result[10].cumulative_vwbas - 2.02198) < 0.00001);
}
C++ DataFrame