diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentDefinition.h b/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentDefinition.h index 34d5784ed..f762743e2 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentDefinition.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentDefinition.h @@ -48,7 +48,7 @@ class InstrumentDefinition { _def); } - const char* security_group() const { + const char *security_group() const { return std::visit( overloaded{[](::mdp3::MDInstrumentDefinitionFuture54 &m) { return m.securityGroup(); @@ -63,8 +63,8 @@ class InstrumentDefinition { }}, _def); } - - const char* asset() const { + + const char *asset() const { return std::visit( overloaded{[](::mdp3::MDInstrumentDefinitionFuture54 &m) { return m.asset(); @@ -79,7 +79,23 @@ class InstrumentDefinition { }}, _def); } - + + ::mdp3::SecurityTradingStatus::Value trading_status() const { + return std::visit( + overloaded{[](::mdp3::MDInstrumentDefinitionFuture54 &m) { + return m.mDSecurityTradingStatus(); + }, + + [](::mdp3::MDInstrumentDefinitionOption55 &m) { + return m.mDSecurityTradingStatus(); + }, + + [](::mdp3::MDInstrumentDefinitionSpread56 &m) { + return m.mDSecurityTradingStatus(); + }}, + _def); + } + private: mutable std::variant<::mdp3::MDInstrumentDefinitionFuture54, ::mdp3::MDInstrumentDefinitionOption55, diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.cpp index bb4c8d38f..29dcea741 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.cpp @@ -1,46 +1,255 @@ #include "InstrumentSnapshot.h" +namespace bts::spartan::mdp3 { -// namespace bts::spartan::mdp3 { +struct InstrumentSnapshot::PImpl { + PImpl(MDP3SecurityID id) : security_id(id) {} -// struct InstrumentSnapshot::PImpl { + // Books + std::array books; + std::array implied_books; + // Orders + Orders orders; + // Fills + Fills fills; -// // Book snapshot control -// size_t snapshot_tot_num_reports; -// size_t snapshot_last_update_time; -// size_t snapshot_last_msg_seq_num_processed; -// size_t snapshot_number_chunks; -// size_t snapshot_last_chunk; + // stats + md::IntPrice high_limit_price; + void set_high_limit_price(md::IntPrice); + md::IntPrice low_limit_price; + md::IntPrice max_price_variation; + MDP3Quantity cleared_volume; + MDP3Quantity electronic_volume; + md::IntPrice fixing_price; + MDP3Quantity open_interest; + md::IntPrice open_price; + md::IntPrice high_bid; + md::IntPrice low_offer; + md::IntPrice settlement_price; + md::IntPrice high_trade_price; + md::IntPrice low_trade_price; + MaybeInstrumentDefinition definition; -// // Definition update control -// size_t last_update_time; + const MDP3SecurityID security_id; + ::mdp3::SecurityTradingStatus::Value trading_status; -// size_t rpt_seq; -// size_t transact_time; + std::string _definition_store; +}; -// // stats -// md::IntPrice high_limit_price; -// md::IntPrice low_limit_price; -// uint64_t cleared_volume = 0; -// uint64_t electronic_volume = 0; -// md::IntPrice fixing_price; -// uint64_t open_interest = 0; -// md::IntPrice open_price; -// md::IntPrice high_bid; -// md::IntPrice low_offer; -// md::IntPrice settlement_price; +InstrumentSnapshot::InstrumentSnapshot(MDP3SecurityID id) : me(new PImpl(id)) {} -// // books -// std::array mbp_book; -// std::array mbp_implied_book; -// MBOBook mbo_book; +InstrumentSnapshot::~InstrumentSnapshot() {} -// // Definition -// InstrumentDefinition definition; -// std::string definition_store; -// }; +MDP3SecurityID InstrumentSnapshot::security_id() const { + return me->security_id; +} -// InstrumentSnapshot::InstrumentSnapshot(::mdp3::SnapshotFullRefresh52 m) { -// } +MaybeInstrumentDefinition InstrumentSnapshot::definition() const { + return me->definition; +} -// } +void InstrumentSnapshot::set_instrument_definition( + InstrumentDefinition const &def) { + me->definition = def; +} + +::mdp3::SecurityTradingStatus::Value +InstrumentSnapshot::trading_status() const { + return me->trading_status; +} + +void InstrumentSnapshot::set_trading_status( + ::mdp3::SecurityTradingStatus::Value t) const { + me->trading_status = t; +} + +md::IntPrice InstrumentSnapshot::high_limit_price() const { + return me->high_limit_price; +} +void InstrumentSnapshot::set_high_limit_price(md::IntPrice v) { + me->high_limit_price = v; +} + +md::IntPrice InstrumentSnapshot::low_limit_price() const { + return me->low_limit_price; +} +void InstrumentSnapshot::set_low_limit_price(md::IntPrice v) { + me->low_limit_price = v; +} + +md::IntPrice InstrumentSnapshot::max_price_variation() const { + return me->max_price_variation; +} +void InstrumentSnapshot::set_max_price_variation(md::IntPrice v) { + me->max_price_variation = v; +} + +MDP3Quantity InstrumentSnapshot::cleared_volume() const { + return me->cleared_volume; +} +void InstrumentSnapshot::set_cleared_volume(MDP3Quantity v) { + me->cleared_volume = v; +} + +MDP3Quantity InstrumentSnapshot::electronic_volume() const { + return me->electronic_volume; +} +void InstrumentSnapshot::set_electronic_volume(MDP3Quantity v) { + me->electronic_volume = v; +} + +md::IntPrice InstrumentSnapshot::fixing_price() const { + return me->fixing_price; +} +void InstrumentSnapshot::set_fixing_price(md::IntPrice v) { + me->fixing_price = v; +} + +MDP3Quantity InstrumentSnapshot::open_interest() const { + return me->open_interest; +} +void InstrumentSnapshot::set_open_interest(MDP3Quantity v) { + me->open_interest = v; +} + +md::IntPrice InstrumentSnapshot::open_price() const { return me->open_price; } +void InstrumentSnapshot::set_open_price(md::IntPrice v) { me->open_price = v; } + +md::IntPrice InstrumentSnapshot::high_bid() const { return me->high_bid; } +void InstrumentSnapshot::set_high_bid(md::IntPrice v) { me->high_bid = v; } + +md::IntPrice InstrumentSnapshot::low_offer() const { return me->low_offer; } +void InstrumentSnapshot::set_low_offer(md::IntPrice v) { me->low_offer = v; } + +md::IntPrice InstrumentSnapshot::settlement_price() const { + return me->settlement_price; +} +void InstrumentSnapshot::set_settlement_price(md::IntPrice v) { + me->settlement_price = v; +} + +md::IntPrice InstrumentSnapshot::high_trade_price() const { + return me->high_trade_price; +} +void InstrumentSnapshot::set_high_trade_price(md::IntPrice v) { + me->high_trade_price = v; +} + +md::IntPrice InstrumentSnapshot::low_trade_price() const { + return me->low_trade_price; +} +void InstrumentSnapshot::set_low_trade_price(md::IntPrice v) { + me->low_trade_price = v; +} + +Book const &InstrumentSnapshot::book(api::Side s) const { + return me->books[s - api::SIDE_BUY]; +} + +ImpliedBook const &InstrumentSnapshot::implied_book(api::Side s) const { + return me->implied_books[s - api::SIDE_BUY]; +} + +template +void InstrumentSnapshot::insert(api::Side s, std::array &books, + size_t idx, BookLevel const &level) { + auto &book = books[s - api::SIDE_BUY]; + bts_assert_d(idx - 1 < book.size()); + for (size_t i = book.size() - 2; i >= idx - 1; i++) + book[i + 1] = book[i]; + book[idx - 1] = level; +} + +template +void InstrumentSnapshot::update(api::Side s, std::array &books, + size_t idx, BookLevel const &level) { + auto &book = books[s - api::SIDE_BUY]; + bts_assert_d(idx - 1 < book.size()); + book[idx - 1] = level; +} + +template +void InstrumentSnapshot::remove(api::Side s, std::array &books, + size_t idx) { + auto &book = books[s - api::SIDE_BUY]; + bts_assert_d(idx - 1 < book.size()); + for (size_t i = idx - 1; i < book.size() - 2; i++) + book[i] = book[i + 1]; + book[book.size() - 1] = {}; +} + + +void InstrumentSnapshot::insert_book_level(api::Side s, size_t idx, + BookLevel const &level) { + insert(s, me->books, idx, level); +} + +void InstrumentSnapshot::update_book_level(api::Side s, size_t idx, + BookLevel const &level) { + update(s, me->books, idx, level); +} + +void InstrumentSnapshot::remove_book_level(api::Side s, size_t idx) { + remove(s, me->books, idx); +} + +void InstrumentSnapshot::insert_implied_book_level(api::Side s, size_t idx, + BookLevel const &level) { + insert(s, me->implied_books, idx, level); +} + +void InstrumentSnapshot::update_implied_book_level(api::Side s, size_t idx, + BookLevel const &level) { + update(s, me->implied_books, idx, level); +} + +void InstrumentSnapshot::remove_implied_book_level(api::Side s, size_t idx) { + remove(s, me->implied_books, idx); +} + +Orders const &InstrumentSnapshot::orders() const { + return me->orders; +} + +void InstrumentSnapshot::insert_order(MDP3Order const&order) { + bts_assert_d(me->orders.count(order.key) == 0); + me->orders.insert({order.key, order}); +} + +void InstrumentSnapshot::update_order(MDP3Order const& order) { + auto it = me->orders.find(order.key); + bts_assert_d(it != me->orders.end()); + it->second = order; +} + +void InstrumentSnapshot::remove_order(MDP3OrderKey id) { + bts_assert_d(me->orders.count(id)); + me->orders.erase(id); +} + +Fills const&InstrumentSnapshot::fills() const { + return me->fills; +} + +void InstrumentSnapshot::add_fill(MDP3Fill const&f) { + me->fills.emplace_back(f); +} + +void InstrumentSnapshot::update_fill(MDP3Fill const&f) { + for (auto &ff : me->fills) + if (ff.key == f.key && ff.trade_entry_id == f.trade_entry_id) { + ff = f; + break; + } +} + +void InstrumentSnapshot::remove_fill(MDP3TradeEntryID tid, MDP3OrderKey oid) { + auto it = std::find_if(me->fills.begin(), me->fills.end(), + [tid,oid] (auto const&f) { + return f.key == oid && f.trade_entry_id == tid; + }); + if (it != me->fills.end()) + me->fills.erase(it); +} + +} // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.h b/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.h index 2094a31a9..51f9990a2 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.h @@ -12,6 +12,9 @@ class SnapshotFullRefresh52; namespace bts::spartan::mdp3 { +// Describes the state of the exchange at a specific time with respect +// to a single instrument. + class InstrumentSnapshot { public: InstrumentSnapshot(MDP3SecurityID); @@ -25,10 +28,35 @@ class InstrumentSnapshot { // Trading status ::mdp3::SecurityTradingStatus::Value trading_status() const; void set_trading_status(::mdp3::SecurityTradingStatus::Value) const; - + + // Book + Book const &book(api::Side) const; + void insert_book_level(api::Side, size_t idx,BookLevel const&); + void update_book_level(api::Side, size_t idx,BookLevel const&); + void remove_book_level(api::Side, size_t idx); + + // Implied Book + ImpliedBook const &implied_book(api::Side) const; + void insert_implied_book_level(api::Side, size_t idx, BookLevel const&); + void update_implied_book_level(api::Side, size_t idx, BookLevel const&); + void remove_implied_book_level(api::Side, size_t idx); + + // Orders + Orders const &orders() const; + void insert_order(MDP3Order const&); + void update_order(MDP3Order const&); + void remove_order(MDP3OrderKey); + + // Fills + Fills const&fills() const; + void add_fill(MDP3Fill const&); + void update_fill(MDP3Fill const&); + void remove_fill(MDP3TradeEntryID, MDP3OrderKey ); + // Stats md::IntPrice high_limit_price() const; void set_high_limit_price(md::IntPrice); + md::IntPrice low_limit_price() const; void set_low_limit_price(md::IntPrice); md::IntPrice max_price_variation() const; @@ -54,31 +82,10 @@ class InstrumentSnapshot { md::IntPrice low_trade_price() const; void set_low_trade_price(md::IntPrice ) ; - // Book - Book const &book(api::Side) const; - void insert_book_level(api::Side, int level,BookLevel const&l); - void update_book_level(api::Side, int level,BookLevel const&l); - void remove_book_level(api::Side, int level); - - // Implied Book - ImpliedBook const &implied_book(api::Side) const; - void insert_implied_book_level(api::Side, int level,BookLevel const&l); - void update_implied_book_level(api::Side, int level,BookLevel const&l); - void remove_implied_book_level(api::Side, int level); - - // Orders - Orders const &orders() const; - void insert_order(MDP3Order const&); - void update_order(MDP3Order const&); - void remove_order(MDP3OrderKey); - - // Fills - Fills const&fills() const; - void add_fill(MDP3Fill const&); - void update_fill(MDP3Fill const&); - void remove_fill(MDP3TradeEntryID); - private: + template void insert(api::Side,std::array &, size_t idx,BookLevel const&); + template void update(api::Side,std::array &, size_t idx, BookLevel const&); + template void remove(api::Side,std::array &, size_t idx); struct PImpl; std::unique_ptr me; }; diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/RealTimeFeedParser.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/RealTimeFeedParser.cpp index cb93e19be..f8fd2a1f9 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/RealTimeFeedParser.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/RealTimeFeedParser.cpp @@ -13,6 +13,7 @@ namespace bts::spartan::mdp3 { enum class RealTimeFeedParser::State { INITIAL_RECOVERY, REAL_TIME_PROCESSING }; struct RealTimeFeedParser::TradeSummary { + ::mdp3::MDUpdateAction::Value update_action; InstrumentSnapshot *security; md::IntPrice price; size_t n_orders; @@ -375,7 +376,8 @@ void RealTimeFeedParser::operator()( bts_assert(iptr); ev.record_security_change(iptr.get()); TradeSummary summary; - summary.security = iptr.get(); + summary.update_action = md_entry.mDUpdateAction(); + summary.security = iptr.get(); summary.price = md::IntPrice{md_entry.mDEntryPx().mantissa(), md_entry.mDEntryPx().exponent()}; summary.n_orders = md_entry.numberOfOrders(); @@ -486,9 +488,9 @@ void RealTimeFeedParser::on_end_of_event() { // trade summary messages have to be processed together once all of // them have been received. We do this here, because we know the // event is over and therefore so are the trade summaries - size_t current_order_detail = 0 ; + size_t current_order_detail = 0; for (auto &summary : me->pending_ts) { - for (size_t i = 0; i < summary.n_orders;i++) { + for (size_t i = 0; i < summary.n_orders; i++) { auto &detail = me->pending_order_detail[current_order_detail++]; MDP3Fill f; f.key = detail.order_id; @@ -498,15 +500,26 @@ void RealTimeFeedParser::on_end_of_event() { if (summary.aggressor != ::mdp3::AggressorSide::NoAggressor) { if (i == 0) f.aggressor = MDP3Fill::THIS; - else + else f.aggressor = MDP3Fill::OTHER; - } - else + } else f.aggressor = MDP3Fill::IMPLIED; - summary.security->add_fill(f); + switch (summary.update_action) { + case ::mdp3::MDUpdateAction::New: + summary.security->add_fill(f); + break; + case ::mdp3::MDUpdateAction::Change: + summary.security->update_fill(f); + break; + case ::mdp3::MDUpdateAction::Delete: + summary.security->remove_fill(f.trade_entry_id, f.key); + break; + default: + bts_assert(false); + } } } me->observer.on_event(me->current_event); } - + } // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/mdp3_common.h b/src/server/sparkd/cmds/spartan/md/mdp3/mdp3_common.h index 6277b7bb7..d0300e821 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/mdp3_common.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/mdp3_common.h @@ -38,7 +38,7 @@ using MDP3OrderPriority = class BookLevel { public: md::IntPrice price; - MDP3Quantity quantity; + MDP3Quantity quantity = MDP3Quantity {0}; int32_t n_orders; };