#pragma once #include "../common.h" #include "lib/utils/bts_export.h" #include "lib/utils/warn_ctrl.h" #include "lib/utils/bts_array_size.h" #include "lib/utils/bts_assert.h" #include "lib/utils/bts_no_copy.h" #include "lib/utils/ValueWrapper.h" #include #include namespace bts { namespace md { using SecurityKey = utils::ValueWrapper; using ExchangeKey = utils::ValueWrapper; GCC_DIAG_OFF("-Weffc++") // No Virtual Destructors by design #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[32]; // 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 FixedStockSecurity : public FixedSecurity { public: FixedStockSecurity() { type = api::SECURITYTYPE_STOCK; }; }; 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 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(sec.ByteSize()) { 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(this) + sizeof(FixedSyntheticSecurity)); } const uint8_t* buffer() const { return (reinterpret_cast(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); // Stack allocates a security of the given type template void with_security(SecurityType type, size_t leg_count, const api::Security* psynthetic, C cb) { switch(type) { case api::SECURITYTYPE_STOCK: { FixedStockSecurity security; 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 buffer; buffer.resize(psynthetic->ByteSize() + 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 void with_security_like(const api::Security& security, C cb) { return with_security(securitytype_of(security), num_legs(security), &security, cb); } }}//namespace