diff --git a/src/data_files/CMakeDeps.cmake b/src/data_files/CMakeDeps.cmake index bddfdd268..e71f89cf5 100644 --- a/src/data_files/CMakeDeps.cmake +++ b/src/data_files/CMakeDeps.cmake @@ -1 +1 @@ -set(sources files/cert_secure_logon.txt;files/futures.coconut.xz;files/onixs_ice/ConnectivityConfiguration.APITest.xml;files/onixs_ice/ConnectivityConfiguration.PerfTest.xml;files/onixs_ice/ConnectivityConfiguration.Production.xml;files/perf_trace/compiled.css;files/perf_trace/index.html;files/perf_trace/jquery-3.3.1.min.js;files/perf_trace/main.js;files/perf_trace/perf_log.sql;files/perf_trace/perf_trace_analysis.sql;files/perf_trace/plotly-latest.min.js;files/small_es.pcap.xz) \ No newline at end of file +set(sources files/cert_secure_logon.txt;files/futures.coconut.xz;files/onixs_ice/ConnectivityConfiguration.APITest.xml;files/onixs_ice/ConnectivityConfiguration.APITest.xml.orig;files/onixs_ice/ConnectivityConfiguration.PerfTest.xml;files/onixs_ice/ConnectivityConfiguration.Production.xml;files/perf_trace/compiled.css;files/perf_trace/index.html;files/perf_trace/jquery-3.3.1.min.js;files/perf_trace/main.js;files/perf_trace/perf_log.sql;files/perf_trace/perf_trace_analysis.sql;files/perf_trace/plotly-latest.min.js;files/small_es.pcap.xz) \ No newline at end of file diff --git a/src/lib/domain_model/Security.h b/src/lib/domain_model/Security.h index eafe97380..50205f815 100644 --- a/src/lib/domain_model/Security.h +++ b/src/lib/domain_model/Security.h @@ -115,6 +115,25 @@ inline Price better_price(api::Side side, Price p1, Price p2) { return is_equal_or_better(side, p1, p2) ? p1 : p2; } +// Returns the worst possible price representable in the Price data type for the side in question +inline Price worst_price(api::Side side) { + switch (side) { + case api::SIDE_BUY: + return Price {std::numeric_limits::min()}; + case api::SIDE_SELL: + return Price {std::numeric_limits::max()}; + default: + bts_assert(false); + break; + } + bts_assert(false); + return Price(0); +} + +inline Price best_price(api::Side side) { + return worst_price(other_side(side)); +} + inline Price decode_exchange_price(api::Security const &sec, Price in) { return Price(in.value() * sec.contract_spec().price_multiplier()); } diff --git a/src/server/sparkd/cmds/QuickTest.cpp b/src/server/sparkd/cmds/QuickTest.cpp index b2dc71354..29c2d103a 100644 --- a/src/server/sparkd/cmds/QuickTest.cpp +++ b/src/server/sparkd/cmds/QuickTest.cpp @@ -6,6 +6,7 @@ #include "lib/tracing/PerfLog.h" #include "lib/utils/datetime.h" +#include "lib/config/protos/logging.pb.h" #include "lib/domain_model/sod.h" #include "lib/logging/logging.h" #include "lib/utils/datetime.h" @@ -30,55 +31,80 @@ namespace bts { namespace cmd { using namespace bts::orderman; using namespace spartan::mdp3; +using namespace spartan; -void dump_snapshots() { - using namespace bts::spartan::mdp3; - using namespace bts::spartan; +struct TimeWriter { + mdp3::MDP3PacketView packet; + void on_begin_packet(mdp3::MDP3PacketView const &p) { packet = p; } + void on_end_packet() { + std::cout << packet.sending_time() << std::endl; + } - auto file = "/home/ppinto/es_md/es_all.pcap"; - auto feed_map = guess_feed_map(file); + template void operator()(M const &) { + } +}; +void print_times(std::string const &file) { + struct PacketBuffers { + spartan::mdp3::MDP3Packet i; + spartan::mdp3::MDP3Packet n; + }; + auto feed_map = spartan::mdp3::guess_feed_map(file); bts_assert(feed_map); auto reader = PCapReader{file}; - struct JSON_Writer { - mdp3::MDP3PacketView packet; - void on_begin_packet(mdp3::MDP3PacketView const &p) { packet = p; } - void on_end_packet() {} + mdp3::AB_Arbitrageur ab; - void operator()(::mdp3::SnapshotFullRefresh52 const &m) { - if (m.securityID() == 15234) - std::cout << "{\"SeqNum\": " << packet.seq_num() << ", " - << " \"SendTime\": " << packet.sending_time() << ", " - << " \"Size\": " << packet.size() << "," - << " \"Msg\": " << m << "}\n"; + reader.on_packet() = [&](auto const &info, auto const &payload) { + if ((info.dst_ip == feed_map.incremental[0].ip && + info.dst_port == feed_map.incremental[0].port)) { + TimeWriter tw; + parse_packet(MDP3PacketView {payload},tw); } - }; - - JSON_Writer writer; - reader.on_packet() = [&](auto const &, auto const &payload) { - parse_packet(mdp3::MDP3PacketView{payload}, writer); + return true; }; while (reader.read_next()) ; } -int QuickTest::execute(std::vector const &) { - // dump_snapshots(); +int QuickTest::execute(std::vector const &args) { + auto cfg = config::get(); + cfg.mutable_console()->set_default_level("Fatal"); + log::configure_logging(cfg); + + auto file = args[0]; + print_times(file); + return 0; + using namespace bts::spartan::mdp3; struct Obs : IChannelObserver { void on_recovery_complete( - std::vector const &) override {} + std::vector const &secs) override { + + for (auto s : secs) { + std::cout << "Bootstrap -----------------------------------------" + "------------------------------" + << std::endl; + std::cout << "Symbol: " << s->definition().symbol() << std::endl + << "Status: " << s->trading_status() << std::endl + << "Orders:\n" + << s->orders(); + } + } void on_real_time_event(MDP3Event const &ev) override { + for (auto s : ev.affected_securities) { if (s->order_changes().size() || s->fills().size()) { - std::cout << "symbol: " << s->definition().symbol() << "\n" - << "seq_num: " << ev.first_packet_sn << " to " - << ev.last_packet_sn << "\n" - << "changes:\n" + std::cout << "Event " << ev.transact_time + << " -----------------------------" << std::endl; + std::cout << "Symbol: " << s->definition().symbol() << std::endl + << "Status: " << s->trading_status() << std::endl + << "Seq Num: " << ev.first_packet_sn << " to " + << ev.last_packet_sn << std::endl + << "Orders:\n" << s->order_changes() << "Fills:\n" << s->fills(); } @@ -89,7 +115,7 @@ int QuickTest::execute(std::vector const &) { } } obs; - spartan::observe_channel("/home/ppinto/es_md/es_all.pcap", obs); + spartan::observe_channel(file, obs); return 0; } diff --git a/src/server/sparkd/cmds/Spartan.cpp b/src/server/sparkd/cmds/Spartan.cpp index 391cbe1ff..e98e84def 100644 --- a/src/server/sparkd/cmds/Spartan.cpp +++ b/src/server/sparkd/cmds/Spartan.cpp @@ -1,20 +1,216 @@ #include "Spartan.h" #include "server/sparkd/cmds/spartan/algo/RunTAS.h" #include "server/sparkd/cmds/spartan/analysis/RunAnalysis.h" +#include "server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.h" +#include "server/sparkd/cmds/spartan/md/mdp3/handler/IChannelObserver.h" #include +#include #include namespace bts { namespace cmd { +int export_tables(std::string const &file, std::string const &out_dir) { + using namespace bts::spartan::mdp3; + + struct Obs : IChannelObserver { + + Obs(std::string const &out_dir) + : events(out_dir + "/events.csv"), + event_items(out_dir + "/event_items.csv"), + securities(out_dir + "/securities.csv") { + + events << "event_id, transact_time,first_packet_sn,last_packet_sn," + "first_packet_send_time,last_packet_send_time" + << std::endl; + + event_items + << "event_id, transact_time,security_id,trading_status,type," + "change,order_key,side," + "priority,price,quantity,trade_entry_id,aggressor" + << std::endl; + + securities + << "security_id,symbol,security_group,asset,last_update_time" + << std::endl; + } + + std::ofstream events; + std::ofstream event_items; + std::ofstream orders; + std::ofstream securities; + size_t event_id = 0; + + void on_recovery_complete( + std::vector const &secs) override { + for (auto s : secs) { + securities << s->security_id().value() << "," << s->symbol() << "," + << s->definition().security_group() << "," + << s->definition().asset() << "," + << s->definition().last_update_time() << std::endl; + + for (auto const &[id, order] : s->orders()) + event_items << 0 << "," // event_id + + << 0 << "," // transact time + + << s->security_id().value() << "," // security id + + << s->trading_status() << "," // trading status + + << "deduced_order," // type + + << "init," // change + + << id << "," // order key + + << api::Side_Name(order.side) << "," // side + + << order.priority.value() << "," // priority + + << order.price.price() << "," // price + + << order.quantity.value() << "," // quantity + + << "," // trade_entry_id + + << "n/a" // aggressor + + << std::endl; + } + } + + void on_real_time_event(MDP3Event const &ev) override { + event_id++; + + events << event_id << "," + + << ev.transact_time << "," + + << ev.first_packet_sn << "," + + << ev.last_packet_sn << "," + + << ev.first_packet_send_time << "," + + << ev.last_packet_send_time << std::endl; + + for (auto s : ev.affected_securities) { + for (auto const &change : s->order_changes()) + event_items << event_id << "," + + << ev.transact_time.value() << "," // transact time + + << s->security_id().value() << "," // security id + + << s->trading_status() << "," // trading status + + << "order_change," // type + + << change_name(change.change) << "," + + << change.order.key.value() << "," // order key + + << api::Side_Name(change.order.side) << "," // side + + << change.order.priority.value() << "," // priority + + << change.order.price.price() << "," // price + + << change.order.quantity.value() << "," // quantity + + << "," // trade_entry_id + + << "n/a" // aggressor + + << std::endl; + + for (auto const &f : s->fills()) + event_items << event_id << "," + + << ev.transact_time.value() << "," // transact time + + << s->security_id().value() << "," // security id + + << s->trading_status() << "," // trading status + + << "fill," // type + + << "," + + << f.key.value() << "," // order key + + << "," // side + + << "," // priority + + << f.price << "," // price + + << f.quantity << "," // quantity + + << f.trade_entry_id << "," // trade_entry_id + + << aggressor_name(f.aggressor) // aggressor + + << std::endl; + + if (s->security_id().value() == 12181 && event_id == 1065) + std::cout << "There is" << std::endl; + auto deduced_order = s->deduced_order_change(); + if (deduced_order) { + event_items << event_id << "," + + << ev.transact_time.value() << "," // transact time + + << s->security_id().value() << "," // security id + + << s->trading_status() << "," // trading status + + << "order," // type + + << change_name(deduced_order->change) << "," + + << deduced_order->order.key.value() + << "," // order key + + << api::Side_Name(deduced_order->order.side) + << "," // side + + << "," // priority + + << deduced_order->order.price.price() << "," // price + + << deduced_order->order.quantity.value() + << "," // quantity + + << "," // trade_entry_id + + << "n/a" // aggressor + + << std::endl; + } + } + } + + void on_real_time_channel_loss() override { + std::cout << "Observed loss, resetting\n"; + } + } obs(out_dir); + + spartan::observe_channel(file, obs); + return 0; +} + int Spartan::execute(std::vector const &args) { auto error = 1; if (args.size() > 0) { - if (args[0] == "pcap_to_csv") + if (args[0] == "pcap_to_msgs") error = spartan::convert_pcap_to_csv_summary(args); else if (args[0] == "pcap_to_json") error = spartan::convert_pcap_to_json(args); + else if (args[0] == "pcap_to_tables") + error = export_tables(args[1], args[2]); } if (error) std::cout << help(); @@ -22,15 +218,9 @@ int Spartan::execute(std::vector const &args) { } std::string Spartan::help() { - return fmt::format("\tpcap_to_csv pcap_file\n" - "\tfat_pcap_to_csv pcap_file\n" - "\tfat_pcap_to_json pcap_file\n" + return fmt::format("\tpcap_to_msgs pcap_file\n" "\tpcap_to_json pcap_file\n" - "\trun_tas_from_pcap pcap_file\n" - "\tdetect_gaps pcap_file\n" - "\tfat_pcap_to_snapshot_csv_summary\n" - "\ttest\n" - "\trun_tas_from_mcast\n"); + "\tpcap_to_tables pcap_file output_dir\n"); } } // namespace cmd diff --git a/src/server/sparkd/cmds/spartan/analysis/RunAnalysis.cpp b/src/server/sparkd/cmds/spartan/analysis/RunAnalysis.cpp index 533fd61cd..ac6993f82 100644 --- a/src/server/sparkd/cmds/spartan/analysis/RunAnalysis.cpp +++ b/src/server/sparkd/cmds/spartan/analysis/RunAnalysis.cpp @@ -44,15 +44,24 @@ void cme_ts_to_str(uint64_t cme_ts, std::stringstream *out) { } struct JSON_Writer { + + std::stringstream json_buffer; + nlohmann::json json_obj; mdp3::MDP3PacketView packet; + void on_begin_packet(mdp3::MDP3PacketView const &p) { packet = p; } void on_end_packet() {} template void operator()(M const &m) { - std::cout << "{\"SeqNum\": " << packet.seq_num() << ", " - << " \"SendTime\": " << packet.sending_time() << ", " - << " \"Size\": " << packet.size() << "," - << " \"Msg\": " << m << "}\n"; + json_obj["SeqNum"] = packet.seq_num(); + json_obj["SendTime"] = packet.sending_time() ; + json_obj["Size"] = packet.size(); + + json_buffer.str(""); + json_buffer.clear(); + json_buffer << m; + json_obj["Msg"] = nlohmann::json::parse(json_buffer.str()); + std::cout << json_obj.dump(4) << std::endl; } }; 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 18186924f..f5a72d139 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.cpp @@ -249,32 +249,25 @@ void InstrumentSnapshot::clear_order_changes() { me->order_changes.clear(); } void InstrumentSnapshot::insert_order(MDP3Order const &order) { bts_assert_d(me->orders.count(order.key) == 0); me->orders.insert({order.key, order}); - me->order_changes.emplace_back(OrderChange{order.key, - order.price, - order.quantity, - OrderChange::added}); + me->order_changes.emplace_back(OrderChange{order, OrderChange::added}); } void InstrumentSnapshot::update_order(MDP3Order const &order) { auto it = me->orders.find(order.key); bts_assert_d(it != me->orders.end()); it->second = order; - me->order_changes.emplace_back(OrderChange{order.key, - order.price, - order.quantity, - OrderChange::updated}); + me->order_changes.emplace_back(OrderChange{order, OrderChange::updated}); } void InstrumentSnapshot::remove_order(MDP3OrderKey id) { - bts_assert_d(me->orders.count(id)); - me->orders.erase(id); - me->order_changes.emplace_back(OrderChange{id, - {}, {}, - OrderChange::removed}); + auto it = me->orders.find(id); + bts_assert(it != me->orders.end()); + me->order_changes.emplace_back( + OrderChange{it->second, OrderChange::removed}); + me->orders.erase(it); } -Book InstrumentSnapshot::build_book_from_orders (api::Side side) const -{ +Book InstrumentSnapshot::build_book_from_orders(api::Side side) const { auto invert = side == api::SIDE_BUY ? -1 : 1; auto book_idx = std::map(); for (auto &o : orders()) @@ -294,7 +287,7 @@ Book InstrumentSnapshot::build_book_from_orders (api::Side side) const return mbo_book; } -void InstrumentSnapshot::set_books_from_orders() { +void InstrumentSnapshot::set_books_from_orders() { me->books[0] = build_book_from_orders(api::SIDE_BUY); me->books[1] = build_book_from_orders(api::SIDE_SELL); } @@ -337,24 +330,58 @@ void InstrumentSnapshot::remove_fill(MDP3TradeEntryID tid, MDP3OrderKey oid) { void InstrumentSnapshot::clear_fills() { me->fills.clear(); } -auto get_orders(InstrumentSnapshot const &snap, api::Side side) { - std::vector result; - for (auto &o : snap.orders()) { - auto &order = o.second; - if (order.side == side) - result.push_back(order); +MaybeOrderChange InstrumentSnapshot::deduced_order_change() const { + if (fills().empty()) + return {}; + + Fills aggressor_fills; + std::copy_if(fills().begin(), fills().end(), + std::back_inserter(aggressor_fills), [](auto const &f) { + return f.aggressor == MDP3Fill::THIS_BUY || + f.aggressor == MDP3Fill::THIS_SELL; + }); + + if (aggressor_fills.empty()) + return {}; + + OrderChange result; + result.order.key = aggressor_fills[0].key; + result.order.security = security_id(); + result.order.side = aggressor_fills[0].aggressor == MDP3Fill::THIS_BUY + ? api::SIDE_BUY + : api::SIDE_SELL; + result.order.priority = {}; + result.order.price = aggressor_fills[0].price; + result.order.quantity = MDP3Quantity{0}; + + for (auto &f : aggressor_fills) { + if (securities::is_better(result.order.side, + Price(result.order.price.price()), + Price(f.price.price()))) + result.order.price = f.price; + result.order.quantity.value() += result.order.quantity.value(); } - std::sort(result.begin(), result.end(), - [side](auto const &o1, auto const &o2) { - if (o1.price.price() != o2.price.price()) { - if (side == api::SIDE_BUY) - return o2.price.price() < o1.price.price(); - else - return o1.price.price() < o2.price.price(); - } else - return o1.priority.value() < o2.priority.value(); - }); - return result; + + // Lets find out if this is a new or existing order. If it was an + // existing order then we must have a delete or replace in our list + // of order changes. If we do not then this must be a new order. + result.change = OrderChange::added; + for (auto &o : order_changes()) + if (o.order.key == result.order.key) { + result.change = OrderChange::updated; + break; + } + + // Now lets find out if this was a full fill or a partial fill. A + // partial fill would have left the order in the books. In that + // case we can use the information in the book to correct our + // estimated quantity and prices from above. + auto it = orders().find(result.order.key); + if (it != orders().end()) { + result.order.quantity.value() += it->second.quantity.value(); + result.order.price = it->second.price; + } + return {result}; } std::ostream &operator<<(std::ostream &out, InstrumentSnapshot const &snap) { @@ -374,11 +401,11 @@ std::ostream &operator<<(std::ostream &out, InstrumentSnapshot const &snap) { out << "Order Changes -----------------------------------\n"; for (auto &change : snap.order_changes()) out << change << "\n"; - auto orders = get_orders(snap, api::SIDE_BUY); + auto orders = sort_orders(snap.orders(), api::SIDE_BUY); out << "Buy Orders -----------------------------------\n"; for (auto &order : orders) out << order << "\n"; - orders = get_orders(snap, api::SIDE_SELL); + orders = sort_orders(snap.orders(), api::SIDE_SELL); out << "Sell Orders ----------------------------------\n"; for (auto &order : orders) out << order << "\n"; diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.h b/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.h index 7ebe26bf6..ca136b899 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/common/InstrumentSnapshot.h @@ -62,6 +62,10 @@ class InstrumentSnapshot { void remove_order(MDP3OrderKey); Book build_book_from_orders (api::Side ) const; void set_books_from_orders() ; + // Deduced order changes, that is the implied order change that + // would explain the last event processed in this snapshot as + // described by order_changes() and fills() + MaybeOrderChange deduced_order_change() const; // Reset books in response to a channel reset message void reset_books(); diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h b/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h index 51c126903..2a36532ca 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/common/mdp3_common.h @@ -52,8 +52,8 @@ class BookLevel { }; inline std::ostream &operator<<(std::ostream &s, BookLevel const &l) { - s << " " << l.quantity.value() << " @ " << l.price.price() << " (" << l.n_orders - << " orders)"; + s << " " << l.quantity.value() << " @ " << l.price.price() << " (" + << l.n_orders << " orders)"; return s; } @@ -67,23 +67,26 @@ struct MDP3Order { MDP3Quantity quantity; }; + struct OrderChange { - MDP3OrderKey key; - md::IntPrice price; - MDP3Quantity quantity; + MDP3Order order; enum ChangeType { added, removed, updated } change; }; +using MaybeOrderChange = std::optional; -inline std::ostream &operator<<(std::ostream &s, OrderChange const &ch) { - s << " ok: " << ch.key.value() << " " - << " price: " << ch.price.price() - << " qty: " << ch.quantity.value() - << " " << (ch.change == OrderChange::added - ? "create" - : (ch.change == OrderChange::updated ? "update" : "delete")); - return s; +inline const char*change_name (OrderChange::ChangeType ct) { + switch(ct) { + case OrderChange::added: + return "added"; + case OrderChange::removed: + return "removed"; + case OrderChange::updated: + return "updated"; + } + return ""; } + using OrderChanges = std::vector; inline std::ostream &operator<<(std::ostream &s, MDP3Order const &order) { @@ -96,6 +99,15 @@ inline std::ostream &operator<<(std::ostream &s, MDP3Order const &order) { return s; } +inline std::ostream &operator<<(std::ostream &s, OrderChange const &ch) { + s << " " + << (ch.change == OrderChange::added + ? "create" + : (ch.change == OrderChange::updated ? "update" : "delete")) + << ch.order; + return s; +} + inline std::ostream &operator<<(std::ostream &s, OrderChanges const &changes) { for (auto c : changes) s << " " << c << "\n"; @@ -103,16 +115,60 @@ inline std::ostream &operator<<(std::ostream &s, OrderChanges const &changes) { } using Orders = std::unordered_map; + + using MDP3TradeEntryID = utils::ValueWrapper; struct MDP3Fill { MDP3OrderKey key; md::IntPrice price; MDP3Quantity quantity; + MDP3Quantity summary_quantity; MDP3TradeEntryID trade_entry_id; - enum Aggressor { THIS, OTHER, IMPLIED } aggressor; + enum Aggressor {THIS_BUY, THIS_SELL, OTHER, IMPLIED } aggressor; }; +inline const char*aggressor_name (MDP3Fill::Aggressor ag) { + switch(ag) { + case MDP3Fill::THIS_BUY: + return "yes_buy"; + case MDP3Fill::THIS_SELL: + return "yes_sell"; + case MDP3Fill::OTHER: + return "no"; + case MDP3Fill::IMPLIED: + return "implied"; + } + return ""; +} + +inline std::vector sort_orders(Orders const &orders, api::Side side) { + std::vector result; + for (auto &o : orders) { + auto &order = o.second; + if (order.side == side) + result.push_back(order); + } + std::sort(result.begin(), result.end(), + [side](auto const &o1, auto const &o2) { + if (o1.price.price() != o2.price.price()) { + if (side == api::SIDE_BUY) + return o2.price.price() < o1.price.price(); + else + return o1.price.price() < o2.price.price(); + } else + return o1.priority.value() < o2.priority.value(); + }); + return result; +} + +inline std::ostream &operator<<(std::ostream &out, Orders const &orders) { + auto sorted = sort_orders(orders, api::SIDE_BUY); + for (auto &o : sorted) + out << o << std::endl; + return out; +} + inline std::ostream &operator<<(std::ostream &s, MDP3Fill const &fill) { s << " ok: " << fill.key.value() << " " << "price: " << fill.price.price() << " " diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/realtime/RealTimeHandler.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/realtime/RealTimeHandler.cpp index e448b7d54..8eb37f8e2 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/realtime/RealTimeHandler.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/realtime/RealTimeHandler.cpp @@ -12,6 +12,7 @@ struct RealTimeHandler::TradeSummary { ::mdp3::MDUpdateAction::Value update_action; InstrumentSnapshot *security; md::IntPrice price; + MDP3Quantity quantity; size_t n_orders; ::mdp3::AggressorSide::Value aggressor; MDP3TradeEntryID trade_entry_id; @@ -27,7 +28,7 @@ struct RealTimeHandler::PImpl { PImpl(IRealTimeObserver &o) : instruments(), current_packet_send_time(MDP3TimeStamp{0}), current_packet_sn(0), current_event(), observer(o), pending_ts(), - pending_order_detail() {} + pending_trade_order_detail() {} std::unordered_map> instruments; @@ -37,7 +38,7 @@ struct RealTimeHandler::PImpl { MDP3Event current_event; IRealTimeObserver &observer; std::vector pending_ts; - std::vector pending_order_detail; + std::vector pending_trade_order_detail; }; struct RealTimeHandler::EventGuard { @@ -93,7 +94,7 @@ struct RealTimeHandler::EventGuard { owner.on_end_of_event(); info.reset(); owner.me->pending_ts.clear(); - owner.me->pending_order_detail.clear(); + owner.me->pending_trade_order_detail.clear(); } } @@ -434,6 +435,7 @@ void RealTimeHandler::operator()( summary.security = iptr; summary.price = md::IntPrice{md_entry.mDEntryPx().mantissa(), md_entry.mDEntryPx().exponent()}; + summary.quantity = MDP3Quantity{md_entry.mDEntrySize()}; summary.n_orders = md_entry.numberOfOrders(); summary.aggressor = md_entry.aggressorSide(); summary.trade_entry_id = MDP3TradeEntryID{md_entry.mDTradeEntryID()}; @@ -444,7 +446,7 @@ void RealTimeHandler::operator()( TradeOrderDetail detail; detail.order_id = MDP3OrderKey{order_id_entry.orderID()}; detail.quantity = MDP3Quantity{order_id_entry.lastQty()}; - me->pending_order_detail.emplace_back(detail); + me->pending_trade_order_detail.emplace_back(detail); }); } @@ -552,25 +554,50 @@ void RealTimeHandler::operator()(::mdp3::ChannelReset4 &r) { } void RealTimeHandler::on_end_of_event() { - // trade summary messages have to be processed together once all of + // Trade summary messages have to be processed together once all of // them have been received. We do this here, because we know the // event is over and therefore so are the trade summaries size_t current_order_detail = 0; for (auto &summary : me->pending_ts) { for (size_t i = 0; i < summary.n_orders; i++) { - auto &detail = me->pending_order_detail[current_order_detail++]; + auto &detail = me->pending_trade_order_detail[current_order_detail++]; MDP3Fill f; f.key = detail.order_id; f.price = summary.price; f.quantity = detail.quantity; + f.summary_quantity = summary.quantity; f.trade_entry_id = summary.trade_entry_id; - if (summary.aggressor != ::mdp3::AggressorSide::NoAggressor) { - if (i == 0) - f.aggressor = MDP3Fill::THIS; - else + if (i == 0) { + switch (summary.aggressor) { + case ::mdp3::AggressorSide::NoAggressor: + f.aggressor = MDP3Fill::IMPLIED; + break; + case ::mdp3::AggressorSide::Buy: + f.aggressor = MDP3Fill::THIS_BUY; + break; + case ::mdp3::AggressorSide::Sell: + f.aggressor = MDP3Fill::THIS_SELL; + break; + case ::mdp3::AggressorSide::NULL_VALUE: + bts_assert(false); + break; + } + } + else { + switch (summary.aggressor) { + case ::mdp3::AggressorSide::NoAggressor: + f.aggressor = MDP3Fill::IMPLIED; + break; + case ::mdp3::AggressorSide::Buy: + case ::mdp3::AggressorSide::Sell: f.aggressor = MDP3Fill::OTHER; - } else - f.aggressor = MDP3Fill::IMPLIED; + break; + case ::mdp3::AggressorSide::NULL_VALUE: + bts_assert(false); + break; + } + } + switch (summary.update_action) { case ::mdp3::MDUpdateAction::New: summary.security->add_fill(f); @@ -589,8 +616,8 @@ void RealTimeHandler::on_end_of_event() { me->observer.on_real_time_event(me->current_event); for (auto s : me->current_event.affected_securities) { - const_cast(s)->clear_fills(); - const_cast(s)->clear_order_changes(); + const_cast(s)->clear_fills(); + const_cast(s)->clear_order_changes(); } } diff --git a/src/server/sparkd/cmds/spartan/sql_tools/msg.sql b/src/server/sparkd/cmds/spartan/sql_tools/msg.sql deleted file mode 100644 index 294a56a87..000000000 --- a/src/server/sparkd/cmds/spartan/sql_tools/msg.sql +++ /dev/null @@ -1,32 +0,0 @@ -drop table msg; -create table if not exists -msg ( - "HumanTransactTime" TEXT, - "HumanSendingTime" TEXT, - "Latency" numeric, - "TransactTime" numeric, - "SendingTime" numeric, - "SeqNum" numeric, - "MsgType" TEXT, - "TimeInQueue" numeric, - "MatchEventIndicator" numeric -); - -.mode csv -.import "/home/ppinto/es_md/es_i.csv" msg - -drop view event_len; -create view event_len as -select transacttime, count(*) len from msg group by transacttime; - -drop view summary_event_len; -create view summary_event_len as -select - count(*) n_events, - msg_count len -from - (select transacttime, count(*) msg_count from msg group by transacttime) t -group by - len - -