303 lines
11 KiB
C++
303 lines
11 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),
|
|
cboe_class_key(0),
|
|
cme_market_segment_id(0),
|
|
cme_unit_of_measure_qty(0),
|
|
price_break(0),
|
|
low_tick(0),
|
|
high_tick(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));
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
std::string security_descriptor(const FixedSecurity& security) {
|
|
const char separator = ' ';
|
|
std::ostringstream result;
|
|
// If the same symbol is listed at 2 different exchanges it should have a different ID
|
|
result << security.symbol() << separator << Venue_Name(security.exchange);
|
|
switch(security.type) {
|
|
case SECURITYTYPE_OPTION: {
|
|
auto* psec = (FixedOptionSecurity*)&security;
|
|
result << separator;
|
|
write_date_string(psec->expiration_year, psec->expiration_month, psec->expiration_day, result);
|
|
result << separator << psec->strike << (psec->call_put == PUTORCALL_CALL ? 'C' : 'P');
|
|
break;
|
|
}
|
|
case SECURITYTYPE_FUTURE: {
|
|
auto* psec = (FixedFutureSecurity*)&security;
|
|
result << separator;
|
|
write_date_string(psec->expiration_year, psec->expiration_month, psec->expiration_day, result);
|
|
break;
|
|
}
|
|
case SECURITYTYPE_SPREAD: {
|
|
// Since a fixed spread is normalized instead of writing out the leg details
|
|
// we write out the fixed ids of the legs. If any leg is "invalid_id" return an error
|
|
auto* psec = (FixedSpreadSecurityL*)&security;
|
|
bts_assert(psec->leg_count() > 0);
|
|
for (size_t x = 0; x < psec->leg_count(); ++x) {
|
|
auto& leg = psec->legs[x];
|
|
if (leg.key == invalid_security_key) return "";
|
|
result << separator
|
|
<< leg.key << "-q"
|
|
<< leg.leg_quantity
|
|
<< "p" << leg.leg_price << "d"
|
|
<< leg.leg_delta << (leg.leg_side == SIDE_BUY ? "BUY" : "SELL");
|
|
}
|
|
break;
|
|
}
|
|
case SECURITYTYPE_STOCK: // already have symbol+exchange
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return result.str();
|
|
}
|
|
std::string security_descriptor(const api::Security& security) {
|
|
std::string result;
|
|
with_security(security.Contract_case(), num_legs(security), [&](FixedSecurity& fs) {
|
|
convert_security(security, fs, true);
|
|
result = security_descriptor(fs);
|
|
});
|
|
return result;
|
|
}
|
|
|
|
void get_contract_spec(const FixedSecurity& source, api::ContractSpecification& spec) {
|
|
spec.set_cash_per_point(source.cme_unit_of_measure_qty);
|
|
spec.set_price_break(source.price_break);
|
|
spec.set_low_tick(source.low_tick);
|
|
spec.set_high_tick(source.high_tick);
|
|
// 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());
|
|
}
|
|
|
|
Security convert_security(const FixedSecurity& security) {
|
|
Security res;
|
|
convert_security(security, &res);
|
|
return res;
|
|
}
|
|
void convert_security(const FixedSecurity& source, Security* pres) {
|
|
bts_assert(pres);
|
|
Security& dest = *pres;
|
|
dest.set_id(source.key);
|
|
dest.set_exchange(source.exchange);
|
|
dest.set_symbol(source.symbol());
|
|
|
|
switch(source.exchange) {
|
|
case Venue::VENUE_GLBX:
|
|
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());
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
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_STOCK: break;
|
|
default:
|
|
bts_assert(false);
|
|
}
|
|
|
|
get_contract_spec(source, *dest.mutable_contract_spec());
|
|
}
|
|
|
|
size_t recursive_leg_count(const Security& security) {
|
|
if (security.Contract_case() != Security::ContractCase::kSpread) return 0;
|
|
size_t result = security.spread().legs_size();
|
|
for (const auto& leg : security.spread().legs()) {
|
|
result += recursive_leg_count(leg.security());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
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;
|
|
default:
|
|
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();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
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());
|
|
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;
|
|
default:
|
|
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();
|
|
|
|
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].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_STOCK: break;
|
|
default:
|
|
bts_assert(false);
|
|
}
|
|
}
|
|
|
|
}}//namespace
|
|
|
|
#endif
|