Files
spark/src/lib/md/Structure/structure.h
T
2017-09-21 16:51:48 -04:00

217 lines
6.5 KiB
C++

#pragma once
#include "FixedSecurity.h"
#include "PriceSize.h"
#include "lib/config/config.h"
#include "../common.h"
#include "lib/utils/TimeStamp.h"
#include "lib/config/protos/MarketData.pb.h"
#include <atomic>
#include <array>
/*
* 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 md2 {
//
// HOT Data
///////////////////////
using utils::TimeStamp;
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 bbo_count;
size_t depth_count;
};
// These should not be cache line aligned because they are small enough for multiple to fit on one line easily
struct ALIGNED_(32) HotEntry {
HotEntry() : timestamp(), bid(), ask(), key(invalid_security_key), sequence(0), status(co2::COCONUTSECURITYSTATUS_OK) {}
uint32_t size() const { return sizeof(HotEntry); }
TimeStamp timestamp; // 8
SingleWriterPriceSizeStore bid; // 8
SingleWriterPriceSizeStore ask; // 8
security_key key; // 4
uint16_t sequence; // 2
uint8_t status; // 1
};
const uint32_t COCONUT_MAX_DEPTH = 10;
struct ALIGNED_(CACHE_LINE) HotDepthEntry {
HotDepthEntry() :
timestamp(), key(invalid_security_key), status(co2::COCONUTSECURITYSTATUS_OK), book(), sequence(0)
{ }
uint32_t size() const { return sizeof(HotDepthEntry); }
TimeStamp timestamp; // 8
security_key key; // NOTE: key is at a different offset than it is in HotEntry
uint8_t status; // 1
// Interleave bid/ask values so the top of book is always on the first line
SingleWriterPriceSizeStore book[COCONUT_MAX_DEPTH*2];
uint16_t sequence; // 2
SingleWriterPriceSizeStore& bid(uint32_t depth) { return book[depth*2]; }
const SingleWriterPriceSizeStore& bid(uint32_t depth) const { return book[depth*2]; }
SingleWriterPriceSizeStore& ask(uint32_t depth) { return book[depth*2+1]; }
const SingleWriterPriceSizeStore& ask(uint32_t depth) const { return book[depth*2+1]; }
};
//
// COLD Data, brrr
///////////////////////
struct ALIGNED_(CACHE_LINE) ColdHeader {
ColdHeader() :
count(0),
segment_count(0),
create_time(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),
platform(co2::PLATFORM_UNSET),
trade_count(0),
coconut_status_count(0),
exchange_status_count(0),
rfq_count(0),
customer_bbo_count(0)
{}
// uint32_t version;
size_t count;
size_t segment_count;
TimeStamp create_time;
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;
Platform platform;
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> customer_bbo_count;
// Ring buffer for LastTrade for all securities in this list
uint8_t trade_buffer[1024*1024]; //1mb
// 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_price(NO_PRICE),
settlement_date(),
previous_settlement_price(NO_PRICE),
previous_settlement_date(),
open_price(NO_PRICE),
open_date(),
close_price(NO_PRICE),
close_date(),
session_high(NO_PRICE),
session_low(NO_PRICE),
open_interest(NO_PRICE),
session_high_bid(NO_PRICE),
session_low_offer(NO_PRICE),
fixing_price(NO_PRICE)
{ }
TimeStamp misc_timestamp;
// Settlement
Price settlement_price;
datetime::Date settlement_date;
Price 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 open_interest;
Price session_high_bid;
Price session_low_offer;
Price fixing_price;
bool operator==(const MiscData& other) const {
return
settlement_price == other.settlement_price &&
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 &&
open_interest == other.open_interest &&
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(co2::TRADINGSTATUS_UNKNOWN),
last_trade_updated(),
last_trade(),
total_volume(0),
last_trade_time(),
customer_bbo_updated(),
customer_bid(),
customer_ask(),
misc(),
deleted(false)
{
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; }
co2::TradingStatus exchange_status;
// Last Trade
TimeStamp last_trade_updated;
SingleWriterPriceSizeStore last_trade;
size_t total_volume;
datetime::LLDateTime last_trade_time;
// Customer BBO
TimeStamp customer_bbo_updated;
SingleWriterPriceSizeStore customer_bid;
SingleWriterPriceSizeStore customer_ask;
MiscData misc;
bool deleted;
FixedSecurity security;
};
}}