399 lines
16 KiB
C++
399 lines
16 KiB
C++
#ifndef NO_CPP0X
|
|
|
|
#include "FixedSecurity.h"
|
|
#include "../PriceSize.h"
|
|
#include <iomanip>
|
|
|
|
#ifdef _WIN32
|
|
#pragma warning(disable: 4351)
|
|
#endif
|
|
|
|
using namespace std;
|
|
using namespace bts::api;
|
|
using std::size_t;
|
|
|
|
namespace bts {
|
|
namespace md {
|
|
|
|
FixedSecurity::FixedSecurity() :
|
|
size(sizeof(FixedSecurity)),
|
|
key(invalid_security_key),
|
|
numeric_id(0),
|
|
type(api::SECURITYTYPE_STOCK),
|
|
exchange(api::VENUE_UNSET),
|
|
display_factor(0),
|
|
price_break(0),
|
|
low_tick(0),
|
|
high_tick(0),
|
|
cash_per_point(0),
|
|
cboe_class_key(0),
|
|
cme_market_segment_id(0),
|
|
cme_main_fraction(0),
|
|
cme_sub_fraction(0),
|
|
cme_price_display_format(0),
|
|
ice_price_denominator(0),
|
|
ice_deal_denominator(0),
|
|
ice_settle_denominator(0),
|
|
_symbol(),
|
|
_cfi_code(),
|
|
_security_desc(),
|
|
_alt_symbol()
|
|
{
|
|
memset(_symbol, 0, BTS_ARRAYSIZE(_symbol));
|
|
memset(_alt_symbol, 0, BTS_ARRAYSIZE(_alt_symbol));
|
|
memset(_cfi_code, 0, BTS_ARRAYSIZE(_cfi_code));
|
|
memset(_security_desc, 0, BTS_ARRAYSIZE(_security_desc));
|
|
memset(_platform_id, 0, BTS_ARRAYSIZE(_platform_id));
|
|
|
|
memset(_ice_market_description, 0, BTS_ARRAYSIZE(_ice_market_description));
|
|
memset(_ice_cleared_alias, 0, BTS_ARRAYSIZE(_ice_cleared_alias));
|
|
memset(_ice_product_name, 0, BTS_ARRAYSIZE(_ice_product_name));
|
|
memset(_ice_hub_alias, 0, BTS_ARRAYSIZE(_ice_hub_alias));
|
|
memset(_ice_strip_name, 0, BTS_ARRAYSIZE(_ice_strip_name));
|
|
memset(_mic_code, 0, BTS_ARRAYSIZE(_mic_code));
|
|
}
|
|
|
|
FixedSecurity::~FixedSecurity(){}
|
|
|
|
FixedFutureSecurity::FixedFutureSecurity():
|
|
expiration_year(0),
|
|
expiration_month(0),
|
|
expiration_day(0)
|
|
{
|
|
type = api::SECURITYTYPE_FUTURE;
|
|
size = sizeof(FixedFutureSecurity);
|
|
}
|
|
|
|
FixedOptionSecurity::FixedOptionSecurity() :
|
|
expiration_year(0),
|
|
expiration_month(0),
|
|
expiration_day(0),
|
|
strike(0)
|
|
{
|
|
type = api::SECURITYTYPE_OPTION;
|
|
size = sizeof(FixedOptionSecurity);
|
|
}
|
|
|
|
FixedSpreadLeg::FixedSpreadLeg():
|
|
key(invalid_security_key),
|
|
leg_side(Side_MIN),
|
|
leg_quantity(0),
|
|
leg_price(0),
|
|
leg_delta(1)
|
|
{
|
|
}
|
|
|
|
void write_date_string(uint32_t year, uint32_t month, uint32_t day, std::ostringstream& result) {
|
|
result << std::setw(2) << std::setfill('0') << month << "/"
|
|
<< std::setw(2) << std::setfill('0') << day << "/"
|
|
<< year - 2000;
|
|
}
|
|
|
|
void write_leg_descriptor(std::ostringstream& result, char separator, const api::ContractLeg& leg) {
|
|
result << separator
|
|
<< leg.security().id() << "-q"
|
|
<< leg.quantity()
|
|
<< "p" << leg.price_multiplier() << "d"
|
|
<< leg.leg_delta() << (leg.side() == SIDE_BUY ? "BUY" : "SELL");
|
|
}
|
|
|
|
std::string security_descriptor(const api::Security& security) {
|
|
const char separator = ' ';
|
|
std::ostringstream result;
|
|
if(security.exchange() == Venue::VENUE_ICE || security.exchange() == Venue::VENUE_SIMICE)
|
|
result << security.ice().security_desc() << separator << Venue_Name(security.exchange());
|
|
else
|
|
result << security.symbol() << separator << Venue_Name(security.exchange());
|
|
switch(security.Contract_case()) {
|
|
case Security::ContractCase::kOption: {
|
|
result << separator;
|
|
const auto& o = security.option();
|
|
write_date_string(o.expiration_year(), o.expiration_month(), o.expiration_day(), result);
|
|
result << separator << o.strike() << (o.call_put() == PUTORCALL_CALL ? 'C' : 'P');
|
|
break;
|
|
}
|
|
case Security::ContractCase::kFuture: {
|
|
result << separator;
|
|
const auto& f = security.future();
|
|
write_date_string(f.expiration_year(), f.expiration_month(), f.expiration_day(), result);
|
|
break;
|
|
}
|
|
case Security::ContractCase::kSpread: {
|
|
// If the security key of a leg is invalid return an error
|
|
const auto& s = security.spread();
|
|
bts_assert(s.legs_size() > 0);
|
|
for (const auto& leg : s.legs()) {
|
|
if (leg.security().id() == invalid_security_key) return "";
|
|
write_leg_descriptor(result, separator, leg);
|
|
}
|
|
break;
|
|
}
|
|
case Security::ContractCase::kSynthetic: {
|
|
const auto& s = security.synthetic();
|
|
bts_assert(s.legs_size() > 0);
|
|
result << separator << s.name() << " pc=" << SyntheticContract::PriceCalc_Name(s.price_calc());
|
|
for (const auto& leg : s.legs()) {
|
|
if (leg.security().id() == invalid_security_key) return ""; // unlikey with synthetics
|
|
write_leg_descriptor(result, separator, leg);
|
|
}
|
|
break;
|
|
}
|
|
case Security::ContractCase::CONTRACT_NOT_SET:
|
|
bts_assert(security.Contract_case() != Security::ContractCase::CONTRACT_NOT_SET);
|
|
}
|
|
return result.str();
|
|
}
|
|
|
|
void get_contract_spec(const FixedSecurity& source, api::ContractSpecification& spec) {
|
|
spec.set_price_break(source.price_break);
|
|
spec.set_low_tick(source.low_tick);
|
|
spec.set_high_tick(source.high_tick);
|
|
spec.set_cash_per_point(source.cash_per_point);
|
|
// Account for price multiplier
|
|
spec.set_price_multiplier(pow10(source.display_factor));
|
|
spec.set_price_break(spec.price_break() * spec.price_multiplier());
|
|
spec.set_low_tick(spec.low_tick() * spec.price_multiplier());
|
|
spec.set_high_tick(spec.high_tick() * spec.price_multiplier());
|
|
|
|
// Tick info
|
|
spec.set_main_fraction(source.cme_main_fraction);
|
|
spec.set_sub_fraction(source.cme_sub_fraction);
|
|
spec.set_price_display_format(source.cme_price_display_format);
|
|
}
|
|
|
|
Security convert_security(const FixedSecurity& security) {
|
|
Security res;
|
|
convert_security(security, &res);
|
|
return res;
|
|
}
|
|
void convert_security(const FixedSecurity& source, Security* presult) {
|
|
bts_assert(presult);
|
|
Security& dest = *presult;
|
|
dest.set_id(source.key);
|
|
dest.set_exchange(source.exchange);
|
|
dest.set_symbol(source.symbol());
|
|
|
|
switch(source.exchange) {
|
|
case Venue::VENUE_GLBX:
|
|
case Venue::VENUE_SIM:
|
|
dest.mutable_glbx()->set_exchange_id(source.numeric_id);
|
|
dest.mutable_glbx()->set_security_desc(source.security_desc());
|
|
dest.mutable_glbx()->set_cfi_code(source.cfi_code());
|
|
dest.mutable_glbx()->set_cme_symbol(source.alt_symbol());
|
|
if (source.cme_market_segment_id != 0) dest.mutable_glbx()->set_market_segment_id(source.cme_market_segment_id);
|
|
break;
|
|
case Venue::VENUE_ICE:
|
|
dest.mutable_ice()->set_exchange_id(source.numeric_id);
|
|
dest.mutable_ice()->set_security_desc(source.security_desc());
|
|
dest.mutable_ice()->set_price_denominator(source.ice_price_denominator);
|
|
dest.mutable_ice()->set_deal_denominator(source.ice_deal_denominator);
|
|
dest.mutable_ice()->set_settle_denominator(source.ice_settle_denominator);
|
|
|
|
dest.mutable_ice()->set_market_description(source.ice_market_description());
|
|
dest.mutable_ice()->set_cleared_alias(source.ice_cleared_alias());
|
|
dest.mutable_ice()->set_product_name(source.ice_product_name());
|
|
dest.mutable_ice()->set_hub_alias(source.ice_hub_alias());
|
|
dest.mutable_ice()->set_strip_name(source.ice_strip_name());
|
|
dest.mutable_ice()->set_mic_code(source.mic_code());
|
|
break;
|
|
case Venue::VENUE_BTS:
|
|
break;
|
|
default:
|
|
bts_assert(false);
|
|
}
|
|
|
|
switch(source.type) {
|
|
case SECURITYTYPE_FUTURE: {
|
|
bts_assert(source.size == sizeof(FixedFutureSecurity));
|
|
const auto* psec = static_cast<const FixedFutureSecurity*>(&source);
|
|
dest.mutable_future()->set_expiration_day(psec->expiration_day);
|
|
dest.mutable_future()->set_expiration_month(psec->expiration_month);
|
|
dest.mutable_future()->set_expiration_year(psec->expiration_year);
|
|
break;
|
|
}
|
|
case SECURITYTYPE_OPTION: {
|
|
bts_assert(source.size == sizeof(FixedOptionSecurity));
|
|
const auto* psec = static_cast<const FixedOptionSecurity*>(&source);
|
|
dest.mutable_option()->set_expiration_day(psec->expiration_day);
|
|
dest.mutable_option()->set_expiration_month(psec->expiration_month);
|
|
dest.mutable_option()->set_expiration_year(psec->expiration_year);
|
|
dest.mutable_option()->set_strike(psec->strike);
|
|
dest.mutable_option()->set_call_put(psec->call_put);
|
|
break;
|
|
}
|
|
case SECURITYTYPE_SPREAD: {
|
|
const auto* psec = static_cast<const FixedSpreadSecurityL*>(&source);
|
|
auto* spread = dest.mutable_spread();
|
|
spread->set_normalized(true);
|
|
for (size_t leg_index = 0; leg_index < psec->leg_count(); ++leg_index) {
|
|
const FixedSpreadLeg& fleg = psec->legs[leg_index];
|
|
auto* leg = spread->add_legs();
|
|
leg->set_side(fleg.leg_side);
|
|
leg->set_quantity(fleg.leg_quantity);
|
|
leg->set_price_multiplier(fleg.leg_price);
|
|
leg->set_leg_delta(fleg.leg_delta);
|
|
leg->mutable_security()->set_id(fleg.key);
|
|
leg->mutable_security()->set_exchange(source.exchange);
|
|
}
|
|
break;
|
|
}
|
|
case SECURITYTYPE_SYNTHETIC: {
|
|
const auto* psec = static_cast<const FixedSyntheticSecurity*>(&source);
|
|
psec->deserialize(dest);
|
|
bts_assert(dest.id() == source.key);
|
|
bts_assert(dest.exchange() == source.exchange);
|
|
bts_assert(dest.symbol() == source.symbol());
|
|
break;
|
|
}
|
|
case SECURITYTYPE_STOCK:
|
|
case SECURITYTYPE_UNSET:
|
|
case SecurityType_INT_MAX_SENTINEL_DO_NOT_USE_:
|
|
case SecurityType_INT_MIN_SENTINEL_DO_NOT_USE_:
|
|
break;
|
|
}
|
|
|
|
get_contract_spec(source, *dest.mutable_contract_spec());
|
|
}
|
|
|
|
SecurityType securitytype_of_contractcase(Security::ContractCase cc) {
|
|
switch(cc) {
|
|
case Security::ContractCase::kFuture: return SECURITYTYPE_FUTURE;
|
|
case Security::ContractCase::kOption: return SECURITYTYPE_OPTION;
|
|
case Security::ContractCase::kSpread: return SECURITYTYPE_SPREAD;
|
|
case Security::ContractCase::kSynthetic: return SECURITYTYPE_SYNTHETIC;
|
|
case Security::ContractCase::CONTRACT_NOT_SET:
|
|
bts_assert(cc != Security::ContractCase::CONTRACT_NOT_SET);
|
|
}
|
|
bts_assert(false);
|
|
}
|
|
SecurityType securitytype_of(const Security& s) {
|
|
return securitytype_of_contractcase(s.Contract_case());
|
|
}
|
|
size_t num_legs(const Security& sec) {
|
|
if (sec.Contract_case() == api::Security::ContractCase::kSpread) {
|
|
return sec.spread().legs_size();
|
|
}
|
|
else if (sec.Contract_case() == api::Security::ContractCase::kSynthetic) {
|
|
return sec.synthetic().legs_size();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
uint64_t exchange_id(const Security& security) {
|
|
switch(security.ExchangeData_case()) {
|
|
case Security::ExchangeDataCase::kCboe: return security.cboe().exchange_id();
|
|
case Security::ExchangeDataCase::kGlbx: return security.glbx().exchange_id();
|
|
case Security::ExchangeDataCase::kCfe: return security.cfe().exchange_id();
|
|
case Security::ExchangeDataCase::kIce: return security.ice().exchange_id();
|
|
// Debatable whether this should be an error or not.
|
|
// If is possible that we might create a spread with no exchange in the future
|
|
// and if we did this would be a runtime error.
|
|
case Security::ExchangeDataCase::EXCHANGEDATA_NOT_SET: return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void convert_security(const Security& src, FixedSecurity& dest, bool set_security_key) {
|
|
dest.key = set_security_key ? src.id() : invalid_security_key;
|
|
dest.type = securitytype_of_contractcase(src.Contract_case());
|
|
dest.symbol(src.symbol());
|
|
dest.exchange = src.exchange();
|
|
dest.display_factor = to_exponent(src.contract_spec().price_multiplier());
|
|
|
|
switch(src.ExchangeData_case()) {
|
|
case Security::ExchangeDataCase::kGlbx:
|
|
dest.numeric_id = src.glbx().exchange_id();
|
|
dest.security_desc(src.glbx().security_desc());
|
|
dest.cfi_code(src.glbx().cfi_code());
|
|
dest.alt_symbol(src.glbx().cme_symbol());
|
|
dest.cme_market_segment_id = src.glbx().market_segment_id();
|
|
break;
|
|
case Security::ExchangeDataCase::kCboe:
|
|
dest.cboe_class_key = src.cboe().class_key();
|
|
break;
|
|
case Security::ExchangeDataCase::kCfe:
|
|
dest.cboe_class_key = src.cfe().class_key();
|
|
break;
|
|
case Security::ExchangeDataCase::kIce:
|
|
dest.numeric_id = src.ice().exchange_id();
|
|
dest.security_desc(src.ice().security_desc());
|
|
dest.ice_price_denominator = src.ice().price_denominator();
|
|
dest.ice_deal_denominator = src.ice().deal_denominator();
|
|
dest.ice_settle_denominator = src.ice().settle_denominator();
|
|
|
|
dest.ice_market_description(src.ice().market_description());
|
|
dest.ice_cleared_alias(src.ice().cleared_alias());
|
|
dest.ice_product_name(src.ice().product_name());
|
|
dest.ice_hub_alias(src.ice().hub_alias());
|
|
dest.ice_strip_name(src.ice().strip_name());
|
|
dest.mic_code(src.ice().mic_code());
|
|
break;
|
|
case Security::ExchangeDataCase::EXCHANGEDATA_NOT_SET:
|
|
break;
|
|
}
|
|
dest.price_break = src.contract_spec().price_break() / src.contract_spec().price_multiplier();
|
|
dest.low_tick = src.contract_spec().low_tick() / src.contract_spec().price_multiplier();
|
|
dest.high_tick = src.contract_spec().high_tick() / src.contract_spec().price_multiplier();
|
|
dest.cash_per_point = src.contract_spec().cash_per_point();
|
|
dest.cme_main_fraction = src.contract_spec().main_fraction();
|
|
dest.cme_sub_fraction = src.contract_spec().sub_fraction();
|
|
dest.cme_price_display_format = src.contract_spec().price_display_format();
|
|
|
|
switch(securitytype_of(src)) {
|
|
case SECURITYTYPE_FUTURE: {
|
|
FixedFutureSecurity& future = static_cast<FixedFutureSecurity&>(dest);
|
|
future.expiration_year = src.future().expiration_year();
|
|
future.expiration_month = src.future().expiration_month();
|
|
future.expiration_day = src.future().expiration_day();
|
|
break;
|
|
}
|
|
case SECURITYTYPE_OPTION: {
|
|
FixedOptionSecurity& option = static_cast<FixedOptionSecurity&>(dest);
|
|
option.expiration_year = src.option().expiration_year();
|
|
option.expiration_month = src.option().expiration_month();
|
|
option.expiration_day = src.option().expiration_day();
|
|
option.strike = src.option().strike();
|
|
option.call_put = src.option().call_put();
|
|
break;
|
|
}
|
|
case SECURITYTYPE_SPREAD: {
|
|
FixedSpreadSecurityXL& spread = static_cast<FixedSpreadSecurityXL&>(dest);
|
|
// set_leg_count sets up the size of the spread and verified that the number of legs does not exceed the allocated space
|
|
spread.set_leg_count(src.spread().legs_size()); // NOT recursive as Co2 spreads are normalized
|
|
for (int x = 0; x < src.spread().legs_size(); ++x) {
|
|
const auto& src_leg = src.spread().legs(x);
|
|
spread.legs[x].key = set_security_key ? src_leg.security().id() : invalid_security_key;
|
|
spread.legs[x].exchange_key = ExchangeKey(exchange_id(src_leg.security()));
|
|
spread.legs[x].leg_side = src_leg.side();
|
|
spread.legs[x].leg_quantity = src_leg.quantity();
|
|
spread.legs[x].leg_price = src_leg.price_multiplier();
|
|
spread.legs[x].leg_delta = src_leg.leg_delta();
|
|
}
|
|
break;
|
|
}
|
|
case SECURITYTYPE_SYNTHETIC: {
|
|
FixedSyntheticSecurity& synth = static_cast<FixedSyntheticSecurity&>(dest);
|
|
bts_assert(synth.buffer_size() > 0);
|
|
// Due to how we create a FixedSecurity this FixedSecurity should already be populated
|
|
bts_assert(synth.leg_count() == (unsigned)src.synthetic().legs_size());
|
|
#ifndef NDEBUG
|
|
{
|
|
Security temp;
|
|
static_cast<FixedSyntheticSecurity*>(&dest)->deserialize(temp);
|
|
bts_assert_d(temp.SerializeAsString() == src.SerializeAsString());
|
|
}
|
|
#endif
|
|
break;
|
|
}
|
|
case SECURITYTYPE_STOCK: break;
|
|
case SECURITYTYPE_UNSET:
|
|
case SecurityType_INT_MAX_SENTINEL_DO_NOT_USE_:
|
|
case SecurityType_INT_MIN_SENTINEL_DO_NOT_USE_:
|
|
bts_assert(false);
|
|
}
|
|
}
|
|
|
|
}}//namespace
|
|
|
|
#endif
|