From e1fb9319a005b92a4cf411415b07a1291cb9f559 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Thu, 8 Oct 2020 14:31:19 -0400 Subject: [PATCH 1/2] better error checking and validation in the matcher --- .../matcher/FifoPriceLevelMatcher.cpp | 4 ++-- .../md/mdp3/back_testing/matcher/Matcher.cpp | 11 ++++------- .../md/mdp3/back_testing/matcher/MatchingCommon.h | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoPriceLevelMatcher.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoPriceLevelMatcher.cpp index a463dff11..b4121d767 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoPriceLevelMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoPriceLevelMatcher.cpp @@ -86,7 +86,7 @@ FifoPriceLevelMatcher::_one_pass_cross_order(MDP3OrderKey cross_ord_key, MDP3Quantity(filled), _price }; - UtilFunctions::validate_update(passive_upd); + MatcherUtils::validate_outgoing_update(passive_upd); res->push_back(passive_upd); OrderUpdate agg_upd = { @@ -99,7 +99,7 @@ FifoPriceLevelMatcher::_one_pass_cross_order(MDP3OrderKey cross_ord_key, MDP3Quantity(filled), _price }; - UtilFunctions::validate_update(agg_upd); + MatcherUtils::validate_outgoing_update(agg_upd); res->push_back(agg_upd); } if(_trace.log(o.key) || _trace.log(cross_ord_key)) diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp index c40a767ae..2d8d05edf 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp @@ -1,13 +1,8 @@ #include "Matcher.h" +#include "MatchingCommon.h" namespace bts::spartan::mdp3{ -void validate_order_change(const OrderChange& c){ - if(c.change == OrderChange::ChangeType::added || - c.change == OrderChange::ChangeType::updated) - bts_assert(c.order.quantity.value()>0); -} - Matcher::Matcher() : _matcher_map(){ @@ -27,13 +22,15 @@ void Matcher::match(ChangeSrc, const std::vector& changes, std::vector* res) { for (const auto& c : changes){ - validate_order_change(c); + MatcherUtils::validate_incoming_order_change(c); auto it = _matcher_map.find(c.order.security); bts_assert (it != _matcher_map.end()); auto& sec_matcher = it->second.matcher; sec_matcher.on_order_change(c, res); it->second.dirty = true; } + for(const auto& u : *res) + MatcherUtils::validate_outgoing_update(std::get(u)); // all order activity is not reflecting in the // books for (auto& kv : _matcher_map){ diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/MatchingCommon.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/MatchingCommon.h index 8d2bf9b79..588fa8895 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/MatchingCommon.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/MatchingCommon.h @@ -40,17 +40,26 @@ public: bool is_ignored_event(size_t eid) const{ return ignored_events.find(eid) != ignored_events.end(); } + void log_incoming_change(const OrderChange&){ + } + void log_outgoing_update(const OrderUpdate&){ + } }; -class UtilFunctions{ +class MatcherUtils{ public: - static void validate_update(const OrderUpdate& u){ + static void validate_incoming_order_change(const OrderChange& c){ + if(c.change == OrderChange::ChangeType::added || + c.change == OrderChange::ChangeType::updated) + bts_assert(c.order.quantity.value()>0); + } + static void validate_outgoing_update(const OrderUpdate& u){ if (u.status == OrderUpdate::Status::PARTIALLY_FILLED || u.status == OrderUpdate::Status::FILLED) bts_assert(u.last_fills.value() > 0); if (u.status == OrderUpdate::Status::CANCELED) bts_assert(u.last_fills.value() == 0); - } + } }; struct MatcherIB { From 74d10749b79f077c5cf00506bf72cee3b28b963d Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Thu, 8 Oct 2020 23:38:05 -0400 Subject: [PATCH 2/2] Bug where orders changing levels and getting completely filled were not marked as inactive --- .../matcher/FifoSecurityMatcher.cpp | 18 +++++++-- .../md/mdp3/back_testing/matcher/Matcher.cpp | 8 +++- .../md/mdp3/back_testing/matcher/Matcher.h | 2 + .../back_testing/matcher/MatchingCommon.h | 39 +++++++++---------- 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoSecurityMatcher.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoSecurityMatcher.cpp index 1093ad16c..261fdfea4 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoSecurityMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/FifoSecurityMatcher.cpp @@ -39,8 +39,10 @@ FifoSecurityMatcher::_find_order(MDP3OrderKey key) { auto map_it = _order_map.find(key); if(map_it != _order_map.end()){ auto& ord = map_it->second; - FifoPriceLevelMatcher* p_level = _find_price_level(ord.side,ord.price); - return std::make_tuple(p_level,&ord); + if(ord.is_active){ + FifoPriceLevelMatcher* p_level = _find_price_level(ord.side,ord.price); + return std::make_tuple(p_level,&ord); + } } return std::tuple( nullptr, nullptr); @@ -66,7 +68,7 @@ void FifoSecurityMatcher::_cleanup() { }); } void FifoSecurityMatcher::_on_new_order(const MDP3Order &ord, - std::vector* res) { + std::vector* res) { _log_trace_order(ord,"FifoSecurityMatcher::_on_new_order begin"); auto& opp_book = _book(_opp_side(ord.side)); uint32_t new_ord_rem = total_quantity(ord).value(); @@ -114,6 +116,16 @@ void FifoSecurityMatcher::_on_new_order(const MDP3Order &ord, ord_corrected.quantity = MDP3Quantity(new_ord_rem); price_level->on_new_same_side_order(ord_corrected); } + else{ + // since _on_new_order is also the same code path + // thats called when an order changes price levels + auto it = _order_map.find(ord.key); + if(it != _order_map.end()){ + MatcherOrder& ex_ord = it->second; + ex_ord.quantity = MDP3Quantity(0); + ex_ord.is_active = false; + } + } } void FifoSecurityMatcher::_on_order_update(const MDP3Order &ord, diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp index 2d8d05edf..88a65ea0d 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.cpp @@ -5,7 +5,8 @@ namespace bts::spartan::mdp3{ Matcher::Matcher() : -_matcher_map(){ +_matcher_map(), +_trace(){ } Matcher::Matcher(Matcher&& src) { @@ -21,7 +22,8 @@ Matcher::Matcher (std::vector const& inst_defs){ void Matcher::match(ChangeSrc, const std::vector& changes, std::vector* res) { - for (const auto& c : changes){ + for (const auto& c : changes){ + _trace.log_incoming_change(c); MatcherUtils::validate_incoming_order_change(c); auto it = _matcher_map.find(c.order.security); bts_assert (it != _matcher_map.end()); @@ -29,6 +31,8 @@ void Matcher::match(ChangeSrc, sec_matcher.on_order_change(c, res); it->second.dirty = true; } + for(const auto& u : *res) + _trace.log_outgoing_update(std::get(u)); for(const auto& u : *res) MatcherUtils::validate_outgoing_update(std::get(u)); // all order activity is not reflecting in the diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.h index 403ab43c4..040cd0f9f 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/Matcher.h @@ -4,6 +4,7 @@ #include "../simulator/AlgoEvent.h" #include "../../common/mdp3_common.h" #include "FifoSecurityMatcher.h" +#include "MatchingCommon.h" namespace bts::spartan::mdp3 { @@ -16,6 +17,7 @@ private: bool dirty = false; }; std::map _matcher_map; + Trace _trace; public: Matcher (); Matcher (std::vector const&); diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/MatchingCommon.h b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/MatchingCommon.h index 588fa8895..3815d7bc1 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/MatchingCommon.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/back_testing/matcher/MatchingCommon.h @@ -11,25 +11,10 @@ struct Trace{ public: const std::map traced_orders = { - {MDP3OrderKey(647262819778),true} - }; - const std::map traced_events = - { - {4416,true}, - {4498 ,true}, - {25041,true}, - {32518,true}, - {32843,true}, - {33264,true}, - {37687,true}, - {40431,true}, - {41833,true}, - {42948,true}, - {44973,true}, - }; - const std::map ignored_events = - { + {MDP3OrderKey(765159167395),true} }; + const std::map traced_events = {}; + const std::map ignored_events = {}; bool log(MDP3OrderKey key, size_t eid=0) const{ return traced_orders.find(key) != traced_orders.end() || traced_events.find(eid) != traced_events.end(); @@ -40,9 +25,23 @@ public: bool is_ignored_event(size_t eid) const{ return ignored_events.find(eid) != ignored_events.end(); } - void log_incoming_change(const OrderChange&){ + std::string _side(api::Side s){ + return s == api::Side::SIDE_BUY ? "BUY" : "SELL"; } - void log_outgoing_update(const OrderUpdate&){ + void log_incoming_change(const OrderChange& c){ + if(traced_orders.find(c.order.key) != traced_orders.end()) + LOG(OrderMan, Info, + "Matcher incoming change:{} ok:{} side:{} price:{} qty:{} ib:{}", + c.change, c.order.key, _side(c.order.side), c.order.price, + c.order.quantity, + c.order.iceberg_quantity.has_value() ? + c.order.iceberg_quantity.value().value() : 0); + } + void log_outgoing_update(const OrderUpdate& u){ + if(traced_orders.find(u.key) != traced_orders.end()) + LOG(OrderMan, Info, + "Matcher outgoing update:{} ok:{} side:{} last_fp:{} last_fq:{}", + u.status, u.key, _side(u.side), u.last_price, u.last_fills); } };