From 456e9bf357b384e42b76f3a84ae7fd96aac2bee8 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Thu, 30 Jul 2020 16:11:35 -0400 Subject: [PATCH] Finished real time feed processor. --- .../spartan/md/mdp3/IRealTimeFeedObserver.h | 12 ++ .../spartan/md/mdp3/InstrumentSnapshot.cpp | 13 ++ .../cmds/spartan/md/mdp3/InstrumentSnapshot.h | 3 + .../spartan/md/mdp3/RealTimeFeedParser.cpp | 142 ++++++++++++------ .../cmds/spartan/md/mdp3/RealTimeFeedParser.h | 54 ++++++- .../sparkd/cmds/spartan/md/mdp3/mdp3_common.h | 3 +- 6 files changed, 173 insertions(+), 54 deletions(-) create mode 100644 src/server/sparkd/cmds/spartan/md/mdp3/IRealTimeFeedObserver.h diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/IRealTimeFeedObserver.h b/src/server/sparkd/cmds/spartan/md/mdp3/IRealTimeFeedObserver.h new file mode 100644 index 000000000..7c7fa317b --- /dev/null +++ b/src/server/sparkd/cmds/spartan/md/mdp3/IRealTimeFeedObserver.h @@ -0,0 +1,12 @@ +#pragma once + +namespace bts::spartan::mdp3 { + +class MDP3EventInfo; + +class IRealTimeFeedObserver { +public: + virtual void on_event(MDP3EventInfo const&) = 0; +}; + +} diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.cpp index 29dcea741..175b3ff88 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.cpp @@ -227,6 +227,19 @@ void InstrumentSnapshot::remove_order(MDP3OrderKey id) { me->orders.erase(id); } +void InstrumentSnapshot::reset_books() { + for (auto &b : me->books) + for (auto &l : b) + l = {}; + + for (auto &b : me->implied_books) + for (auto &l : b) + l = {}; + + me->orders.clear(); + me->fills.clear(); +} + Fills const&InstrumentSnapshot::fills() const { return me->fills; } diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.h b/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.h index 51f9990a2..fa316ebbc 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/InstrumentSnapshot.h @@ -47,6 +47,9 @@ class InstrumentSnapshot { void update_order(MDP3Order const&); void remove_order(MDP3OrderKey); + // Reset books in response to a channel reset message + void reset_books(); + // Fills Fills const&fills() const; void add_fill(MDP3Fill const&); diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/RealTimeFeedParser.cpp b/src/server/sparkd/cmds/spartan/md/mdp3/RealTimeFeedParser.cpp index f8fd2a1f9..a3fed2a7e 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/RealTimeFeedParser.cpp +++ b/src/server/sparkd/cmds/spartan/md/mdp3/RealTimeFeedParser.cpp @@ -1,5 +1,6 @@ #include "RealTimeFeedParser.h" #include "IMDP3FeedObserver.h" +#include "IRealTimeFeedObserver.h" #include "InstrumentSnapshot.h" #include "MDP3Parser.h" #include "mdp3_common.h" @@ -10,8 +11,6 @@ namespace bts::spartan::mdp3 { -enum class RealTimeFeedParser::State { INITIAL_RECOVERY, REAL_TIME_PROCESSING }; - struct RealTimeFeedParser::TradeSummary { ::mdp3::MDUpdateAction::Value update_action; InstrumentSnapshot *security; @@ -28,26 +27,26 @@ struct RealTimeFeedParser::TradeOrderDetail { struct RealTimeFeedParser::PImpl { - PImpl(std::vector> &initial_instruments, - IMDP3FeedObserver &observer) - : state(State::INITIAL_RECOVERY), instruments(), current_event(), - observer(observer) { - for (auto &i : initial_instruments) - instruments[i->security_id()] = std::move(i); - } - State state; + PImpl() + : instruments(), current_event(), observer(null_observer), pending_ts(), + pending_order_detail(), null_observer(), terminated(false) {} std::unordered_map> instruments; MDP3EventInfo current_event; - IMDP3FeedObserver &observer; + IRealTimeFeedObserver &observer; std::vector pending_ts; std::vector pending_order_detail; + struct NullObserver : public IRealTimeFeedObserver { + void on_event(MDP3EventInfo const &) {} + } null_observer; + bool terminated; }; struct RealTimeFeedParser::EventGuard { template EventGuard(RealTimeFeedParser &owner, MDP3PacketView const &v, M &m) : owner(owner) { + bts_assert_d(!owner.me->terminated); auto &info = owner.me->current_event; if (info.transact_time == MDP3TimeStamp{0}) { // Starting a new event @@ -68,6 +67,7 @@ struct RealTimeFeedParser::EventGuard { EventGuard(RealTimeFeedParser &owner, MDP3PacketView const &v, InstrumentDefinition &def) : owner(owner) { + bts_assert_d(!owner.me->terminated); auto &info = owner.me->current_event; if (info.transact_time == MDP3TimeStamp{0}) { // Starting a new event @@ -104,16 +104,33 @@ struct RealTimeFeedParser::EventGuard { RealTimeFeedParser &owner; }; -RealTimeFeedParser::RealTimeFeedParser( - std::vector> &instruments, - IMDP3FeedObserver &observer) - : me(new PImpl(instruments, observer)) {} +RealTimeFeedParser::RealTimeFeedParser() : me(new PImpl()) {} RealTimeFeedParser::~RealTimeFeedParser() {} -void RealTimeFeedParser::begin_realtime() { - bts_assert(me->state == State::INITIAL_RECOVERY); - me->state = State::REAL_TIME_PROCESSING; +void RealTimeFeedParser::set_observer(IRealTimeFeedObserver &o) { + me->observer = o; +} + +void RealTimeFeedParser::add_snapshot( + std::unique_ptr snapshot) { + bts_assert(!is_processing_multi_message_event()); + bts_assert(!me->terminated); + bts_assert(find_instrument(snapshot->security_id()) == nullptr); + me->instruments[snapshot->security_id()] = std::move(snapshot); +} + +bool RealTimeFeedParser::is_processing_multi_message_event() const { + return me->current_event.transact_time != MDP3TimeStamp{}; +} + +std::vector> RealTimeFeedParser::remove_all_securities_and_terminate() { + bts_assert(!me->terminated); + + std::vector> result; + for (auto &p : me->instruments) + result.push_back(std::move(p.second)); + return result; } api::Side convert_side(::mdp3::MDEntryTypeBook::Value v) { @@ -136,9 +153,10 @@ void RealTimeFeedParser::operator()(MDP3PacketView const &pv, m.noMDEntries().forEach([&ev, this](auto &md_entry) { auto sid = MDP3SecurityID{md_entry.securityID()}; - auto &iptr = me->instruments[sid]; - bts_assert(iptr); - ev.record_security_change(iptr.get()); + auto iptr = find_instrument(sid); + if (!iptr) + return; + ev.record_security_change(iptr); auto side = convert_side(md_entry.mDEntryType()); auto level = BookLevel{md::IntPrice{md_entry.mDEntryPx().mantissa(), md_entry.mDEntryPx().exponent()}, @@ -226,9 +244,10 @@ void RealTimeFeedParser::operator()( m.noMDEntries().forEach([&ev, this](auto &md_entry) { auto sid = MDP3SecurityID{md_entry.securityID()}; - auto &iptr = me->instruments[sid]; - bts_assert(iptr); - ev.record_security_change(iptr.get()); + auto iptr = find_instrument(sid); + if (!iptr) + return; + ev.record_security_change(iptr); switch (md_entry.mDEntryType()) { case ::mdp3::MDEntryTypeDailyStatistics::ClearedVolume: @@ -257,9 +276,10 @@ void RealTimeFeedParser::operator()( m.noMDEntries().forEach([&ev, this](auto &md_entry) { auto sid = MDP3SecurityID{md_entry.securityID()}; - auto &iptr = me->instruments[sid]; - bts_assert(iptr); - ev.record_security_change(iptr.get()); + auto iptr = find_instrument(sid); + if (!iptr) + return; + ev.record_security_change(iptr); iptr->set_high_limit_price( md::IntPrice{md_entry.highLimitPrice().mantissa(), md_entry.highLimitPrice().exponent()}); @@ -278,9 +298,10 @@ void RealTimeFeedParser::operator()( m.noMDEntries().forEach([&ev, this](auto &md_entry) { auto sid = MDP3SecurityID{md_entry.securityID()}; - auto &iptr = me->instruments[sid]; - bts_assert(iptr); - ev.record_security_change(iptr.get()); + auto iptr = find_instrument(sid); + if (!iptr) + return; + ev.record_security_change(iptr); MDP3Order order; order.key = MDP3OrderKey{md_entry.orderID()}; @@ -314,9 +335,10 @@ void RealTimeFeedParser::operator()( m.noMDEntries().forEach([&ev, this](auto &md_entry) { auto sid = MDP3SecurityID{md_entry.securityID()}; - auto &iptr = me->instruments[sid]; - bts_assert(iptr); - ev.record_security_change(iptr.get()); + auto iptr = find_instrument(sid); + if (!iptr) + return; + ev.record_security_change(iptr); switch (md_entry.mDUpdateAction()) { case ::mdp3::MDUpdateAction::New: @@ -372,12 +394,14 @@ void RealTimeFeedParser::operator()( m.noMDEntries().forEach([&ev, this](auto &md_entry) { auto sid = MDP3SecurityID{md_entry.securityID()}; - auto &iptr = me->instruments[sid]; - bts_assert(iptr); - ev.record_security_change(iptr.get()); + auto iptr = find_instrument(sid); + if (!iptr) + return; + ev.record_security_change(iptr); + TradeSummary summary; summary.update_action = md_entry.mDUpdateAction(); - summary.security = iptr.get(); + summary.security = iptr; summary.price = md::IntPrice{md_entry.mDEntryPx().mantissa(), md_entry.mDEntryPx().exponent()}; summary.n_orders = md_entry.numberOfOrders(); @@ -399,9 +423,11 @@ void RealTimeFeedParser::operator()(MDP3PacketView const &pv, EventGuard ev(*this, pv, m); m.noMDEntries().forEach([&ev, this](auto &md_entry) { auto sid = MDP3SecurityID{md_entry.securityID()}; - auto &iptr = me->instruments[sid]; - bts_assert(iptr); - ev.record_security_change(iptr.get()); + auto iptr = find_instrument(sid); + if (!iptr) + return; + ev.record_security_change(iptr); + iptr->set_electronic_volume(MDP3Quantity{md_entry.mDEntrySize()}); }); } @@ -411,11 +437,16 @@ void RealTimeFeedParser::on_instrument_definition(MDP3PacketView const &pv, Def &d) { InstrumentDefinition def{d}; EventGuard ev(*this, pv, def); + auto sid = MDP3SecurityID{d.securityID()}; - auto &iptr = me->instruments[sid]; - if (!iptr) - iptr.reset(new InstrumentSnapshot(sid)); - ev.record_security_change(iptr.get()); + auto iptr = find_instrument(sid); + if (!iptr) { + auto new_snapshot = std::make_unique (sid); + iptr = new_snapshot.get(); + me->instruments[sid] = std::move(new_snapshot); + } + ev.record_security_change(iptr); + iptr->set_instrument_definition(def); } @@ -445,10 +476,13 @@ void RealTimeFeedParser::operator()(MDP3PacketView const &pv, // This applies only to the security if (ss.securityID() != ss.securityIDNullValue()) { + auto sid = MDP3SecurityID{ss.securityID()}; - auto &iptr = me->instruments[sid]; - bts_assert(iptr); - ev.record_security_change(iptr.get()); + auto iptr = find_instrument(sid); + if (!iptr) + return; + ev.record_security_change(iptr); + iptr->set_trading_status(ss.securityTradingStatus()); return; } @@ -484,6 +518,12 @@ void RealTimeFeedParser::operator()(MDP3PacketView const &pv, } } +void RealTimeFeedParser::operator()(MDP3PacketView const &pv, ::mdp3::ChannelReset4 &r) { + EventGuard ev(*this, pv, r); + for (auto &p : me->instruments) + p.second->reset_books(); +} + 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 @@ -522,4 +562,12 @@ void RealTimeFeedParser::on_end_of_event() { me->observer.on_event(me->current_event); } +InstrumentSnapshot *RealTimeFeedParser::find_instrument(MDP3SecurityID id) { + auto it = me->instruments.find(id); + if (it == me->instruments.end()) + return nullptr; + else + return it->second.get(); +} + } // namespace bts::spartan::mdp3 diff --git a/src/server/sparkd/cmds/spartan/md/mdp3/RealTimeFeedParser.h b/src/server/sparkd/cmds/spartan/md/mdp3/RealTimeFeedParser.h index 1adf66f6e..b989f0544 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/RealTimeFeedParser.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/RealTimeFeedParser.h @@ -1,5 +1,6 @@ #pragma once +#include "mdp3_common.h" #include #include @@ -22,15 +23,54 @@ class SecurityStatus30; namespace bts::spartan::mdp3 { class InstrumentSnapshot; -class IMDP3FeedObserver; +class IRealTimeFeedObserver; class MDP3PacketView; +// Updates a set of instruments snapshots with the incremental changes +// reported in the real time incremental feed. Callers must ensure +// that the snapshots they pass are synchronized with the real time +// feeed, that is that for any added snapshot the next message +// processed by this class must be the message expected by the +// snapshot. In addition to updating the passed snapshots this class +// will also create new snapshots for any new securities discovered +// through the real time incremental feed. class RealTimeFeedParser { public: - RealTimeFeedParser(std::vector> &, - IMDP3FeedObserver &); + RealTimeFeedParser(); ~RealTimeFeedParser(); - void begin_realtime(); + + void set_observer(IRealTimeFeedObserver &); + + // Ownership of the passed snapshot is transfered to this + // class. Caller must ensure that the snapshot is synchronized with + // the real time feed, that is the next MDP3 message received by + // this instance is exactly the next that the snapshot + // expects. Additionally, callers must ensure that no snapshots are + // added while this class is in the middle of processing a multi + // message event, that is while is_processing_multi_message_event() + // returns true. Note that if this class obersves a security + // definition message for a security that has not been previously + // added through add_snapshot() it will assume that the instrument + // was just created and has an empty book so subsquent incremental + // update messages must be consistent with an initially empty book. + void add_snapshot(std::unique_ptr snapshot); + + // Discrete exchange events like a single order entry are + // potentially described through multiple MDP3 messages. Messages + // describing a single event have to be processed together as a + // unit. While processing a multi event message the snapshots + // maintained by this class can be in an inconsistent state. For + // that reason this class does not allow adding or removeing + // snapshots during event processing. + bool is_processing_multi_message_event() const; + + // Stop processing incremental udpates and transfer ownership of + // all snapshots to the caller. It is invalid to call any other + // method after this one. It is also a fatal error to invoke this + // method if is_processing_multi_message_event() returns true. + std::vector> + remove_all_securities_and_terminate(); + void operator()(MDP3PacketView const &, ::mdp3::MDIncrementalRefreshBook46 &); void operator()(MDP3PacketView const &, @@ -53,16 +93,18 @@ class RealTimeFeedParser { ::mdp3::MDInstrumentDefinitionSpread56 &); void operator()(MDP3PacketView const &, ::mdp3::QuoteRequest39 &); void operator()(MDP3PacketView const &, ::mdp3::SecurityStatus30 &); + void operator()(MDP3PacketView const &, ::mdp3::ChannelReset4 &); private: - enum class State; struct PImpl; struct EventGuard; struct TradeSummary; struct TradeOrderDetail; std::unique_ptr me; + InstrumentSnapshot *find_instrument(MDP3SecurityID); void on_end_of_event(); - template void on_instrument_definition(MDP3PacketView const &, Def &); + template + void on_instrument_definition(MDP3PacketView const &, Def &); }; } // 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 d0300e821..4014291a5 100644 --- a/src/server/sparkd/cmds/spartan/md/mdp3/mdp3_common.h +++ b/src/server/sparkd/cmds/spartan/md/mdp3/mdp3_common.h @@ -18,7 +18,8 @@ using MDP3SecurityID = class InstrumentSnapshot; -struct MDP3EventInfo { +class MDP3EventInfo { +public: MDP3TimeStamp transact_time = MDP3TimeStamp{0}; MDP3TimeStamp first_packet_send_time; MDP3TimeStamp last_packet_send_time;