From 0e552bbcadc67c621f1a0b7640621cb8c3887ece Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Tue, 22 Sep 2020 11:15:08 -0400 Subject: [PATCH] Improved order deduction. Improved iceberg deduction. --- .../md/mdp3/common/InstrumentSnapshot.cpp | 62 +++++++++++++------ .../cmds/spartan/md/mdp3/utils/mdp3_utils.cpp | 29 +++++---- 2 files changed, 59 insertions(+), 32 deletions(-) diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp index 969cd5969..cd0b28fc5 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp @@ -350,33 +350,54 @@ OrderChanges InstrumentSnapshot::deduced_order_changes() const { trading_status() != ::mdp3::SecurityTradingStatus::ReadyToTrade) return {}; else { - struct AggFillRecord { + struct AggOrderRec { MDP3OrderKey ok; Fills fills; }; - std::vector aggressor_fills; - auto get_agg_order = - [&aggressor_fills](MDP3OrderKey ok) -> AggFillRecord & { - for (auto &agg_rec : aggressor_fills) + std::vector agg_orders; + OrderChanges deduced_orders; + + // Return the AggOrderRec for a given order + auto get_agg_order = [&agg_orders](MDP3OrderKey ok) -> AggOrderRec & { + for (auto &agg_rec : agg_orders) if (agg_rec.ok == ok) return agg_rec; - aggressor_fills.push_back(AggFillRecord{ok, {}}); - return aggressor_fills.back(); + agg_orders.push_back(AggOrderRec{ok, {}}); + return agg_orders.back(); }; - OrderChanges result; + // Group all aggressor fills for a given order together in the + // aggressor_fills container for (auto const &f : fills()) if (f.aggressor == MDP3Fill::THIS_BUY || f.aggressor == MDP3Fill::THIS_SELL) get_agg_order(f.key).fills.push_back(f); - for (auto const &agg_rec : aggressor_fills) { - auto &aggressor_fills = agg_rec.fills; + // In some cases its possible for an order to trade both + // aggressively and passively. This can happen if the initially + // aggressive order became the top of the book, but then + // immediately crossed with a stop order that was triggered by + // the first round of trades. To account for that possibility we + // have to do a second pass over the fills to add any non + // aggressive fills we might see for the initially aggressive + // orders. + for (auto &agg_order : agg_orders) { + for (auto const &f : fills()) + if (agg_order.ok == f.key && + f.aggressor != MDP3Fill::THIS_BUY && + f.aggressor != MDP3Fill::THIS_SELL ) + agg_order.fills.push_back(f); + } + + // For each aggressor order, add up its fills and calculate a + // price that would explain all the observed fills + for (auto const &agg_order : agg_orders) { + auto &aggressor_fills = agg_order.fills; bts_assert(aggressor_fills.size()); - OrderChange &deduced_order = result.emplace_back(); + OrderChange &deduced_order = deduced_orders.emplace_back(); deduced_order.previous_order = {}; - deduced_order.order.key = agg_rec.ok; + deduced_order.order.key = agg_order.ok; deduced_order.order.security = security_id(); deduced_order.order.side = aggressor_fills[0].aggressor == MDP3Fill::THIS_BUY @@ -399,7 +420,7 @@ OrderChanges InstrumentSnapshot::deduced_order_changes() const { // fills in this event. We now examine the order book that CME // communicated and adjust these deduced orders so that they are // consistent with it as well. - for (auto &deduced_order : result) { + for (auto &deduced_order : deduced_orders) { deduced_order.change = OrderChange::added; for (auto &o : order_changes()) if (o.order.key == deduced_order.order.key) { @@ -417,7 +438,8 @@ OrderChanges InstrumentSnapshot::deduced_order_changes() const { } } - // Now we make some adjustment to account for special circunstances + // Now we make some final adjustment to account for special + // circunstances. for (auto &o : order_changes()) if (o.change == OrderChange::added) { // Its possible that in some cases for a new order to be added @@ -425,22 +447,22 @@ OrderChanges InstrumentSnapshot::deduced_order_changes() const { // multiple stop orders are activated simultaneously and some of // them trade and some do not. auto it = std::find_if( - result.begin(), result.end(), + deduced_orders.begin(), deduced_orders.end(), [k = o.order.key](auto const &d) { return d.order.key == k; }); - if (it == result.end()) - result.push_back(o); + if (it == deduced_orders.end()) + deduced_orders.push_back(o); } else if (o.change == OrderChange::removed) { // It is also possible for an order to be removed even if // it had no fills because of self match prevention. auto it = std::find_if( - fills().begin(),fills().end(), + fills().begin(), fills().end(), [k = o.order.key](auto const &f) { return f.key == k; }); if (it == fills().end()) // Removes would happen before anything else - result.insert(result.begin(),o); + deduced_orders.insert(deduced_orders.begin(), o); } - return result; + return deduced_orders; } } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.cpp index d5fee7ba2..740989535 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.cpp @@ -370,13 +370,11 @@ struct ObsWrapper : mdp3::IChannelObserver { std::stringstream str; mdp3::cme_ts_to_time_str(ev.transact_time.value(), &str); std::cout << "Packet time: " << str.str() - << " Event ID: " << ev.event_id - << " (" + << " Event ID: " << ev.event_id << " (" << (ev.first_packet_sn - previous_packet) << " pkt/sec, " << (ev.first_packet_send_time.value() - previous_send_ts) / 1000000000 - << "x speedup)" - << std::endl; + << "x speedup)" << std::endl; previous_packet = ev.first_packet_sn; previous_send_ts = ev.first_packet_send_time.value(); } @@ -622,8 +620,7 @@ int to_csv(std::string const &file, std::string const &out_dir) { auto ice = find_icebergs(file); for (auto &i : ice) icebergs << i.first.value() << "," << i.second.estimated_quantity.value() - << "," << i.second.filled - << std::endl; + << "," << i.second.filled << std::endl; return 0; } @@ -670,7 +667,7 @@ find_icebergs(std::string const &pcap) { for (auto const &o : s->order_changes()) { changes_in_event[o.order.key] = o; if (o.change == OrderChange::removed) { - for (auto const &f : s->fills()) + for (auto const &f : s->fills()) if (f.key == o.order.key) { order_data[o.order.key].filled = true; break; @@ -699,11 +696,16 @@ find_icebergs(std::string const &pcap) { break; auto &change = it->second; - // A passive order must have already existed before - // this event occurred. - bts_assert(change.change != OrderChange::added); - if (change.change == OrderChange::updated) { - // So we have a passive order that + + // Sometimes an aggressor order is also passively + // filled. This is rare but it can happen due to stop + // order triggering. In this case we have no previous + // state for the order so we cannot determine wheter its + // an iceberg so we have to bail. + if (change.change == OrderChange::added) + break; + else if (change.change == OrderChange::updated) { + // So we have a passive order tha // was partially filled. If its new // size is not simply it previous // quantity minus the fill size then @@ -736,6 +738,9 @@ find_icebergs(std::string const &pcap) { di.key = k; di.estimated_quantity.value() = od.accumulated_fills; di.filled = od.filled; + // If we have not seen an iceberg order fill completely then + // all we can say is that its hidden size is at least one + // contract bigger than the observed fills. if (!od.filled) di.estimated_quantity.value() += 1; }