277 lines
9.5 KiB
C++
277 lines
9.5 KiB
C++
#ifndef NO_CPP0X
|
|
|
|
#include "ShmDumper.h"
|
|
|
|
#include "lib/utils/SharedList.h"
|
|
|
|
#include "../Shm/MDStructure.h"
|
|
#include "lib/logging/logging.h"
|
|
|
|
#include <iostream>
|
|
#include <iomanip>
|
|
|
|
namespace bts {
|
|
namespace md {
|
|
using namespace utils;
|
|
using namespace std;
|
|
using namespace api;
|
|
|
|
ShmDumper::ShmDumper(const string& name) : _name(name)
|
|
{}
|
|
|
|
ShmDumper::~ShmDumper() {}
|
|
|
|
double dump(TimeStamp time) {
|
|
return (TimeStamp::now() - time).seconds();
|
|
}
|
|
|
|
string dump(IntPrice price) {
|
|
stringstream res;
|
|
res << std::setprecision(11) << price.price();
|
|
return res.str();
|
|
}
|
|
string dump(PriceSize ps) {
|
|
stringstream res;
|
|
res << "{";
|
|
res << "'price': " << std::setprecision(11) << ps.price();
|
|
if (!ps.is_nan()) {
|
|
res << " (^" << (int)ps.exponent() << ")";
|
|
}
|
|
res << ", 'size': " << ps.size() << " ";
|
|
// res << "'mant': " << ps.mantissa() << ", ";
|
|
// res << "'exp': " << (int)ps.exponent() << " ";
|
|
res << "}";
|
|
return res.str();
|
|
}
|
|
|
|
string dump(const AtomicPriceSize& pss) {
|
|
return dump(std::get<PriceSize>(pss.load()));
|
|
}
|
|
|
|
string dump_header(const ColdHeader& header) {
|
|
stringstream res;
|
|
res << "{";
|
|
res << "'count': " << header.count << ", ";
|
|
res << "'segment_count': " << header.segment_count << ", ";
|
|
res << "'create_time': " << dump(header.create_time) << ", ";
|
|
res << "'heartbeat': " << dump(header.heartbeat) << ", ";
|
|
res << "'security_count': " << header.security_count << ", ";
|
|
res << "'has_depth': " << std::boolalpha << header.has_depth << ", ";
|
|
res << "'platform': '" << Platform_Name(header.platform) << "', ";
|
|
res << "'trade_count': " << header.trade_count << ", ";
|
|
res << "'coconut_status_count': " << header.coconut_status_count << ", ";
|
|
res << "'exchange_status_count': " << header.exchange_status_count << ", ";
|
|
res << "'rfq_count': " << header.rfq_count << ", ";
|
|
res << "'gaps':" << header.gap_count << ", ";
|
|
res << "}";
|
|
return res.str();
|
|
}
|
|
void write_date_string(uint32_t year, uint32_t month, uint32_t day, std::ostringstream& result);
|
|
string dump_security(const FixedSecurity& security) {
|
|
ostringstream res;
|
|
res << "{";
|
|
res << "'key': " << security.key << ", ";
|
|
res << "'symbol': '" << security.symbol() << "', ";
|
|
res << "'type': '" << api::SecurityType_Name(security.type) << "', ";
|
|
res << "'exchange': '" << api::Venue_Name(security.exchange) << "', ";
|
|
res << "'alt_symbol': '" << security.alt_symbol() << "', ";
|
|
res << "'cfi_code': '" << security.cfi_code() << "', ";
|
|
res << "'security_desc': '" << security.security_desc() << "', ";
|
|
res << "'platform_id': '" << security.platform_id() << "', ";
|
|
res << "'display_factor': " << (int)security.display_factor << ", ";
|
|
res << "'numeric_id': " << security.numeric_id << ", ";
|
|
res << "'cboe_class_key': " << security.cboe_class_key << ", ";
|
|
res << "'main_frac': " << (int)security.cme_main_fraction << ", ";
|
|
res << "'sub_fract': " << (int)security.cme_sub_fraction << ", ";
|
|
res << "'price_format': " << (int)security.cme_price_display_format << ", ";
|
|
res << "'market_segment': " << (int)security.cme_market_segment_id << ", ";
|
|
|
|
res << "'price_break': " << security.price_break << ", ";
|
|
res << "'low_tick': " << security.low_tick << ", ";
|
|
res << "'high_tick': " << security.high_tick << ", ";
|
|
res << "'cpp': " << security.cash_per_point << ", ";
|
|
|
|
switch(security.type) {
|
|
case SECURITYTYPE_STOCK: break;
|
|
case SECURITYTYPE_FUTURE: {
|
|
auto* psec = static_cast<const FixedFutureSecurity*>(&security);
|
|
res << "'expiration': '";
|
|
write_date_string(psec->expiration_year, psec->expiration_month, psec->expiration_day, res);
|
|
res << "' ";
|
|
break;
|
|
}
|
|
case SECURITYTYPE_OPTION: {
|
|
auto* psec = static_cast<const FixedOptionSecurity*>(&security);
|
|
res << "'expiration': '";
|
|
write_date_string(psec->expiration_year, psec->expiration_month, psec->expiration_day, res);
|
|
res << "', ";
|
|
res << "'strike': " <<psec->strike << ", ";
|
|
res << "'callput': '" << api::PutOrCall_Name(psec->call_put) << "' ";
|
|
break;
|
|
}
|
|
case SECURITYTYPE_SPREAD: {
|
|
auto* psec = static_cast<const FixedSpreadSecurityXL*>(&security);
|
|
res << "'legs': [";
|
|
for (size_t x = 0; x < psec->leg_count(); ++x) {
|
|
auto& leg = psec->legs[x];
|
|
if (x > 0) res << ",";
|
|
res << "{ 'key': " << leg.key
|
|
<< ", 'quantity': " << leg.leg_quantity
|
|
<< ", 'leg_price': " << leg.leg_price
|
|
<< ", 'delta': " << leg.leg_delta
|
|
<< ", 'side': '" << api::Side_Name(leg.leg_side)
|
|
<< "' }";
|
|
}
|
|
res << "]";
|
|
break;
|
|
}
|
|
default: break;
|
|
}
|
|
|
|
res << "}";
|
|
return res.str();
|
|
}
|
|
|
|
string dump(datetime::Date date) {
|
|
stringstream res;
|
|
res << "{";
|
|
res << "'year': " << date.year << ", ";
|
|
res << "'month': " << (int)date.month << ", ";
|
|
res << "'day': " << (int)date.day;
|
|
res << "}";
|
|
return res.str();
|
|
}
|
|
|
|
bool has_value(PriceSize p) { return p.size() > 0; }
|
|
bool has_value(const AtomicPriceSize& p) { return has_value(std::get<PriceSize>(p.load())); }
|
|
|
|
string dump_entry(const ColdEntry& entry) {
|
|
stringstream res;
|
|
res << "{";
|
|
res << "'security': " << dump_security(entry.security) << ", ";
|
|
if (has_value(entry.last_trade)) res << "'last_trade': " << dump(entry.last_trade) << ", ";
|
|
if (entry.total_volume > 0) res << "'total_volume': " << entry.total_volume << ", ";
|
|
if (entry.last_trade_time > 0) res << "'last_trade_time_ll': " << entry.last_trade_time << ", ";
|
|
if (entry.misc.settlement_date.year > 0) res << "'settlement_date': " << dump(entry.misc.settlement_date) << ", ";
|
|
if (!(entry.misc.previous_settlement_price.is_nan())) res << "'previous_settlement_price': " << entry.misc.previous_settlement_price.price() << ", ";
|
|
if (entry.misc.previous_settlement_date.year > 0) res << "'previous_settlement_date': " << dump(entry.misc.previous_settlement_date) << ", ";
|
|
res << "'deleted': " << std::boolalpha << entry.deleted << " ";
|
|
res << "}";
|
|
return res.str();
|
|
}
|
|
|
|
string dump_entry(const HotDepthEntry& entry) {
|
|
stringstream res;
|
|
res << "{ ";
|
|
res << "'timestamp': " << dump(entry.time()) << ", ";
|
|
res << "'key': " << entry.key << ", ";
|
|
res << "'settlement': '" << dump(entry.settlement) << "', ";
|
|
res << "'status': '" << CoconutSecurityStatus_Name((CoconutSecurityStatus)entry.status) << "', ";
|
|
res << "'sequence': '" << entry.sequence << "', ";
|
|
|
|
res << "\n 'bid': [";
|
|
for (const auto& level : entry.direct) {
|
|
if (!level.bid().is_nan()) {
|
|
res << dump(level.bid()) << ", ";
|
|
}
|
|
}
|
|
res << "], ";
|
|
|
|
res << "\n 'ask': [";
|
|
for (const auto& level : entry.direct) {
|
|
if (!level.ask().is_nan()) {
|
|
res << dump(level.ask()) << ", ";
|
|
}
|
|
}
|
|
res << "], ";
|
|
|
|
res << "\n 'implied_bid': [";
|
|
for (const auto& level : entry.implied) {
|
|
if (!level.bid().is_nan()) {
|
|
res << dump(level.bid()) << ", ";
|
|
}
|
|
}
|
|
res << "], ";
|
|
|
|
res << "\n 'implied_ask': [";
|
|
for (const auto& level : entry.implied) {
|
|
if (!level.ask().is_nan()) {
|
|
res << dump(level.ask()) << ", ";
|
|
}
|
|
}
|
|
res << "]";
|
|
|
|
res << " }";
|
|
return res.str();
|
|
}
|
|
|
|
string dump_entry(const HotEntry& entry) {
|
|
stringstream res;
|
|
res << "{ ";
|
|
res << "'timestamp': " << dump(entry.time()) << ", ";
|
|
res << "'key': " << entry.key << ", ";
|
|
res << "'bid': " << dump(entry.book.bid()) << ", ";
|
|
res << "'ask': " << dump(entry.book.ask()) << ", ";
|
|
res << "'status': '" << CoconutSecurityStatus_Name((CoconutSecurityStatus)entry.status) << "', ";
|
|
res << "'sequence': '" << entry.sequence << "', ";
|
|
res << " }";
|
|
return res.str();
|
|
}
|
|
|
|
void ShmDumper::dump_cold() {
|
|
SharedListReader<ColdHeader, ColdEntry> reader(_name+"_cold");
|
|
if (!reader.is_initialized()) { LOG(md,Info,"Unable to initialize section {}", _name); return; }
|
|
const auto& header = reader.header();
|
|
// dump the header
|
|
cout << "{ " << endl;
|
|
cout << "'header': " << dump_header(header) << "," << endl;
|
|
cout << "'entries': [" << endl;
|
|
bool first = true;
|
|
reader.all_entries([&](const ColdEntry& entry) {
|
|
if (!first) { cout << "," << endl; }
|
|
cout << dump_entry(entry);
|
|
first = false;
|
|
});
|
|
cout << endl << "]}" << endl;
|
|
}
|
|
|
|
template <class TEntry>
|
|
void dump_hot_t(SharedListReader<HotHeader, TEntry>& reader) {
|
|
if (!reader.is_initialized()) { LOG(md,Info,"Unable to initialize section "); return; }
|
|
auto& header = reader.header();
|
|
cout << "{ " << endl;
|
|
cout << "'header': { 'count': " << header.count
|
|
<< ", 'segment_count': " << header.segment_count
|
|
<< ", 'direct_count': " << header.direct_count
|
|
<< ", 'implied_count': " << header.implied_count << "}," << endl;
|
|
cout << "'entries': [ " << endl;
|
|
reader.all_entries([&](const TEntry& entry) { cout << dump_entry(entry) << "," << endl; });
|
|
cout << "] }" << endl;
|
|
}
|
|
|
|
void ShmDumper::dump_hot() {
|
|
SharedListReader<ColdHeader, ColdEntry> cold_reader(_name + "_cold");
|
|
if (!cold_reader.is_initialized())
|
|
{
|
|
LOG(md,Info,
|
|
"Unable to initialize section {}_cold - needed to "
|
|
"determine metadata.",_name);
|
|
return;
|
|
}
|
|
|
|
const auto& header = cold_reader.header();
|
|
if (header.has_depth) {
|
|
SharedListReader<HotHeader, HotDepthEntry> reader(_name + "_hot");
|
|
dump_hot_t(reader);
|
|
}
|
|
else {
|
|
SharedListReader<HotHeader, HotEntry> reader(_name + "_hot");
|
|
dump_hot_t(reader);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}}//namespace
|
|
#endif
|