392 lines
12 KiB
C++
392 lines
12 KiB
C++
#pragma once
|
|
|
|
#include "../common.h"
|
|
#include "lib/core/FMT_IMPL.h"
|
|
#include "lib/core/TimeStamp.h"
|
|
#include "lib/io/JsonDumper.h"
|
|
#include "lib/io/JsonReader.h"
|
|
#include "lib/utils/ValueWrapper.h"
|
|
#include "lib/utils/bts_array_size.h"
|
|
#include "lib/utils/bts_assert.h"
|
|
#include "lib/utils/bts_export.h"
|
|
#include "lib/utils/bts_no_copy.h"
|
|
#include "lib/utils/warn_ctrl.h"
|
|
#include "lib/domain_model/SecurityKey.h"
|
|
|
|
#include <nlohmann/json.hpp>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
namespace bts {
|
|
namespace md {
|
|
|
|
using SecurityKey = bts::SecurityKey;
|
|
using ExchangeKey = utils::ValueWrapper<struct ExchangeKeyTag, uint64_t>;
|
|
|
|
GCC_DIAG_OFF("-Weffc++") // No Virtual Destructors by design
|
|
#ifndef __clang__
|
|
GCC_DIAG_OFF("-Wstringop-truncation")
|
|
#endif
|
|
|
|
#define STRING_PROPERTY(NAME) \
|
|
const char *NAME() const { return _##NAME; } \
|
|
void NAME(const char *val, size_t size) { \
|
|
strncpy(_##NAME, val, std::min(BTS_ARRAYSIZE(_##NAME) - 1, size)); \
|
|
_##NAME[std::min(BTS_ARRAYSIZE(_##NAME) - 1, size)] = '\0'; \
|
|
} \
|
|
void NAME(const std::string &val) { NAME(val.c_str(), val.size()); }
|
|
|
|
struct ColdEntry;
|
|
// Caution: FixedSecurity is a variable sized structure
|
|
// Use FixedSecurity::size to determine size not sizeof()
|
|
class BTS_EXPORT FixedSecurity {
|
|
protected:
|
|
FixedSecurity();
|
|
friend ColdEntry;
|
|
|
|
public:
|
|
FixedSecurity(const FixedSecurity &) = delete;
|
|
~FixedSecurity();
|
|
|
|
STRING_PROPERTY(symbol);
|
|
STRING_PROPERTY(alt_symbol);
|
|
STRING_PROPERTY(cfi_code);
|
|
STRING_PROPERTY(security_desc);
|
|
STRING_PROPERTY(platform_id);
|
|
|
|
size_t size;
|
|
security_key key;
|
|
uint64_t numeric_id;
|
|
api::SecurityType type;
|
|
api::Venue exchange;
|
|
|
|
int8_t display_factor;
|
|
|
|
double price_break;
|
|
double low_tick;
|
|
double high_tick;
|
|
double cash_per_point;
|
|
|
|
uint32_t cboe_class_key;
|
|
uint8_t cme_market_segment_id;
|
|
uint8_t cme_main_fraction;
|
|
uint8_t cme_sub_fraction;
|
|
uint8_t cme_price_display_format;
|
|
int8_t ice_price_denominator;
|
|
int8_t ice_deal_denominator;
|
|
int8_t ice_settle_denominator;
|
|
bool is_ice_strip;
|
|
|
|
STRING_PROPERTY(ice_market_description);
|
|
STRING_PROPERTY(ice_cleared_alias);
|
|
STRING_PROPERTY(ice_product_name);
|
|
STRING_PROPERTY(ice_hub_alias);
|
|
STRING_PROPERTY(ice_strip_name);
|
|
STRING_PROPERTY(mic_code);
|
|
|
|
private:
|
|
char _symbol[16]; // CME defines this as a char[6] but I'm padding
|
|
char _cfi_code[8]; // CME defines this as char[6]
|
|
char _security_desc[64]; // cme security desc FIX=107 //ice wants this to be
|
|
// 35
|
|
char _alt_symbol[16]; // "cme symbol" or alternate symbol
|
|
char _platform_id[64];
|
|
|
|
char _ice_market_description[120];
|
|
char _ice_cleared_alias[15];
|
|
char _ice_product_name[62];
|
|
char _ice_hub_alias[80];
|
|
char _ice_strip_name[39];
|
|
char _mic_code[4];
|
|
};
|
|
|
|
class FixedSpotSecurity : public FixedSecurity {
|
|
public:
|
|
FixedSpotSecurity() { type = api::SECURITYTYPE_SPOT; };
|
|
};
|
|
|
|
class BTS_EXPORT FixedFutureSecurity : public FixedSecurity {
|
|
public:
|
|
FixedFutureSecurity();
|
|
uint32_t expiration_year;
|
|
uint32_t expiration_month;
|
|
uint32_t expiration_day;
|
|
};
|
|
|
|
class BTS_EXPORT FixedOptionSecurity : public FixedSecurity {
|
|
public:
|
|
FixedOptionSecurity();
|
|
uint32_t expiration_year;
|
|
uint32_t expiration_month;
|
|
uint32_t expiration_day;
|
|
double strike;
|
|
api::PutOrCall call_put;
|
|
};
|
|
|
|
struct BTS_EXPORT FixedSpreadLeg {
|
|
FixedSpreadLeg();
|
|
security_key key;
|
|
ExchangeKey exchange_key;
|
|
Side leg_side;
|
|
double leg_quantity;
|
|
double leg_price;
|
|
double leg_delta;
|
|
};
|
|
|
|
template <int LEGCOUNT = 64>
|
|
class FixedSpreadSecurity : public FixedSecurity {
|
|
char _summary[128];
|
|
|
|
public:
|
|
FixedSpreadSecurity() : _leg_count(0), _leg_max(LEGCOUNT), legs() {
|
|
type = api::SECURITYTYPE_SPREAD;
|
|
size = sizeof(FixedSpreadSecurity<1>);
|
|
memset(&_summary, 0, BTS_ARRAYSIZE(_summary));
|
|
}
|
|
void set_leg_count(size_t count) {
|
|
bts_assert(count > 0);
|
|
bts_assert(count <= LEGCOUNT);
|
|
bts_assert(count <= _leg_max);
|
|
_leg_count = count;
|
|
size = sizeof(FixedSpreadSecurity<1>) +
|
|
(sizeof(FixedSpreadLeg) * (count - 1));
|
|
}
|
|
STRING_PROPERTY(summary);
|
|
|
|
static size_t static_count() { return LEGCOUNT; }
|
|
uint32_t leg_count() const { return _leg_count; }
|
|
|
|
private:
|
|
// memcpy only please
|
|
uint32_t _leg_count;
|
|
uint32_t _leg_max;
|
|
BTS_NO_COPY(FixedSpreadSecurity);
|
|
|
|
public:
|
|
// NO members after legs array
|
|
FixedSpreadLeg legs[LEGCOUNT];
|
|
};
|
|
|
|
// We store the normalized legs for a normal spread because they aren't
|
|
// available when it is created A synthetic security is always fully defined
|
|
class FixedSyntheticSecurity : public FixedSecurity {
|
|
public:
|
|
FixedSyntheticSecurity(const api::Security &sec)
|
|
: _leg_count(sec.synthetic().legs_size()),
|
|
_buffer_size(static_cast<std::uint32_t>(sec.ByteSizeLong())) {
|
|
type = api::SECURITYTYPE_SYNTHETIC;
|
|
sec.SerializeToArray(buffer(), _buffer_size);
|
|
size = sizeof(FixedSyntheticSecurity) + _buffer_size;
|
|
}
|
|
uint32_t leg_count() const { return _leg_count; }
|
|
uint32_t buffer_size() const { return _buffer_size; }
|
|
|
|
void deserialize(api::Security &dest) const {
|
|
dest.ParseFromArray(buffer(), _buffer_size);
|
|
}
|
|
|
|
private:
|
|
uint8_t *buffer() {
|
|
return (
|
|
reinterpret_cast<uint8_t *>(this) + sizeof(FixedSyntheticSecurity));
|
|
}
|
|
const uint8_t *buffer() const {
|
|
return (
|
|
reinterpret_cast<const uint8_t *>(this) +
|
|
sizeof(FixedSyntheticSecurity));
|
|
}
|
|
uint32_t _leg_count;
|
|
uint32_t _buffer_size;
|
|
};
|
|
|
|
// Stack allocate a FixedSpreadSecurity with a high number of legs then memcpy
|
|
// the right size to shared memory
|
|
typedef FixedSpreadSecurity<2> FixedSpreadSecurityXS;
|
|
typedef FixedSpreadSecurity<16> FixedSpreadSecurityS;
|
|
typedef FixedSpreadSecurity<128> FixedSpreadSecurityL;
|
|
typedef FixedSpreadSecurity<512> FixedSpreadSecurityXL;
|
|
|
|
std::string security_descriptor(const api::Security &);
|
|
|
|
// Convert a fixedsecurity to a normal Security - caveat: spreads will be
|
|
// normalized
|
|
Security convert_security(const FixedSecurity &);
|
|
void convert_security(const FixedSecurity &, Security *);
|
|
void convert_security(
|
|
const Security &src, FixedSecurity &dest, bool set_security_key = false);
|
|
|
|
SecurityType securitytype_of_contractcase(Security::ContractCase cc);
|
|
SecurityType securitytype_of(const Security &);
|
|
size_t num_legs(const Security &);
|
|
uint64_t exchange_id(const Security &security);
|
|
|
|
void to_json(nlohmann::json &j, const FixedSpreadLeg &f);
|
|
|
|
void to_json(nlohmann::json &j, const FixedSecurity &f);
|
|
|
|
std::ostream &operator<<(std::ostream &out, const FixedSecurity &f) noexcept;
|
|
|
|
//////////////////////////////////////////////////
|
|
// Definition
|
|
//////////////////////////////////////////////////
|
|
|
|
inline void to_json(nlohmann::json &j, const FixedSpreadLeg &f) {
|
|
j = nlohmann::json{
|
|
{"key", f.key},
|
|
{"exch_key", f.exchange_key.value()},
|
|
{"side", Side_Name(f.leg_side)},
|
|
{"qty", f.leg_quantity},
|
|
{"price", f.leg_price},
|
|
{"delta", f.leg_delta},
|
|
};
|
|
}
|
|
|
|
inline void to_json(nlohmann::json &j, const FixedSecurity &f) {
|
|
j = nlohmann::json{
|
|
{"key", f.key},
|
|
{"symbol", f.symbol()},
|
|
{"type", api::SecurityType_Name(f.type)},
|
|
{"venue", api::Venue_Name(f.exchange)},
|
|
{"alt_symbol", f.alt_symbol()},
|
|
{"cfi_code", f.cfi_code()},
|
|
{"security_desc", f.security_desc()},
|
|
{"platform_id", f.platform_id()},
|
|
{"display_factor", f.display_factor},
|
|
{"numeric_id", f.numeric_id},
|
|
{"cboe_class_key", f.cboe_class_key},
|
|
{"main_frac", (int)f.cme_main_fraction},
|
|
{"sub_frac", (int)f.cme_sub_fraction},
|
|
{"price_format", (int)f.cme_price_display_format},
|
|
{"market_segment", (int)f.cme_market_segment_id},
|
|
|
|
{"price_break", f.price_break},
|
|
{"low_tick", f.low_tick},
|
|
{"high_tick", f.high_tick},
|
|
{"cpp", f.cash_per_point},
|
|
|
|
{"ice_market_description", f.ice_market_description()},
|
|
{"ice_cleared_alias", f.ice_cleared_alias()},
|
|
{"ice_product_name", f.ice_product_name()},
|
|
{"ice_hub_alias", f.ice_hub_alias()},
|
|
{"ice_strip_name", f.ice_strip_name()},
|
|
{"mic_code", f.mic_code()},
|
|
};
|
|
|
|
switch (f.type) {
|
|
case api::SECURITYTYPE_SPOT:
|
|
break;
|
|
case api::SECURITYTYPE_FUTURE: {
|
|
auto *fut = static_cast<const FixedFutureSecurity *>(&f);
|
|
core::TimeStamp exp;
|
|
core::TimeStamp::from_ymd(
|
|
fut->expiration_year, fut->expiration_month, fut->expiration_day,
|
|
&exp);
|
|
j["expiration"] = exp.to_iso_string();
|
|
break;
|
|
}
|
|
case api::SECURITYTYPE_OPTION: {
|
|
auto *opt = static_cast<const FixedOptionSecurity *>(&f);
|
|
core::TimeStamp exp;
|
|
core::TimeStamp::from_ymd(
|
|
opt->expiration_year, opt->expiration_month, opt->expiration_day,
|
|
&exp);
|
|
j["expiration"] = exp.to_iso_string();
|
|
j["strike"] = opt->strike;
|
|
j["callput"] = api::PutOrCall_Name(opt->call_put);
|
|
break;
|
|
}
|
|
case api::SECURITYTYPE_SPREAD: {
|
|
auto *spd = static_cast<const FixedSpreadSecurityXL *>(&f);
|
|
std::vector<FixedSpreadLeg> legs{spd->legs, spd->legs + spd->leg_count()};
|
|
j["legs"] = legs;
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
inline std::ostream &operator<<(
|
|
std::ostream &out, const FixedSecurity &f) noexcept {
|
|
out << io::JsonDumper::dump(f);
|
|
return out;
|
|
}
|
|
|
|
// Stack allocates a security of the given type
|
|
template <class C>
|
|
void with_security(
|
|
SecurityType type,
|
|
size_t leg_count,
|
|
const api::Security *psynthetic,
|
|
C cb) {
|
|
switch (type) {
|
|
case api::SECURITYTYPE_SPOT: {
|
|
FixedSpotSecurity security;
|
|
bts_assert(security.type == type);
|
|
cb(security);
|
|
return;
|
|
}
|
|
case api::SECURITYTYPE_FUTURE: {
|
|
FixedFutureSecurity security;
|
|
bts_assert(security.type == type);
|
|
cb(security);
|
|
return;
|
|
}
|
|
case api::SECURITYTYPE_OPTION: {
|
|
FixedOptionSecurity security;
|
|
bts_assert(security.type == type);
|
|
cb(security);
|
|
return;
|
|
}
|
|
case api::SECURITYTYPE_SPREAD: {
|
|
if (leg_count <= 4) {
|
|
FixedSpreadSecurity<4> security;
|
|
cb(security);
|
|
} else if (leg_count <= FixedSpreadSecurityS::static_count()) {
|
|
FixedSpreadSecurityS security;
|
|
cb(security);
|
|
} else if (leg_count <= FixedSpreadSecurityL::static_count()) {
|
|
FixedSpreadSecurityL security;
|
|
cb(security);
|
|
} else if (leg_count <= FixedSpreadSecurityXL::static_count()) {
|
|
FixedSpreadSecurityXL security;
|
|
cb(security);
|
|
} else {
|
|
std::stringstream msg("Too many legs (");
|
|
msg << leg_count;
|
|
msg << ")";
|
|
bts_assert_msg(false, msg.str());
|
|
}
|
|
return;
|
|
}
|
|
case api::SECURITYTYPE_SYNTHETIC: {
|
|
bts_assert(psynthetic);
|
|
// Use a vector to allocate the right size
|
|
// Then use the FixedSynthetic constructor to serialize the api::Security
|
|
// into those bytes We could optimize this by using a fixed size stack
|
|
// buffer and only heap allocating if we exceed that but in general
|
|
// FixedSynthetic is not performance sensitive
|
|
std::vector<uint8_t> buffer;
|
|
buffer.resize(
|
|
psynthetic->ByteSizeLong() + sizeof(FixedSyntheticSecurity));
|
|
auto *psec = new (&buffer[0]) FixedSyntheticSecurity(*psynthetic);
|
|
cb(*psec);
|
|
return;
|
|
}
|
|
case api::SECURITYTYPE_UNSET:
|
|
case api::SecurityType_INT_MAX_SENTINEL_DO_NOT_USE_:
|
|
case api::SecurityType_INT_MIN_SENTINEL_DO_NOT_USE_:
|
|
break;
|
|
}
|
|
bts_assert(false);
|
|
}
|
|
|
|
template <class C>
|
|
void with_security_like(const api::Security &security, C cb) {
|
|
return with_security(
|
|
securitytype_of(security), num_legs(security), &security, cb);
|
|
}
|
|
|
|
} // namespace md
|
|
} // namespace bts
|