diff --git a/src/lib/md/test/md_test_utils.h b/src/lib/md/test/md_test_utils.h new file mode 100644 index 000000000..23a82070f --- /dev/null +++ b/src/lib/md/test/md_test_utils.h @@ -0,0 +1,32 @@ +#pragma once + +#include "lib/md/IntPrice.h" +#include "lib/md/PriceSize.h" +#include + +namespace bts { +namespace md { +namespace test { + +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; + } +}; + +struct Book { + std::vector bids; + std::vector asks; +}; + +Book compute_synthetic_book(const api::Security& security, const std::vector& leg_books); + +std::ostream& operator<< (std::ostream& os, const PS& val); + + +}}}//namespace \ No newline at end of file diff --git a/src/lib/md/test/synthetic_book_merge_tests.cpp b/src/lib/md/test/synthetic_book_merge_tests.cpp index a6d23b534..25d97cfab 100644 --- a/src/lib/md/test/synthetic_book_merge_tests.cpp +++ b/src/lib/md/test/synthetic_book_merge_tests.cpp @@ -1,6 +1,7 @@ #ifndef _WIN32 #include "catch/catch.hpp" #include "../MDConsumerHandle.h" +#include "md_test_utils.h" #include "Co2TestInstrument.h" #include "lib/utils/sane_algorithm.h" #include @@ -23,16 +24,6 @@ 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 << " }"; @@ -149,6 +140,56 @@ void set_leg_params(SyntheticStrategy& strat, uint32_t security_key, const spark map->insert({ security_key, params }); } + +Book compute_synthetic_book(const api::Security& security, const std::vector& leg_books) { + auto synthetic = std::make_shared(); + synthetic->security.CopyFrom(security); + vector leg_instruments; + for (const auto& leg : synthetic->security.synthetic().legs()) { + const auto& security = leg.security(); + Co2TestInstrument instr(security.symbol(), securitytype_of(security)); + convert_security(security, instr.cold().security, true); + leg_instruments.push_back(instr); + synthetic->legs.push_back(instr.consumer()); + } + + // Write leg books + for (size_t x = 0; x < leg_books.size(); ++x) { + std::array bids = {}, asks = {}; + std::transform( + leg_books[x].bids.begin(), + leg_books[x].bids.end(), + bids.begin(), + [](const PS& ps) { + return PriceSize_from_double<7>(ps.price, ps.size); + }); + std::transform( + leg_books[x].asks.begin(), + leg_books[x].asks.end(), + asks.begin(), + [](const PS& ps) { + return PriceSize_from_double<7>(ps.price, ps.size); + }); + auto ts = TimeStamp::now(); + leg_instruments[x].producer().update_direct(ts, bids, asks); + } + + // Read synthetic book back + MDConsumerHandle synthetic_handle { synthetic }; + std::array raw_bids, raw_asks; + TimeStamp t; + synthetic_handle.get_direct_book(t, COCONUT_MAX_DEPTH, raw_bids.data(), raw_asks.data()); + Book result {}; + sane::map(raw_bids, result.bids, [](PriceSize ps) -> PS { return {ps.price(), ps.size()}; }); + sane::map(raw_asks, result.asks, [](PriceSize ps) -> PS { return {ps.price(), ps.size()}; }); + + return result; +} + +/////////////////////// +// Test Cases +/////////////////////// + TEST_CASE("SyntheticBookMerge", "[md][synthetic]") { shared_ptr synthetic; vector test_instr;