203 lines
5.6 KiB
C++
203 lines
5.6 KiB
C++
#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 <string>
|
|
#include <sstream>
|
|
|
|
namespace bts {
|
|
namespace md {
|
|
|
|
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()); }
|
|
|
|
// Caution: FixedSecurity is a variable sized structure
|
|
// Use FixedSecurity::size to determine size not sizeof()
|
|
class BTS_EXPORT FixedSecurity
|
|
{
|
|
public:
|
|
FixedSecurity();
|
|
|
|
~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;
|
|
|
|
uint32_t cboe_class_key;
|
|
|
|
uint32_t cme_market_segment_id;
|
|
double cme_unit_of_measure_qty;
|
|
|
|
|
|
double price_break;
|
|
double low_tick;
|
|
double high_tick;
|
|
|
|
private:
|
|
// COPY OK BTS_NO_COPY(FixedSecurity);
|
|
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
|
|
char _alt_symbol[16]; // "cme symbol" or alternate symbol
|
|
char _platform_id[64];
|
|
};
|
|
|
|
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;
|
|
Side leg_side;
|
|
double leg_quantity;
|
|
double leg_price;
|
|
double leg_delta;
|
|
};
|
|
|
|
template <int LEGCOUNT=16>
|
|
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];
|
|
};
|
|
|
|
// Stack allocate a FixedSpreadSecurity with a high number of legs then memcpy the right size to shared memory
|
|
typedef FixedSpreadSecurity<16> FixedSpreadSecurityS;
|
|
typedef FixedSpreadSecurity<128> FixedSpreadSecurityL;
|
|
typedef FixedSpreadSecurity<512> FixedSpreadSecurityXL;
|
|
|
|
std::string security_descriptor(const api::Security&);
|
|
// std::string security_descriptor(const FixedSecurity&);
|
|
|
|
// Convert a fixedsecurity to a normal Security - caveat: spreads will be normalized
|
|
Security convert_security(const FixedSecurity&);
|
|
void convert_security(const FixedSecurity&, Security*);
|
|
size_t recursive_leg_count(const 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&);
|
|
|
|
// Stack allocates a security of the given type
|
|
template <class C>
|
|
void with_security(SecurityType type, size_t leg_count, C cb) {
|
|
switch(type) {
|
|
case api::SECURITYTYPE_STOCK: {
|
|
FixedSecurity security;
|
|
security.type = api::SECURITYTYPE_STOCK;
|
|
cb(security);
|
|
break;
|
|
}
|
|
case api::SECURITYTYPE_FUTURE: {
|
|
FixedFutureSecurity security;
|
|
bts_assert(security.type == type);
|
|
cb(security);
|
|
break;
|
|
}
|
|
case api::SECURITYTYPE_OPTION: {
|
|
FixedOptionSecurity security;
|
|
bts_assert(security.type == type);
|
|
cb(security);
|
|
break;
|
|
}
|
|
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());
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
bts_assert(false);
|
|
}
|
|
}
|
|
template <class C>
|
|
void with_security(Security::ContractCase type, size_t leg_count, C cb) {
|
|
return with_security(securitytype_of_contractcase(type), leg_count, cb);
|
|
}
|
|
|
|
|
|
}}//namespace
|
|
|