Files
spark/src/lib/md/Diag/MDPeriodicDumper.cpp
T

191 lines
6.4 KiB
C++

#include "MDPeriodicDumper.h"
#include "lib/md/MDConsumer.h"
#include "lib/utils/TimeStamp.h"
#include "lib/utils/Dispatcher.h"
#include "lib/utils/datetime.h"
#include <memory>
#include <string>
#include <iostream>
#include <iomanip>
namespace bts {
namespace md {
/*
* Design considerations
*
* - We want this test to be 'fair'
* - We want a manageable amount of data
* -
*/
struct Entry {
uint32_t seq;
MDConsumerHandle handle;
std::unique_ptr<api::Security> security;
};
std::string print_book(
PrintFormat format,
const Entry& e,
utils::TimeStamp twrite,
utils::TimeStamp tread,
uint32_t seq,
bool ambiguous,
const std::array<PriceSize,COCONUT_MAX_DEPTH>& bid,
const std::array<PriceSize,COCONUT_MAX_DEPTH>& ask) {
std::stringstream ss;
if(format == PrintFormat::Human) {
ss << std::endl;
ss << e.security->glbx().security_desc() << " (" << e.handle.key() << ")" << std::endl;
ss << "Sequence: " << seq;
if (ambiguous) ss << "*";
ss << std::endl;
ss << "TWrit: " << twrite.to_uint64() << " ns" << std::endl;
ss << "TRead: " << tread.to_uint64() << " ns" << std::endl;
char buffer[512];
snprintf(buffer, 512, "%-5s %-14s | %-14s %-5s\n", "sz", "bid", "ask", "sz");
ss << buffer;
ss << "-------------------- - -------------------- \n";
for (auto x = COCONUT_MAX_DEPTH; x != 0; --x) {
auto a = ask[x-1];
if (!os::is_nan(a.price())) {
snprintf(buffer, 512, "%5s %14s | %14.7f %5d\n", "", "", a.price(), a.size());
ss << buffer;
}
}
ss << "-------------------- - -------------------- \n";
for (uint32_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
auto b = bid[x];
if (!os::is_nan(b.price())) {
snprintf(buffer, 512, "%5d %14.7f | \n", b.size(), b.price());
ss << buffer;
}
}
}
else if (format == PrintFormat::CSV) {
// Symbol, Id, Sequence, Ambiguous, TWrite, TRead, BidBook, AskBook
ss << e.security->glbx().security_desc() << ","
<< e.security->id() << ","
<< seq << ","
<< (ambiguous ? "true," : "false,")
<< (e.handle.status() == co2::COCONUTSECURITYSTATUS_OK ? "OK," : "GAP,")
<< twrite.to_uint64() << ","
<< tread.to_uint64() << ",";
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
if (!bid[x].is_nan()) {
ss << " " << std::setprecision(11) << bid[x].price() << "-";
if (bid[x].implied_size() > 0) { ss << "*"; }
ss << bid[x].size();
}
}
ss << ",";
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
if (!ask[x].is_nan()) {
ss << " " << std::setprecision(11) << ask[x].price() << "-";
if (ask[x].implied_size() > 0) { ss << "*"; }
ss << ask[x].size();
}
}
ss << std::endl;
}
return ss.str();
}
datetime::Date expiration(const api::Security& sec) {
auto f = sec.future();
return { (uint16_t)f.expiration_year(), (uint8_t)f.expiration_month(), (uint8_t)f.expiration_day() };
}
std::vector<Entry> filter_entries(std::vector<Entry>& entries) {
// Choose the front month for all symbols
auto today = datetime::today();
std::map<UserSymbol, Entry*> map;
for (auto& e : entries) {
auto type = securitytype_of(*e.security);
// Only futures because front month is complicated
if (type != api::SECURITYTYPE_FUTURE) continue;
UserSymbol key { e.security->symbol(), e.security->exchange(), type };
auto& current_entry = map[key];
if (current_entry == nullptr) {
current_entry = &e;
}
else {
auto cur = expiration(*current_entry->security);
auto next = expiration(*e.security);
if (next > today && next < cur) {
current_entry = &e;
}
}
}
std::vector<Entry> result;
for (const auto& pair : map) {
result.push_back(std::move(*pair.second));
}
return result;
}
void write_header(PrintFormat format, long seq_mod, const std::vector<Entry>& entries) {
if (format == PrintFormat::Human) {
std::cout << "Dumping every sequence % " << seq_mod << " == 0 for\n";
std::cout << "Selected securities: " << std::endl;
for (const auto& e : entries) {
std::cout << e.security->glbx().security_desc() << std::endl;
}
}
else {
// CSV header
std::cout << "Symbol,Id,Sequence,Ambiguous,Status,TWrite,TRead,BidBook,AskBook";
// for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
// std::cout << "," << "Bid" << x << ", DirectBidSize" << x << ", ImpliedBidSize" << x;
// }
// for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
// std::cout << "," << "Ask" << x << ", AskSize" << x << ", ImpliedAskSize" << x;
// }
std::cout << std::endl;
}
}
void run_periodic_dumper(const std::set<UserSymbol>& symbols, uint32_t seq_mod, PrintFormat format, bool monitor_all) {
MDConsumer consumer(symbols, nullptr);
// read all securities into a vector
std::vector<Entry> entries;
consumer.walk_security_list([&](const api::Security& sec, MDConsumerHandle handle) {
entries.push_back({ 0, handle, std::make_unique<api::Security>(sec) });
});
if (!monitor_all) {
entries = filter_entries(entries);
}
write_header(format, seq_mod, entries);
auto& disp = utils::Dispatcher::background();
// spin around on the vector detecting changes
std::array<PriceSize,COCONUT_MAX_DEPTH> bid,ask;
while(true) {
for(auto& e : entries) {
auto sequence = e.handle.sequence();
if (sequence != e.seq && sequence % seq_mod == 0) {
e.seq = sequence;
auto tread = TimeStamp::now();
utils::TimeStamp twrite;
e.handle.get_book(twrite, COCONUT_MAX_DEPTH, bid.data(), ask.data());
auto seq2 = e.handle.sequence();
// print book to stdout (async?)
disp.post([=,&e]() {
std::cout << print_book(format, e, twrite, tread, sequence, (sequence != seq2), bid, ask);
});
}
}
}
}
}}//ns