#pragma once #include "lib/utils/bts_no_copy.h" #include "lib/utils/bts_export.h" #include "lib/md/Shm/MDStructure.h" #include "lib/sparkproto/SparkProtos.pb.h" #include "UserSymbol.h" #include "lib/utils/sane_algorithm.h" #include #include namespace bts { namespace md { class MDConsumerHandle; struct SyntheticStrategy { spark::ExecutionStrategy strategy; api::Security security; std::vector legs; TimeStamp updated; }; TimeStamp copy_bbo(const HotEntry& src, PriceSize& out_bid, PriceSize& out_ask); TimeStamp copy_depth(const HotDepthEntry&, size_t count, PriceSize* bid, PriceSize* ask); TimeStamp copy_depth(const SyntheticStrategy&, size_t count, PriceSize* bid, PriceSize* ask); enum class HandleReadResult { NO_CHANGE, BUSY, SUCCESS }; inline void read_direct( const HotDepthEntry& hot, size_t count, PriceSize* bid, PriceSize* ask) { bts_assert_d(count <= COCONUT_MAX_DEPTH); count = std::min(count, (size_t)COCONUT_MAX_DEPTH); for (size_t x = 0; x < count; ++x) { hot.read_direct(x, &bid[x], &ask[x]); } } inline void read_implied( const HotDepthEntry& hot, size_t count, PriceSize* bid, PriceSize* ask) { bts_assert_d(count <= COCONUT_MAX_IMPLIED_DEPTH); count = std::min(count, (size_t)COCONUT_MAX_IMPLIED_DEPTH); for (size_t x = 0; x < count; ++x) { hot.read_implied(x, &bid[x], &ask[x]); } } inline void read_combined( const HotDepthEntry& hot, size_t count, PriceSize* bid, PriceSize* ask) { bts_assert_d(count <= COCONUT_MAX_DEPTH); size_t xda=0, xdb=0, xia=0, xib=0; if (hot.implied[xib].bid().is_nan()) xib = COCONUT_MAX_IMPLIED_DEPTH; if (hot.implied[xia].ask().is_nan()) xia = COCONUT_MAX_IMPLIED_DEPTH; for (size_t x = 0; x < count; ++x) { if (xib < COCONUT_MAX_IMPLIED_DEPTH) { auto direct = hot.direct[xdb].bid(); auto implied = hot.implied[xib].bid(); // Compare bids if (direct.int_price() == implied.int_price()) { bid[x] = PriceSize( direct.int_price(), direct.size(), ImpliedSize(implied.size())); xdb += 1; xib += 1; } // Bids grow down, take the higher price else if (implied.int_price() < direct.int_price()) { bid[x] = direct; xdb += 1; } else { bid[x] = implied; xib += 1; } } else { bid[x] = hot.direct[xdb].bid(); xdb += 1; } if (xia < COCONUT_MAX_IMPLIED_DEPTH) { auto direct = hot.direct[xda].ask(); auto implied = hot.implied[xia].ask(); // Compare asks if (direct.int_price() == implied.int_price()) { ask[x] = PriceSize( direct.int_price(), direct.size(), ImpliedSize(implied.size())); xda += 1; xia += 1; } // asks grow up, take the lower price else if (direct.int_price() < implied.int_price()) { ask[x] = direct; xda += 1; } else { ask[x] = implied; xia += 1; } } else { ask[x] = hot.direct[xda].ask(); xda += 1; } } } template TimeStamp optimistic_copy( Read read, const HotDepthEntry& hot, size_t count, PriceSize* bid, PriceSize* ask) { TimeStamp t; int attempts = 0; const int max_attempts = 1619200; do { t = hot.time(); read(hot, count, bid, ask); ++attempts; } while((t != hot.time() || t == TimeStamp::invalid()) && attempts < max_attempts); if (attempts >= max_attempts) { return TimeStamp::invalid(); } return t; } // Can be an invalid handle if you request a security which has not been discovered yet. class BTS_EXPORT MDConsumerHandle { public: MDConsumerHandle(); explicit MDConsumerHandle(const std::shared_ptr&); ~MDConsumerHandle(); MDConsumerHandle(const MDConsumerHandle& other); void init(const ColdHeader&, const ColdEntry&, const void* hot); bool is_initialized() const { return _type != HANDLETYPE_VOID; } bool is_synthetic() const { return _type == HANDLETYPE_SYNTHETIC; } // bool has_depth() const { return _type == HANDLETYPE_DEPTH || _type == HANDLETYPE_SYNTHETIC; }; bool is_heart_beating() const; TimeStamp heartbeat() const; CoconutSecurityStatus status() const; uint16_t sequence() const; security_key key() const { switch(_type) { case HANDLETYPE_VOID: return invalid_security_key; case HANDLETYPE_DEPTH: return book.hot->key; case HANDLETYPE_BBO: return bbo.hot->key; case HANDLETYPE_SYNTHETIC: return synthetic.def->security.id(); } bts_assert(false); } UserSymbol get_usersymbol() const { switch(_type) { case HANDLETYPE_VOID: return UserSymbol(); case HANDLETYPE_DEPTH: return UserSymbol(book.cdata->security.symbol(), book.cdata->security.exchange, book.cdata->security.type); case HANDLETYPE_BBO: return UserSymbol(bbo.cdata->security.symbol(), bbo.cdata->security.exchange, bbo.cdata->security.type); case HANDLETYPE_SYNTHETIC: return UserSymbol(synthetic.def->security.symbol(), synthetic.def->security.exchange(), securitytype_of(synthetic.def->security)); } return UserSymbol(); } Security get_normalized_security() const { switch(_type) { case HANDLETYPE_VOID: bts_assert_msg(false, "Handle not initialized"); case HANDLETYPE_DEPTH: return convert_security(book.cdata->security); case HANDLETYPE_BBO: return convert_security(bbo.cdata->security); case HANDLETYPE_SYNTHETIC: return synthetic.def->security; } bts_assert(false); } TimeStamp last_market_update() const { switch(_type) { case HANDLETYPE_VOID: return TimeStamp::invalid(); case HANDLETYPE_DEPTH: return book.hot->time(); case HANDLETYPE_BBO: return bbo.hot->time(); case HANDLETYPE_SYNTHETIC: return std::max( synthetic.def->updated, sane::max(synthetic.def->legs, [](const MDConsumerHandle& h) { return h.last_market_update(); })); } bts_assert(false); } HandleReadResult get_direct_book(TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const { return get_book(read_direct, time, copydepth, bids, asks); } HandleReadResult get_implied_book(TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const { return get_book(read_implied, time, copydepth, bids, asks); } HandleReadResult get_book(TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const { return get_book(read_combined, time, copydepth, bids, asks); } bool get_last_trade(TimeStamp& time, PriceSize& out_trade, datetime::LLDateTime& out_trade_timestamp, size_t* total_volume=nullptr) const; IntPrice settlement_price() const { switch(_type) { case HANDLETYPE_DEPTH: return book.hot->settlement; case HANDLETYPE_BBO: return bbo.hot->settlement; default: return INT_NAN; } } datetime::Date settlement_date() const { return this->misc().settlement_date; } TimeStamp misc_timestamp() const; MiscData misc() const; MDConsumerHandle& operator=(const MDConsumerHandle& other) { _type = HANDLETYPE_VOID; switch(other._type) { case HANDLETYPE_VOID: break; case HANDLETYPE_DEPTH: book = other.book; break; case HANDLETYPE_BBO: bbo = other.bbo; break; case HANDLETYPE_SYNTHETIC: synthetic.def = other.synthetic.def; break; } std::atomic_thread_fence(std::memory_order_release); _type = other._type; return *this; } private: friend class MDConsumerInst; template HandleReadResult get_book(Read read, TimeStamp& time, uint32_t copydepth, PriceSize* bids, PriceSize* asks) const { bts_assert_d(copydepth <= COCONUT_MAX_DEPTH); if (!is_initialized() || copydepth == 0) return HandleReadResult::NO_CHANGE; switch(_type) { case HANDLETYPE_VOID: return HandleReadResult::NO_CHANGE; case HANDLETYPE_DEPTH: if (book.hot->time() > time || book.hot->time() == TimeStamp::invalid()) { TimeStamp temp = optimistic_copy(read, *book.hot, copydepth, bids, asks); if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } time = temp; return HandleReadResult::SUCCESS; } return HandleReadResult::NO_CHANGE; case HANDLETYPE_BBO: bts_assert_msg(false,"Not impelmented"); return HandleReadResult::NO_CHANGE; case HANDLETYPE_SYNTHETIC: auto t = last_market_update(); if (t > time || t == TimeStamp::invalid()) { TimeStamp temp = copy_depth(*synthetic.def, std::min(copydepth, COCONUT_MAX_DEPTH), bids, asks); if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } time = temp; return HandleReadResult::SUCCESS; } return HandleReadResult::NO_CHANGE; } bts_assert(false); } void set_hot(const void* hot) { switch(_type) { case HANDLETYPE_DEPTH: book.hot = (HotDepthEntry*)hot; break; case HANDLETYPE_BBO: bbo.hot = (HotEntry*)hot; break; default: bts_assert(false); } } void set_cold(const ColdHeader* header, const ColdEntry* cold) { bts_assert(header != nullptr); bts_assert(cold != nullptr); if (header->has_depth) { book.cheader = header; book.cdata = cold; _type = HANDLETYPE_DEPTH; } else { bbo.cheader = header; bbo.cdata = cold; _type = HANDLETYPE_BBO; } } enum HandleType : uint8_t { HANDLETYPE_VOID, HANDLETYPE_DEPTH, HANDLETYPE_BBO, HANDLETYPE_SYNTHETIC }; struct DepthHandle { const HotDepthEntry* hot; const ColdEntry* cdata; const ColdHeader* cheader; }; struct BBOHandle { const HotEntry* hot; const ColdEntry* cdata; const ColdHeader* cheader; }; struct SyntheticHandle { std::shared_ptr def; }; HandleType _type; union { DepthHandle book; BBOHandle bbo; SyntheticHandle synthetic; }; }; }}//namespace