Files
spark/src/server/sparkd/cmds/spartan/analysis/RunAnalysis.cpp
T

205 lines
7.4 KiB
C++

#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 <boost/date_time/posix_time/conversion.hpp>
#include <boost/date_time/posix_time/ptime.hpp>
#include <iostream>
#include <nlohmann/json.hpp>
namespace bts {
namespace spartan {
struct PacketBuffers {
mdp3::MDP3Packet i;
mdp3::MDP3Packet n;
mdp3::MDP3Packet smbo;
};
mdp3::AB_Arbitrageur ab;
auto IA_ip = "224.0.31.1";
auto IA_port = 14310;
auto IB_ip = "224.0.32.1";
auto IB_port = 15310;
auto SAMBO_ip = "233.72.75.1";
auto SAMBO_port = 23310;
auto SBMBO_ip = "233.72.75.64";
auto SBMBO_port = 22310;
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 {
mdp3::MDP3PacketView packet;
void on_begin_packet(mdp3::MDP3PacketView const &p) { packet = p; }
void on_end_packet() {}
template <class M> void operator()(M const &m) {
std::cout << "{\"SeqNum\": " << packet.seq_num() << ", "
<< " \"SendTime\": " << packet.sending_time() << ", "
<< " \"Size\": " << packet.size() << ","
<< " \"Msg\": " << m << "}\n";
}
};
int convert_pcap_to_json(std::vector<std::string> const &args) {
std::array<PacketBuffers, 2> buffers;
auto reader = PCapReader{args[1]};
JSON_Writer writer;
reader.on_packet() = [&](auto const &info, auto const &payload) {
if ((info.dst_ip == IA_ip && info.dst_port == IA_port)) {
buffers[0].i = mdp3::MDP3Packet{payload};
} else if ((info.dst_ip == IB_ip && info.dst_port == IB_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, writer);
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;
}
template <class M, decltype(((M *)nullptr)->transactTime()) = 0>
uint64_t get_transact_time(M &m) {
return m.transactTime();
}
template <class M, decltype(((M *)nullptr)->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 <class M> 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<M>() << ","
<< (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<std::string> const &args) {
std::array<PacketBuffers, 2> buffers;
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 == IA_ip && info.dst_port == IA_port)) {
buffers[0].i = mdp3::MDP3Packet{payload};
} else if ((info.dst_ip == IB_ip && info.dst_port == IB_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;
}
} // namespace spartan
} // namespace bts