Files
spark/src/lib/md/test/synthetic_book_merge_tests.cpp
T

469 lines
20 KiB
C++

#ifndef _WIN32
#include "catch/catch.hpp"
#include "../MDConsumerHandle.h"
#include "Co2TestInstrument.h"
#include "lib/utils/sane_algorithm.h"
#include <memory>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <tuple>
#include <ostream>
#define ENABLE_BENCHMARK 0
#if ENABLE_BENCHMARK
#include <benchmark/benchmark.h>
#endif
using namespace std;
namespace bts {
int synthetic_book_merge_tests = 0;
namespace md {
namespace test {
// std::tuple is hard to read in the debugger
struct PS {
double price;
Size size;
bool operator == (const PS& other) const {
// Convert to IntPrice to avoid double rounding issues
auto p1 = IntPrice_from_double<11>(price);
auto p2 = IntPrice_from_double<11>(other.price);
return p1 == p2 && size == other.size;
}
};
ostream& operator<< (ostream& os, const PS& val) {
os << "{ " << val.price << "," << val.size << " }";
return os;
}
void setup_market(MDProducerHandle producer, const vector<PS>& bids, const vector<PS>& asks) {
auto ts = TimeStamp::now();
std::array<PriceSize,COCONUT_MAX_DEPTH> bids_a = { }, asks_a = { };
std::transform(bids.begin(), bids.end(), bids_a.begin(), [](const PS& ps) { return PriceSize_from_double<7>(ps.price, ps.size); });
std::transform(asks.begin(), asks.end(), asks_a.begin(), [](const PS& ps) { return PriceSize_from_double<7>(ps.price, ps.size); });
producer.update_direct(ts, bids_a, asks_a);
}
void dump_market(const MDConsumerHandle& handle) {
array<PriceSize,COCONUT_MAX_DEPTH> raw_bids,raw_asks;
TimeStamp t;
handle.get_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data());
vector<PS> src_bids, src_asks;
sane::map(raw_bids, src_bids, [](const PriceSize& ps) -> PS {return {ps.price(), ps.size()};});
sane::map(raw_asks, src_asks, [](const PriceSize& ps) -> PS {return {ps.price(), ps.size()}; });
std::cout << std::endl << "bid: ";
for (const auto& bid : src_bids) {
if (bid.size != 0) std::cout << bid;
}
std::cout << std::endl << "ask: ";
for (const auto& ask : src_asks) {
if (ask.size != 0) std::cout << ask;
}
std::cout << std::endl;
}
void expect_market(const MDConsumerHandle& handle, const vector<PS> bids, const vector<PS>& asks) {
array<PriceSize,COCONUT_MAX_DEPTH> raw_bids,raw_asks;
TimeStamp t;
handle.get_direct_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data());
vector<PS> src_bids, src_asks;
sane::map(raw_bids, src_bids, [](const PriceSize& ps) -> PS {return {ps.price(), ps.size()};});
sane::map(raw_asks, src_asks, [](const PriceSize& ps) -> PS {return {ps.price(), ps.size()}; });
for (size_t x =0; x < bids.size(); ++x) {
REQUIRE(src_bids[x] == bids[x]);
}
for (size_t x = bids.size(); x < src_bids.size(); ++x) {
REQUIRE(src_bids[x].size == 0);
REQUIRE(os::is_nan(src_bids[x].price));
}
for (size_t x = 0; x < asks.size(); ++x) {
REQUIRE(src_asks[x] == asks[x]);
}
for (size_t x = asks.size(); x < src_asks.size(); ++x) {
REQUIRE(src_asks[x].size == 0);
REQUIRE(os::is_nan(src_asks[x].price));
}
}
// Crate some securities with given side
// (quantity, multiplier, tick_size, side)
tuple<shared_ptr<SyntheticStrategy>,vector<Co2TestInstrument>> make_securities(const vector<tuple<double,double,double,api::Side>>& leg_descr) {
vector<Co2TestInstrument> legs;
auto synthetic = make_shared<SyntheticStrategy>();
uint32_t security_key = 100;
for (const auto& ldesc : leg_descr) {
double quantity, mult, tick_size;
api::Side side;
std::tie(quantity, mult, tick_size, side) = ldesc;
Co2TestInstrument instr("ES", api::SECURITYTYPE_FUTURE);
instr.producer().set_key(security_key);
++security_key;
auto& s = synthetic->security;
s.mutable_contract_spec()->set_low_tick(tick_size);
s.mutable_contract_spec()->set_high_tick(tick_size);
auto* ps = s.mutable_synthetic();
auto l = ps->mutable_legs()->Add();
l->mutable_security()->CopyFrom(instr.consumer().get_normalized_security());
l->set_quantity(quantity);
l->set_price_multiplier(quantity * (side == api::SIDE_BUY ? 1.0 : -1));
l->set_side(side);
synthetic->legs.push_back(instr.consumer());
legs.push_back(instr);
}
return { synthetic, legs };
}
// tuple = (quantity, tick_size, side)
tuple<shared_ptr<SyntheticStrategy>,vector<Co2TestInstrument>> make_securities(const vector<tuple<double, double,api::Side>>& leg_descr) {
std::vector<tuple<double,double,double,api::Side>> legs;
for (const auto& t : leg_descr) {
double quantity, tick_size;
api::Side side;
std::tie(quantity, tick_size, side) = t;
legs.push_back({ quantity, quantity, tick_size, side });
}
return make_securities(legs);
}
void set_leg_params(SyntheticStrategy& strat, uint32_t security_key, const spark::LegSpreaderParams& params) {
auto* map = strat.strategy.mutable_leg_params_map();
map->insert({ security_key, params });
}
TEST_CASE("SyntheticBookMerge", "[md][synthetic]") {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;
tie(synthetic, test_instr) = make_securities({ {1.0, 0.25, api::SIDE_BUY}, {1.0, 0.25, api::SIDE_SELL }});
auto es_buy1 = test_instr[0];
auto es_sell1 = test_instr[1];
MDConsumerHandle synthetic_handle{synthetic};
// Simplest case
SECTION("Simple") {
setup_market(es_buy1.producer(), {{2000.0, 10}, {1999.75, 5}, {1999.5,7}}, {{2000.25, 15}, {2000.5, 5}, {2001.0,10}});
setup_market(es_sell1.producer(), {{2003.0, 5}, {2002.75, 15}, {2002.5,12}}, {{2003.25, 12}, {2003.5, 3}, {2003.75,5}});
// Sanity test
expect_market(es_buy1.consumer(), {{2000.0, 10}, {1999.75, 5}, {1999.5,7}}, {{2000.25, 15}, {2000.5, 5}, {2001.0,10}});
expect_market(es_sell1.consumer(), {{2003.0, 5}, {2002.75, 15}, {2002.5,12}}, {{2003.25, 12}, {2003.5, 3}, {2003.75,5}});
// Real test
expect_market(synthetic_handle, {{-3.25,10}, {-3.5, 2}, {-3.75,3}, {-4.25,5}}, {{-2.75,5},{-2.5,10},{-2.25,5},{-1.5,10}});
}
SECTION("Full Depth") {
// make sure we don't off-by-1 the end of the array
setup_market(es_buy1.producer(), {{90,10}, {89, 5}, {88,5}, {87,5}, {86,5}, {85,5}, {84,5}, {83,5}, {82,5}, {81,10}},
{{100,10}, {101, 5}, {102,5}, {103,5}, {104,5}, {105,5}, {106,5}, {107,5}, {108,5}, {109,10}});
setup_market(es_sell1.producer(),{{90,10}, {89, 5}, {88,5}, {87,5}, {86,5}, {85,5}, {84,5}, {83,5}, {82,5}, {81,10}},
{{100,10}, {101, 5}, {102,5}, {103,5}, {104,5}, {105,5}, {106,5}, {107,5}, {108,5}, {109,10}});
expect_market(synthetic_handle, {{-10,10}, {-12, 5}, {-14,5}, {-16,5}, {-18,5}, {-20,5}, {-22,5}, {-24,5}, {-26,5}, {-28,10}},
{{10,10}, {12, 5}, {14,5}, {16,5}, {18,5}, {20,5}, {22,5}, {24,5}, {26,5}, {28,10}});
}
// test empty markets
SECTION("Empty market") {
setup_market(es_buy1.producer(), {}, {{2000.25, 15}, {2000.5, 5}, {2001.0,10}});
setup_market(es_sell1.producer(), {}, {{2003.25, 12}, {2003.5, 3}, {2003.75,5}});
expect_market(synthetic_handle, {}, {});
}
}
TEST_CASE("SyntheticBook+1-2", "[md][synthetic]") {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;
tie(synthetic, test_instr) = make_securities({ {1.0, 0.01, api::SIDE_BUY}, {2.0, 0.01, api::SIDE_SELL }});
auto es_buy1 = test_instr[0];
auto es_sell2 = test_instr[1];
MDConsumerHandle synthetic_handle{synthetic};
// Test available size=fraction of leg quantity
SECTION("buy 1 sell 2") {
setup_market(es_buy1.producer(),
{{190,1}, {180,11}, {170,4}, {160,10}},
{{200,2}, {210,6}, {220,8}, {230,12}});
setup_market(es_sell2.producer(),
{{90,5}, {80,9}, {70,11}, {60,10}, {50,1}},
{{100,16}, {110,8}, {120,12}, {130,5}});
expect_market(synthetic_handle,
{{-10,1}, {-20,7}, {-40,4}, {-70,4}, {-80,2}, {-100,2}},
{{20,2}, {50,5}, {70,1}, {80,4}, {100,4}, {110,1}, {130,1}});
//precise {{20,2}, {40,1}, {50,4}, {70,1}, {80,4}, {90,1}, {100,3}, {110,1}, {120,1}});
}
}
TEST_CASE("SyntheticBook+2-2", "[md][synthetic]") {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;
tie(synthetic, test_instr) = make_securities({ {2.0, 0.25, api::SIDE_BUY}, {2.0, 0.25, api::SIDE_SELL }});
auto es_buy2 = test_instr[0];
auto es_sell2 = test_instr[1];
MDConsumerHandle synthetic_handle{synthetic};
setup_market(es_buy2.producer(),
{{2000.0, 10}, {1999.75, 5}, {1999.5,7}},
{{2000.25, 15}, {2000.5, 5}, {2001.0,10}});
setup_market(es_sell2.producer(),
{{2003.0, 5}, {2002.75, 15}, {2002.5,12}},
{{2003.25, 12}, {2003.5, 3}, {2003.75,5}});
expect_market(synthetic_handle,
{{-6.5,5}, {-7.0,1}, {-7.5,1}, {-8.5,3}},
//precise {{-6.5,5}, {-7.0,1}, {-7.5,1}, {-8.0,1}, {-8.5,2}},
{{-5.50,2}, {-5.00,5}, {-4.5,3}, {-3.00,5}});
//precise {{-5.50,2}, {-5.25,1}, {-5.00,4}, {-4.75,1}, {-4.5,2}, {-3.00,5}});
}
TEST_CASE("Synthetic3Legs", "[md][synthetic]") {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;
tie(synthetic, test_instr) = make_securities({ {1.0, 0.1, api::SIDE_BUY}, {1.0, 0.1, api::SIDE_SELL }, { 1.0, 0.1, api::SIDE_BUY}});
auto leg1_buy = test_instr[0];
auto leg2_sell = test_instr[1];
auto leg3_buy = test_instr[2];
MDConsumerHandle synthetic_handle(synthetic);
SECTION("Simple") {
setup_market(leg1_buy.producer(),
{{1000,2},{1001,11},{1002,4}},
{{999,8}, {998,15}, {997,3}});
setup_market(leg2_sell.producer(),
{{2000,9}, {2010,5}, {2020,4} },
{{1990,3}, {1980,7}, {1970,13} });
setup_market(leg3_buy.producer(),
{{200,2}, {201,3}, {202,16} },
{{199,10}, {198,11}, {197,10} });
expect_market(synthetic_handle,
{{-790,2}, {-788,1}, {-778,2}, {-777,5}, {-767,3}, {-766,4}},
{{-802,8}, {-803,1}, {-813,1}, {-814,4}, {-824,4}});
}
}
TEST_CASE("AllBlank", "[md][synthetic]") {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;
tie(synthetic, test_instr) = make_securities({ {1.0, 0.1, api::SIDE_BUY}, {1.0, 0.1, api::SIDE_SELL }, { 1.0, 0.1, api::SIDE_BUY}});
auto leg1 = test_instr[0];
auto leg2 = test_instr[1];
MDConsumerHandle synthetic_handle(synthetic);
setup_market(leg1.producer(),
{},
{});
setup_market(leg2.producer(),
{},
{});
expect_market(synthetic_handle,
{},
{});
}
TEST_CASE("SPARK-955", "[md][synthetic]") {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;
tie(synthetic, test_instr) = make_securities({ {1.0, 0.25, api::SIDE_BUY}, {2.0, 0.25, api::SIDE_SELL }, { 1.0, 0.25, api::SIDE_BUY}});
auto esm8 = test_instr[0];
auto esu8 = test_instr[1];
auto esz8 = test_instr[2];
MDConsumerHandle synthetic_handle(synthetic);
setup_market(esm8.producer(),
{{2594.50, 47}, {2594.25,63}, {2594.00,117}, {2593.75,137}},
{{2594.75, 52}, {2595.00,138}, {2594.25,119}, {2595.50,128}});
setup_market(esu8.producer(),
{{2598.50, 14}, {2598.25, 15}, {2598.00,16}, {2597.75, 15}},
{{2599.75, 1}, {2600.00, 16}, {2600.25,15}, {2600.50, 75}});
setup_market(esz8.producer(),
{{2601.25, 2}, {2600.50, 3}, {2600.25, 10}},
{{2607.25, 3}, {2607.50, 1}, {2608.25, 9}});
expect_market(synthetic_handle,
// If we do strict price this is right. If we're pessimistic we get {-4.25,2}
{ {-4.25, 2}, { -5.00, 3}, {-5.25, 3}, {-5.75, 7} },
//precise { {-4.00, 1}, {-4.25, 1}, { -5.00, 3}, {-5.25, 3}, {-5.50,1}, {-5.75, 6} },
{ {5.00, 3}, {5.25, 1}, {6.00,3}, {6.50,6} });
}
TEST_CASE("SPARK-954", "[md][synthetic]") {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;
SECTION("price multiplier = 0.9,1.1") {
tie(synthetic, test_instr) = make_securities({ {0.9, 0.25, api::SIDE_BUY}, {1.1, 0.25, api::SIDE_SELL } });
auto leg1 = test_instr[0];
auto leg2 = test_instr[1];
MDConsumerHandle synthetic_handle(synthetic);
setup_market(leg1.producer(),
{ {2796.75,22}, {2796.50,38}, {2796.25,57}, {2796.00,59}, {2795.75,60}, {2795.50,67}, {2795.25,68}, {2795.00,84}, {2794.75,115}, {2794.50,98}, },
{ {2797.00,158}, {2797.25,110}, {2797.50,83}, {2797.75,168}, {2798.00,130}, {2798.25,180}, {2798.50,75}, {2798.75,89}, {2799.00,123}, {2799.25,193}, });
setup_market(leg2.producer(),
{ {2800.50,1}, {2800.25,6}, {2800.00,8}, {2799.75,14}, {2799.50,15}, {2799.25,14}, {2799.00,44}, {2798.75,13}, {2798.50,18}, {2798.25,14}, },
{ {2801.00,1}, {2801.25,4}, {2801.50,16}, {2801.75,15}, {2802.00,16}, {2802.25,15}, {2802.50,45}, {2802.75,13}, {2803.00,14}, {2803.25,14}, });
expect_market(synthetic_handle,
{ {-564.50,4}, {-564.75,15}, {-565.00,5}, {-565.25,8}, {-565.50,15}, {-565.75,13}, {-566.00,6}, {-566.25,35}, {-566.50,12}, {-566.75,13}, },
{ {-562.75,6}, {-562.50,7}, {-562.25,13}, {-562.00,13}, {-561.75,13}, {-561.50,40}, {-561.25,12}, {-561.00,16}, {-560.75,13}, });
}
SECTION("price multiplier = 1.0,-1.3") {
tie(synthetic, test_instr) = make_securities({ {1.0, 0.005, api::SIDE_BUY}, {1.3, 0.005, api::SIDE_SELL } });
auto leg1 = test_instr[0];
auto leg2 = test_instr[1];
MDConsumerHandle synthetic_handle(synthetic);
setup_market(leg1.producer(),
{ { 14.185, 1 }, { 14.180, 7 } },
{ { 14.205, 1 }, { 14.210, 5 } });
setup_market(leg2.producer(),
{ { 14.255, 10 }, { 14.250, 42 }, { 14.230, 1 }, { 14.220, 1 }, { 14.215, 1 }, { 14.210, 3 }, { 14.205, 1 }, { 14.200, 2 } },
{ { 14.265, 18 }, { 14.270, 19 } });
expect_market(synthetic_handle,
{ { -4.360, 1 }, { -4.365, 7 } },
{ { -4.325, 1 }, { -4.320, 5 } });
}
}
TEST_CASE("AggregationAcrossLevels", "[md][synthetic]") {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;
SECTION("buy 1 sell 3") {
tie(synthetic, test_instr) = make_securities({ {1.0, 0.005, api::SIDE_BUY}, {3, 0.005, api::SIDE_SELL } });
auto leg1 = test_instr[0];
auto leg2 = test_instr[1];
MDConsumerHandle synthetic_handle(synthetic);
setup_market(leg1.producer(),
{ {16.215, 6} },
{ {16.220, 5} }
);
setup_market(leg2.producer(),
{ {16.500, 2 }, { 16.495, 2 } },
{ {16.505, 1 }, { 16.510, 1 }, { 16.515, 1 }, {16.520,1}, {16.525,1}, {16.530,1}}
);
expect_market(synthetic_handle,
{ {-33.330,1}, {-33.375, 1} },
{ { -33.265,1} }
);
}
SECTION("buy 1 sell 4") {
tie(synthetic, test_instr) = make_securities({ {1.0, 0.005, api::SIDE_BUY}, {4, 0.005, api::SIDE_SELL } });
auto leg1 = test_instr[0];
auto leg2 = test_instr[1];
MDConsumerHandle synthetic_handle(synthetic);
setup_market(leg1.producer(),
{ {16.215, 6} },
{ {16.220, 5} }
);
setup_market(leg2.producer(),
{ {16.500, 2 }, { 16.495, 2 } },
{ {16.505, 1 }, { 16.510, 1 }, { 16.515, 1 }, {16.520,1}, {16.525,1}, {16.530,1}, {16.535,2}}
);
expect_market(synthetic_handle,
{ { -49.865,1}, {-49.925,1} },
{ { -49.760,1} }
);
}
}
TEST_CASE("Synthetic_net_change", "[md][synthetic]") {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;
tie(synthetic, test_instr) = make_securities({ {1.0, 0.01, api::SIDE_BUY}, {2.0, 0.01, api::SIDE_SELL }});
auto es_buy1 = test_instr[0];
auto es_sell2 = test_instr[1];
MDConsumerHandle synthetic_handle{synthetic};
// Set legs to use net change
spark::LegSpreaderParams lparam;
lparam.set_leg_md_calc(spark::LegSpreaderParams::NET_CHANGE);
set_leg_params(*synthetic, es_buy1.producer().key(), lparam);
set_leg_params(*synthetic, es_sell2.producer().key(), lparam);
// set settlement prices
es_buy1.producer().update_settlement(TimeStamp::now(), { 100, 0 }, datetime::today());
es_sell2.producer().update_settlement(TimeStamp::now(), { 50, 0 }, datetime::today());
SECTION("buy 1 sell 2 Net") {
setup_market(es_buy1.producer(),
{{190,1}, {180,11}, {170,4}, {160,10}},
{{200,2}, {210,6}, {220,8}, {230,12}});
setup_market(es_sell2.producer(),
{{90,5}, {80,9}, {70,11}, {60,10}, {50,1}},
{{100,16}, {110,8}, {120,12}, {130,5}});
// Validate merge
expect_market(synthetic_handle,
{{-10,1}, {-20,7}, {-40,4}, {-70,4}, {-80,2}, {-100,2}},
{{20,2}, {50,5}, {70,1}, {80,4}, {100,4}, {110,1}, {130,1}});
// precise {{20,2}, {40,1}, {50,4}, {70,1}, {80,4}, {90,1}, {100,3}, {110,1}, {120,1}});
}
}
//
// Benchmarks
//
#if ENABLE_BENCHMARK
void synthetic_get_book_speed2(benchmark::State& state) {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;
tie(synthetic, test_instr) = make_securities({ {1.0, 0.25, api::SIDE_BUY}, {2.0, 0.25, api::SIDE_SELL } });
auto esm8 = test_instr[0];
auto esu8 = test_instr[1];
MDConsumerHandle synthetic_handle(synthetic);
setup_market(esm8.producer(),
{{2594.50, 47}, {2594.25,63}, {2594.00,117}, {2593.75,137}},
{{2594.75, 52}, {2595.00,138}, {2594.25,119}, {2595.50,128}});
setup_market(esu8.producer(),
{{2598.50, 14}, {2598.25, 15}, {2598.00,16}, {2597.75, 15}},
{{2599.75, 1}, {2600.00, 16}, {2600.25,15}, {2600.50, 75}});
for (auto _ : state) {
array<PriceSize,COCONUT_MAX_DEPTH> raw_bids,raw_asks;
TimeStamp t;
synthetic_handle.get_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data());
}
}
BENCHMARK(synthetic_get_book_speed2);
void synthetic_get_book_speed3(benchmark::State& state) {
shared_ptr<SyntheticStrategy> synthetic;
vector<Co2TestInstrument> test_instr;
tie(synthetic, test_instr) = make_securities({ {1.0, 0.25, api::SIDE_BUY}, {2.0, 0.25, api::SIDE_SELL }, { 1.0, 0.25, api::SIDE_BUY}});
auto esm8 = test_instr[0];
auto esu8 = test_instr[1];
auto esz8 = test_instr[2];
MDConsumerHandle synthetic_handle(synthetic);
setup_market(esm8.producer(),
{{2594.50, 47}, {2594.25,63}, {2594.00,117}, {2593.75,137}},
{{2594.75, 52}, {2595.00,138}, {2594.25,119}, {2595.50,128}});
setup_market(esu8.producer(),
{{2598.50, 14}, {2598.25, 15}, {2598.00,16}, {2597.75, 15}},
{{2599.75, 1}, {2600.00, 16}, {2600.25,15}, {2600.50, 75}});
setup_market(esz8.producer(),
{{2601.25, 20}, {2600.50, 3}, {2600.25, 100}},
{{2607.25, 3}, {2607.50, 1}, {2608.25, 900}});
for (auto _ : state) {
array<PriceSize,COCONUT_MAX_DEPTH> raw_bids,raw_asks;
TimeStamp t;
synthetic_handle.get_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data());
}
}
BENCHMARK(synthetic_get_book_speed3);
#endif
}}}//namespace
#endif