#ifndef NO_CPP0X #include "MDProducerHandle.h" #include "common.h" namespace bts { namespace md { using namespace datetime; using namespace co2; 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 == api::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); } } } void MDProducerHandle::set_legs(const std::vector& legs) { bts_assert(cdata().security.type == api::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_bbo(TimeStamp time, const PriceSize bid, const PriceSize ask, uint16_t seq) { if (!has_depth()) { auto oldtime = hdata().time(); hdata().set_time(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().set_time(time); hdata().sequence = seq; hheader().bbo_count++; } else { hdata().set_time(oldtime); } } else { auto oldtime = hdepth().time(); hdepth().set_time(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().set_time(time); hdepth().sequence = seq; hheader().bbo_count++; } else { hdepth().set_time(oldtime); } } } void MDProducerHandle::set_status(CoconutSecurityStatus status) { if (has_depth()) { hdepth().status = status; } else { hdata().status = status; } } 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 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; std::array blank; for (size_t x = 0; x < blank.size(); ++x) { blank[x] = PriceSize::NO_MARKET; } update_book(ts, blank, blank, 0); } }}//namespace #endif