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 e7db7038e..991211ede 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoMergedSecurityMatcher.cpp @@ -1,12 +1,13 @@ #include "FifoMergedSecurityMatcher.h" #include "FifoImpliedSecurityMatcher.h" #include "FifoSecurityMatcher.h" -#include "fill_factory.h" #include "common/MatcherOneof.h" #include "common/MatchingSession.h" +#include "fill_factory.h" #include "lib/md/IntPrice.h" #include "lib/orderman/orderman_utils/Formatter.h" #include "lib/orderman/orderman_utils/PriceComp.h" +#include "market_data/common/OrderModification.h" #include "market_data/utils/book_utils.h" #include "market_data/utils/order_utils.h" #include "utils/ActionSummarizerMacros.h" @@ -154,14 +155,14 @@ void FifoMergedSecurityMatcher::on_order_change( case OrderChange::ChangeType::updated: { // Iceberg quantity should be null (it is only non-null when it is added). bts_assert(!ch.order.iceberg_quantity.has_value()); - auto prev_ord_tup = matcher()->find_order(ch.order.key); - auto prev_ord = std::get<1>(prev_ord_tup); + auto [prev_price_lev, prev_ord] = matcher()->find_order(ch.order.key); if (prev_ord == nullptr) { bts_assert_msg( _me->ignore_ops_on_deleted_orders, "unable to process order-update event: failed to find order."); break; } + bts_assert(prev_price_lev != nullptr); auto new_ch = ch; if (new_ch.order.quantity == 0) { // TODO: Unify price-change-only code/terminology. For now, the @@ -175,18 +176,29 @@ void FifoMergedSecurityMatcher::on_order_change( // get the correct partial and total fill quantities. if (prev_ord->ib.has_value()) new_ch.order.iceberg_quantity = prev_ord->ib; - auto orig_total = get_total_qty(new_ch.order); + std::optional new_qty = std::nullopt; + if (ch.order.quantity != 0) + new_qty = ch.order.quantity; _fill(new_ch.order, effects, fills, false); - auto total = get_total_qty(new_ch.order); if (new_ch.order.quantity.value() == 0) { matcher()->on_order_completed(ch.order.key, event_id); } else { - if (new_ch.order.iceberg_quantity.has_value() && orig_total == total) { - // The order was not filled at all. Let the direct security matcher - // determine what affects (if any) the update has on the iceberg quantity. - new_ch.order.iceberg_quantity = std::nullopt; + auto &new_ord = new_ch.order; + if (new_ord.iceberg_quantity.has_value() && new_qty.has_value()) { + if (new_ord.quantity > prev_ord->ib.value().total) + new_ord.quantity = prev_ord->ib.value().total; + new_ord.iceberg_quantity.value().show = std::min( + new_ord.iceberg_quantity.value().total, new_qty.value()); } - matcher()->on_order_change(new_ch, effects, event_id); + MDP3Order old_ord; + old_ord.key = prev_ord->key; + old_ord.security = security_id(); + old_ord.side = prev_ord->side; + old_ord.price = prev_ord->price; + old_ord.quantity = prev_ord->quantity; + old_ord.iceberg_quantity = prev_ord->ib; + auto mod = OrderModification::deduce(old_ord, new_ch.order); + matcher()->update_order(new_ch.order.key, mod, event_id); } } else { // As far as we know, the only type of update that could trigger fills 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 621c07276..2b1812da1 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.cpp @@ -1,6 +1,7 @@ #include "FifoSecurityMatcher.h" -#include "back_testing/matcher/iceberg_helper.h" #include "domain/SecuritySnapshot.h" +#include "iceberg_helper.h" +#include "market_data/common/OrderModification.h" #include "market_data/utils/order_utils.h" #include @@ -138,6 +139,45 @@ void FifoSecurityMatcher::_on_new_order( } } +void FifoSecurityMatcher::update_order(OrderKey key, const OrderModification &mod, AlgoEventID event_id) { + if (!mod.has_changes()) + return; + auto [curr_price_lev, curr_ord] = _find_order(key); + bts_assert(curr_ord != nullptr); + bts_assert(curr_price_lev != nullptr); + if (mod.iceberg_quantity.has_changes() && !curr_ord->ib.has_value()) { + // Need both show and total qty to initialize iceberg quantity + bts_assert( + mod.iceberg_quantity.show.has_value() && + mod.iceberg_quantity.total.has_value()); + curr_ord->ib = IcebergQuantity(); + } + + MDP3Order upd_ord; + upd_ord.key = key; + upd_ord.security = _sec; + upd_ord.side = curr_price_lev->side(); + upd_ord.quantity = + mod.quantity.has_value() ? mod.quantity.value() : curr_ord->quantity; + upd_ord.iceberg_quantity = curr_ord->ib; + + if (mod.iceberg_quantity.show.has_value()) + upd_ord.iceberg_quantity.value().show = mod.iceberg_quantity.show.value(); + if (mod.iceberg_quantity.total.has_value()) + upd_ord.iceberg_quantity.value().total = + mod.iceberg_quantity.total.value(); + + if (mod.price.has_value() && mod.price.value() != curr_ord->price) { + curr_price_lev->remove_from_price_level(key); + upd_ord.price = mod.price.value(); + _get_or_add_price_level(curr_price_lev->side(), mod.price.value()) + ->on_new_same_side_order(upd_ord, event_id); + } else { + curr_price_lev->on_order_update(upd_ord, curr_ord, event_id); + } + _invalidate(); +} + void FifoSecurityMatcher::_on_order_update( const MDP3Order &ord, bool price_only, std::vector *res, AlgoEventID event_id) { _log_trace_order(ord, "FifoSecurityMatcher::_on_order_update begin", event_id); @@ -357,6 +397,10 @@ void FifoSecurityMatcher::on_order_change( _on_order_remove(o.order.key, res, event_id); break; } + _invalidate(); +} + +void FifoSecurityMatcher::_invalidate() { _invalidate_mbp(); if (_running_op_count++ > _clean_up_op_threshold) { _running_op_count = 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 36dd4efc8..ffae314fb 100644 --- a/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.h +++ b/src/server/sparkd/cmds/spartan/back_testing/matcher/FifoSecurityMatcher.h @@ -12,6 +12,8 @@ namespace bts::spartan::mdp3 { +struct OrderModification; + class FifoSecurityMatcher : public SecurityMatcher { public: struct FifoSecurityMatcherArgs { @@ -73,6 +75,7 @@ class FifoSecurityMatcher : public SecurityMatcher { _bids.invalidate(); _asks.invalidate(); } + void _invalidate(); // the complexity is O(lgN+lgM == lg(M*N)), since first it first searchess // for the order, from the order it gets the price, side and searches // for price level in the book (flat_set) @@ -138,6 +141,7 @@ class FifoSecurityMatcher : public SecurityMatcher { AlgoEventID = no_algo_event()) override; void on_order_completed(OrderKey key, AlgoEventID = no_algo_event()); + void update_order(OrderKey, const OrderModification&, AlgoEventID = no_algo_event()); // For testing std::optional has_same_orders(Orders const &); size_t total_n_orders(Side) const; diff --git a/src/server/sparkd/cmds/spartan/market_data/common/IcebergQuantityModification.cc b/src/server/sparkd/cmds/spartan/market_data/common/IcebergQuantityModification.cc new file mode 100644 index 000000000..151e1b78e --- /dev/null +++ b/src/server/sparkd/cmds/spartan/market_data/common/IcebergQuantityModification.cc @@ -0,0 +1,21 @@ +#include "IcebergQuantityModification.h" + +namespace bts::spartan::mdp3 { + +inline bool IcebergQuantityModification::has_changes() const { + return show.has_value() || total.has_value(); +} + +inline std::ostream &operator<<( + std::ostream &o, const IcebergQuantityModification &m) { + if (m.show.has_value()) { + o << "show: " << m.show.value(); + if (m.total.has_value()) + o << ", total: " << m.total.value(); + } else if (m.total.has_value()) { + o << "total: " << m.total.value(); + } + return o; +} + +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/market_data/common/IcebergQuantityModification.cpp b/src/server/sparkd/cmds/spartan/market_data/common/IcebergQuantityModification.cpp new file mode 100644 index 000000000..5bd291989 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/market_data/common/IcebergQuantityModification.cpp @@ -0,0 +1,24 @@ +#include "IcebergQuantityModification.h" +#include "server/sparkd/cmds/spartan/domain/IcebergQuantity.h" + +namespace bts::spartan::mdp3 { + +IcebergQuantityModification IcebergQuantityModification::deduce( + const IcebergQuantity &old_iceberg, + const IcebergQuantity &new_iceberg) { + IcebergQuantityModification mod; + if (new_iceberg.show != old_iceberg.show) + mod.show = new_iceberg.show; + if (new_iceberg.total != old_iceberg.total) + mod.total = new_iceberg.total; + return mod; +} + +void IcebergQuantityModification::apply(IcebergQuantity &target) const { + if (show.has_value()) + target.show = show.value(); + if (total.has_value()) + target.total = total.value(); +} + +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/market_data/common/IcebergQuantityModification.h b/src/server/sparkd/cmds/spartan/market_data/common/IcebergQuantityModification.h new file mode 100644 index 000000000..f30d1da11 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/market_data/common/IcebergQuantityModification.h @@ -0,0 +1,38 @@ +#pragma once +#include "server/sparkd/cmds/spartan/domain/Quantity.h" +#include +#include + +namespace bts::spartan::mdp3 { + +struct IcebergQuantity; + +struct IcebergQuantityModification { + /** + * Initialize an IcebergQuantityModification with no changes + */ + static IcebergQuantityModification empty(); + /** + * Initialize an IcebergQuantityModification that represents the transition + * between two states + */ + static IcebergQuantityModification deduce( + const IcebergQuantity &old_iceberg, const IcebergQuantity &new_iceberg); + /** + * Determine whether or not the modification has any changes + */ + bool has_changes() const; + /** + * Apply modifications to a particular iceberg quantity + */ + void apply(IcebergQuantity &) const; + + std::optional show; + std::optional total; +}; + +std::ostream &operator<<(std::ostream &, const IcebergQuantityModification &); + +} // namespace bts::spartan::mdp3 + +#include "IcebergQuantityModification.cc" diff --git a/src/server/sparkd/cmds/spartan/market_data/common/OrderModification.cc b/src/server/sparkd/cmds/spartan/market_data/common/OrderModification.cc new file mode 100644 index 000000000..00064716d --- /dev/null +++ b/src/server/sparkd/cmds/spartan/market_data/common/OrderModification.cc @@ -0,0 +1,24 @@ +#include "OrderModification.h" + +namespace bts::spartan::mdp3 { + +inline OrderModification OrderModification::empty() { + return OrderModification(); +} + +inline bool OrderModification::has_changes() const { + return price.has_value() || quantity.has_value(); +} + +inline std::ostream &operator<<(std::ostream &o, const OrderModification &m) { + if (m.price.has_value()) { + o << "price: " << m.price.value(); + if (m.quantity.has_value()) + o << ", quantity: " << m.quantity.value(); + } else if (m.quantity.has_value()) { + o << "quantity: " << m.quantity.value(); + } + return o; +} + +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/market_data/common/OrderModification.cpp b/src/server/sparkd/cmds/spartan/market_data/common/OrderModification.cpp new file mode 100644 index 000000000..e1813d72f --- /dev/null +++ b/src/server/sparkd/cmds/spartan/market_data/common/OrderModification.cpp @@ -0,0 +1,36 @@ +#include "OrderModification.h" +#include "mdp3_common.h" + +namespace bts::spartan::mdp3 { + +OrderModification OrderModification::deduce( + const MDP3Order &old_order, const MDP3Order &new_order) { + OrderModification mod; + if (new_order.quantity != old_order.quantity) + mod.quantity = new_order.quantity; + if (new_order.price != old_order.price) + mod.price = new_order.price; + if (new_order.iceberg_quantity.has_value()) { + if (old_order.iceberg_quantity.has_value()) { + if (new_order.iceberg_quantity.value().show != + old_order.iceberg_quantity.value().show) + mod.iceberg_quantity.show = new_order.iceberg_quantity.value().show; + if (new_order.iceberg_quantity.value().total != + old_order.iceberg_quantity.value().total) + mod.iceberg_quantity.total = new_order.iceberg_quantity.value().total; + } else { + mod.iceberg_quantity.show = new_order.iceberg_quantity.value().show; + mod.iceberg_quantity.total = new_order.iceberg_quantity.value().total; + } + } + return mod; +} + +void OrderModification::apply(MDP3Order &target) const { + if (price.has_value()) + target.price = price.value(); + if (quantity.has_value()) + target.quantity = quantity.value(); +} + +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/market_data/common/OrderModification.h b/src/server/sparkd/cmds/spartan/market_data/common/OrderModification.h new file mode 100644 index 000000000..2910a71a8 --- /dev/null +++ b/src/server/sparkd/cmds/spartan/market_data/common/OrderModification.h @@ -0,0 +1,41 @@ +#pragma once +#include "IcebergQuantityModification.h" +#include "server/sparkd/cmds/spartan/domain/Price.h" +#include "server/sparkd/cmds/spartan/domain/Quantity.h" +#include +#include + +namespace bts::spartan::mdp3 { + +struct Order; + +struct OrderModification { + /** + * Initialize an OrderModification with no changes + */ + static OrderModification empty(); + /** + * Initialize an OrderModification that represents the transition between two + * states + */ + static OrderModification deduce( + const Order &old_order, const Order &new_order); + /** + * Determine whether or not the modification has any changes + */ + bool has_changes() const; + /** + * Apply modifications to a particular order + */ + void apply(Order &) const; + + std::optional price; + std::optional quantity; + IcebergQuantityModification iceberg_quantity; +}; + +std::ostream &operator<<(std::ostream &, const OrderModification &); + +} // namespace bts::spartan::mdp3 + +#include "OrderModification.cc"