From f4bb9544d45ebfd4e554a9dcb2d5c1d5b97ea688 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 22 Jan 2021 13:38:29 -0600 Subject: [PATCH] fix: Fill same-priced passive icebergs evenly When an agressor order crosses multiple [direct/implied] passive markets at the same price, the aggressor is filled against the visible quantity in each passive market, before passive icebergs (if any) are reloaded. This simple iterative step is repeated until the aggressor is filled, or no longer crosses the market. --- .../matcher/FifoMergedSecurityMatcher.cpp | 96 ++++++++--------- .../matcher/FifoMergedSecurityMatcher.h | 1 - .../matcher/common/MatcherOneof.cc | 31 ++++++ .../matcher/common/MatcherOneof.cpp | 23 ++++ .../matcher/common/MatcherOneof.h | 30 ++++++ .../matcher/common/MatchingSession.cpp | 100 ++++++++++++++++++ .../matcher/common/MatchingSession.h | 48 +++++++++ 7 files changed, 276 insertions(+), 53 deletions(-) create mode 100644 src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOneof.cc create mode 100644 src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOneof.cpp create mode 100644 src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOneof.h create mode 100644 src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatchingSession.cpp create mode 100644 src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatchingSession.h 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 b54c333b1..3ebed7b95 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp @@ -1,7 +1,7 @@ -#include "back_testing/matcher/FifoMergedSecurityMatcher.h" -#include "back_testing/matcher/FifoImpliedSecurityMatcher.h" -#include "back_testing/matcher/FifoSecurityMatcher.h" +#include "FifoMergedSecurityMatcher.h" #include "back_testing/matcher/fill_factory.h" +#include "common/MatcherOneof.h" +#include "common/MatchingSession.h" #include "lib/md/IntPrice.h" #include "lib/orderman/orderman_utils/Formatter.h" #include "lib/orderman/orderman_utils/PriceComp.h" @@ -23,6 +23,7 @@ using namespace ranges; using namespace bts::spartan::mdp3::book_utils; namespace bts::spartan::mdp3 { + struct FifoMergedSecurityMatcher::PImpl { PImpl(const FifoMergedSecurityMatcher::InitArgs &args) : security_id(args.security_id), @@ -92,21 +93,7 @@ FifoMergedSecurityMatcher::add_implied_matcher(SecurityID sec) { _me->implied_matchers.push_back(m); return m; } -FifoImpliedSecurityMatcher * -FifoMergedSecurityMatcher::_best_implied_matcher(Side side) { - if (!_me->implied_matching_status) - return nullptr; - FifoImpliedSecurityMatcher *rv = nullptr; - Price best_price; - for (auto &im : _me->implied_matchers) { - auto itop = im->top_of_book(side); - if (itop.quantity.value() > 0 && itop.price.is_better(side, best_price)) { - rv = im; - best_price = itop.price; - } - } - return rv; -} + bool FifoMergedSecurityMatcher::implied_matching_status() const { return _me->implied_matching_status; } @@ -330,11 +317,6 @@ void FifoMergedSecurityMatcher::_fill( 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)); -} - Quantity FifoMergedSecurityMatcher::_fill( OrderKey key, Side side, @@ -344,43 +326,32 @@ Quantity FifoMergedSecurityMatcher::_fill( FillDepot &fills) { // auto oside = other_side(side); - bts::orderman::SidePriceComp oside_comp(map_side(oside)); - while (quantity.value() > 0) { + MatchingSession match_session( + *_me->matcher, _me->implied_matchers, oside, price); + auto match = match_session.get_next(); + auto ord_desc = [&]() { return fmt_order(key, side, price, quantity); }; + while (quantity.value() > 0 && + match.matcher_case() != MatcherOneof::Case::None) { + // At the end of each iteration, assert that 'quantity' decreased. The + // fill qty is deducted from 'quantity' so this indicates that the order + // was actually filled. This guards against an infinite loop. + auto orig_qty = quantity; // Fill one price level at a time. // While one market may have the best 1st price level, // another may have the best 2nd price level. - auto *imatcher = _best_implied_matcher(oside); - auto itop = - imatcher == nullptr ? SmallBookLevel() : imatcher->top_of_book(oside); - 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(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, itop.quantity); - auto rem = imatcher->fill(key, side, target_fill_qty, effects, fills); - auto fill_qty = target_fill_qty - rem; - quantity -= fill_qty; - DIAG("filled ", fill_qty, ". Order now: ", ord_desc()); - } else { - // fill with the direct market + switch (match.matcher_case()) { + case MatcherOneof::Case::Direct: { + auto [top_price, top_qty] = match.direct().book_level(oside, 0); DIAG( "filling ", ord_desc(), " in direct market ", - fmt_price_level(oside, dtop)); + fmt_price_level(oside, top_price, top_qty)); bool local_effects = effects == nullptr; if (local_effects) effects = new std::vector(); uint32_t fills_pos = effects->size(); - auto target_fill_qty = std::min(quantity, dtop.quantity); + auto target_fill_qty = std::min(quantity, top_qty); auto rem = matcher()->fill( - key, side, dtop.price, target_fill_qty, effects, true); + key, side, top_price, target_fill_qty, effects, true); auto fill_qty = target_fill_qty - rem; quantity -= fill_qty; DIAG("filled ", fill_qty, ". Order now: ", ord_desc()); @@ -414,12 +385,33 @@ Quantity FifoMergedSecurityMatcher::_fill( delete effects; effects = nullptr; } - if (!aggr_fill.price.is_nan()) { + if (!aggr_fill.price.is_nan()) fills.add_fill(security_id(), key, aggr_fill); - } + break; } + case MatcherOneof::Case::Implied: { + auto top = match.implied().top_of_book(oside); + DIAG( + "filling ", ord_desc(), " in implied market ", + fmt_price_level(oside, top)); + auto target_fill_qty = std::min(quantity, top.quantity); + auto rem = + match.implied().fill(key, side, target_fill_qty, effects, fills); + auto fill_qty = target_fill_qty - rem; + quantity -= fill_qty; + DIAG("filled ", fill_qty, ". Order now: ", ord_desc()); + break; + } + default: + bts_assert_msg(false, "unexpected MatcherOneof::Case"); + break; + } + // Assert fill(s) occurred (the fill qty is deducted from 'quantity'). + bts_assert(quantity < orig_qty); + match = match_session.get_next(); } bts_assert(quantity.value() >= 0); return quantity; } + } // namespace bts::spartan::mdp3 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 d2230b26d..7b3450970 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.h +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.h @@ -43,7 +43,6 @@ class FifoMergedSecurityMatcher { // hide copy constr FifoMergedSecurityMatcher(const FifoMergedSecurityMatcher &); - FifoImpliedSecurityMatcher *_best_implied_matcher(Side); // the order's params (quantity, and iceberg_quantity if applicable) // will be appropriately adjusted according to the number of fills. void _fill( diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOneof.cc b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOneof.cc new file mode 100644 index 000000000..72532addb --- /dev/null +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOneof.cc @@ -0,0 +1,31 @@ +#include "MatcherOneof.h" + +namespace bts::spartan::mdp3 { + +inline MatcherOneof::Case MatcherOneof::matcher_case() const { + if (_direct != nullptr) + return Case::Direct; + if (_implied != nullptr) + return Case::Implied; + return Case::None; +} + +inline const char *MatcherOneof::Case_Name(const MatcherOneof::Case &c) { + switch (c) { + case MatcherOneof::Case::Direct: + return "Direct"; + case MatcherOneof::Case::Implied: + return "Implied"; + case MatcherOneof::Case::None: + return "None"; + default: + return "Undefined"; + } +} + +inline std::ostream &operator<<(std::ostream &o, const MatcherOneof::Case &c) { + o << MatcherOneof::Case_Name(c); + return o; +} + +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOneof.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOneof.cpp new file mode 100644 index 000000000..605120ea5 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOneof.cpp @@ -0,0 +1,23 @@ +#include "MatcherOneof.h" + +namespace bts::spartan::mdp3 { + +MatcherOneof::MatcherOneof() : _direct(nullptr), _implied(nullptr) {} + +MatcherOneof::MatcherOneof(FifoSecurityMatcher *direct) + : _direct(direct), _implied(nullptr) {} + +MatcherOneof::MatcherOneof(FifoImpliedSecurityMatcher *implied) + : _direct(nullptr), _implied(implied) {} + +FifoSecurityMatcher &MatcherOneof::direct() { + bts_assert(_direct != nullptr); + return *_direct; +} + +FifoImpliedSecurityMatcher &MatcherOneof::implied() { + bts_assert(_implied != nullptr); + return *_implied; +} + +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOneof.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOneof.h new file mode 100644 index 000000000..aa2107853 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOneof.h @@ -0,0 +1,30 @@ +#pragma once +#include "../FifoImpliedSecurityMatcher.h" +#include "../FifoSecurityMatcher.h" +#include + +namespace bts::spartan::mdp3 { + +struct MatcherOneof { + MatcherOneof(); + MatcherOneof(FifoSecurityMatcher *); + MatcherOneof(FifoImpliedSecurityMatcher *); + + enum Case { None, Direct, Implied }; + + static const char *Case_Name(const MatcherOneof::Case &); + + Case matcher_case() const; + FifoSecurityMatcher &direct(); + FifoImpliedSecurityMatcher &implied(); + + private: + FifoSecurityMatcher *_direct; + FifoImpliedSecurityMatcher *_implied; +}; + +std::ostream &operator<<(std::ostream &, const MatcherOneof::Case &); + +} // namespace bts::spartan::mdp3 + +#include "MatcherOneof.cc" diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatchingSession.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatchingSession.cpp new file mode 100644 index 000000000..e654244b0 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatchingSession.cpp @@ -0,0 +1,100 @@ +#include "MatchingSession.h" +#include "../FifoImpliedSecurityMatcher.h" +#include "../FifoSecurityMatcher.h" +#include "MatcherOneof.h" +#include "server/sparkd/cmds/spartan/domain/Price.h" +#include "server/sparkd/cmds/spartan/domain/Side.h" +#include + +namespace bts::spartan::mdp3 { + +struct MatchingSession::PImpl { + FifoSecurityMatcher &direct; + std::vector &implied; + Side side; + Price limit_price{}; + Price last_used_price{}; + bool direct_used_at_price = false; + std::set implied_used_at_price{}; +}; + +MatchingSession::MatchingSession( + FifoSecurityMatcher &direct, + std::vector &implied, + Side side) + : MatchingSession(direct, implied, side, {}) {} + +MatchingSession::MatchingSession( + FifoSecurityMatcher &direct, + std::vector &implied, + Side aggressor_side, + Price aggressor_price) + : _me(new MatchingSession::PImpl{ + direct, implied, aggressor_side, aggressor_price}) {} + +MatchingSession::~MatchingSession() {} + +MatcherOneof MatchingSession::get_next() { + Price best_price = _me->limit_price; + // Candidates should be populated with the indices of the implied matcher(s) + // with the best price. They are stored by index because it is the simplest + // way to distinguish them from one another. The identity of each implied + // matcher is otherwise represented by its legs (a combination of pairs of + // security id and quantity). + std::set candidates; + for (uint32_t i = 0; i < _me->implied.size(); i++) { + auto top = _me->implied[i]->top_of_book(_me->side); + if (top.quantity > 0 && !best_price.is_better(_me->side, top.price)) { + if (top.price.is_better(_me->side, best_price)) { + candidates.clear(); + best_price = top.price; + } + candidates.insert(i); + } + } + + auto [dprice, dqty] = _me->direct.book_level(_me->side, 0); + if (dqty == 0 && candidates.empty()) + return MatcherOneof(); + _me->direct_used_at_price &= dprice == _me->last_used_price; + + auto get_imkt = [&]() -> std::tuple { + if (candidates.empty()) + return {nullptr, 0}; + auto it = candidates.begin(); + for (; it != candidates.end(); it++) { + if (!_me->implied_used_at_price.count(*it)) + break; + } + if (it == candidates.end()) + it = candidates.begin(); + return {_me->implied[*it], *it}; + }; + + if (dprice.is_better_or_equal(_me->side, best_price)) { + if (dprice == best_price) { + if (_me->direct_used_at_price) { + auto [imkt, imkt_index] = get_imkt(); + if (imkt != nullptr) { + _me->implied_used_at_price.insert(imkt_index); + return MatcherOneof(imkt); + } + } + } else { + _me->implied_used_at_price.clear(); + } + _me->direct_used_at_price = true; + _me->last_used_price = dprice; + return MatcherOneof(&_me->direct); + } + _me->direct_used_at_price = false; + if (best_price != _me->last_used_price) { + _me->implied_used_at_price.clear(); + _me->last_used_price = best_price; + } + auto [imkt, imkt_index] = get_imkt(); + _me->implied_used_at_price.insert(imkt_index); + return MatcherOneof(imkt); +} + +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatchingSession.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatchingSession.h new file mode 100644 index 000000000..c14629bcc --- /dev/null +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatchingSession.h @@ -0,0 +1,48 @@ +#pragma once +#include "server/sparkd/cmds/spartan/domain/Side.h" +#include +#include + +namespace bts::spartan::mdp3 { + +class FifoSecurityMatcher; +class FifoImpliedSecurityMatcher; +class Price; +struct MatcherOneof; + +/** + * Models matching logic for an aggressor order on entry + */ +class MatchingSession { + public: + MatchingSession( + FifoSecurityMatcher &direct_matcher, + std::vector &implied_matchers, + Side passive_side); + + MatchingSession( + FifoSecurityMatcher &direct_matcher, + std::vector &implied_matchers, + Side aggressor_side, + Price aggressor_price); + + ~MatchingSession(); + + /** + * Get the market against which the aggressor should be filled + * + * @note The matching session should be used to fill the aggressor through a + * single iterative step: Call get_next to get the target matcher market, + * and fill the aggressor at the top price. Repeat that step until the + * order is either filled to completion, or no longer crosses any market. + * When the order no longer crosses market, the resultant MatcherOneof's + * matcher_case will be 'MatcherOneof::Case::None.' + */ + MatcherOneof get_next(); + + private: + struct PImpl; + std::unique_ptr _me; +}; + +} // namespace bts::spartan::mdp3