From 9f6dc5da115e9e63fa57e5c8df0a0959c9f50928 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 20 Jan 2021 23:58:07 -0600 Subject: [PATCH] Pipe event id through the matcher for better tracing --- .../spartan/back_testing/common/Trace.cpp | 42 +++++++++++---- .../cmds/spartan/back_testing/common/Trace.h | 13 ++++- .../matcher/FifoMatcherContext.cpp | 10 ++-- .../back_testing/matcher/FifoMatcherContext.h | 6 ++- .../matcher/FifoMergedSecurityMatcher.cpp | 15 +++--- .../matcher/FifoMergedSecurityMatcher.h | 2 +- .../matcher/FifoPriceLevelMatcher.cpp | 50 ++++++++++------- .../matcher/FifoPriceLevelMatcher.h | 35 +++++++----- .../matcher/FifoSecurityMatcher.cpp | 53 ++++++++++--------- .../matcher/FifoSecurityMatcher.h | 32 +++++++---- .../spartan/back_testing/matcher/Matcher.cpp | 9 ++-- .../spartan/back_testing/matcher/Matcher.h | 7 ++- .../back_testing/matcher/SecurityMatcher.h | 4 +- .../recorders/MatcherRecorder.cpp | 3 +- 14 files changed, 177 insertions(+), 104 deletions(-) diff --git a/src/server/sparkd/cmds/spartan/back_testing/common/Trace.cpp b/src/server/sparkd/cmds/spartan/back_testing/common/Trace.cpp index 51d8975f9..db4ee1070 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/common/Trace.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/common/Trace.cpp @@ -7,27 +7,49 @@ bool Trace::log(OrderKey key, EventID eid) const { traced_events.find(eid) != traced_events.end(); } +bool Trace::log(OrderKey key, AlgoEventID aeid) const { + return log(key, aeid.first); +} + bool Trace::log(EventID eid) const { return traced_events.find(eid) != traced_events.end(); } +bool Trace::log(AlgoEventID aeid) const { return log(aeid.first); } + bool Trace::is_ignored_event(EventID eid) const { return ignored_events.find(eid) != ignored_events.end(); } -void Trace::log_incoming_change(const SmallOrderChange &c) { - if (traced_orders.find(c.order.key) != traced_orders.end()) - LOG(OrderMan, Diag, - "Matcher incoming change:{} ok:{} side:{} price:{} qty:{} ib:{}", - c.change, c.order.key, c.order.side, c.order.price, c.order.quantity, - c.order.iceberg_quantity); +bool Trace::is_ignored_event(AlgoEventID aeid) const { + return is_ignored_event(aeid.first); } -void Trace::log_outgoing_update(const SimOrderUpdate &u) { - if (traced_orders.find(u.key) != traced_orders.end()) +void Trace::log_incoming_change(const SmallOrderChange &c, AlgoEventID aeid) { + if (traced_orders.find(c.order.key) != traced_orders.end() || + traced_events.find(aeid.first) != traced_events.end()) LOG(OrderMan, Diag, - "Matcher outgoing update:{} ok:{} side:{} last_fp:{} last_fq:{}", - u.status, u.key, u.side, u.last_price, u.last_fills); + "[event {}]Matcher incoming change:{} ok:{} side:{} price:{} qty:{} " + "ib:{}", + aeid, c.change, c.order.key, c.order.side, c.order.price, + c.order.quantity, c.order.iceberg_quantity); +} + +void Trace::log_incoming_change(const SmallOrderChange &c, EventID eid) { + log_incoming_change(c, {eid, 0}); +} + +void Trace::log_outgoing_update(const SimOrderUpdate &u, AlgoEventID aeid) { + if (traced_orders.find(u.key) != traced_orders.end() || + traced_events.find(aeid.first) != traced_events.end()) + LOG(OrderMan, Diag, + "[event {}]Matcher outgoing update:{} ok:{} side:{} last_fp:{} " + "last_fq:{}", + aeid, u.status, u.key, u.side, u.last_price, u.last_fills); +} + +void Trace::log_outgoing_update(const SimOrderUpdate &u, EventID eid) { + log_outgoing_update(u, {eid, 0}); } } // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/back_testing/common/Trace.h b/src/server/sparkd/cmds/spartan/back_testing/common/Trace.h index a53221986..d138c83ef 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/common/Trace.h +++ b/src/server/sparkd/cmds/spartan/back_testing/common/Trace.h @@ -19,10 +19,19 @@ class Trace { const IgnoredEvents & = {}); bool log(OrderKey key, EventID eid = no_event()) const; + bool log(OrderKey key, AlgoEventID eid) const; + bool log(EventID eid) const; + bool log(AlgoEventID eid) const; + bool is_ignored_event(EventID eid) const; - void log_incoming_change(const SmallOrderChange &); - void log_outgoing_update(const SimOrderUpdate &); + bool is_ignored_event(AlgoEventID eid) const; + + void log_incoming_change(const SmallOrderChange &, EventID = no_event()); + void log_incoming_change(const SmallOrderChange &, AlgoEventID); + + void log_outgoing_update(const SimOrderUpdate &, EventID = no_event()); + void log_outgoing_update(const SimOrderUpdate &, AlgoEventID); const std::map traced_orders = {}; const std::map traced_events = {}; diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.cpp index 4f937fd7d..893adb499 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.cpp @@ -182,22 +182,24 @@ void FifoMatcherContext::on_order_change( SecurityID sec_id, const std::vector &changes, std::vector *effects, - FillDepot &fills) { + FillDepot &fills, + AlgoEventID event_id) { // // SUMMARIZE(_log_header()); _ensure_sync_implied_candidates(); auto *matcher = _get_or_add_matcher(sec_id); bts_assert(matcher != nullptr); for (auto ch : changes) - matcher->on_order_change(ch, effects, fills); + matcher->on_order_change(ch, effects, fills, event_id); } void FifoMatcherContext::on_order_change( const SmallOrderChange &change, std::vector *effects, - FillDepot &fills) { + FillDepot &fills, + AlgoEventID event_id) { // - on_order_change(change.order.security, {change}, effects, fills); + on_order_change(change.order.security, {change}, effects, fills, event_id); } /////////////////////// diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.h index 390f10104..7d246390e 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.h +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMatcherContext.h @@ -42,11 +42,13 @@ struct FifoMatcherContext { SecurityID sec_id, const std::vector &changes, std::vector *effects, - FillDepot &fills); + FillDepot &fills, + AlgoEventID = no_algo_event()); void on_order_change( const SmallOrderChange &change, std::vector *effects, - FillDepot &fills); + FillDepot &fills, + AlgoEventID = no_algo_event()); void print_all_bbos(std::ostream &out); std::optional has_same_orders(SecurityID, const Orders &); std::tuple diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp index 30d48538f..a039dc005 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp @@ -117,11 +117,12 @@ void FifoMergedSecurityMatcher::implied_matching_status(bool value) { void FifoMergedSecurityMatcher::on_order_change( const SmallOrderChange &ch, std::vector *effects, - FillDepot &fills) { + FillDepot &fills, + AlgoEventID event_id) { if (!implied_matching_status()) { // there is no "merged matching" when implied matching is off, // just pass the order changes the direct matcher. - matcher()->on_order_change(ch, effects); + matcher()->on_order_change(ch, effects, event_id); return; } bts_assert_msg( @@ -155,13 +156,13 @@ void FifoMergedSecurityMatcher::on_order_change( new_ch.order.iceberg_quantity = prev_ord->ib; _fill(new_ch.order, effects, fills, false); if (new_ch.order.quantity.value() == 0) { - matcher()->on_order_completed(ch.order.key, effects); + matcher()->on_order_completed(ch.order.key, event_id); } else { // Now that the order has been (possibly) filled, the iceberg // quantity is set back to null, before the order is passed to the // (direct) security matcher new_ch.order.iceberg_quantity = std::nullopt; - matcher()->on_order_change(new_ch, effects); + matcher()->on_order_change(new_ch, effects, event_id); } } else { // As far as we know, the only type of update that could trigger fills @@ -170,7 +171,7 @@ void FifoMergedSecurityMatcher::on_order_change( // market, we may observe down the road: an implied market with a leg // for this security's same side market, that also has a leg size // greater than 1. - matcher()->on_order_change(ch, effects); + matcher()->on_order_change(ch, effects, event_id); } break; } @@ -178,11 +179,11 @@ void FifoMergedSecurityMatcher::on_order_change( auto new_ch = ch; _fill(new_ch.order, effects, fills, false); if (get_total_qty(new_ch.order).value() > 0) - matcher()->on_order_change(new_ch, effects); + matcher()->on_order_change(new_ch, effects, event_id); break; } case OrderChange::ChangeType::removed: { - matcher()->on_order_change(ch, effects); + matcher()->on_order_change(ch, effects, event_id); break; } } diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.h index 84f27e833..25ad602a0 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.h +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.h @@ -25,7 +25,7 @@ struct FifoMergedSecurityMatcher { void implied_matching_status(bool value); bool implied_matching_status() const; void on_order_change( - const SmallOrderChange &, std::vector *, FillDepot &); + const SmallOrderChange &, std::vector *, FillDepot &, AlgoEventID = no_algo_event()); FifoImpliedSecurityMatcher *add_implied_matcher(SecurityID); FifoSecurityMatcher *matcher(); const FifoSecurityMatcher *matcheri() const; diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.cpp index 9c477d0c9..f36d253ca 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.cpp @@ -23,11 +23,12 @@ MatcherOrder *FifoPriceLevelMatcher::_find(OrderKey key) { return nullptr; } -void FifoPriceLevelMatcher::_logically_delete(MatcherOrder &o) { - _log_order(o, "FifoPriceLevelMatcher::_logically_delete"); +void FifoPriceLevelMatcher::_logically_delete(MatcherOrder &o, AlgoEventID event_id) { + _log_order(o, "FifoPriceLevelMatcher::_logically_delete", event_id); o.is_active = false; } -void FifoPriceLevelMatcher::_add_order(const MDP3Order &ord) { +void FifoPriceLevelMatcher::_add_order(const MDP3Order &ord, AlgoEventID event_id) { + _log_order(ord, fmt::format("{} begin", __FUNCTION__), event_id); MatcherOrder new_ord( ord.key, ord.side, ord.price, ord.quantity, _priority++, ord.iceberg_quantity); @@ -61,7 +62,8 @@ FifoPriceLevelMatcher::_one_pass_cross_order( OrderKey cross_ord_key, Quantity cross_ord_qty, bool add_agg_side_to_match_effect, - std::vector *res) { + std::vector *res, + AlgoEventID event_id) { // go through all the orders and see which ones get filled // (orders are sorted by priority) for (MatcherOrder *op : _orders) { @@ -97,7 +99,7 @@ FifoPriceLevelMatcher::_one_pass_cross_order( res->push_back(agg_upd); } } - if (_trace.log(o.key) || _trace.log(cross_ord_key)) + if (_trace.log(o.key, event_id) || _trace.log(cross_ord_key, event_id)) LOG(OrderMan, Diag, "FifoPriceLevelMatcher::_one_pass_cross_order " "{} (new_qty:{}) crossed by {} (new_qty:{})", @@ -116,15 +118,15 @@ FifoPriceLevelMatcher::_one_pass_cross_order( return std::make_tuple(total_quantity(), cross_ord_qty); } void FifoPriceLevelMatcher::_log_order( - const MatcherOrder &ord, const std::string &msg) { - if (_trace.log(ord.key)) + const MatcherOrder &ord, const std::string &msg, AlgoEventID event_id) { + if (_trace.log(ord.key, event_id)) LOG(OrderMan, Diag, "{} key:{} side:{} qty:{} ib_qty:{} price:{}", msg, ord.key, static_cast(side()), ord.quantity, ord.ib.has_value() ? ord.ib.value().total : Quantity(0), price()); } void FifoPriceLevelMatcher::_log_order( - const MDP3Order &ord, const std::string &msg) { - if (_trace.log(ord.key)) + const MDP3Order &ord, const std::string &msg, AlgoEventID event_id) { + if (_trace.log(ord.key, event_id.first)) LOG(OrderMan, Diag, "{} key:{} side:{} qty:{} ib_qty:{} price:{}", msg, ord.key, static_cast(ord.side), ord.quantity, ord.iceberg_quantity.has_value() ? FMT(ord.iceberg_quantity.value()) @@ -204,29 +206,32 @@ void FifoPriceLevelMatcher::copy_values_to( } // order interactions ///////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// -void FifoPriceLevelMatcher::on_new_same_side_order(const MDP3Order &new_ord) { +void FifoPriceLevelMatcher::on_new_same_side_order(const MDP3Order &new_ord, AlgoEventID event_id) { bts_assert(new_ord.price == _price); bts_assert(new_ord.side == _side); - _add_order(new_ord); + _add_order(new_ord, event_id); } std::tuple FifoPriceLevelMatcher::on_new_opp_order( OrderKey opp_ord_key, Quantity cross_qty, bool add_agg_side_to_match_effect, - std::vector *res) { + std::vector *res, + AlgoEventID event_id) { auto rv = _one_pass_cross_order( - opp_ord_key, cross_qty, add_agg_side_to_match_effect, res); + opp_ord_key, cross_qty, add_agg_side_to_match_effect, res, event_id); // continue until either the crossing order is consumed // or the price level is consumed while (std::get<0>(rv).value() > 0 && std::get<1>(rv).value() > 0) rv = _one_pass_cross_order( - opp_ord_key, std::get<1>(rv), add_agg_side_to_match_effect, res); + opp_ord_key, std::get<1>(rv), add_agg_side_to_match_effect, res, + event_id); return rv; } void FifoPriceLevelMatcher::on_order_update( - const MDP3Order &upd_ord, MatcherOrder *ord_ptr_hint) { + const MDP3Order &upd_ord, MatcherOrder *ord_ptr_hint, AlgoEventID event_id) { + _log_order(upd_ord, fmt::format("{} begin", __FUNCTION__), event_id); // ord_ptr_hint is a hint, if its populated use, else find bts_assert(upd_ord.side == _side); bts_assert(upd_ord.price == _price); @@ -249,10 +254,11 @@ void FifoPriceLevelMatcher::on_order_update( ord.ib.value() != upd_ord.iceberg_quantity.value()) ord.ib = upd_ord.iceberg_quantity; } + _log_order(ord, fmt::format("{} end", __FUNCTION__), event_id); } void FifoPriceLevelMatcher::on_order_cancel( - MatcherOrder &order, std::vector *res) { - _logically_delete(order); + MatcherOrder &order, std::vector *res, AlgoEventID event_id) { + _logically_delete(order, event_id); SimOrderUpdate o = { order.key, SimOrderUpdate::Status::CANCELED, @@ -265,12 +271,16 @@ void FifoPriceLevelMatcher::on_order_cancel( res->push_back(o); } void FifoPriceLevelMatcher::on_order_remove( - OrderKey k, std::vector *res) { + OrderKey k, std::vector *res, AlgoEventID event_id) { MatcherOrder *ord_p = _find(k); bts_assert(ord_p != nullptr); - on_order_cancel(*ord_p, res); + on_order_cancel(*ord_p, res, event_id); } -void FifoPriceLevelMatcher::remove_from_price_level(OrderKey k) { +void FifoPriceLevelMatcher::remove_from_price_level(OrderKey k, AlgoEventID event_id) { + if (_trace.log(k, event_id.first)) + LOG(OrderMan, Diag, + "[event {}, sec {}, price {}]FifoPriceLevelMatcher removing order {}", + event_id, _sec, price(), k); // puts a nullptr in place of where this order was // (perioric cleanup will physically delete the slot) auto it = std::find_if(_orders.begin(), _orders.end(), [&](MatcherOrder *o) { diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.h index 0f17d210a..430c74b2c 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.h +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.h @@ -30,8 +30,8 @@ class FifoPriceLevelMatcher { /////////////////////////////////////////////////////////////////////////////// private: MatcherOrder *_find(OrderKey key); - void _logically_delete(MatcherOrder &o); - void _add_order(const MDP3Order &ord); + void _logically_delete(MatcherOrder &o, AlgoEventID = no_algo_event()); + void _add_order(const MDP3Order &ord, AlgoEventID = no_algo_event()); // crosses the cross_order with existing orders. returns // the remaining qty in the level and the cross order void _reload_icebergs(); @@ -42,9 +42,12 @@ class FifoPriceLevelMatcher { OrderKey cross_ord_key, Quantity cross_ord_qty, bool add_agg_side_to_match_effect, - std::vector *res); - void _log_order(const MatcherOrder &ord, const std::string &msg); - void _log_order(const MDP3Order &ord, const std::string &msg); + std::vector *res, + AlgoEventID = no_algo_event()); + void _log_order( + const MatcherOrder &ord, const std::string &msg, AlgoEventID = no_algo_event()); + void _log_order( + const MDP3Order &ord, const std::string &msg, AlgoEventID = no_algo_event()); public: // constructors //////////////////////////////////////////////////////////// @@ -98,7 +101,7 @@ class FifoPriceLevelMatcher { // order interactions ////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// // order with the same side and price as this price level - void on_new_same_side_order(const MDP3Order &new_ord); + void on_new_same_side_order(const MDP3Order &new_ord, AlgoEventID = no_algo_event()); // order update such that only the qty changed (other changes // are handled at a higher level) // new order such that it crosses this price level @@ -106,13 +109,19 @@ class FifoPriceLevelMatcher { OrderKey opp_ord_key, Quantity opp_ord_qty, bool add_agg_side_to_match_effect, - std::vector * = nullptr); - void - on_order_update(const MDP3Order &upd_ord, MatcherOrder *ord_hint = nullptr); - void - on_order_cancel(MatcherOrder &order, std::vector * = nullptr); - void on_order_remove(OrderKey k, std::vector * = nullptr); - void remove_from_price_level(OrderKey k); + std::vector * = nullptr, + AlgoEventID = no_algo_event()); + void on_order_update( + const MDP3Order &upd_ord, + MatcherOrder *ord_hint = nullptr, + AlgoEventID = no_algo_event()); + void on_order_cancel( + MatcherOrder &order, + std::vector * = nullptr, + AlgoEventID = no_algo_event()); + void on_order_remove( + OrderKey k, std::vector * = nullptr, AlgoEventID = no_algo_event()); + void remove_from_price_level(OrderKey k, AlgoEventID = no_algo_event()); void cleanup(); std::string print_mbo(size_t n_orders) const; }; diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp index ba2ba9b2f..42cb4017d 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp @@ -88,13 +88,13 @@ FifoPriceLevelMatcher *FifoSecurityMatcher::_get_or_add_price_level( return price_level; } -void FifoSecurityMatcher::_add_resting_order(const MDP3Order &ord) { - _get_or_add_price_level(ord.side, ord.price)->on_new_same_side_order(ord); +void FifoSecurityMatcher::_add_resting_order(const MDP3Order &ord, AlgoEventID event_id) { + _get_or_add_price_level(ord.side, ord.price)->on_new_same_side_order(ord, event_id); } void FifoSecurityMatcher::_on_new_order( - const MDP3Order &ord, std::vector *res) { - _log_trace_order(ord, "FifoSecurityMatcher::_on_new_order begin"); + const MDP3Order &ord, std::vector *res, AlgoEventID event_id) { + _log_trace_order(ord, "FifoSecurityMatcher::_on_new_order begin", event_id); auto &opp_book = _book(other_side(ord.side)); uint32_t new_ord_rem = total_quantity(ord).value(); for (FifoPriceLevelMatcher &pl : opp_book) { @@ -139,8 +139,8 @@ void FifoSecurityMatcher::_on_new_order( } void FifoSecurityMatcher::_on_order_update( - const MDP3Order &ord, bool price_only, std::vector *res) { - _log_trace_order(ord, "FifoSecurityMatcher::_on_order_update begin"); + const MDP3Order &ord, bool price_only, std::vector *res, AlgoEventID event_id) { + _log_trace_order(ord, "FifoSecurityMatcher::_on_order_update begin", event_id); auto [price_level, curr_ord] = _find_order(ord.key); if (price_level == nullptr || curr_ord == nullptr) { if (_ignore_ops_on_deleted_orders) @@ -177,17 +177,17 @@ void FifoSecurityMatcher::_on_order_update( // check for price change if (price_level->price() != new_ord.price) { // remove from current level and add to another - price_level->remove_from_price_level(curr_ord->key); - _on_new_order(new_ord, res); + price_level->remove_from_price_level(curr_ord->key, event_id); + _on_new_order(new_ord, res, event_id); } else { - price_level->on_order_update(new_ord, curr_ord); + price_level->on_order_update(new_ord, curr_ord, event_id); } - _log_trace_order(ord, "FifoSecurityMatcher::on_order_update end"); + _log_trace_order(ord, "FifoSecurityMatcher::on_order_update end", event_id); } void FifoSecurityMatcher::_on_order_remove( - OrderKey key, std::vector *res) { - if (_trace.log(key)) + OrderKey key, std::vector *res, AlgoEventID event_id) { + if (_trace.log(key, event_id)) LOG(OrderMan, Diag, "FifoSecurityMatcher::_on_order_remove " "eid:{} key:{}", @@ -344,23 +344,23 @@ FifoSecurityMatcher::book_level(Side side, size_t idx) const { return std::make_tuple(Price{}, Quantity(0)); } void FifoSecurityMatcher::on_order_change( - SmallOrderChange const &o, std::vector *res) { + SmallOrderChange const &o, std::vector *res, AlgoEventID event_id) { if (!o.affect_book) { _on_order_not_affecting_books(o, res); return; } switch (o.change) { case OrderChange::added: - _on_new_order(o.order, res); + _on_new_order(o.order, res, event_id); break; case OrderChange::updated: - _on_order_update(o.order, false, res); + _on_order_update(o.order, false, res, event_id); break; case OrderChange::updated_price: - _on_order_update(o.order, true, res); + _on_order_update(o.order, true, res, event_id); break; case OrderChange::removed: - _on_order_remove(o.order.key, res); + _on_order_remove(o.order.key, res, event_id); break; } _invalidate_mbp(); @@ -370,7 +370,7 @@ void FifoSecurityMatcher::on_order_change( } } -void FifoSecurityMatcher::on_order_completed(OrderKey key) { +void FifoSecurityMatcher::on_order_completed(OrderKey key, AlgoEventID event_id) { // todo: actual implementation // (this is a hack that treats completed orders like cancels) auto [pl, ord] = _find_order(key); @@ -380,7 +380,7 @@ void FifoSecurityMatcher::on_order_completed(OrderKey key) { MDP3Order order; order.key = key; SmallOrderChange ch(order, OrderChange::ChangeType::removed); - on_order_change(ch); + on_order_change(ch, nullptr, event_id); } Quantity FifoSecurityMatcher::fill( @@ -389,11 +389,12 @@ Quantity FifoSecurityMatcher::fill( Price agg_price, Quantity qty, std::vector *res, - bool add_aggr_fill) { + bool add_aggr_fill, + AlgoEventID event_id) { FifoPriceLevelMatcher *top = _top(other_side(side)); if (top != nullptr) { bts_assert(top->price() == agg_price || agg_price.is_nan()); - auto t = top->on_new_opp_order(aggr_key, qty, add_aggr_fill, res); + auto t = top->on_new_opp_order(aggr_key, qty, add_aggr_fill, res, event_id); _invalidate_mbp(); return std::get<1>(t); } @@ -490,17 +491,17 @@ void FifoSecurityMatcher::_log_trace_order_state(const std::string &msg) { } } void FifoSecurityMatcher::_log_trace_order( - const MDP3Order &ord, const std::string &msg) { - if (_trace.log(ord.key, _event_id)) { + const MDP3Order &ord, const std::string &msg, AlgoEventID event_id) { + if (_trace.log(ord.key, event_id)) { LOG(OrderMan, Diag, "{} eid:{} key:{} side:{} qty:{} ib_qty:{} price:{} book:{}", msg, - _event_id, ord.key, static_cast(ord.side), ord.quantity, + event_id, ord.key, static_cast(ord.side), ord.quantity, ord.iceberg_quantity, ord.price.to_double(), print_book()); _log_trace_order_state(msg); } } -void FifoSecurityMatcher::_log_mbo(size_t n_levels, size_t n_orders) { - if (_trace.log(_event_id)) { +void FifoSecurityMatcher::_log_mbo(size_t n_levels, size_t n_orders, AlgoEventID event_id) { + if (_trace.log(event_id)) { std::ostringstream buff; buff << "eid:" << _event_id << std::endl; size_t i = 0; diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.h index 0528ded37..36dd4efc8 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.h +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.h @@ -81,12 +81,17 @@ class FifoSecurityMatcher : public SecurityMatcher { FifoPriceLevelMatcher *_top(Side side); void _cleanup(); // order interaction - void _on_new_order(const MDP3Order &, std::vector * = nullptr); + void _on_new_order( + const MDP3Order &, + std::vector * = nullptr, + AlgoEventID = no_algo_event()); void _on_order_update( const MDP3Order &, bool price_only = false, - std::vector * = nullptr); - void _on_order_remove(OrderKey, std::vector * = nullptr); + std::vector * = nullptr, + AlgoEventID = no_algo_event()); + void _on_order_remove( + OrderKey, std::vector * = nullptr, AlgoEventID = no_algo_event()); void _on_order_not_affecting_books( const SmallOrderChange &o, std::vector *res); std::string _log_header() const; @@ -122,12 +127,17 @@ class FifoSecurityMatcher : public SecurityMatcher { Price price, Quantity qty, std::vector *res, - bool add_aggr_fill); - std::tuple - book_level(Side side, size_t idx) const override; + bool add_aggr_fill, + AlgoEventID = no_algo_event()); + + std::tuple book_level(Side side, size_t idx) const override; + void on_order_change( - SmallOrderChange const &, std::vector * = nullptr) override; - void on_order_completed(OrderKey key); + SmallOrderChange const &, + std::vector * = nullptr, + AlgoEventID = no_algo_event()) override; + + void on_order_completed(OrderKey key, AlgoEventID = no_algo_event()); // For testing std::optional has_same_orders(Orders const &); size_t total_n_orders(Side) const; @@ -156,8 +166,8 @@ class FifoSecurityMatcher : public SecurityMatcher { private: void _log_trace_order_state(const std::string &msg); - void _log_trace_order(const MDP3Order &ord, const std::string &msg); - void _log_mbo(size_t n_levels, size_t n_orders); + void _log_trace_order(const MDP3Order &ord, const std::string &msg, AlgoEventID = no_algo_event()); + void _log_mbo(size_t n_levels, size_t n_orders, AlgoEventID = no_algo_event()); // Inserts the specified order at the appropriate price level without // attempting to fill it. While this won't matter in most cases // (resting orders don't cross the opposite market anyways) 'on_new_order' @@ -171,7 +181,7 @@ class FifoSecurityMatcher : public SecurityMatcher { // This is obviously problematic if the aim is to inject an exact state // into the matcher, as the order's visible quantity is often less than // the show quantity (e.g., after partial fills, before the next reload). - void _add_resting_order(const MDP3Order &); + void _add_resting_order(const MDP3Order &, AlgoEventID = no_algo_event()); FifoPriceLevelMatcher *_get_or_add_price_level(Side side, Price); }; } // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.cpp index 9699f38be..491d0128f 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.cpp @@ -135,7 +135,8 @@ void MergedMatcher::reset( void MergedMatcher::match( ChangeSrc, SmallOrderChangeVec const &changes, - std::vector *res) { + std::vector *res, + AlgoEventID event_id) { _me->match_count_this_per++; _me->match_count++; // The caller is not obligated to pass an empty vector. Consider an event @@ -164,7 +165,7 @@ void MergedMatcher::match( } for (const auto &c : changes) { - _me->trace.log_incoming_change(c); + _me->trace.log_incoming_change(c, event_id); MatcherUtils::validate_incoming_order_change(c); switch (c.change) { case OrderChange::ChangeType::added: @@ -181,12 +182,12 @@ void MergedMatcher::match( // todo: make the fills param optional, as // it will often be more info than we care abt. FillDepot fills; - _asset_matcher(c.order.security).on_order_change(c, res, fills); + _asset_matcher(c.order.security).on_order_change(c, res, fills, event_id); } // log/validate order updates if (res != nullptr) { for (auto it = res->begin() + effects_origin; it != res->end(); it++) - _me->trace.log_outgoing_update(std::get(*it)); + _me->trace.log_outgoing_update(std::get(*it), event_id); for (auto it = res->begin() + effects_origin; it != res->end(); it++) MatcherUtils::validate_outgoing_update(std::get(*it)); // add mbp updates diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.h index 35e4104f0..7d882d9d3 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.h +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/Matcher.h @@ -28,8 +28,11 @@ class MergedMatcher { enum ChangeSrc { ALGO, SIMULATOR_INPUT }; void add_def(const SecurityDefinition &def); - void - match(ChangeSrc, SmallOrderChangeVec const &, std::vector *); + void match( + ChangeSrc, + SmallOrderChangeVec const &, + std::vector *, + AlgoEventID = no_algo_event()); BookSide book(SecurityID, Side); ImpliedBookSide implied_book(SecurityID, Side); diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/SecurityMatcher.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/SecurityMatcher.h index 8fc8f203d..105a7615d 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/SecurityMatcher.h +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/SecurityMatcher.h @@ -18,6 +18,8 @@ class SecurityMatcher { book_level(Side, size_t level = 0) const = 0; // mutators virtual void on_order_change( - SmallOrderChange const &, std::vector * = nullptr) = 0; + SmallOrderChange const &, + std::vector * = nullptr, + AlgoEventID = no_algo_event()) = 0; }; } // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/market_data/feed_observers/recorders/MatcherRecorder.cpp b/src/server/sparkd/cmds/spartan/market_data/feed_observers/recorders/MatcherRecorder.cpp index f140e994c..0f8a985e3 100644 --- a/src/server/sparkd/cmds/spartan/market_data/feed_observers/recorders/MatcherRecorder.cpp +++ b/src/server/sparkd/cmds/spartan/market_data/feed_observers/recorders/MatcherRecorder.cpp @@ -89,7 +89,8 @@ static inline MatcherRecorder::MatcherProcResults process_changes( auto schanges = to_small_change_vec(changes, icebergs, event_id); stopwatch.start(); matcher.match( - MergedMatcher::ChangeSrc::SIMULATOR_INPUT, schanges, &effects); + MergedMatcher::ChangeSrc::SIMULATOR_INPUT, schanges, &effects, + {event_id, 0}); stopwatch.stop(); } bool mbp_correct = true;