Files
spark/src/lib/md/recording/MDReplayer.cpp
T

169 lines
5.8 KiB
C++

#if !defined(_WIN32) //&& defined(SPARK_ENABLE_CCAPI)
#include "MDReplayer.h"
#include "lib/config/config.h"
#include "lib/config/protos/system_config.pb.h"
#include "lib/core/TimeStamp.h"
#include "lib/domain/Book.h"
#include "lib/domain/Price.h"
#include "lib/domain/Quantity.h"
#include "lib/domain/Security.h"
#include "lib/domain/SecurityCommon.h"
#include "lib/domain/SecurityID.h"
#include "lib/domain/SecurityMarketDataStatistics.h"
#include "lib/domain/Side.h"
#include "lib/domain/TradeRecord.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/logging/logging.h"
#include "lib/md/GeneralMarketDataListener.h"
#include "lib/md/MDProducer.h"
#include "lib/md/protos/co2.pb.h"
#include "lib/md/recording/MDRecorder.h"
#include "lib/os/os.h"
#include "lib/utils/PBUtils.h"
#include "lib/utils/bts_assert.h"
#include <fstream>
#include <string>
namespace bts::md::recording {
using namespace bts::md;
void copy_books(const bts::co2::BookMessage &from, bts::domain::Book &to) {
to.clear_to_end();
const auto copy_side = [](const auto &from_side, auto &to_side) {
size_t i = 0;
auto sz = to_side.size();
for (const bts::co2::PriceSizeMessage &pz : from_side) {
if (i < sz) {
auto &to_b = to_side[i++];
to_b.total_quantity = to_b.direct_quantity =
bts::domain::Quantity(pz.size());
// reason for doing this is that bts::domain::PriceSize expects an
// exponent of -9 only
bts::md::IntPrice p_aux =
bts::md::IntPrice(pz.mantissa(), pz.exponent());
to_b.price = bts::domain::Price::from_double(p_aux.price());
}
}
};
copy_side(from.bids(), to[bts::domain::Side::Buy]);
copy_side(from.asks(), to[bts::domain::Side::Sell]);
}
struct MDReplayer ::PImpl {
bts::api::Venue venue;
std::set<uint32_t> sec_keys = {};
uint64_t start_time = bts::core::TimeStamp::now_ns();
GeneralMarketDataListener listener;
PImpl(
bts::api::Venue venue,
bts::md::GeneralSecurityIndex &index,
bts::md::MDProducer &md_producer)
: venue(venue), listener(index, md_producer, venue) {}
};
MDReplayer::MDReplayer(
bts::api::Venue venue,
bts::md::GeneralSecurityIndex &index,
bts::md::MDProducer &md_producer)
: me(new PImpl(venue, index, md_producer)) {
LOG(md, Info, "MDReplayer::MDReplayer venue: {}", Venue_Name(venue));
}
MDReplayer::~MDReplayer() {}
void MDReplayer::run() {
std::string directory = MDRecorder::recording_path();
auto include_file = [&](const std::string &file_name) {
return MDRecorder::is_md_recording_file(file_name, me->venue);
};
std::string file_name = os::find_newest_file(directory, include_file);
if (file_name.empty()) {
LOG(md, Error, "MDReplayer::run could not find a file to replay");
return;
}
file_name = directory + "/" + file_name;
LOG(md, Info, "MDReplayer::run venue: {} file_name: {}",
bts::api::Venue_Name(me->venue), file_name);
while (true) {
_replay_file_once(file_name);
}
}
void MDReplayer::_replay_file_once(const std::string &file_name) {
auto log_progress = [](auto i) {
if (i++ % 10000 == 0)
LOG(md, Info, "MDReplayer::_replay_once no of decoded msgs: {}", i);
};
LOG(md, Info, "MDReplayer::_replay_file_once replaying file: {}", file_name);
std::fstream in(file_name, std::ios::binary | std::ios::in);
co2::RecordedMDMessage rec_msg;
int i = 0;
uint64_t beg_ns = bts::core::TimeStamp::now_ns();
uint64_t last_msg_ts = 0;
while (!in.eof() && utils::pb::readbuf(in, &rec_msg)) {
if (i++ == 0) {
last_msg_ts = rec_msg.timestamp();
_replay_message(rec_msg);
} else {
bts_assert(last_msg_ts != 0);
uint64_t this_msg_ts = rec_msg.timestamp();
bts_assert(this_msg_ts >= last_msg_ts);
uint64_t ts_diff = this_msg_ts - last_msg_ts;
last_msg_ts = this_msg_ts;
std::this_thread::sleep_for(std::chrono::nanoseconds(ts_diff));
_replay_message(rec_msg);
}
log_progress(i);
}
uint64_t end_ns = bts::core::TimeStamp::now_ns();
LOG(md, Info, "MDReplayer::_replay_file_once time: {} ns", end_ns - beg_ns);
in.close();
}
void MDReplayer::_replay_message(const bts::co2::RecordedMDMessage &m) {
switch (m.MDRecord_case()) {
case (co2::RecordedMDMessage::MDRecordCase::kSecurity): {
LOG(md, Diag, "MDReplayer::_replay_message m: {}",
utils::pb::to_string(m));
bts::domain::Security new_sec = m.security();
me->sec_keys.insert(m.security().id());
std::vector<domain::Security const *> sec_vec = {};
sec_vec.push_back(&new_sec);
me->listener.on_securities(sec_vec);
break;
}
case (co2::RecordedMDMessage::MDRecordCase::kTotalBook): {
LOG(md, Diag, "MDReplayer::_replay_message m: {}",
utils::pb::to_string(m));
const bts::co2::TotalBook &tot_book = m.total_book();
bts_assert(me->sec_keys.find(m.sec_key()) != me->sec_keys.end());
if (tot_book.has_direct_book()) {
// we can cache this instance and re-use it for perf
bts::domain::Book book = {};
copy_books(tot_book.direct_book(), book);
book.set_security_id(bts::domain::SecurityID(m.sec_key()));
me->listener.on_book(book);
}
break;
}
case (co2::RecordedMDMessage::MDRecordCase::kMisc): {
bts::domain::SecurityMarketDataStatistics stats(m.sec_key(), m.misc());
me->listener.on_security_statistics(stats);
break;
}
case (co2::RecordedMDMessage::MDRecordCase::kTrade): {
bts::domain::TradeRecord tr(m.trade());
std::vector<domain::TradeRecord const *> trades;
trades.push_back(&tr);
me->listener.on_trades(trades);
break;
}
default: {
}
}
}
} // namespace bts::md::recording
#endif