From c53561c6ef3c64cf33c24858dced565df7fe4e12 Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Mon, 23 Sep 2024 18:39:12 +0000 Subject: [PATCH] Creating scaffolding for incremental book updates in GeneralMarketDataListener --- src/lib/md/GeneralMarketDataListener.cpp | 107 +++++++++++++++++------ src/lib/md/GeneralMarketDataListener.h | 8 +- src/lib/md/IMarketDataListener.h | 9 +- 3 files changed, 94 insertions(+), 30 deletions(-) diff --git a/src/lib/md/GeneralMarketDataListener.cpp b/src/lib/md/GeneralMarketDataListener.cpp index 3552f3b01..dc2389097 100644 --- a/src/lib/md/GeneralMarketDataListener.cpp +++ b/src/lib/md/GeneralMarketDataListener.cpp @@ -21,6 +21,7 @@ #include "lib/domain/TradeRecord.h" #include "lib/domain_model/Security.pb.h" #include "lib/logging/logging.h" +#include "lib/md/IMarketDataListener.h" #include "lib/md/IntPrice.h" #include "lib/md/MDProducer.h" #include "lib/md/MDProducerHandle.h" @@ -73,6 +74,35 @@ struct CCSecSearchResult { bool exists_now; }; +struct IncrementalBookData { + std::map book_map; + + void update_incremental(const IMarketDataListener::BookOp &) { + // retreive the Book from the map and udpate it. domain::Book has a lot of functions + // already, add more if you want to. but the idea is to update the book in place, ie + // if a level needs to be added push the lower levels out + } + domain::Book const *get_const_book(domain::SecurityID id) const { + auto it = book_map.find(id); + if (it == book_map.end()) { + return nullptr; + } + return &(it->second); + } + domain::Book *get_book(domain::SecurityID id) { + auto it = book_map.find(id); + if (it == book_map.end()) { + return nullptr; + } + return &(it->second); + } + + void overwrite_book(const domain::Book &book) { + domain::SecurityID id = book.security_id(); + book_map[id] = book; + } +}; + struct GeneralMarketDataListener::PImpl { // if this is set to true, then the security key passed on in the // on_securities function is used. If this is not set to true, then a new key @@ -94,6 +124,7 @@ struct GeneralMarketDataListener::PImpl { std::unordered_map cc_sec_id_map = {}; MDArray bids = {}; MDArray asks = {}; + IncrementalBookData inc_book_data = {}; explicit PImpl( GeneralSecurityIndex &index, MDProducer &md_producer, @@ -234,7 +265,8 @@ void GeneralMarketDataListener::on_security_statistics( auto handle = _find_handle(sec_stats.security_id); bts_assert_msgf( handle != nullptr, - "GeneralMarketDataListener::on_security_statistics null handle. id: {}", + "GeneralMarketDataListener::on_security_statistics null handle. id: " + "{}", sec_stats.security_id); auto now = utils::TimeStamp::now(); bts::datetime::Date date_temp = {}; @@ -246,31 +278,21 @@ void GeneralMarketDataListener::on_security_statistics( handle->handle.set_total_volume(sec_stats.volume.value()); } -void GeneralMarketDataListener::on_book(const bts::domain::Book &book, std::optional time) { +void GeneralMarketDataListener::on_book( + const bts::domain::Book &book, std::optional time) { LOG(md, Diag, "GeneralMarketDataListener::on_book sec_id: {}", book.security_id()); - if(!time.has_value()){ - time = utils::TimeStamp::now(); - } - me->n_bk_updates++; - auto handle = _find_handle(book.security_id()); - if (handle == nullptr) { - LOG(md, Error, - "GeneralMarketDataListener::on_book, could not find sec_id: {}", - book.security_id()); - return; - } - copy_books(book, me->bids, bts::domain::Side::Value::Buy); - copy_books(book, me->asks, bts::domain::Side::Value::Sell); - _possibly_match_with_sim_orders( - bts::md::SecurityKey(book.security_id().value())); - LOG(md, Diag, - "GeneralMarketDataListener::on_book post id: {} bid: {}/{}/{} ask: " - "{}/{}/{}", - handle->id, me->bids.at(0).price(), me->bids.at(0).direct_size(), - me->bids.at(0).size(), me->asks.at(0).price(), - me->asks.at(0).direct_size(), me->asks.at(0).size()); - handle->handle.update_direct(time.value(), me->bids, me->asks); + // in case this is called externally in conjunction iwth inc book udpates + me->inc_book_data.overwrite_book(book); + _on_book(book, time); +} + +void GeneralMarketDataListener::on_book_increment( + const IMarketDataListener::BookOp &book_op) { + me->inc_book_data.update_incremental(book_op); + domain::Book const *book_ptr = me->inc_book_data.get_const_book(book_op.id); + bts_assert(book_ptr != nullptr); + _on_book(*book_ptr); } void GeneralMarketDataListener::on_trades( @@ -351,7 +373,8 @@ void GeneralMarketDataListener::_add_future(const bts::domain::Security &sec) { const auto &f_sec = std::get(sec.security); get_date_from_epoch_nanos(f_sec.expiration.nanos(), year, month, day); LOG(md, Info, - "GeneralMarketDataListener::_add_future nanos: {} yy: {} mm: {} dd: {}", + "GeneralMarketDataListener::_add_future nanos: {} yy: {} mm: {} dd: " + "{}", f_sec.expiration.nanos(), year, month, day); ff_sec.expiration_year = year; ff_sec.expiration_month = month; @@ -394,7 +417,8 @@ void GeneralMarketDataListener::_add_spread(const bts::domain::Security &sec) { auto it = me->cc_sec_id_map.find(l.security_id); if (it == me->cc_sec_id_map.end()) { LOG(md, Error, - "GeneralMarketDataListener::_add_spread, could not resolve leg. " + "GeneralMarketDataListener::_add_spread, could not resolve " + "leg. " "id: {}; leg_id: {}", sec.id(), l.security_id); return; @@ -431,7 +455,8 @@ void GeneralMarketDataListener::_commit_security(const FixedSecurity &sec) { // Add it to the index so it can receive messages auto kp = me->index.add_security(sec.numeric_id, handle); LOG(md, Info, - "GeneralMarketDataListener::_add_security_to_shm added sec: {} id: {} " + "GeneralMarketDataListener::_add_security_to_shm added sec: {} id: " + "{} " "num_id: {} n_secs: {}", std::string(handle.security().security_desc()), kp->id, handle.security().numeric_id, me->index.n_secs()); @@ -468,5 +493,33 @@ void GeneralMarketDataListener::_possibly_match_with_sim_orders( } } +void GeneralMarketDataListener::_on_book( + const domain::Book &book, std::optional time) { + LOG(md, Diag, "GeneralMarketDataListener::_on_book sec_id: {}", + book.security_id()); + if (!time.has_value()) { + time = utils::TimeStamp::now(); + } + me->n_bk_updates++; + auto handle = _find_handle(book.security_id()); + if (handle == nullptr) { + LOG(md, Error, + "GeneralMarketDataListener::_on_book, could not find sec_id: {}", + book.security_id()); + return; + } + copy_books(book, me->bids, bts::domain::Side::Value::Buy); + copy_books(book, me->asks, bts::domain::Side::Value::Sell); + _possibly_match_with_sim_orders( + bts::md::SecurityKey(book.security_id().value())); + LOG(md, Diag, + "GeneralMarketDataListener::_on_book post id: {} bid: {}/{}/{} ask: " + "{}/{}/{}", + handle->id, me->bids.at(0).price(), me->bids.at(0).direct_size(), + me->bids.at(0).size(), me->asks.at(0).price(), + me->asks.at(0).direct_size(), me->asks.at(0).size()); + handle->handle.update_direct(time.value(), me->bids, me->asks); +} + } // namespace bts::md //#endif diff --git a/src/lib/md/GeneralMarketDataListener.h b/src/lib/md/GeneralMarketDataListener.h index fce00dd67..d14af272e 100644 --- a/src/lib/md/GeneralMarketDataListener.h +++ b/src/lib/md/GeneralMarketDataListener.h @@ -23,7 +23,10 @@ class GeneralMarketDataListener : public IMarketDataListener { void on_security_state(const domain::SecurityState &security_state) override; void on_security_statistics( const domain::SecurityMarketDataStatistics &security_stats) override; - void on_book(const domain::Book &book, std::optional time = std::nullopt) override; + void on_book( + const domain::Book &book, + std::optional time = std::nullopt) override; + void on_book_increment(const BookOp &) override; void on_trades( const std::vector &trades) override; void on_rfqs(const std::vector &rfqs) override; @@ -49,5 +52,8 @@ class GeneralMarketDataListener : public IMarketDataListener { FixedSecurity &to) const; uint64_t _generate_security_key(const bts::domain::Security &sec); void _possibly_match_with_sim_orders(bts::md::SecurityKey key); + void _on_book( + const domain::Book &book, + std::optional time = std::nullopt); }; } // namespace bts::md diff --git a/src/lib/md/IMarketDataListener.h b/src/lib/md/IMarketDataListener.h index 053cdf1d1..f0b42e81e 100644 --- a/src/lib/md/IMarketDataListener.h +++ b/src/lib/md/IMarketDataListener.h @@ -1,6 +1,7 @@ #pragma once #include "lib/api/services/OrderService.pb.h" #include "lib/domain/Book.h" +#include "lib/domain/SecurityID.h" #include "lib/md/PriceSize.h" #include @@ -19,8 +20,10 @@ class MarketDataError; // The is the interface that registers itself to IMarketDataGateway and receives // all market data via callbacks. These callbacks are always on the same thread. class IMarketDataListener { + public: enum BookOpType { Add = 0, REMOVE, MODIFY }; struct BookOp { + domain::SecurityID id; bts::api::Side side; BookOpType op_type; // for op == REMOVE size in price_size is no-op @@ -38,8 +41,10 @@ class IMarketDataListener { const domain::SecurityState &security_state) = 0; virtual void on_security_statistics( const domain::SecurityMarketDataStatistics &security_stats) = 0; - virtual void on_book(const domain::Book &book, std::optional time = std::nullopt) = 0; - virtual void on_book_increment(const BookOp &) {} + virtual void on_book( + const domain::Book &book, + std::optional time = std::nullopt) = 0; + virtual void on_book_increment(const IMarketDataListener::BookOp &) {} virtual void on_trades( const std::vector &trades) = 0; virtual void on_rfqs(const std::vector &rfqs) = 0;