refactor: Break shared mem md code up into smaller files

For easier reading/navigation
This commit is contained in:
John Colvin committed 2024-10-21 14:31:48 +00:00
1 parent e6d41cf01a
commit dd90bf183a
42 files changed
+630 -494

No files matched your search

@@ -7,8 +7,7 @@
#include "spark/domain_model/Security.h"
#include "spark/md/MBOProcessor.h"
#include "spark/md/MDProducer.h"
#include "spark/md/Shm/FixedSecurity.h"
#include "spark/md/Shm/MDStructure.h"
#include "spark/md/shm/fixed_security.h"
#include "spark/orderman/orderman_utils/fix_order_codec.h"
#include "spark/persistence/SqliteStore.h"
#include "spark/utils/Dispatcher.h"
@@ -1,7 +1,7 @@
#pragma once
#include "spark/md/PriceSize.h"
#include "spark/md/Shm/MDStructure.h"
#include "spark/md/shm/hot_depth_entry.h"
namespace bts {
namespace api {
@@ -6,9 +6,9 @@
#include "spark/md/IntPrice.h"
#include "spark/md/MDProducer.h"
#include "spark/md/PriceSize.h"
#include "spark/md/Shm/FixedSecurity.h"
#include "spark/md/common.h"
#include "spark/md/protos/co2.pb.h"
#include "spark/md/shm/fixed_security.h"
#include "spark/orderman/gateways/sim/SimExecutionGateway.h"
#include "spark/orderman/gateways/sim/SimExecutionGatewayFactory.h"
#include "spark/persistence/IDGenerator.h"
@@ -1,26 +1,21 @@
#include "MDRecorder.h"
#include "spark/api/md/MarketDataConfig.pb.h"
#include "spark/api/md/MarketDataConfigUtils.h"
#include "spark/api/services/MarketDataRelay.client.pb.h"
#include "spark/api/services/MarketDataRelay.pb.h"
#include "spark/config/config.h"
#include "spark/domain_model/Security.pb.h"
#include "spark/md/MDClient.h"
#include "spark/md/MDConsumer.h"
#include "spark/md/MDConsumerHandle.h"
#include "spark/md/PriceSize.h"
#include "spark/md/Shm/MDStructure.h"
#include "spark/md/common.h"
#include "spark/md/protos/co2.pb.h"
#include "spark/md/shm/misc_data.h"
#include "spark/utils/PBUtils.h"
#include "spark/utils/bts_assert.h"
#include "spark/utils/datetime.h"
#include "spark/utils/thread_manager.h"
#include "system/logging/logging.h"
#include "system/protobufs/config/system_config.pb.h"
#include "system/time_stamp.h"
#include <array>
#include <boost/filesystem.hpp>
#include <chrono>
#include <fstream>
+4 -1
View File
@@ -117,7 +117,10 @@ namespace mbts.co2.tests
var entry = new MDConsumerHandle.ColdEntry();
ulong entry_size;
Co2NativeTestMethods.co2_get_sample_cold(out entry_size, ref entry);
Assert.AreEqual(entry_size, (ulong)Marshal.SizeOf(entry), "If this test fails it's probably due to an addition to the FixedSecurity struct in FixedSecurity.h");
Assert.AreEqual(
entry_size,
(ulong)Marshal.SizeOf(entry),
"If this test fails it's probably due to an addition to the FixedSecurity struct in fixed_security.h");
// Last trade
(var time, var last) = entry.last_trade.load();
+4 -4
View File
@@ -127,8 +127,8 @@ adding security type
modified: src/lib/domain_model/Security.proto
modified: src/lib/md/MDConsumer.cpp
modified: src/lib/md/SecurityMapper.cpp
modified: src/lib/md/Shm/FixedSecurity.cpp
modified: src/lib/md/Shm/FixedSecurity.h
modified: src/lib/md/shm/fixed_security.cpp
modified: src/lib/md/shm/fixed_security.h
modified: src/lib/md/ccApi/CCMain.cpp
modified: src/lib/md/ccApi/CCMarketDataListener.cpp
modified: src/lib/md/relay/RelayClient.cpp
@@ -189,8 +189,8 @@ adding security type
modified: src/lib/domain_model/Security.proto
modified: src/lib/md/MDConsumer.cpp
modified: src/lib/md/SecurityMapper.cpp
modified: src/lib/md/Shm/FixedSecurity.cpp
modified: src/lib/md/Shm/FixedSecurity.h
modified: src/lib/md/shm/fixed_security.cpp
modified: src/lib/md/shm/fixed_security.h
modified: src/lib/md/ccApi/CCMain.cpp
modified: src/lib/md/ccApi/CCMarketDataListener.cpp
modified: src/lib/md/relay/RelayClient.cpp
+8 -1
View File
@@ -1,13 +1,20 @@
#ifndef NO_CPP0X
#include "ShmDumper.h"
#include "spark/md/Shm/MDStructure.h"
#include "spark/md/PriceSize.h"
#include "spark/md/shm/cold_entry.h"
#include "spark/md/shm/cold_header.h"
#include "spark/md/shm/hot_depth_entry.h"
#include "spark/md/shm/hot_entry.h"
#include "spark/md/shm/hot_header.h"
#include "spark/utils/SharedList.h"
#include "spark/utils/TimeStamp.h"
#include "system/json/json_dumper.h"
#include "system/logging/logging.h"
#include <iomanip>
#include <iostream>
#include <nlohmann/json.hpp>
namespace bts {
namespace md {
+3 -1
View File
@@ -1,7 +1,9 @@
#pragma once
#include "spark/md/Shm/FixedSecurity.h"
#include "spark/md/shm/fixed_security.h"
#include "spark/utils/bts_export.h"
#include "spark/utils/bts_no_copy.h"
#include <string>
namespace bts {
@@ -25,9 +25,9 @@
#include "spark/md/PriceSize.h"
#include "spark/md/SecurityIDRecorder.h"
#include "spark/md/SecurityIndex.h"
#include "spark/md/Shm/FixedSecurity.h"
#include "spark/md/Shm/MDStructure.h"
#include "spark/md/shm/fixed_security.h"
#include "spark/md/protos/co2.pb.h"
#include "spark/md/shm/hot_depth_entry.h"
#include "spark/orderman/actors/SODPosition.h"
#include "spark/orderman/gateways/sim/SimExecutionGateway.h"
#include "spark/orderman/gateways/sim/SimExecutionGatewayFactory.h"
@@ -4,7 +4,7 @@
#include "spark/domain_model/Security.pb.h"
#include "spark/md/GeneralSecurityIndex.h"
#include "spark/md/IMarketDataListener.h"
#include "spark/md/Shm/FixedSecurity.h"
#include "spark/md/shm/fixed_security.h"
#include "spark/persistence/IDGenerator.h"
namespace bts::md {
+6 -3
View File
@@ -2,11 +2,14 @@
#include "UserSymbol.h"
#include "domain/protobufs/trading_status.pb.h"
#include "spark/md/Shm/MDStructure.h"
#include "spark/md/shm/cold_entry.h"
#include "spark/md/shm/cold_header.h"
#include "spark/md/shm/hot_depth_entry.h"
#include "spark/md/shm/hot_entry.h"
#include "spark/sparkproto/SparkProtos.pb.h"
#include "spark/utils/bts_export.h"
#include "spark/utils/bts_no_copy.h"
#include "spark/utils/TimeStamp.h"
#include "spark/utils/sane_algorithm.h"
#include <atomic>
#include <vector>
-1
View File
@@ -2,7 +2,6 @@
#include "MDProducer.h"
#include "MemorySegmentUtils.h"
#include "SecurityIDRecorder.h"
#include "Shm/MDStructure.h"
#include "spark/domain_model/Security.pb.h"
#include "spark/md/Diag/ShmDumper.h"
#include "spark/md/ShmSegmentName.h"
+2 -1
View File
@@ -6,8 +6,9 @@
#include "MDProducerChannel.h"
#include "MDProducerHandle.h"
#include "SecurityMapper.h"
#include "Shm/FixedSecurity.h"
#include "common.h"
#include "spark/md/shm/fixed_security.h"
#include <memory>
#include <mutex>
#include <vector>
+5 -4
View File
@@ -1,14 +1,15 @@
#pragma once
#include "MDProducerChannel.h"
#include "Shm/MDStructure.h"
#include "domain/protobufs/aggressor_side.pb.h"
#include "spark/utils/bts_export.h"
#include "spark/utils/bts_no_copy.h"
#include "spark/md/shm/cold_entry.h"
#include "spark/md/shm/cold_header.h"
#include "spark/md/shm/hot_depth_entry.h"
#include "spark/md/shm/hot_entry.h"
#include "spark/md/shm/hot_header.h"
#include "spark/utils/datetime.h"
#include <tbb/concurrent_queue.h>
#include <vector>
namespace bts {
namespace co2 {
+3 -70
View File
@@ -1,18 +1,11 @@
#pragma once
#include "IntPrice.h"
#include "SizeTypes.h"
#include "common.h"
#include "spark/md/IntPrice.h"
#include "spark/md/SizeTypes.h"
#include "spark/md/protos/co2.pb.h"
#include "spark/utils/Optimistic.h"
#include "spark/utils/bts_assert.h"
#include "spark/utils/bts_no_copy.h"
#include "spark/utils/warn_ctrl.h"
#include "system/os.h"
#include <atomic>
#include <cmath>
#include <limits>
#include <nlohmann/json.hpp>
namespace bts {
@@ -95,66 +88,6 @@ typedef utils::Optimistic<PriceSize> AtomicPriceSize;
void to_json(nlohmann::json &j, const PriceSize &ps);
//
// PriceSize has grown to 12 bytes but because of 8 byte alignment it will pad
// out to 16 bytes This structure can store the same data in 24 bytes so it's
// more efficient for shared memory
struct PriceLevel {
PriceLevel() : _bid(), _ask(), _bid_size(0), _ask_size(0) {}
PriceLevel(const PriceSize &bid, const PriceSize &ask)
: _bid(bid.int_price()), _ask(ask.int_price()), _bid_size(bid.size()),
_ask_size(ask.size()) {}
IntPrice _bid;
IntPrice _ask;
Size _bid_size;
Size _ask_size;
const IntPrice &bid_price() const { return _bid; }
const IntPrice &ask_price() const { return _ask; }
Size bid_size() const { return _bid_size; }
Size ask_size() const { return _ask_size; }
bool operator==(const PriceLevel &other) const {
return _ask_size == other._ask_size && _bid_size == other._bid_size &&
_bid == other._bid && _ask == other._ask;
}
bool operator!=(const PriceLevel &other) const {
return _ask_size != other._ask_size || _bid_size != other._bid_size ||
_bid != other._bid || _ask != other._ask;
}
bool update(const PriceLevel &temp) {
if (*this != temp) {
*this = temp;
return true;
}
return false;
}
PriceSize bid() const { return PriceSize(_bid, _bid_size); }
PriceSize ask() const { return PriceSize(_ask, _ask_size); }
};
static_assert(sizeof(PriceLevel) == 24, "PriceLevel should be small");
struct ImpliedPriceLevel : private PriceLevel {
ImpliedPriceLevel() : PriceLevel() {}
ImpliedPriceLevel(const PriceSize &bid, const PriceSize &ask)
: PriceLevel(bid, ask) {}
using PriceLevel::ask_price;
using PriceLevel::ask_size;
using PriceLevel::bid_price;
using PriceLevel::bid_size;
using PriceLevel::operator==;
using PriceLevel::operator!=;
bool operator=(const PriceLevel &other) { return update(other); }
PriceSize bid() const { return PriceSize(_bid, 0, ImpliedSize(_bid_size)); }
PriceSize ask() const { return PriceSize(_ask, 0, ImpliedSize(_ask_size)); }
};
static_assert(
sizeof(ImpliedPriceLevel) == sizeof(PriceLevel),
"ImpliedPriceLevel should not be bigger than PriceLevel");
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
+1 -1
View File
@@ -8,7 +8,7 @@
#include "spark/config/config.h"
#include "spark/config/protos/market_data.pb.h"
#include "spark/domain_model/Security.h"
#include "spark/md/Shm/FixedSecurity.h"
#include "spark/md/shm/fixed_security.h"
#include "system/protobufs/config/system_config.pb.h"
#include <boost/container/flat_set.hpp>
-1
View File
@@ -2,7 +2,6 @@
#include "MDProducerHandle.h"
#include "common.h"
#include "spark/md/Shm/MDStructure.h"
#include "spark/utils/bts_no_copy.h"
#include "system/logging/logging.h"
+3 -1
View File
@@ -1,5 +1,7 @@
#pragma once
#include "Shm/FixedSecurity.h"
#include "shm/fixed_security.h"
#include <memory>
#include <vector>
-381
View File
@@ -1,381 +0,0 @@
#pragma once
#include "FixedSecurity.h"
#include "domain/protobufs/trading_status.pb.h"
#include "spark/config/config.h"
#include "spark/config/protos/market_data.pb.h"
#include "spark/md/PriceSize.h"
#include "spark/md/common.h"
#include "spark/md/protos/co2.pb.h"
#include "spark/utils/TimeStamp.h"
#include "system/bool_name.h"
#include "system/time_stamp.h"
#include <array>
#include <atomic>
#include <cstddef>
#include <nlohmann/json.hpp>
/*
* Coconut 2.0 separates data into high (hot) and low (cold) priority based on
* how fast the clients need to access the data Any data critical for the
* quoter/eyes goes in the Hot structure and is polled very fast. This data
* should be kept as small as possible and should be cache-line aligned Any
* other data goes in the Cold list and will be accessed more slowly
*/
namespace bts {
namespace md {
//
// HOT Data
///////////////////////
const uint32_t shm_version = 107;
struct ALIGNED_(CACHE_LINE) HotHeader {
size_t count;
size_t segment_count;
// Just the counters. Publisher process can compute messages/second by
// polling these counters
size_t direct_count;
size_t implied_count;
};
// These should not be cache line aligned because they are small enough for
// multiple to fit on one line easily
class HotEntry {
std::atomic<utils::TimeStamp> timestamp; // 8
public:
HotEntry()
: timestamp(utils::TimeStamp(0)), book(), key(invalid_security_key),
sequence(0), status(co2::COCONUTSECURITYSTATUS_OK) {}
uint32_t size() const { return sizeof(HotEntry); }
PriceLevel book; // 24
security_key key; // 4
uint32_t sequence; // 4
IntPrice settlement; // 8
uint8_t status; // 1
void set_time(const utils::TimeStamp &t) {
timestamp.store(t, std::memory_order_release);
}
utils::TimeStamp time() const {
return timestamp.load(std::memory_order_acquire);
}
};
static_assert(sizeof(HotEntry) <= CACHE_LINE, "HotEntry should be small");
void to_json(nlohmann::json &j, const HotHeader *h);
void to_json(nlohmann::json &j, const HotHeader &h);
void to_json(nlohmann::json &j, const HotEntry *h);
void to_json(nlohmann::json &j, const HotEntry &h);
const uint32_t COCONUT_MAX_DEPTH = 10;
const uint32_t COCONUT_MAX_IMPLIED_DEPTH = 2;
class HotDepthEntry {
std::atomic<utils::TimeStamp> timestamp; // 8
public:
HotDepthEntry()
: timestamp(utils::TimeStamp(0)), key(invalid_security_key), sequence(0),
direct(), implied(), status(co2::COCONUTSECURITYSTATUS_OK) {}
uint32_t size() const { return sizeof(HotDepthEntry); }
security_key
key; // 4 NOTE: key is at a different offset than it is in HotEntry
uint32_t sequence; // 4
IntPrice settlement; // 8
std::array<PriceLevel, COCONUT_MAX_DEPTH> direct; // 240
std::array<ImpliedPriceLevel, COCONUT_MAX_IMPLIED_DEPTH> implied; // 48
uint8_t status; // 1
void read_direct(uint32_t depth, PriceSize *bid, PriceSize *ask) const {
*bid = direct[depth].bid();
*ask = direct[depth].ask();
}
void read_implied(uint32_t depth, PriceSize *bid, PriceSize *ask) const {
*bid = implied[depth].bid();
*ask = implied[depth].ask();
}
bool write_direct(
uint32_t depth, const PriceSize &bid, const PriceSize &ask) {
bts_assert_d(depth < direct.size());
auto temp = PriceLevel(bid, ask);
if (direct[depth] != temp) {
direct[depth] = temp;
return true;
}
return false;
}
bool write_implied(
uint32_t depth, const PriceSize &bid, const PriceSize &ask) {
bts_assert_d(depth < implied.size());
auto temp = PriceLevel(bid, ask);
if (implied[depth] != temp) {
implied[depth] = temp;
return true;
}
return false;
}
void set_time(const utils::TimeStamp &t) {
timestamp.store(t, std::memory_order_release);
}
utils::TimeStamp time() const {
return timestamp.load(std::memory_order_acquire);
}
};
static_assert(sizeof(HotDepthEntry) <= 320, "HotDepthEntry should be small");
void to_json(nlohmann::json &j, const HotDepthEntry *h);
void to_json(nlohmann::json &j, const HotDepthEntry &h);
//
// COLD Data, brrr
///////////////////////
struct ALIGNED_(CACHE_LINE) ColdHeader {
ColdHeader()
: version(shm_version), count(0), segment_count(0),
create_time(utils::TimeStamp::now()), heartbeat(),
disable_heartbeat(
config::get<md::PublisherOptions>().disable_heartbeat()),
heartbeat_ms(config::get<md::PublisherOptions>().heartbeat_ms()),
heartbeat_boot_seconds(
config::get<md::PublisherOptions>().heartbeat_boot_seconds()),
security_count(0), has_depth(false), trade_count(0),
coconut_status_count(0), exchange_status_count(0), rfq_count(0) {}
uint32_t version;
size_t count;
size_t segment_count;
utils::TimeStamp create_time;
utils::TimeStamp heartbeat;
bool disable_heartbeat;
uint32_t heartbeat_ms;
uint32_t heartbeat_boot_seconds;
uint32_t security_count; // incremented when a security is made public
bool has_depth;
std::atomic<size_t> trade_count;
std::atomic<size_t> coconut_status_count;
std::atomic<size_t> exchange_status_count;
std::atomic<size_t> rfq_count;
std::atomic<size_t> gap_count;
// Ring buffer for LastTrade for all securities in this list
uint8_t trade_buffer[1024 * 1024 * 4]; // 4mb
// Ring buffer for RFQ
// static const int rfq_buffer_entry_size = 64;
typedef std::array<uint8_t, 64> RFQBuffer;
uint8_t rfq_buffer[1024 * 256]; // 256k
};
struct MiscData {
MiscData()
: misc_timestamp(), settlement_date(),
previous_settlement_price(INT_NAN), previous_settlement_date(),
open_price(NO_PRICE), open_date(), close_price(NO_PRICE), close_date(),
session_high(NO_PRICE), session_low(NO_PRICE),
session_high_bid(NO_PRICE), session_low_offer(NO_PRICE),
fixing_price(NO_PRICE) {}
utils::TimeStamp misc_timestamp;
// Settlement
datetime::Date settlement_date;
IntPrice previous_settlement_price;
datetime::Date previous_settlement_date;
// Opening/Closing
Price open_price;
datetime::Date open_date;
Price close_price;
datetime::Date close_date;
// Misc stats for current session
Price session_high;
Price session_low;
Price session_high_bid;
Price session_low_offer;
Price fixing_price;
bool operator==(const MiscData &other) const {
return settlement_date == other.settlement_date &&
previous_settlement_price == other.previous_settlement_price &&
previous_settlement_date == other.previous_settlement_date &&
open_price == other.open_price && open_date == other.open_date &&
close_price == other.close_price &&
close_date == other.close_date &&
session_high == other.session_high &&
session_low == other.session_low &&
session_high_bid == other.session_high_bid &&
session_low_offer == other.session_low_offer &&
fixing_price == other.fixing_price;
}
bool operator!=(const MiscData &other) const { return !(*this == other); }
};
struct ColdEntry {
ColdEntry(size_t security_size)
: exchange_status(domain::protobufs::TRADINGSTATUS_UNKNOWN),
last_trade(), total_volume(0), last_trade_time(), misc(),
from_spread(0), deleted(0) {
this->security.size = security_size;
}
static size_t fixed_size() {
return sizeof(ColdEntry) - sizeof(FixedSecurity);
}
size_t size() const { return fixed_size() + this->security.size; }
domain::protobufs::TradingStatus exchange_status;
// Last Trade
AtomicPriceSize last_trade;
size_t total_volume;
datetime::LLDateTime last_trade_time;
MiscData misc;
int from_spread;
int deleted;
FixedSecurity security;
};
void to_json(nlohmann::json &j, const ColdHeader *c);
void to_json(nlohmann::json &j, const ColdHeader &c);
void to_json(nlohmann::json &j, const ColdEntry *c);
void to_json(nlohmann::json &j, const ColdEntry &c);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const HotHeader *h) { to_json(j, *h); }
inline void to_json(nlohmann::json &j, const HotHeader &h) {
j = {
{"count", h.count},
{"segment_count", h.segment_count},
{"direct_count", h.direct_count},
{"implied_count", h.implied_count},
};
}
inline void to_json(nlohmann::json &j, const HotEntry *h) { to_json(j, *h); }
inline void to_json(nlohmann::json &j, const HotEntry &h) {
const core::TimeStamp timestamp{h.time().to_uint64()};
j = {
{"timestamp", timestamp.to_iso_string()},
{"key", h.key},
{"bid", h.book.bid()},
{"ask", h.book.ask()},
{"status",
co2::CoconutSecurityStatus_Name((CoconutSecurityStatus)h.status)},
{"seq", h.sequence},
};
}
inline void to_json(nlohmann::json &j, const HotDepthEntry *h) {
to_json(j, *h);
}
inline void to_json(nlohmann::json &j, const HotDepthEntry &h) {
const core::TimeStamp timestamp{h.time().to_uint64()};
j = {
{"timestamp", timestamp.to_iso_string()},
{"key", h.key},
{"settle", h.settlement.price()},
{"status",
co2::CoconutSecurityStatus_Name((co2::CoconutSecurityStatus)h.status)},
{"seq", h.sequence},
};
std::vector<PriceSize> bids;
std::vector<PriceSize> asks;
for (const auto &l : h.direct) {
if (!l.bid().is_nan()) {
bids.push_back(l.bid());
}
if (!l.ask().is_nan()) {
asks.push_back(l.ask());
}
}
j["bids"] = bids;
j["asks"] = asks;
std::vector<PriceSize> ibids;
std::vector<PriceSize> iasks;
for (const auto &l : h.implied) {
if (!l.bid().is_nan()) {
ibids.push_back(l.bid());
}
if (!l.ask().is_nan()) {
iasks.push_back(l.ask());
}
}
j["ibids"] = ibids;
j["iasks"] = iasks;
}
inline void to_json(nlohmann::json &j, const ColdHeader *c) { to_json(j, *c); }
inline void to_json(nlohmann::json &j, const ColdHeader &c) {
j = {
{"version", c.version},
{"count", c.count},
{"segment_count", c.segment_count},
{"create_time",
core::TimeStamp{c.create_time.to_uint64()}.to_iso_string()},
{"hb", core::TimeStamp{c.heartbeat.to_uint64()}.to_iso_string()},
{"disable_hb", core::BoolName::yes_no(c.disable_heartbeat)},
{"hb_ms", c.heartbeat_ms},
{"hb_boot_secs", c.heartbeat_boot_seconds},
{"sec_count", c.security_count},
{"has_depth", core::BoolName::yes_no(c.has_depth)},
{"trade_count", static_cast<std::size_t>(c.trade_count)},
{"coconut_status_count",
static_cast<std::size_t>(c.coconut_status_count)},
{"exchange_status_count",
static_cast<std::size_t>(c.exchange_status_count)},
{"rfq_count", static_cast<std::size_t>(c.rfq_count)},
{"gap_count", static_cast<std::size_t>(c.gap_count)},
};
}
inline void to_json(nlohmann::json &j, const ColdEntry *c) { to_json(j, *c); }
inline void to_json(nlohmann::json &j, const ColdEntry &c) {
j = {{"security", c.security}};
const auto last_trade = std::get<PriceSize>(c.last_trade.load());
j["last_trade_price"] = last_trade.size() > 0 ? last_trade.price() : 0;
j["last_trade_qty"] = last_trade.size();
j["last_trade_time"] = c.last_trade_time;
j["volume"] = c.total_volume;
core::TimeStamp settle_date;
core::TimeStamp::from_ymd(
c.misc.settlement_date.year, c.misc.settlement_date.month,
c.misc.settlement_date.day, &settle_date);
j["settle_date"] = settle_date.to_iso_string();
j["prev_settle_price"] = c.misc.previous_settlement_price.price();
core::TimeStamp prev_settle_date;
core::TimeStamp::from_ymd(
c.misc.previous_settlement_date.year,
c.misc.previous_settlement_date.month,
c.misc.previous_settlement_date.day, &prev_settle_date);
j["prev_settle_date"] = prev_settle_date.to_iso_string();
j["status"] = domain::protobufs::TradingStatus_Name(c.exchange_status);
j["deleted"] = core::BoolName::yes_no(c.deleted);
}
inline bool is_heart_beat_ok(const ColdHeader &header) {
uint32_t s_heartbeat_ms = header.heartbeat_ms;
bool s_disable_heartbeat = header.disable_heartbeat;
uint32_t s_grace_period_ms = header.heartbeat_boot_seconds * 1000;
TimeStamp hb = header.heartbeat;
TimeStamp now = TimeStamp::now();
return (header.version == shm_version) &&
(s_disable_heartbeat || (now - hb).milliseconds() < s_heartbeat_ms ||
(now - header.create_time).milliseconds() < s_grace_period_ms);
}
} // namespace md
} // namespace bts
+1 -1
View File
@@ -1,7 +1,7 @@
#pragma once
#include "spark/md/Shm/FixedSecurity.h"
#include "spark/md/cme/CMEChannel.h"
#include "spark/md/shm/fixed_security.h"
#include <vector>
+1 -1
View File
@@ -4,8 +4,8 @@
#include "SecurityIDRecorder.h"
#include "SecurityIndex.h"
#include "SettlementRecorder.h"
#include "Shm/FixedSecurity.h"
#include "SymbolLoader.h"
#include "common.h"
#include "spark/md/shm/fixed_security.h"
// TODO: separate coconut2.h into consumer and producer sides
@@ -0,0 +1,26 @@
#pragma once
#include "spark/md/price_level.h"
namespace bts::md {
struct ImpliedPriceLevel : private PriceLevel {
ImpliedPriceLevel() : PriceLevel() {}
ImpliedPriceLevel(const PriceSize &bid, const PriceSize &ask)
: PriceLevel(bid, ask) {}
using PriceLevel::ask_price;
using PriceLevel::ask_size;
using PriceLevel::bid_price;
using PriceLevel::bid_size;
using PriceLevel::operator==;
using PriceLevel::operator!=;
bool operator=(const PriceLevel &other) { return update(other); }
PriceSize bid() const { return PriceSize(_bid, 0, ImpliedSize(_bid_size)); }
PriceSize ask() const { return PriceSize(_ask, 0, ImpliedSize(_ask_size)); }
};
static_assert(
sizeof(ImpliedPriceLevel) == sizeof(PriceLevel),
"ImpliedPriceLevel should not be bigger than PriceLevel");
} // namespace bts::md
+7 -2
View File
@@ -2,11 +2,16 @@
#include "spark/domain_model/Security.pb.h"
#include "spark/md/MemorySegmentUtils.h"
#include "spark/md/Shm/FixedSecurity.h"
#include "spark/md/Shm/MDStructure.h"
#include "spark/md/ShmSegmentName.h"
#include "spark/md/queue_struct.h"
#include "spark/md/shm/cold_entry.h"
#include "spark/md/shm/cold_header.h"
#include "spark/md/shm/fixed_security.h"
#include "spark/md/shm/hot_depth_entry.h"
#include "spark/md/shm/hot_entry.h"
#include "spark/md/shm/hot_header.h"
#include "spark/utils/SharedList.h"
#include <btree/map.h>
namespace bts::md {
+50
View File
@@ -0,0 +1,50 @@
#pragma once
#include "spark/md/PriceSize.h"
namespace bts::md {
//
// PriceSize has grown to 12 bytes but because of 8 byte alignment it will pad
// out to 16 bytes This structure can store the same data in 24 bytes so it's
// more efficient for shared memory
struct PriceLevel {
PriceLevel() : _bid(), _ask(), _bid_size(0), _ask_size(0) {}
PriceLevel(const PriceSize &bid, const PriceSize &ask)
: _bid(bid.int_price()), _ask(ask.int_price()), _bid_size(bid.size()),
_ask_size(ask.size()) {}
IntPrice _bid;
IntPrice _ask;
Size _bid_size;
Size _ask_size;
const IntPrice &bid_price() const { return _bid; }
const IntPrice &ask_price() const { return _ask; }
Size bid_size() const { return _bid_size; }
Size ask_size() const { return _ask_size; }
bool operator==(const PriceLevel &other) const {
return _ask_size == other._ask_size && _bid_size == other._bid_size &&
_bid == other._bid && _ask == other._ask;
}
bool operator!=(const PriceLevel &other) const {
return _ask_size != other._ask_size || _bid_size != other._bid_size ||
_bid != other._bid || _ask != other._ask;
}
bool update(const PriceLevel &temp) {
if (*this != temp) {
*this = temp;
return true;
}
return false;
}
PriceSize bid() const { return PriceSize(_bid, _bid_size); }
PriceSize ask() const { return PriceSize(_ask, _ask_size); }
};
static_assert(sizeof(PriceLevel) == 24, "PriceLevel should be small");
} // namespace bts::md
+2 -4
View File
@@ -28,7 +28,7 @@ The MDConsumer is the dual of the MDProducer. It provides an on_security() call
* SecurityMapper*
* SettlementRecorder
* SyntheticSecurityService
* FixedSecurity.h / MDStructure.h
* fixed_security.h / cold_header.h / cold_entry.h / hot_header.h / hot_entry.h / hot_depth_entry.h
* SharedList.h
## MBOProcessor*
@@ -125,11 +125,9 @@ In Edge SettlementRecorder stored the settlements for a security so we could pre
Each synthetic security is a api::Security and a chunk of metadata. This service handles API requests to create/update synthetics and fires callbacks for MDConsumer which does the work of producing synethic books.
## MDStructure.h
### Hot vs Cold
## FixedSecurity.h
## fixed_security.h
* variable sized structure in shared mem
* For FixedSpreadSecurity we stack allocate a lot of legs then memcpy() into shared memory only the correct number of legs
* When accessing FixedSpreadSecurity it is cast back to a large number of legs. Iterate legs based on leg_count() not the static leg count.
+32
View File
@@ -0,0 +1,32 @@
#include "cold_entry.h"
#include <nlohmann/json.hpp>
namespace bts::md {
void to_json(nlohmann::json &j, const ColdEntry *c) { to_json(j, *c); }
void to_json(nlohmann::json &j, const ColdEntry &c) {
j = {{"security", c.security}};
const auto last_trade = std::get<PriceSize>(c.last_trade.load());
j["last_trade_price"] = last_trade.size() > 0 ? last_trade.price() : 0;
j["last_trade_qty"] = last_trade.size();
j["last_trade_time"] = c.last_trade_time;
j["volume"] = c.total_volume;
core::TimeStamp settle_date;
core::TimeStamp::from_ymd(
c.misc.settlement_date.year, c.misc.settlement_date.month,
c.misc.settlement_date.day, &settle_date);
j["settle_date"] = settle_date.to_iso_string();
j["prev_settle_price"] = c.misc.previous_settlement_price.price();
core::TimeStamp prev_settle_date;
core::TimeStamp::from_ymd(
c.misc.previous_settlement_date.year,
c.misc.previous_settlement_date.month,
c.misc.previous_settlement_date.day, &prev_settle_date);
j["prev_settle_date"] = prev_settle_date.to_iso_string();
j["status"] = domain::protobufs::TradingStatus_Name(c.exchange_status);
j["deleted"] = c.deleted;
}
} // namespace bts::md
+43
View File
@@ -0,0 +1,43 @@
#pragma once
#include "domain/protobufs/trading_status.pb.h"
#include "spark/md/PriceSize.h"
#include "spark/md/shm/fixed_security.h"
#include "spark/md/shm/misc_data.h"
#include <cstddef>
#include <nlohmann/json_fwd.hpp>
namespace bts::md {
struct ColdEntry {
ColdEntry(std::size_t security_size)
: exchange_status(domain::protobufs::TRADINGSTATUS_UNKNOWN),
last_trade(), total_volume(0), last_trade_time(), misc(),
from_spread(0), deleted(0) {
this->security.size = security_size;
}
static size_t fixed_size() {
return sizeof(ColdEntry) - sizeof(FixedSecurity);
}
size_t size() const { return fixed_size() + this->security.size; }
domain::protobufs::TradingStatus exchange_status;
// Last Trade
AtomicPriceSize last_trade;
size_t total_volume;
datetime::LLDateTime last_trade_time;
MiscData misc;
int from_spread;
int deleted;
FixedSecurity security;
};
void to_json(nlohmann::json &j, const ColdEntry *c);
void to_json(nlohmann::json &j, const ColdEntry &c);
} // namespace bts::md
@@ -0,0 +1,33 @@
#include "cold_header.h"
#include "system/time_stamp.h"
#include <nlohmann/json.hpp>
namespace bts::md {
using std::size_t;
void to_json(nlohmann::json &j, const ColdHeader *c) { to_json(j, *c); }
void to_json(nlohmann::json &j, const ColdHeader &c) {
j = {
{"version", c.version},
{"count", c.count},
{"segment_count", c.segment_count},
{"create_time",
core::TimeStamp{c.create_time.to_uint64()}.to_iso_string()},
{"hb", core::TimeStamp{c.heartbeat.to_uint64()}.to_iso_string()},
{"disable_hb", c.disable_heartbeat},
{"hb_ms", c.heartbeat_ms},
{"hb_boot_secs", c.heartbeat_boot_seconds},
{"sec_count", c.security_count},
{"has_depth", c.has_depth},
{"trade_count", static_cast<size_t>(c.trade_count)},
{"coconut_status_count", static_cast<size_t>(c.coconut_status_count)},
{"exchange_status_count", static_cast<size_t>(c.exchange_status_count)},
{"rfq_count", static_cast<size_t>(c.rfq_count)},
{"gap_count", static_cast<size_t>(c.gap_count)},
};
}
} // namespace bts::md
+77
View File
@@ -0,0 +1,77 @@
#pragma once
#include "spark/config/config.h"
#include "spark/config/protos/market_data.pb.h"
#include "spark/md/shm/shm_version.h"
#include "spark/utils/TimeStamp.h"
#include "system/os.h"
#include <cstddef>
#include <cstdint>
#include <nlohmann/json_fwd.hpp>
namespace bts::md {
//
// COLD Data, brrr
///////////////////////
struct ALIGNED_(CACHE_LINE) ColdHeader {
ColdHeader()
: version(shm_version), count(0), segment_count(0),
create_time(utils::TimeStamp::now()), heartbeat(),
disable_heartbeat(
config::get<md::PublisherOptions>().disable_heartbeat()),
heartbeat_ms(config::get<md::PublisherOptions>().heartbeat_ms()),
heartbeat_boot_seconds(
config::get<md::PublisherOptions>().heartbeat_boot_seconds()),
security_count(0), has_depth(false), trade_count(0),
coconut_status_count(0), exchange_status_count(0), rfq_count(0) {}
std::uint32_t version;
std::size_t count;
std::size_t segment_count;
utils::TimeStamp create_time;
utils::TimeStamp heartbeat;
bool disable_heartbeat;
std::uint32_t heartbeat_ms;
std::uint32_t heartbeat_boot_seconds;
std::uint32_t security_count; // incremented when a security is made public
bool has_depth;
std::atomic<std::size_t> trade_count;
std::atomic<std::size_t> coconut_status_count;
std::atomic<std::size_t> exchange_status_count;
std::atomic<std::size_t> rfq_count;
std::atomic<std::size_t> gap_count;
// Ring buffer for LastTrade for all securities in this list
std::uint8_t trade_buffer[1024 * 1024 * 4]; // 4mb
// Ring buffer for RFQ
// static const int rfq_buffer_entry_size = 64;
typedef std::array<std::uint8_t, 64> RFQBuffer;
std::uint8_t rfq_buffer[1024 * 256]; // 256k
};
bool is_heart_beat_ok(const ColdHeader &header);
void to_json(nlohmann::json &j, const ColdHeader *c);
void to_json(nlohmann::json &j, const ColdHeader &c);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline bool is_heart_beat_ok(const ColdHeader &header) {
std::uint32_t s_heartbeat_ms = header.heartbeat_ms;
bool s_disable_heartbeat = header.disable_heartbeat;
std::uint32_t s_grace_period_ms = header.heartbeat_boot_seconds * 1000;
utils::TimeStamp hb = header.heartbeat;
utils::TimeStamp now = utils::TimeStamp::now();
return (header.version == shm_version) &&
(s_disable_heartbeat || (now - hb).milliseconds() < s_heartbeat_ms ||
(now - header.create_time).milliseconds() < s_grace_period_ms);
}
} // namespace bts::md
@@ -3,7 +3,8 @@
#ifndef NO_CPP0X
#include "../PriceSize.h"
#include "FixedSecurity.h"
#include "spark/md/shm/fixed_security.h"
#include <iomanip>
#ifdef _WIN32
@@ -0,0 +1,42 @@
#include "hot_depth_entry.h"
#include "system/time_stamp.h"
namespace bts::md {
void to_json(nlohmann::json &j, const HotDepthEntry &h) {
const core::TimeStamp timestamp{h.time().to_uint64()};
j = {
{"timestamp", timestamp.to_iso_string()},
{"key", h.key},
{"settle", h.settlement.price()},
{"status",
co2::CoconutSecurityStatus_Name((co2::CoconutSecurityStatus)h.status)},
{"seq", h.sequence},
};
std::vector<PriceSize> bids;
std::vector<PriceSize> asks;
for (const auto &l : h.direct) {
if (!l.bid().is_nan()) {
bids.push_back(l.bid());
}
if (!l.ask().is_nan()) {
asks.push_back(l.ask());
}
}
j["bids"] = bids;
j["asks"] = asks;
std::vector<PriceSize> ibids;
std::vector<PriceSize> iasks;
for (const auto &l : h.implied) {
if (!l.bid().is_nan()) {
ibids.push_back(l.bid());
}
if (!l.ask().is_nan()) {
iasks.push_back(l.ask());
}
}
j["ibids"] = ibids;
j["iasks"] = iasks;
}
} // namespace bts::md
@@ -0,0 +1,102 @@
#pragma once
#include "spark/md/PriceSize.h"
#include "spark/md/common.h"
#include "spark/md/implied_price_level.h"
#include "spark/md/price_level.h"
#include "spark/md/protos/co2.pb.h"
#include "spark/utils/TimeStamp.h"
#include <array>
#include <atomic>
#include <cstdint>
#include <nlohmann/json.hpp>
namespace bts::md {
const std::uint32_t COCONUT_MAX_DEPTH = 10;
const std::uint32_t COCONUT_MAX_IMPLIED_DEPTH = 2;
class HotDepthEntry {
std::atomic<utils::TimeStamp> timestamp; // 8
public:
HotDepthEntry()
: timestamp(utils::TimeStamp(0)), key(invalid_security_key), sequence(0),
direct(), implied(), status(co2::COCONUTSECURITYSTATUS_OK) {}
std::uint32_t size() const { return sizeof(HotDepthEntry); }
security_key
key; // 4 NOTE: key is at a different offset than it is in HotEntry
std::uint32_t sequence; // 4
IntPrice settlement; // 8
std::array<PriceLevel, COCONUT_MAX_DEPTH> direct; // 240
std::array<ImpliedPriceLevel, COCONUT_MAX_IMPLIED_DEPTH> implied; // 48
std::uint8_t status; // 1
void read_direct(std::uint32_t depth, PriceSize *bid, PriceSize *ask) const;
void read_implied(std::uint32_t depth, PriceSize *bid, PriceSize *ask) const;
bool write_direct(
std::uint32_t depth, const PriceSize &bid, const PriceSize &ask);
bool write_implied(
std::uint32_t depth, const PriceSize &bid, const PriceSize &ask);
void set_time(const utils::TimeStamp &t);
utils::TimeStamp time() const;
};
static_assert(sizeof(HotDepthEntry) <= 320, "HotDepthEntry should be small");
void to_json(nlohmann::json &j, const HotDepthEntry *h);
void to_json(nlohmann::json &j, const HotDepthEntry &h);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void HotDepthEntry::read_direct(
std::uint32_t depth, PriceSize *bid, PriceSize *ask) const {
*bid = direct[depth].bid();
*ask = direct[depth].ask();
}
inline void HotDepthEntry::read_implied(
std::uint32_t depth, PriceSize *bid, PriceSize *ask) const {
*bid = implied[depth].bid();
*ask = implied[depth].ask();
}
inline bool HotDepthEntry::write_direct(
std::uint32_t depth, const PriceSize &bid, const PriceSize &ask) {
bts_assert_d(depth < direct.size());
auto temp = PriceLevel(bid, ask);
if (direct[depth] != temp) {
direct[depth] = temp;
return true;
}
return false;
}
inline bool HotDepthEntry::write_implied(
std::uint32_t depth, const PriceSize &bid, const PriceSize &ask) {
bts_assert_d(depth < implied.size());
auto temp = PriceLevel(bid, ask);
if (implied[depth] != temp) {
implied[depth] = temp;
return true;
}
return false;
}
inline void HotDepthEntry::set_time(const utils::TimeStamp &t) {
timestamp.store(t, std::memory_order_release);
}
inline utils::TimeStamp HotDepthEntry::time() const {
return timestamp.load(std::memory_order_acquire);
}
inline void to_json(nlohmann::json &j, const HotDepthEntry *h) {
to_json(j, *h);
}
} // namespace bts::md
+61
View File
@@ -0,0 +1,61 @@
#pragma once
#include "spark/md/common.h"
#include "spark/md/price_level.h"
#include "spark/md/protos/co2.pb.h"
#include "spark/utils/TimeStamp.h"
#include "system/time_stamp.h"
#include <atomic>
#include <cstdint>
#include <nlohmann/json.hpp>
namespace bts::md {
// These should not be cache line aligned because they are small enough for
// multiple to fit on one line easily
class HotEntry {
std::atomic<utils::TimeStamp> timestamp{utils::TimeStamp{0}}; // 8
public:
uint32_t size() const { return sizeof(HotEntry); }
PriceLevel book{}; // 24
std::uint32_t key{invalid_security_key}; // 4
std::uint32_t sequence{0}; // 4
IntPrice settlement; // 8
std::uint8_t status{co2::COCONUTSECURITYSTATUS_OK}; // 1
void set_time(const utils::TimeStamp &t) {
timestamp.store(t, std::memory_order_release);
}
utils::TimeStamp time() const {
return timestamp.load(std::memory_order_acquire);
}
};
static_assert(sizeof(HotEntry) <= CACHE_LINE, "HotEntry should be small");
void to_json(nlohmann::json &j, const HotEntry *h);
void to_json(nlohmann::json &j, const HotEntry &h);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const HotEntry *h) { to_json(j, *h); }
inline void to_json(nlohmann::json &j, const HotEntry &h) {
const core::TimeStamp timestamp{h.time().to_uint64()};
j = {
{"timestamp", timestamp.to_iso_string()},
{"key", h.key},
{"bid", h.book.bid()},
{"ask", h.book.ask()},
{"status",
co2::CoconutSecurityStatus_Name((CoconutSecurityStatus)h.status)},
{"seq", h.sequence},
};
}
} // namespace bts::md
+40
View File
@@ -0,0 +1,40 @@
#pragma once
#include "system/os.h"
#include <nlohmann/json.hpp>
namespace bts::md {
//
// HOT Data
///////////////////////
struct ALIGNED_(CACHE_LINE) HotHeader {
size_t count;
size_t segment_count;
// Just the counters. Publisher process can compute messages/second by
// polling these counters
size_t direct_count;
size_t implied_count;
};
void to_json(nlohmann::json &j, const HotHeader *h);
void to_json(nlohmann::json &j, const HotHeader &h);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void to_json(nlohmann::json &j, const HotHeader *h) { to_json(j, *h); }
inline void to_json(nlohmann::json &j, const HotHeader &h) {
j = {
{"count", h.count},
{"segment_count", h.segment_count},
{"direct_count", h.direct_count},
{"implied_count", h.implied_count},
};
}
} // namespace bts::md
+52
View File
@@ -0,0 +1,52 @@
#pragma once
#include "spark/md/IntPrice.h"
#include "spark/utils/TimeStamp.h"
#include "spark/utils/datetime.h"
namespace bts::md {
struct MiscData {
MiscData()
: misc_timestamp(), settlement_date(),
previous_settlement_price(INT_NAN), previous_settlement_date(),
open_price(NO_PRICE), open_date(), close_price(NO_PRICE), close_date(),
session_high(NO_PRICE), session_low(NO_PRICE),
session_high_bid(NO_PRICE), session_low_offer(NO_PRICE),
fixing_price(NO_PRICE) {}
utils::TimeStamp misc_timestamp;
// Settlement
datetime::Date settlement_date;
IntPrice previous_settlement_price;
datetime::Date previous_settlement_date;
// Opening/Closing
double open_price;
datetime::Date open_date;
double close_price;
datetime::Date close_date;
// Misc stats for current session
double session_high;
double session_low;
double session_high_bid;
double session_low_offer;
double fixing_price;
bool operator==(const MiscData &other) const {
return settlement_date == other.settlement_date &&
previous_settlement_price == other.previous_settlement_price &&
previous_settlement_date == other.previous_settlement_date &&
open_price == other.open_price && open_date == other.open_date &&
close_price == other.close_price &&
close_date == other.close_date &&
session_high == other.session_high &&
session_low == other.session_low &&
session_high_bid == other.session_high_bid &&
session_low_offer == other.session_low_offer &&
fixing_price == other.fixing_price;
}
bool operator!=(const MiscData &other) const { return !(*this == other); }
};
} // namespace bts::md
File renamed without changes.
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#include <cstdint>
namespace bts::md {
const std::uint32_t shm_version = 107;
} // namespace bts::md
@@ -1,6 +1,7 @@
#ifndef _WIN32
#include "../MDConsumerHandle.h"
#include "Co2TestInstrument.h"
#include "spark/utils/TimeStamp.h"
#include "spark/utils/sane_algorithm.h"
#include "md_test_utils.h"
#include <algorithm>
@@ -8,7 +8,6 @@
#include "spark/md/GeneralSecurityIndex.h"
#include "spark/md/IntPrice.h"
#include "spark/md/PriceSize.h"
#include "spark/md/Shm/MDStructure.h"
#include "spark/orderman/OrderMsg.h"
#include "spark/orderman/events/NewAdminDataEvent.h"
#include "spark/orderman/events/NewSecuritiesEvent.h"
@@ -6,14 +6,15 @@
#include "spark/md/GeneralSecurityIndex.h"
#include "spark/md/MDClient.h"
#include "spark/md/MDProducer.h"
#include "spark/md/Shm/FixedSecurity.h"
#include "spark/md/protos/co2.pb.h"
#include "spark/md/shm/fixed_security.h"
#include "spark/orderman/CancelOrderMsg.h"
#include "spark/orderman/CancelReplaceOrderMsg.h"
#include "spark/orderman/CreateOrderMsg.h"
#include "spark/orderman/OrderMsg.h"
#include "spark/orderman/gateways/IExecutionGateway.h"
#include "spark/orderman/orderman_utils/orderman_common.h"
#include <tuple>
namespace bts::md {
@@ -1,5 +1,6 @@
#pragma once
#include "spark/domain_model/SecurityKey.h"
#include "spark/md/PriceSize.h"
#include "system/FMT_IMPL.h"