#ifndef NO_CPP0X #include "MDProducerHandle.h" #include "common.h" namespace bts { namespace md2 { using namespace datetime; MDProducerHandle::MDProducerHandle(ColdHeader& cheader, HotHeader& hheader, ColdEntry& centry, void* phot, MDProducerChannel channel) : _hdata(phot), _hheader(&hheader), _cdata(¢ry), _cheader(&cheader), _channel(channel) { } MDProducerHandle::MDProducerHandle() : _hdata(nullptr), _hheader(nullptr), _cdata(nullptr), _cheader(nullptr), _channel() {} MDProducerHandle::~MDProducerHandle() {} MDProducerHandle& MDProducerHandle::operator=(MDProducerHandle const& other) { _hdata = other._hdata; _hheader = other._hheader; _cdata = other._cdata; _cheader = other._cheader; _channel = other._channel; return *this; } void MDProducerHandle::set_key(security_key key) { if (cdata().security.key == key) return; bts_assert(cdata().security.key == invalid_security_key); // ! Assignment order important ! // Once the cold data has a key the security becomes visible so the hot data *must* be able to be found or we assert if (has_depth()) { hdepth().key = key; } else { hdata().key = key; } cdata().security.key = key; // Increment public security count cheader().security_count++; // If this is a spread make sure the legs have valid keys if (security().type == SecurityType::SPREAD) { const auto& psecurity = static_cast(security()); for (size_t x = 0; x < psecurity.leg_count(); ++x) { bts_assert(psecurity.legs[x].key != invalid_security_key); } } } security_key MDProducerHandle::key() const { security_key key = has_depth() ? hdepth().key : hdata().key; bts_assert_d(key == cdata().security.key); return key; } void MDProducerHandle::set_legs(const std::vector& legs) { bts_assert(cdata().security.type == SecurityType::SPREAD); FixedSpreadSecurityL* fss = (FixedSpreadSecurityL*)&cdata().security; bts_assert(fss->leg_count() == legs.size()); for (size_t x = 0; x < legs.size(); ++x) { security_key key = legs[x]; fss->legs[x].key = key; // attempt to validate that this FixedSpreadLeg is valid bts_assert_d(fss->legs[x].leg_quantity > 0); } } void MDProducerHandle::update_bid(TimeStamp time, const PriceSize price, uint32_t depth) { if (!has_depth()) { if (depth == 0 && hdata().bid.store(price)) { hdata().timestamp = time; hheader().bbo_count++; } } else { bts_assert(depth < COCONUT_MAX_DEPTH); if (hdepth().bid(depth).store(price)) { hdepth().timestamp = time; if (depth == 0) { hheader().bbo_count++; hdepth().status = CoconutSecurityStatus::OK; } else { hheader().depth_count++; } } } } void MDProducerHandle::update_ask(TimeStamp time, const PriceSize price, uint32_t depth) { if (!has_depth()) { if (depth == 0 && hdata().ask.store(price)) { hdata().timestamp = time; hheader().bbo_count++; } } else { bts_assert(depth < COCONUT_MAX_DEPTH); if (hdepth().ask(depth).store(price)) { hdepth().timestamp = time; if (depth == 0) { hheader().bbo_count++; hdepth().status = CoconutSecurityStatus::OK; } else { hheader().depth_count++; } } } } void MDProducerHandle::update_bbo(TimeStamp time, const PriceSize bid, const PriceSize ask, uint16_t seq) { if (!has_depth()) { auto oldtime = hdata().timestamp; hdata().timestamp = TimeStamp::invalid(); bool set_bid = hdata().bid.store(bid); bool set_ask = hdata().ask.store(ask); hdata().status = CoconutSecurityStatus::OK; if (set_bid || set_ask) { hdata().timestamp = time; hdata().sequence = seq; hheader().bbo_count++; } else { hdata().timestamp = oldtime; } } else { auto oldtime = hdepth().timestamp; hdepth().timestamp = TimeStamp::invalid(); bool set_bid = hdepth().bid(0).store(bid); bool set_ask = hdepth().ask(0).store(ask); hdepth().status = CoconutSecurityStatus::OK; if (set_bid || set_ask) { hdepth().timestamp = time; hdepth().sequence = seq; hheader().bbo_count++; } else { hdepth().timestamp = oldtime; } } } void MDProducerHandle::set_status(CoconutSecurityStatus::type status) { if (has_depth()) { hdepth().status = status; } else { hdata().status = status; } } void MDProducerHandle::update_numeric_id_hack_for_cboe(uint64_t id) { // This assignment breaks the rules for immutability of the security after security_key is assigned // This hack is for CBOE csm because they change their keys between sessions (*boggle*) cdata().security.numeric_id = id; } void MDProducerHandle::add_trade(TimeStamp time, const PriceSize price, LLDateTime tradetime, uint32_t volume) { cdata().last_trade.store(price); cdata().last_trade_time = tradetime; cdata().total_volume = volume; cdata().last_trade_updated = time; if (key() != invalid_security_key) { co2::TradeMessage trade; trade.set_exponent(price.exponent()); trade.set_mantissa(price.mantissa()); trade.set_size(price.size()); trade.set_time(tradetime); if (volume > 0) trade.set_total_volume(volume); trade.set_key(key()); _channel.add_trade(trade); } } void MDProducerHandle::set_last_trade(TimeStamp time, const PriceSize ps, datetime::LLDateTime tradetime, uint32_t total_volume) { cdata().last_trade.store(ps); cdata().last_trade_time = tradetime; cdata().total_volume = total_volume; cdata().last_trade_updated = time; } void MDProducerHandle::add_rfq(TimeStamp, const co2::QuoteRequestMessage& rfq) { if (rfq.key() != invalid_security_key) { bts_assert(security().key == rfq.key()); _channel.add_rfq(rfq); } } void MDProducerHandle::update_customer_bbo(TimeStamp time, const PriceSize bid, const PriceSize ask) { cdata().customer_bid.store(bid); cdata().customer_ask.store(ask); cdata().customer_bbo_updated = time; cheader().customer_bbo_count++; } void MDProducerHandle::update_exchange_status(TimeStamp, TradingStatus::type status) { cdata().exchange_status = status; cheader().exchange_status_count++; } void MDProducerHandle::update_settlement(TimeStamp time, Price price, Date date) { if (date > cdata().misc.settlement_date) { cdata().misc.previous_settlement_price = cdata().misc.settlement_price; cdata().misc.previous_settlement_date = cdata().misc.settlement_date; } cdata().misc.settlement_price = price; cdata().misc.settlement_date = date; cdata().misc.misc_timestamp = time; } void MDProducerHandle::update_previous_settlement(TimeStamp time, Price price, Date date) { cdata().misc.previous_settlement_price = price; cdata().misc.previous_settlement_date = date; cdata().misc.misc_timestamp = time; } void MDProducerHandle::update_opening(TimeStamp time, Price price, Date date) { cdata().misc.open_price = price; cdata().misc.open_date = date; cdata().misc.misc_timestamp = time; } void MDProducerHandle::update_closing(TimeStamp time, Price price, Date date) { cdata().misc.close_price = price; cdata().misc.close_date = date; cdata().misc.misc_timestamp = time; } void MDProducerHandle::update_session_high(TimeStamp time, Price price) { cdata().misc.session_high = price; cdata().misc.misc_timestamp = time; } void MDProducerHandle::update_session_low(TimeStamp time, Price price) { cdata().misc.session_low = price; cdata().misc.misc_timestamp = time; } void MDProducerHandle::update_open_interest(TimeStamp, Price ) { //cdata().misc.open_interest = price; //cdata().misc_timestamp = TimeStamp::now(); } void MDProducerHandle::update_session_high_bid(TimeStamp, Price ) { //cdata().misc.session_high_bid = price; //cdata().misc_timestamp = TimeStamp::now(); } void MDProducerHandle::update_session_low_offer(TimeStamp, Price ) { //cdata().misc.session_low_offer = price; //cdata().misc_timestamp = TimeStamp::now(); } void MDProducerHandle::update_fixing_price(TimeStamp, Price ) { //cdata().misc.fixing_price = price; //cdata().misc_timestamp = TimeStamp::now(); } void MDProducerHandle::mark_deleted() { TimeStamp ts = TimeStamp::now(); cdata().deleted = true; PriceSize& no_market = PriceSize::NO_MARKET; if (has_depth()) { update_bbo(ts, no_market, no_market); } else { for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { update_bid(ts, no_market, x); update_ask(ts, no_market, x); } } } HotEntry& MDProducerHandle::hdata() const { bts_assert_d(_hdata && !has_depth()); return *static_cast(_hdata); } HotDepthEntry& MDProducerHandle::hdepth() const { bts_assert_d(_hdata && has_depth()); return *static_cast(_hdata); } HotHeader& MDProducerHandle::hheader() const { bts_assert_d(_hheader); return *_hheader; } ColdEntry& MDProducerHandle::cdata() const { bts_assert_d(_cdata); return *_cdata; } ColdHeader& MDProducerHandle::cheader() const { bts_assert_d(_cheader); return *_cheader; } }}//namespace #endif