#pragma once #include "lib/domain_model/SecurityKey.h" #include "lib/domain_model/Security.h" #include "lib/utils/TimeStamp.h" #include "IntPrice.h" #include "ExchangeOrderKey.h" #include "MDProducerHandle.h" #include "google/btree/btree_map.h" #include #include #include #include namespace bts { namespace md { struct SnapshotOrder { ExchangeOrderKey id; IntPrice price; int32_t size; }; struct MBOSnapshot { MBOSnapshot() = default; MBOSnapshot(const MBOSnapshot&) = delete; MBOSnapshot(MBOSnapshot&& other) = delete; utils::TimeStamp updated; std::vector bids; std::vector asks; void copy_from(const MBOSnapshot& src) { bids = src.bids; asks = src.asks; updated = src.updated; } void reset() { updated = utils::TimeStamp(); constexpr int max = 256; if (bids.size() > max) { bids.resize(max); } if (asks.size() > max) { asks.resize(max); } bids.clear(); asks.clear(); } }; namespace inc { // according to this link MBO book only has these updates types // https://www.cmegroup.com/confluence/display/EPICSANDBOX/MDP+3.0+-+Market+Data+Snapshot+Full+Refresh#MDP3.0-MarketDataSnapshotFullRefresh-MarketbyOrder(MBO)DataBlock enum class UpdateType { Bid = 0, Ask = 1, // ImpliedBid, // 'E' // ImpliedAsk, // 'F' // BookReset // 'J' }; // OrderUpdateAction=37708 enum class UpdateAction { New = 0, Change = 1, Delete = 2 }; typedef uint64_t Priority; struct Update { ExchangeOrderKey order_id; Priority priority; IntPrice price; int32_t size; UpdateType type; UpdateAction action; }; } struct MBOWorkingState { typedef std::tuple key_t; struct Data { ExchangeOrderKey id; int32_t size; }; struct LessBid { bool operator()(const key_t& left, const key_t& right) const { const auto& left_price = std::get(left); const auto& right_price = std::get(right); if (left_price == right_price) { return std::get(left) < std::get(right); } return !(left_price < right_price); } }; MBOWorkingState(MDProducerHandle& h) : handle(h), updated(), bid(),ask() {} MDProducerHandle handle; utils::TimeStamp updated; // Used for 'change' messages which update price std::unordered_map orderid_index; btree::btree_map bid; btree::btree_map ask; }; void copy_to_snapshot(MBOWorkingState& state, MBOSnapshot* destination); void apply_incremental(MBOWorkingState&, const inc::Update&); template void apply_incremental(MBOWorkingState& state, const V& vec) { for (const auto& update : vec) { apply_incremental(state, update); } } struct MBOProcessorImpl; class MBOProcessor; class MBOWriter { std::shared_ptr _me; friend class MBOProcessor; MBOWriter(std::shared_ptr&); ~MBOWriter(); public: // // Snapshot api MBOSnapshot* allocate_snapshot(size_t bid_count=0, size_t ask_count=0); void release_snapshot(MBOSnapshot**); void publish_snapshot(SecurityKey, MBOSnapshot**); bool validate_snapshot(uint32_t, SecurityKey, MBOSnapshot*); // // Incremental api // Enqueue the update to the mbo work queue. void enqueue_incremental(MBOWorkingState&, const inc::Update&); }; class MBOReader { std::shared_ptr _me; friend class MBOProcessor; friend class MBOWriter; MBOReader(std::shared_ptr&); ~MBOReader(); public: MBOSnapshot* acquire_snapshot(SecurityKey key) const; void release_snapshot(MBOSnapshot**) const; }; class MBOProcessor { std::shared_ptr _me; MBOWriter _writer; MBOReader _reader; public: MBOProcessor(); MBOWriter& writer(); MBOReader& reader(); }; }} namespace std { std::ostream& operator<<(std::ostream& os, const bts::md::inc::Update&); }