diff --git a/src/server/sparkd/cmds/Spartan.cpp b/src/server/sparkd/cmds/Spartan.cpp index 642154640..575ad0a08 100644 --- a/src/server/sparkd/cmds/Spartan.cpp +++ b/src/server/sparkd/cmds/Spartan.cpp @@ -219,7 +219,7 @@ int polygon_books(const std::string& pcap, const std::string& db, const std::str uint32_t i = 0; bts_assert(bk.size() <= 10); for (; i < bk.size(); i++) - out << "," << std::get<0>(bk[i]) << "," << std::get<1>(bk[i]); + out << "," << bk[i].price << "," << bk[i].quantity.value(); while (i++ < MDP3_MAX_BOOK_DEPTH) out << ",NaN,0"; return out; diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoImpliedSecurityMatcher.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoImpliedSecurityMatcher.cpp index 8dce78430..a1b34bb27 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoImpliedSecurityMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoImpliedSecurityMatcher.cpp @@ -90,8 +90,8 @@ void FifoImpliedSecurityMatcher::print_legs(std::ostream& out) const { append_bbo = false; auto bid = top_of_book(api::Side::SIDE_BUY); auto ask = top_of_book(api::Side::SIDE_SELL); - out << std::setw(8) << FMT("(", std::get<1>(bid), ") ") << std::setw(6) << std::get<0>(bid) << std::setw(6) << std::get<0>(ask) << - FMT("(", std::get<1>(ask), ")"); + out << std::setw(8) << FMT("(", bid.quantity, ") ") << std::setw(6) << bid.price << std::setw(6) << ask.price << + FMT("(", ask.quantity, ")"); } out << std::endl; } @@ -104,28 +104,28 @@ ImpliedBook FifoImpliedSecurityMatcher::book(api::Side side) const { // this isn't the best way to do this (copying), // but the ImpliedBook only has 2 levels in practice, // so it should be fine. - std::vector<_BookLevel> tuple_ib; + SmallBook tuple_ib; ImpliedBook ib; _book(side, tuple_ib, ib.size()); for (uint32_t i = 0; i < tuple_ib.size() && i < ib.size(); i++) { - ib[i].price = std::get<0>(tuple_ib[i]); - ib[i].quantity = std::get<1>(tuple_ib[i]); + ib[i].price = tuple_ib[i].price; + ib[i].quantity = tuple_ib[i].quantity; } return ib; } -_BookLevel FifoImpliedSecurityMatcher::top_of_book(api::Side side) const { - std::vector<_BookLevel> b; +SmallBookLevel FifoImpliedSecurityMatcher::top_of_book(api::Side side) const { + SmallBook b; _book(side, b, 1); if (b.empty()) return {md::IntPrice(), MDP3Quantity(0)}; return b[0]; } -void FifoImpliedSecurityMatcher::_book(api::Side side, std::vector<_BookLevel>& dest, uint32_t depth) const { - std::vector>> im_vec; +void FifoImpliedSecurityMatcher::_book(api::Side side, SmallBook& dest, uint32_t depth) const { + std::vector> im_vec; for (const auto& p : _me->implied_mkt) { - std::vector<_BookLevel> bk; + SmallBook bk; auto [sec_matcher, leg_qty] = p.second; bts_assert(leg_qty.value() != 0); auto leg_side = leg_qty.value() > 0 ? securities::other_side(side) : side; @@ -142,13 +142,13 @@ void FifoImpliedSecurityMatcher::add_leg(MDP3SecurityID sec_id, FifoSecurityMatc } MDP3Quantity FifoImpliedSecurityMatcher::fill(MDP3OrderKey key, api::Side side, MDP3Quantity qty, std::vector* effects, FillDepot& fills) { - std::vector> b; + SmallBook b; auto oside = securities::other_side(side); _book(oside, b, MDP3_MAX_IMPLIED_BOOK_DEPTH); auto orig_qty = qty; // can't fill more than the volume in the book qty = std::min(book_volume(b), qty); - md::IntPrice impl_fill_price = std::get<0>(top_of_book(oside)); + md::IntPrice impl_fill_price = top_of_book(oside).price; bool has_added_aggr_fills = false; int fill_qty = 0; bool local_fills = effects == nullptr; diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoImpliedSecurityMatcher.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoImpliedSecurityMatcher.h index bd0a0f30e..ff5113d9c 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoImpliedSecurityMatcher.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoImpliedSecurityMatcher.h @@ -32,7 +32,7 @@ struct FifoImpliedSecurityMatcher { void add_leg(MDP3SecurityID sec_id, FifoSecurityMatcher* m, MDP3Quantity unit_qty, std::string leg_sym); MDP3SecurityID security_id() const; std::string symbol() const; - std::tuple top_of_book(api::Side side) const; + SmallBookLevel top_of_book(api::Side side) const; void print_legs(std::ostream& out = std::cout) const; private: @@ -42,7 +42,7 @@ struct FifoImpliedSecurityMatcher { FifoImpliedSecurityMatcher(const FifoImpliedSecurityMatcher&); std::string _log_header() const; - void _book(api::Side side, std::vector>& dest, uint32_t depth) const; + void _book(api::Side side, SmallBook& dest, uint32_t depth) const; uint32_t _fill_order(const MDP3Order& order); }; } // namespace bts::spartan::mdp3 \ No newline at end of file diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoMergedSecurityMatcher.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoMergedSecurityMatcher.cpp index eb9d5d127..21440d04c 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoMergedSecurityMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoMergedSecurityMatcher.cpp @@ -89,12 +89,11 @@ FifoMergedSecurityMatcher::_best_implied_matcher(api::Side side) { bts::orderman::SidePriceComp cmp(side); FifoImpliedSecurityMatcher* rv = nullptr; md::IntPrice best_price; - MDP3Quantity best_qty(0); for (auto& im : _me->implied_matchers) { - auto [price, qty] = im->top_of_book(side); - if (qty.value() > 0 && cmp(price, best_price)) { + auto itop = im->top_of_book(side); + if (itop.quantity.value() > 0 && cmp(itop.price, best_price)) { rv = im; - best_price = price; + best_price = itop.price; } } return rv; @@ -280,6 +279,10 @@ void FifoMergedSecurityMatcher::_fill(MDP3Order& order, std::vector set_total_qty(order, rem, reset_show_qty); } +inline SmallBookLevel tuple_to_small_book_level(std::tuple t) { + return SmallBookLevel(std::get<0>(t), std::get<1>(t)); +} + MDP3Quantity FifoMergedSecurityMatcher::_fill( MDP3OrderKey key, api::Side side, @@ -296,19 +299,19 @@ MDP3Quantity FifoMergedSecurityMatcher::_fill( // another may have the best 2nd price level. auto* imatcher = _best_implied_matcher(oside); auto itop = imatcher == nullptr ? - std::make_tuple(bts::md::IntPrice(), MDP3Quantity(0)) : + SmallBookLevel() : imatcher->top_of_book(oside); - auto dtop = matcher()->book_level(oside, 0); - bool is_dcross = !get_price(dtop).is_nan() && !oside_comp(price, get_price(dtop)); - bool is_icross = !get_price(itop).is_nan() && !oside_comp(price, get_price(itop)); + auto dtop = tuple_to_small_book_level(matcher()->book_level(oside, 0)); + bool is_dcross = !dtop.price.is_nan() && !oside_comp(price, dtop.price); + bool is_icross = !itop.price.is_nan() && !oside_comp(price, itop.price); if (!is_dcross && !is_icross) break; // the order crossed at least one of the markets auto ord_desc = [&]() { return fmt_order(key, side, price, quantity); }; - if (!is_dcross || (is_icross && oside_comp(get_price(itop), get_price(dtop)))) { + if (!is_dcross || (is_icross && oside_comp(itop.price, dtop.price))) { // fill with the implied market DIAG("filling ", ord_desc(), " in implied market ", fmt_price_level(oside, itop)); - auto target_fill_qty = std::min(quantity, get_qty(itop)); + auto target_fill_qty = std::min(quantity, itop.quantity); auto rem = imatcher->fill(key, side, target_fill_qty, effects, fills); auto fill_qty = target_fill_qty - rem; quantity -= fill_qty; @@ -320,8 +323,8 @@ MDP3Quantity FifoMergedSecurityMatcher::_fill( if (local_effects) effects = new std::vector(); uint32_t fills_pos = effects->size(); - auto target_fill_qty = std::min(quantity, get_qty(dtop)); - auto rem = matcher()->fill(key, side, get_price(dtop), target_fill_qty, effects, true); + auto target_fill_qty = std::min(quantity, dtop.quantity); + auto rem = matcher()->fill(key, side, dtop.price, target_fill_qty, effects, true); auto fill_qty = target_fill_qty - rem; quantity -= fill_qty; DIAG("filled ", fill_qty, ". Order now: ", ord_desc()); diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h b/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h index 4626206ff..d996835e4 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h @@ -92,6 +92,26 @@ inline std::ostream &operator<<(std::ostream &s, BookLevel const &l) { return s; } +struct SmallBookLevel { + SmallBookLevel() : price(md::INT_NAN), quantity(0) {} + SmallBookLevel(md::IntPrice price, MDP3Quantity quantity) + : price(price), quantity(quantity) {} + + md::IntPrice price; + MDP3Quantity quantity; +}; + +inline bool operator==(const SmallBookLevel& lhs, const SmallBookLevel& rhs) { + return lhs.price == rhs.price && lhs.quantity.value() == rhs.quantity.value(); +} +inline bool operator!=(const SmallBookLevel& lhs, const SmallBookLevel& rhs) { + return lhs.price != rhs.price || lhs.quantity.value() != rhs.quantity.value(); +} +inline std::ostream& operator<<(std::ostream& out, const SmallBookLevel& sbl) { + out << sbl.quantity.value() << " @ " << sbl.price; + return out; +} + struct IcebergQuantity { MDP3Quantity show; MDP3Quantity total; @@ -285,6 +305,7 @@ inline std::ostream &operator<<(std::ostream &s, Fills const &fills) { const auto MDP3_MAX_BOOK_DEPTH = 10; const auto MDP3_MAX_IMPLIED_BOOK_DEPTH = 2; using Book = std::array; +using SmallBook = std::vector; inline std::ostream &operator<<(std::ostream &s, Book const &b) { for (auto const &l : b) diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp index 2e0dd6516..a90ab3dfc 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.cpp @@ -2,15 +2,13 @@ #include "server/sparkd/cmds/spartan/md/mdp3/common/mdp3_quantity_operators.h" namespace bts::spartan::mdp3::book_utils { -using _BookLevel = std::tuple; - -inline bool is_empty(const _BookLevel& bl) { - return std::get<0>(bl).is_nan() || std::get<1>(bl).value() == 0; +inline bool is_empty(const SmallBookLevel& bl) { + return bl.price.is_nan() || bl.quantity.value() == 0; } void compute_synthetic_book( - std::vector<_BookLevel>& dest, - const std::vector>>& + SmallBook& dest, + const std::vector>& leg_mkts, uint32_t depth) { // @@ -18,7 +16,7 @@ void compute_synthetic_book( return; struct Leg { - std::vector> book; + SmallBook book; uint32_t curr_level; MDP3Quantity curr_level_qty_used, leg_qty; @@ -33,10 +31,10 @@ void compute_synthetic_book( // as a result of deliberate action, rather than an inadvertant // side-effect. bts_assert(!is_exhausted()); - return std::get<0>(book[curr_level]); + return book[curr_level].price; } MDP3Quantity curr_level_qty_rem() const { - return std::get<1>(book[curr_level]) - curr_level_qty_used; + return book[curr_level].quantity - curr_level_qty_used; } // The implied market's volume is limited by its thinnest market. // When one leg 'is_exhausted,' the full volume of the implied market @@ -100,21 +98,18 @@ void compute_synthetic_book( } void compute_synthetic_books( - std::vector>& bids_dest, - std::vector>& asks_dest, - const std::vector>, - std::vector>>>& leg_mkts, + SmallBook& bids_dest, + SmallBook& asks_dest, + const std::vector>& + leg_mkts, uint32_t depth) { - std::vector>>> leg_bids; - std::vector>>> leg_asks; - for (auto [qty, price_mult, bids, asks] : leg_mkts) { - leg_bids.emplace_back(qty, price_mult, qty.value() > 0 ? bids : asks); - leg_asks.emplace_back(qty, price_mult, qty.value() < 0 ? bids : asks); - } - compute_synthetic_book(bids_dest, leg_bids, depth); - compute_synthetic_book(asks_dest, leg_asks, depth); + std::vector> leg_bids; + std::vector> leg_asks; + for (auto [qty, price_mult, bids, asks] : leg_mkts) { + leg_bids.emplace_back(qty, price_mult, qty.value() > 0 ? bids : asks); + leg_asks.emplace_back(qty, price_mult, qty.value() < 0 ? bids : asks); } + compute_synthetic_book(bids_dest, leg_bids, depth); + compute_synthetic_book(asks_dest, leg_asks, depth); +} } // namespace bts::spartan::mdp3::book_utils \ No newline at end of file diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.h b/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.h index 082efdc5f..3c9a25144 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/book_utils.h @@ -36,6 +36,12 @@ inline std::string fmt_price_level(api::Side side, md::IntPrice price, MDP3Quant return out.str(); } +inline std::string fmt_price_level(api::Side side, SmallBookLevel l) { + std::stringstream out; + fmt_price_level(out, side, l.price, l.quantity); + return out.str(); +} + inline std::string fmt_price_level(api::Side side, const std::tuple& pl) { return fmt_price_level(side, std::get<0>(pl), std::get<1>(pl)); } @@ -84,16 +90,13 @@ inline std::ostream& fmt_ladder( // TIter should point to BookLevel items. template inline void change_unit_qty( - TIter book_in_first, - TIter book_in_last, - std::vector>& book_out, - MDP3Quantity unit_qty) { + TIter book_in_first, TIter book_in_last, SmallBook& book_out, MDP3Quantity unit_qty) { // bts_assert(unit_qty.value() > 0); int mlev = 0, mlev_qty_used = 0; - std::vector> unit_lev_span; + SmallBook unit_lev_span; while (book_in_first != book_in_last) { - MDP3Quantity mlev_qty_rem(std::get<1>(*book_in_first).value() - mlev_qty_used); + MDP3Quantity mlev_qty_rem(book_in_first->quantity.value() - mlev_qty_used); if (mlev_qty_rem.value() == 0) { mlev_qty_used = 0; mlev++; @@ -103,10 +106,11 @@ inline void change_unit_qty( if (unit_lev_span.size() == 0) { int mlev_units_rem = mlev_qty_rem.value() / unit_qty.value(); if (mlev_units_rem > 0) { - book_out.push_back({std::get<0>(*book_in_first), MDP3Quantity(mlev_units_rem)}); + book_out.emplace_back( + book_in_first->price, MDP3Quantity(mlev_units_rem)); mlev_qty_used += (mlev_units_rem * unit_qty.value()); } else { - unit_lev_span.push_back({std::get<0>(*book_in_first), mlev_qty_rem}); + unit_lev_span.emplace_back(book_in_first->price, mlev_qty_rem); mlev_qty_used = 0; mlev++; book_in_first++; @@ -116,38 +120,34 @@ inline void change_unit_qty( // It will be in its own [output]price level. MDP3Quantity unit_rem(unit_qty); for (auto lev : unit_lev_span) - unit_rem = MDP3Quantity(unit_rem.value() - std::get<1>(lev).value()); + unit_rem = MDP3Quantity(unit_rem.value() - lev.quantity.value()); if (mlev_qty_rem.value() >= unit_rem.value()) { mlev_qty_used += unit_rem.value(); auto unit_price = md::INT_NAN; for (uint32_t adj_lev = 0; adj_lev < unit_lev_span.size(); adj_lev++) { - auto leg_price = std::get<0>(unit_lev_span[adj_lev]) * std::get<1>(unit_lev_span[adj_lev]).value(); + auto leg_price = + unit_lev_span[adj_lev].price * unit_lev_span[adj_lev].quantity.value(); if (unit_price.is_nan()) unit_price = leg_price; - else unit_price += leg_price; + else + unit_price += leg_price; } unit_lev_span.clear(); - book_out.push_back({unit_price, MDP3Quantity(1)}); + book_out.emplace_back(unit_price, MDP3Quantity(1)); } } } } void compute_synthetic_book( - std::vector>& dest, - const std::vector>>>& leg_mkts, + SmallBook& dest, + const std::vector>& leg_mkts, uint32_t depth); void compute_synthetic_books( - std::vector>& bids_dest, - std::vector>& asks_dest, - const std::vector>, - std::vector>>>& leg_mkts, + SmallBook& bids_dest, + SmallBook& asks_dest, + const std::vector>& + leg_mkts, uint32_t depth); } // namespace bts::spartan::mdp3::book_utils \ No newline at end of file diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/order_utils.h b/src/server/sparkd/cmds/spartan/md/mdp3/utils/order_utils.h index 14984c2ed..205e00c3b 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/order_utils.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/order_utils.h @@ -51,8 +51,8 @@ inline void set_total_qty(MDP3Order& order, MDP3Quantity total_qty, bool reset_s order.quantity = total_qty; } -inline MDP3Quantity book_volume(const std::vector>& book) { - auto get_qty = [](const auto& bl) { return std::get<1>(bl).value(); }; +inline MDP3Quantity book_volume(const SmallBook& book) { + auto get_qty = [](const auto& bl) { return bl.quantity.value(); }; return MDP3Quantity(ranges::accumulate(book | ranges::views::transform(get_qty), 0)); } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/queries/SyntheticBookRecorder.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/utils/queries/SyntheticBookRecorder.cpp index 21d2a2623..8d375f0b7 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/queries/SyntheticBookRecorder.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/queries/SyntheticBookRecorder.cpp @@ -21,7 +21,7 @@ using BookMap = using LegBookVec = std::vector< std::tuple>; -SmallBook to_small_book_vec(const Book& bk) { +SmallBook to_small_book(const Book& bk) { SmallBook rv; for (const auto& pl : bk) rv.emplace_back(pl.price, pl.quantity); @@ -59,8 +59,8 @@ void update_books( const InstrumentSnapshot* s = *first; books.insert_or_assign( s->security_id(), std::make_tuple( - event_id, to_small_book_vec(s->book(api::Side::SIDE_BUY)), - to_small_book_vec(s->book(api::Side::SIDE_SELL)))); + event_id, to_small_book(s->book(api::Side::SIDE_BUY)), + to_small_book(s->book(api::Side::SIDE_SELL)))); first++; } } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/queries/SyntheticBookRecorder.h b/src/server/sparkd/cmds/spartan/md/mdp3/utils/queries/SyntheticBookRecorder.h index 020a7b498..256352e73 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/queries/SyntheticBookRecorder.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/queries/SyntheticBookRecorder.h @@ -11,8 +11,6 @@ #include namespace bts::spartan::mdp3 { -using SmallBook = std::vector>; - struct SyntheticMBPUpdate { SyntheticMBPUpdate(); SyntheticMBPUpdate(