MDRecorder almost done, need record implied and RFQ requests, as well as exit after an alloted time of recording
This commit is contained in:
1 parent
dd66e94bfd
commit
d1fe0a03b5
6 files changed
+303
-2
No files matched your search
+1
-1
Submodule docker/linux updated: 6537812262...56c61231f3.
@@ -86,4 +86,12 @@ ALGO:
|
||||
|
||||
AlgoWrapperOrder::me->services->subscribe_to_child_updates
|
||||
AlgoWrapperOrder::on_child_order_update
|
||||
TaskingEyeAlgo::on_child_change
|
||||
TaskingEyeAlgo::on_child_change
|
||||
|
||||
MARKET DATA REPLAY:
|
||||
------------------
|
||||
main.cpp ->
|
||||
Md::Md() ->
|
||||
Record::Record ->
|
||||
RelayClient::record_everything (not a class function) ->
|
||||
RelayClient::RelayScheduler::run
|
||||
@@ -186,3 +186,21 @@ message CoconutReplayHeader {
|
||||
|
||||
int32 naggle_ms = 5;
|
||||
}
|
||||
|
||||
// Message written to the file, for replay
|
||||
message RecordedMDMessage{
|
||||
uint32 sec_key = 10;
|
||||
int64 timestamp = 20;
|
||||
oneof MDRecord {
|
||||
bts.api.Security security = 100;
|
||||
BookMessage book = 200;
|
||||
TradeMessage trade = 300;
|
||||
QuoteRequestMessage rfq = 400;
|
||||
MiscMessage misc = 500;
|
||||
}
|
||||
}
|
||||
|
||||
message Recording{
|
||||
uint32 time_in_seconds = 1;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
#include "MDRecorder.h"
|
||||
#include "lib/api/services/MarketDataRelay.client.pb.h"
|
||||
#include "lib/api/services/MarketDataRelay.pb.h"
|
||||
#include "lib/config/config.h"
|
||||
#include "lib/config/protos/system.pb.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "lib/md/MDClient.h"
|
||||
#include "lib/md/MDConsumer.h"
|
||||
#include "lib/md/MDConsumerHandle.h"
|
||||
#include "lib/md/PriceSize.h"
|
||||
#include "lib/md/Shm/FixedSecurity.h"
|
||||
#include "lib/md/Shm/MDStructure.h"
|
||||
#include "lib/md/common.h"
|
||||
#include "lib/md/protos/co2.pb.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include "lib/utils/thread_manager.h"
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <bits/stdint-uintn.h>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <quickfix/FixValues.h>
|
||||
#include <thread>
|
||||
|
||||
namespace bts::md::recording {
|
||||
|
||||
template <class T>
|
||||
void writebuf(std::ofstream &out, const T &pb) {
|
||||
// auto size = static_cast<std::uint32_t>(pb.ByteSizeLong());
|
||||
// out.write(reinterpret_cast<char *>(&size), sizeof(size));
|
||||
bts_assert(pb.SerializeToOstream(&out));
|
||||
}
|
||||
|
||||
struct MDEntry {
|
||||
SecurityKey key;
|
||||
MDConsumerHandle handle;
|
||||
TimeStamp book_ts = {};
|
||||
TimeStamp trade_ts = {};
|
||||
TimeStamp misc_ts = {};
|
||||
std::array<PriceSize, 10> bids, asks;
|
||||
MDEntry(SecurityKey key, MDConsumerHandle handle)
|
||||
: key(key), handle(handle) {}
|
||||
};
|
||||
|
||||
struct MDRecordingParams {
|
||||
uint32_t total_runtime_secs = 600; // 10 mins
|
||||
uint32_t thread_sleep_time_us = 100;
|
||||
uint32_t security_scan_period_us = 1000000; // 1 sec
|
||||
uint32_t n_security_wait_cycles =
|
||||
security_scan_period_us / thread_sleep_time_us;
|
||||
};
|
||||
|
||||
std::string get_path() {
|
||||
using namespace std;
|
||||
ostringstream file = {};
|
||||
file << "marketdata"
|
||||
<< "-";
|
||||
int year, month, day, hour, minute, unused;
|
||||
datetime::ll_to_parts(
|
||||
datetime::ll_local_now(), year, month, day, hour, minute, unused,
|
||||
unused);
|
||||
file << year << setw(2) << setfill('0') << month << setw(2) << setfill('0')
|
||||
<< day << "-" << setw(2) << setfill('0') << hour << setw(2)
|
||||
<< setfill('0') << minute << ".recording";
|
||||
|
||||
auto p = config::get<config::System>().data_path() + "/md_recording/";
|
||||
boost::filesystem::create_directories(p);
|
||||
return p + file.str();
|
||||
}
|
||||
|
||||
void copy_price(const IntPrice &from, co2::PriceMessage *to) {
|
||||
to->set_mantissa(from.mantissa());
|
||||
to->set_exponent(from.exponent());
|
||||
}
|
||||
|
||||
void copy_pricesize(const PriceSize &from, co2::PriceSizeMessage *to) {
|
||||
to->set_mantissa(from.int_price().mantissa());
|
||||
to->set_exponent(from.int_price().exponent());
|
||||
to->set_size(from.direct_size());
|
||||
}
|
||||
|
||||
void copy_misc(IntPrice settlement, MiscData &misc, co2::MiscMessage *pmisc) {
|
||||
copy_price(settlement, pmisc->mutable_settlement());
|
||||
pmisc->set_settlement_date(misc.settlement_date.to_short());
|
||||
copy_price(
|
||||
misc.previous_settlement_price,
|
||||
pmisc->mutable_previous_settlement_price());
|
||||
pmisc->set_previous_settlement_date(
|
||||
misc.previous_settlement_date.to_short());
|
||||
pmisc->set_open_price(misc.open_price);
|
||||
pmisc->set_open_date(misc.open_date.to_short());
|
||||
pmisc->set_close_price(misc.close_price);
|
||||
pmisc->set_close_date(misc.close_date.to_short());
|
||||
pmisc->set_fixing_price(misc.fixing_price);
|
||||
pmisc->set_session_high(misc.session_high);
|
||||
pmisc->set_session_low(misc.session_low);
|
||||
}
|
||||
|
||||
struct MDRecorder::PImpl {
|
||||
const MDRecordingParams params = {};
|
||||
std::ofstream file = {};
|
||||
uintptr_t mdc_id = 0;
|
||||
bts::api::ServiceContext &ctx;
|
||||
bts::md::MDClient &md_client;
|
||||
bts::co2::MarketDataRelay relay_service;
|
||||
std::map<SecurityKey, MDEntry> handles;
|
||||
size_t sleep_us = 100;
|
||||
size_t sec_sleep_us = 100000;
|
||||
PImpl(bts::api::ServiceContext &ctx, bts::md::MDClient &md_client)
|
||||
: ctx(ctx), md_client(md_client),
|
||||
relay_service(ctx.locate<bts::co2::MarketDataRelay>()) {}
|
||||
};
|
||||
|
||||
MDRecorder::MDRecorder(
|
||||
bts::api::ServiceContext &ctx, bts::md::MDClient &md_client)
|
||||
: me(new PImpl(ctx, md_client)) {
|
||||
auto file_name = get_path();
|
||||
me->file.open(file_name, std::ios::out | std::ios::binary | std::ios::trunc);
|
||||
LOG(md, Info, "MDRecorder::MDRecorder file: {}", file_name);
|
||||
bts::threading::make_thread("MDRecorder", [&]() { _poll_md(); }).detach();
|
||||
}
|
||||
|
||||
MDRecorder::~MDRecorder() {}
|
||||
|
||||
void MDRecorder::_poll_md() {
|
||||
uint32_t sec_scan_counter = 0;
|
||||
while (true) {
|
||||
MDConsumer &mdc = me->md_client.current_consumer();
|
||||
if (me->mdc_id != mdc.id()) {
|
||||
LOG(md, Info, "MDRecorder::_poll_md new consumer id: {}", mdc.id());
|
||||
me->mdc_id = mdc.id();
|
||||
me->handles.clear();
|
||||
_scan_securities();
|
||||
}
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::microseconds(me->params.thread_sleep_time_us));
|
||||
if (++sec_scan_counter % me->params.n_security_wait_cycles == 0) {
|
||||
_scan_securities();
|
||||
sec_scan_counter = 0;
|
||||
}
|
||||
for (auto &kv : me->handles) {
|
||||
MDEntry &md_entry = kv.second;
|
||||
_poll_books(md_entry);
|
||||
_poll_trades(md_entry);
|
||||
_poll_misc(md_entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MDRecorder::_scan_securities() {
|
||||
auto secs = me->md_client.current_consumer().get_securities();
|
||||
LOG(md, Info, "MDRecorder::_scan_securities n_secs: {}", secs.size());
|
||||
for (auto s : secs) {
|
||||
SecurityKey key = SecurityKey(s->id());
|
||||
auto it = me->handles.find(key);
|
||||
if (it == me->handles.end()) {
|
||||
LOG(md, Info,
|
||||
"MDRecorder::_scan_securities adding sec: {}, n_secs: {}", key,
|
||||
me->handles.size());
|
||||
me->handles.emplace(
|
||||
key, MDEntry(
|
||||
key, me->md_client.current_consumer().get_handle(
|
||||
key.value())));
|
||||
bts::co2::RecordedMDMessage m;
|
||||
auto sec_p = m.mutable_security();
|
||||
sec_p->CopyFrom(*s);
|
||||
writebuf(me->file, m);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MDRecorder::_poll_books(MDEntry &md_entry) {
|
||||
TimeStamp ts;
|
||||
md_entry.handle.get_direct_book(
|
||||
ts, 10, static_cast<PriceSize *>(&md_entry.bids[0]),
|
||||
static_cast<PriceSize *>(&md_entry.asks[0]));
|
||||
if (ts > md_entry.book_ts) {
|
||||
md_entry.book_ts = ts;
|
||||
// write to file
|
||||
co2::BookMessage book;
|
||||
for (const PriceSize &ps : md_entry.bids) {
|
||||
co2::PriceSizeMessage *psm = book.add_bids();
|
||||
copy_pricesize(ps, psm);
|
||||
}
|
||||
for (const PriceSize &ps : md_entry.asks) {
|
||||
co2::PriceSizeMessage *psm = book.add_asks();
|
||||
copy_pricesize(ps, psm);
|
||||
}
|
||||
co2::RecordedMDMessage rec_msg;
|
||||
rec_msg.set_sec_key(md_entry.key.value());
|
||||
rec_msg.set_timestamp(0); // todo
|
||||
rec_msg.mutable_book()->CopyFrom(book);
|
||||
writebuf(me->file, rec_msg);
|
||||
}
|
||||
}
|
||||
|
||||
void MDRecorder::_poll_trades(MDEntry &md_entry) {
|
||||
TimeStamp ts;
|
||||
PriceSize t_ps;
|
||||
datetime::LLDateTime trade_ts;
|
||||
size_t total_volume;
|
||||
int from_spread;
|
||||
md_entry.handle.get_last_trade(
|
||||
ts, t_ps, trade_ts, &total_volume, &from_spread);
|
||||
if (ts > md_entry.trade_ts) {
|
||||
md_entry.trade_ts = ts;
|
||||
co2::TradeMessage tm;
|
||||
tm.set_key(md_entry.key.value());
|
||||
tm.set_time(trade_ts);
|
||||
tm.set_mantissa(t_ps.mantissa());
|
||||
tm.set_exponent(t_ps.exponent());
|
||||
tm.set_size(t_ps.direct_size());
|
||||
tm.set_total_volume(total_volume);
|
||||
// aggressor?
|
||||
co2::RecordedMDMessage rec_msg;
|
||||
rec_msg.set_sec_key(md_entry.key.value());
|
||||
rec_msg.set_timestamp(0); // todo
|
||||
rec_msg.mutable_trade()->CopyFrom(tm);
|
||||
writebuf(me->file, rec_msg);
|
||||
}
|
||||
}
|
||||
|
||||
void MDRecorder::_poll_misc(MDEntry &md_entry) {
|
||||
auto misc_data = md_entry.handle.misc();
|
||||
if (misc_data.misc_timestamp > md_entry.misc_ts) {
|
||||
md_entry.misc_ts = misc_data.misc_timestamp;
|
||||
co2::MiscMessage misc_msg;
|
||||
copy_misc(misc_data.previous_settlement_price, misc_data, &misc_msg);
|
||||
co2::RecordedMDMessage rec_msg;
|
||||
rec_msg.set_sec_key(md_entry.key.value());
|
||||
rec_msg.set_timestamp(0); // todo
|
||||
rec_msg.mutable_misc()->CopyFrom(misc_msg);
|
||||
writebuf(me->file, rec_msg);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace bts::md::recording
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include "lib/md/MDClient.h"
|
||||
#include "lib/utils/bts_no_copy.h"
|
||||
#include <memory>
|
||||
|
||||
namespace bts::api {
|
||||
class ServiceContext;
|
||||
}
|
||||
|
||||
namespace bts::md {
|
||||
class MDClient;
|
||||
}
|
||||
|
||||
namespace bts::md::recording {
|
||||
|
||||
struct MDEntry;
|
||||
|
||||
class MDRecorder {
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
|
||||
private:
|
||||
void _poll_md();
|
||||
void _scan_securities();
|
||||
void _poll_books(MDEntry &md_entry);
|
||||
void _poll_trades(MDEntry &md_entry);
|
||||
void _poll_misc(MDEntry &md_entry);
|
||||
|
||||
public:
|
||||
MDRecorder(bts::api::ServiceContext &context, bts::md::MDClient &);
|
||||
~MDRecorder();
|
||||
};
|
||||
} // namespace bts::md::recording
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "lib/md/MBOServiceServer.h"
|
||||
#include "lib/md/MDClient.h"
|
||||
#include "lib/md/SyntheticSecurityService.h"
|
||||
#include "lib/md/recording/MDRecorder.h"
|
||||
#include "lib/md/relay/QueryServer.h"
|
||||
#include "lib/md/service/MarketDataServiceServer.h"
|
||||
#include "lib/metrics/metrics.h"
|
||||
@@ -106,6 +107,7 @@ static void execute() {
|
||||
position::MarginServiceServer margin_service(sc, mdclient);
|
||||
metrics::ShellLatencyServer shell_latency_service(
|
||||
sc, stats, utils::Dispatcher::background());
|
||||
bts::md::recording::MDRecorder md_recorder(sc, mdclient);
|
||||
|
||||
auto bg_health =
|
||||
std::make_shared<metrics::IntCounterMetric>("spark_background_thread");
|
||||
|
||||
Reference in new issue
Block a user