#include "server/sparkd/cmds/spartan/analysis/RunAnalysis.h" #include "../md/mdp3/parsing/MDP3Parser.h" #include "../util/PCapReader.h" #include "lib/utils/overloaded.h" #include "lib/utils/stats.h" #include "lib/utils/type_name.h" #include "server/sparkd/cmds/spartan/md/mdp3/feed_control/AB_Arbitrageur.h" #include "server/sparkd/cmds/spartan/md/mdp3/feed_control/IFeedController.h" #include "server/sparkd/cmds/spartan/md/mdp3/handler/ChannelHandler.h" #include "server/sparkd/cmds/spartan/md/mdp3/handler/IChannelObserver.h" #include "server/sparkd/cmds/spartan/md/mdp3/utils/mdp3_utils.h" #include #include #include #include namespace bts { namespace spartan { struct PacketBuffers { mdp3::MDP3Packet i; mdp3::MDP3Packet n; }; mdp3::AB_Arbitrageur ab; void cme_ts_to_str(uint64_t cme_ts, std::stringstream *out) { auto one_billion = 1000000000; time_t unix_ts = cme_ts / one_billion; auto boost_ts = boost::posix_time::from_time_t(unix_ts); auto year = boost_ts.date().year(); auto month = boost_ts.date().month().as_number(); auto day = boost_ts.date().day().as_number(); int hours = boost_ts.time_of_day().hours(); int minutes = boost_ts.time_of_day().minutes(); int seconds = boost_ts.time_of_day().seconds(); *out << year << "-" << std::setw(2) << std::setfill('0') << month << "-" << std::setw(2) << std::setw(2) << std::setfill('0') << day << "." << std::setw(2) << std::setw(2) << std::setfill('0') << hours << ":" << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(2) << std::setfill('0') << seconds << "." << std::setw(9) << cme_ts - (unix_ts * one_billion); } 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) { 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; } }; int convert_pcap_to_json(std::vector const &args) { std::array buffers; auto reader = PCapReader{args[1]}; JSON_Writer writer; reader.on_packet() = [&](auto const &, auto const &payload) { parse_packet(mdp3::MDP3PacketView{payload}, writer); return true; }; while (reader.read_next()) ; return 0; } template transactTime()) = 0> uint64_t get_transact_time(M &m) { return m.transactTime(); } template lastUpdateTime()) = 0> uint64_t get_transact_time(M &m) { return m.lastUpdateTime(); } struct CSVPrinter { std::stringstream stime_buffer; std::stringstream ttime_buffer; size_t previous_t_time = 0; size_t previous_s_time = 0; mdp3::MDP3PacketView packet; CSVPrinter() { std::cout << "HumanTransactTime,HumanSendingTime," "Latency,TransactTime,SendingTime,SeqNum,MsgType,TimeInQueue," "MatchEventindicator" << std::endl; } void on_begin_packet(mdp3::MDP3PacketView const &v) { packet = v; } void on_end_packet() {} template void print(M &m) { stime_buffer.str(""); stime_buffer.clear(); cme_ts_to_str(packet.sending_time(), &stime_buffer); ttime_buffer.str(""); ttime_buffer.clear(); cme_ts_to_str(get_transact_time(m), &ttime_buffer); auto t = get_transact_time(m); std::cout << ttime_buffer.str() << "," << stime_buffer.str() << "," << (packet.sending_time() - t) / 1000 << "," << t << "," << packet.sending_time() << "," << packet.seq_num() << "," << utils::type_name() << "," << (t < previous_s_time ? ((previous_s_time - t) / 1000) : 0) << "," << m.matchEventIndicator().endOfEvent() << std::endl; previous_s_time = packet.sending_time(); previous_t_time = t; } void operator()(::mdp3::MDIncrementalRefreshOrderBook43 &m) { print(m); } void operator()(::mdp3::MDIncrementalRefreshOrderBook47 &m) { print(m); } void operator()(::mdp3::MDIncrementalRefreshTradeSummary42 &m) { print(m); } void operator()(::mdp3::MDIncrementalRefreshTradeSummary48 &m) { print(m); } void operator()(::mdp3::MDIncrementalRefreshVolume37 &m) { print(m); } void operator()(::mdp3::MDIncrementalRefreshDailyStatistics33 &m) { print(m); } void operator()(::mdp3::MDIncrementalRefreshDailyStatistics49 &m) { print(m); } void operator()(::mdp3::MDIncrementalRefreshLimitsBanding34 &m) { print(m); } void operator()(::mdp3::MDIncrementalRefreshLimitsBanding50 &m) { print(m); } void operator()(::mdp3::MDIncrementalRefreshBook46 &m) { print(m); } void operator()(::mdp3::MDInstrumentDefinitionFuture54 &m) { print(m); } void operator()(::mdp3::MDIncrementalRefreshSessionStatistics35 &m) { print(m); } void operator()(::mdp3::MDIncrementalRefreshSessionStatistics51 &m) { print(m); } void operator()(::mdp3::MDInstrumentDefinitionFuture27 &m) { print(m); } void operator()(::mdp3::ChannelReset4 &m) { print(m); } void operator()(::mdp3::MDIncrementalRefreshBook32 &m) { print(m); } void operator()(::mdp3::MDInstrumentDefinitionOption41 &m) { print(m); } void operator()(::mdp3::MDInstrumentDefinitionOption55 &m) { print(m); } void operator()(::mdp3::MDInstrumentDefinitionSpread29 &m) { print(m); } void operator()(::mdp3::MDInstrumentDefinitionSpread56 &m) { print(m); } void operator()(::mdp3::QuoteRequest39 &m) { print(m); } void operator()(::mdp3::SecurityStatus30 &m) { print(m); } }; int convert_pcap_to_csv_summary(std::vector const &args) { std::array buffers; auto feed_map = mdp3::guess_feed_map(args[1]); bts_assert(feed_map); auto reader = PCapReader{args[1]}; mdp3::AB_Arbitrageur ab; CSVPrinter csv_printer; 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)) { buffers[0].i = mdp3::MDP3Packet{payload}; } else if ((info.dst_ip == feed_map.incremental[0].ip && info.dst_port == feed_map.incremental[0].port)) { buffers[1].i = mdp3::MDP3Packet{payload}; } else return true; auto action = ab(buffers[0].i.view(), buffers[1].i.view()); if (action.action == mdp3::AB_Arbitrageur::APPLY) parse_packet(action.packet, csv_printer); else if (action.action == mdp3::AB_Arbitrageur::RECOVER) { std::cerr << "Unrecoverable loss detecte at sequence number: " << ab.expected_next_sn() - 1 << std::endl; return false; } return true; }; while (reader.read_next()) ; return 0; } void observe_channel(std::string const &file, mdp3::IChannelObserver &obs) { struct Ctrl : mdp3::IFeedController { void start_instrument_feed() override{}; void stop_instrument_feed() override{}; void start_snapshot_feed() override{}; void stop_snapshot_feed() override{}; void start_incremental_feed() override{}; void stop_incremental_feed() override{}; } ctrl; std::array buffers; auto feed_map = mdp3::guess_feed_map(file); bts_assert(feed_map); auto reader = PCapReader{file}; mdp3::AB_Arbitrageur ab; mdp3::ChannelHandler handler(obs, ctrl); 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)) { buffers[0].i.set_bytes(payload); handler.on_incremental_feed(buffers[0].i.view(), buffers[1].i.view()); } else if ((info.dst_ip == feed_map.incremental[1].ip && info.dst_port == feed_map.incremental[1].port)) { buffers[1].i.set_bytes(payload); handler.on_incremental_feed(buffers[0].i.view(), buffers[1].i.view()); } else if ((info.dst_ip == feed_map.instrument.ip && info.dst_port == feed_map.instrument.port)) { handler.on_instrument_feed(mdp3::MDP3PacketView{payload}); } else if ((info.dst_ip == feed_map.snapshot.ip && info.dst_port == feed_map.snapshot.port)) { handler.on_snapshot_feed(mdp3::MDP3PacketView{payload}); } return true; }; while (reader.read_next()) ; } } // namespace spartan } // namespace bts