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 f36d253ca..29ca222df 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.cpp @@ -69,8 +69,8 @@ FifoPriceLevelMatcher::_one_pass_cross_order( for (MatcherOrder *op : _orders) { if (in_this_level(op)) { auto &o = *op; - auto [o_rem, cross_rem, filled] = o.cross(cross_ord_qty.value()); - cross_ord_qty = Quantity(cross_rem); + auto cross_res = o.cross(cross_ord_qty); + cross_ord_qty = cross_res.aggressor_rem(); if (res != nullptr) { SimOrderUpdate passive_upd = { o.key, @@ -79,7 +79,7 @@ FifoPriceLevelMatcher::_one_pass_cross_order( : SimOrderUpdate::Status::FILLED, _sec, _side, - Quantity(filled), + cross_res.filled(), _price, SimOrderUpdate::FillType::PASSIVE}; MatcherUtils::validate_outgoing_update(passive_upd); @@ -92,7 +92,7 @@ FifoPriceLevelMatcher::_one_pass_cross_order( : SimOrderUpdate::Status::FILLED, _sec, other_side(_side), - Quantity(filled), + cross_res.filled(), _price, SimOrderUpdate::FillType::AGGRESSIVE}; MatcherUtils::validate_outgoing_update(agg_upd); @@ -101,12 +101,11 @@ FifoPriceLevelMatcher::_one_pass_cross_order( } 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:{})", - o.key, o_rem, cross_ord_key, cross_rem); - - if (cross_rem > 0) - bts_assert(o_rem == 0); // visible qty should be completely crossed + "FifoPriceLevelMatcher::_one_pass_cross_order {} crossed by " + "{}: {}", + o.key, cross_ord_key, cross_res); + if (cross_res.aggressor_rem() > 0) + bts_assert(cross_res.passive_rem() == 0); // visible qty should be completely crossed else break; // crossed order is completely filled } 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 430c74b2c..112d15b81 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.h +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoPriceLevelMatcher.h @@ -5,6 +5,7 @@ #include "MatchingCommon.h" #include "back_testing/common/Trace.h" #include "back_testing/sim/sim_common.h" +#include "common/MatcherOrder.h" #include "domain/SecuritySnapshot.h" namespace bts::spartan::mdp3 { 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 2b1812da1..a4420aa37 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp @@ -249,23 +249,19 @@ void FifoSecurityMatcher::_on_order_not_affecting_books( // since this does not affect the market all we need to do // is deal with the cross auto &opp_book = _book(other_side(ord.side)); - uint32_t new_ord_rem = total_quantity(ord).value(); + auto new_ord_rem = total_quantity(ord); for (const FifoPriceLevelMatcher &pl : opp_book) { if (!pl.populated()) continue; if (_is_cross(ord, pl)) { - uint32_t level_qty = - static_cast(pl.total_quantity().value()); - auto [new_level_qty_ret, new_ord_rem_ret] = - MatcherOrder::compute_cross(level_qty, new_ord_rem); - auto fill_size = level_qty - new_level_qty_ret; - new_ord_rem = new_ord_rem_ret; + auto cross_res = MatcherOrder::compute_cross(pl.total_quantity(), new_ord_rem); + new_ord_rem = cross_res.aggressor_rem(); if (res != nullptr) res->push_back(SimOrderUpdate{ ord.key, new_ord_rem > 0 ? SimOrderUpdate::Status::PARTIALLY_FILLED : SimOrderUpdate::Status::FILLED, - ord.security, ord.side, Quantity(fill_size), pl.price(), + ord.security, ord.side, cross_res.filled(), pl.price(), SimOrderUpdate::FillType::AGGRESSIVE}); } else break; diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/MatchingCommon.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/MatchingCommon.cpp index 0b578e898..228b69d2b 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/MatchingCommon.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/MatchingCommon.cpp @@ -1,26 +1,4 @@ #include "MatchingCommon.h" namespace bts::spartan::mdp3 { -std::tuple -MatcherOrder::cross(uint32_t cross_qty) { - auto [r, c] = compute_cross(quantity.value(), cross_qty); - bts_assert(quantity.value() - r == cross_qty - c); - auto filled = cross_qty - c; - quantity = Quantity(r); - if (ib.has_value()) { - ib.value().total -= Quantity(filled); - is_active = ib.value().total.value() > 0; - } else - is_active = quantity.value() > 0; - return std::make_tuple(r, c, filled); -} - -void MatcherOrder::reload_ib() { - if (ready_to_load_ib()) { - is_active = ib.value().total.value() > 0; - if (quantity.value() == 0) - quantity = std::min(ib.value().show, ib.value().total); - is_active = quantity.value() > 0; - } -} } // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/MatchingCommon.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/MatchingCommon.h index 3131a67af..2b4d3799b 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/MatchingCommon.h +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/MatchingCommon.h @@ -38,55 +38,4 @@ class MatcherUtils { } }; -struct MatcherOrder { - static std::tuple - compute_cross(uint32_t resting_qty, uint32_t cross_qty) { - if (cross_qty >= resting_qty) { - cross_qty -= resting_qty; - resting_qty = 0; - } else { - bts_assert(resting_qty > cross_qty); - resting_qty -= cross_qty; - cross_qty = 0; - } - return std::make_tuple(resting_qty, cross_qty); - } - OrderKey key; - Side side; - Price price; - Quantity quantity; - uint32_t priority = 0; - std::optional ib; - bool is_active = true; - MatcherOrder( - OrderKey key, - Side side, - Price price, - Quantity qty, // working qty out there - uint32_t priority, - std::optional ib = {}) - : key(key), side(side), price(price), quantity(qty), priority(priority), - ib(ib) {} - // returns the remainder of this order, remainder of opp order - // (ie the new qty NOT the filled qty, for that subtract from - // original) - std::tuple cross(uint32_t cross_qty); - Quantity total_qty() const { - return ib.has_value() ? ib.value().total : quantity; - } - Quantity working_qty() const { return quantity; } - bool ready_to_load_ib() const { - return ib.has_value() && quantity.value() == 0 && - ib.value().total.value() > 0; - } - void reload_ib(); -}; - -inline std::ostream &operator<<(std::ostream &out, const MatcherOrder &ord) { - out << "[" << ord.key << "] " << ord.side << " " << ord.quantity << " @ " - << ord.price << " (ice " << ord.ib << " active " - << (ord.is_active ? "Y" : "N") << ")"; - return out; -} - } // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOrder.cc b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOrder.cc new file mode 100644 index 000000000..598a57180 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOrder.cc @@ -0,0 +1,24 @@ +#include "MatcherOrder.h" + +namespace bts::spartan::mdp3 { + +constexpr bool MatcherOrder::ready_to_load_ib() const { + return ib.has_value() && quantity.value() == 0 && + ib.value().total.value() > 0; +} + +inline std::ostream &operator<<(std::ostream &out, const MatcherOrder &ord) { + out << "[" << ord.key << "] " << ord.side << " " << ord.quantity << " @ " + << ord.price << " (ice " << ord.ib << " active " + << (ord.is_active ? "Y" : "N") << ")"; + return out; +} + +inline std::ostream &operator<<( + std::ostream &out, const MatcherOrder::Cross &cross) { + out << "filled:" << cross.filled() << ", pass rem: " << cross.passive_rem() + << "aggr rem: " << cross.aggressor_rem(); + return out; +} + +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOrder.cpp b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOrder.cpp new file mode 100644 index 000000000..af5dd1e7f --- /dev/null +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOrder.cpp @@ -0,0 +1,69 @@ +#include "MatcherOrder.h" + +namespace bts::spartan::mdp3 { + +MatcherOrder::Cross::Cross() : _passive_rem(0), _aggressor_rem(0), _filled(0) {} + +MatcherOrder::Cross::Cross( + Quantity passive_rem, Quantity aggressor_rem, Quantity filled) + : _passive_rem(passive_rem), _aggressor_rem(aggressor_rem), + _filled(filled) {} + +Quantity MatcherOrder::Cross::passive_rem() const { return _passive_rem; } +Quantity MatcherOrder::Cross::aggressor_rem() const { return _aggressor_rem; } +Quantity MatcherOrder::Cross::filled() const { return _filled; } + +MatcherOrder::MatcherOrder( + OrderKey key, + Side side, + Price price, + Quantity qty, // working qty out there + uint32_t priority, + std::optional ib) + : key(key), side(side), price(price), quantity(qty), priority(priority), + ib(ib) {} + +MatcherOrder::Cross MatcherOrder::compute_cross( + Quantity resting_qty, Quantity cross_qty) { + auto orig_cross_qty = cross_qty; + if (cross_qty >= resting_qty) { + cross_qty -= resting_qty; + resting_qty.value() = 0; + } else { + bts_assert(resting_qty > cross_qty); + resting_qty -= cross_qty; + cross_qty.value() = 0; + } + return Cross(resting_qty, cross_qty, orig_cross_qty - cross_qty); +} + +Quantity MatcherOrder::total_qty() const { + return ib.has_value() ? ib.value().total : quantity; +} + +Quantity MatcherOrder::working_qty() const { return quantity; } + +MatcherOrder::Cross MatcherOrder::cross(Quantity cross_qty) { + auto res = compute_cross(quantity, cross_qty); + bts_assert(quantity.value() - res.passive_rem() == cross_qty - res.aggressor_rem()); + bts_assert(quantity.value() - res.passive_rem() == res.filled()); + auto filled = cross_qty - res.aggressor_rem(); + quantity = res.passive_rem(); + if (ib.has_value()) { + ib.value().total -= Quantity(filled); + is_active = ib.value().total.value() > 0; + } else + is_active = quantity.value() > 0; + return res; +} + +void MatcherOrder::reload_ib() { + if (ready_to_load_ib()) { + is_active = ib.value().total.value() > 0; + if (quantity.value() == 0) + quantity = std::min(ib.value().show, ib.value().total); + is_active = quantity.value() > 0; + } +} + +} // namespace bts::spartan::mdp3 \ No newline at end of file diff --git a/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOrder.h b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOrder.h new file mode 100644 index 000000000..933e2cc0f --- /dev/null +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/common/MatcherOrder.h @@ -0,0 +1,68 @@ +#pragma once +#include "server/sparkd/cmds/spartan/domain/IcebergQuantity.h" +#include "server/sparkd/cmds/spartan/domain/Price.h" +#include "server/sparkd/cmds/spartan/domain/Quantity.h" +#include "server/sparkd/cmds/spartan/domain/Side.h" +#include "server/sparkd/cmds/spartan/domain/order/OrderKey.h" +#include +#include +#include + +namespace bts::spartan::mdp3 { + +struct MatcherOrder { + struct Cross { + Cross(); + Cross(Quantity passive_rem, Quantity aggressor_rem, Quantity filled); + + Quantity passive_rem() const; + Quantity aggressor_rem() const; + Quantity filled() const; + + private: + Quantity _passive_rem; + Quantity _aggressor_rem; + Quantity _filled; + }; + + MatcherOrder( + OrderKey key, + Side side, + Price price, + Quantity qty, // working qty out there + std::uint32_t priority, + std::optional ib = {}); + + /** + * Computes the result of a hypothetical cross + * + * @returns The result of the cross + */ + static Cross compute_cross(Quantity resting_qty, Quantity cross_qty); + + /** + * Fills this order with the specified cross quantity + * + * @returns The result of the cross + */ + Cross cross(Quantity cross_qty); + Quantity total_qty() const; + Quantity working_qty() const; + constexpr bool ready_to_load_ib() const; + void reload_ib(); + + OrderKey key; + Side side; + Price price; + Quantity quantity; + uint32_t priority = 0; + std::optional ib; + bool is_active = true; +}; + +std::ostream &operator<<(std::ostream &, const MatcherOrder &); +std::ostream &operator<<(std::ostream &, const MatcherOrder::Cross &); + +} // namespace bts::spartan::mdp3 + +#include "MatcherOrder.cc" diff --git a/src/server/sparkd/cmds/spartan/market_data/utils/order_utils.cpp b/src/server/sparkd/cmds/spartan/market_data/utils/order_utils.cpp index 3562fd734..636cb54be 100644 --- a/src/server/sparkd/cmds/spartan/market_data/utils/order_utils.cpp +++ b/src/server/sparkd/cmds/spartan/market_data/utils/order_utils.cpp @@ -1,5 +1,5 @@ #include "order_utils.h" -#include "server/sparkd/cmds/spartan/back_testing/matcher/MatchingCommon.h" +#include "back_testing/matcher/common/MatcherOrder.h" namespace bts::spartan::mdp3 { MDP3Order to_mdp3_order(const MatcherOrder &mo) {