Creating scaffolding for incremental book updates in GeneralMarketDataListener
This commit is contained in:
1 parent
3701a70bd9
commit
c53561c6ef
3 files changed
+94
-30
No files matched your search
@@ -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<domain::SecurityID, domain::Book> 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<bts::domain::SecurityID, uint64_t> 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<utils::TimeStamp> time) {
|
||||
void GeneralMarketDataListener::on_book(
|
||||
const bts::domain::Book &book, std::optional<utils::TimeStamp> 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<bts::domain::FutureSecurity>(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<utils::TimeStamp> 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
|
||||
@@ -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<utils::TimeStamp> time = std::nullopt) override;
|
||||
void on_book(
|
||||
const domain::Book &book,
|
||||
std::optional<utils::TimeStamp> time = std::nullopt) override;
|
||||
void on_book_increment(const BookOp &) override;
|
||||
void on_trades(
|
||||
const std::vector<domain::TradeRecord const *> &trades) override;
|
||||
void on_rfqs(const std::vector<domain::RFQ const *> &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<utils::TimeStamp> time = std::nullopt);
|
||||
};
|
||||
} // namespace bts::md
|
||||
@@ -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 <vector>
|
||||
|
||||
@@ -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<utils::TimeStamp> time = std::nullopt) = 0;
|
||||
virtual void on_book_increment(const BookOp &) {}
|
||||
virtual void on_book(
|
||||
const domain::Book &book,
|
||||
std::optional<utils::TimeStamp> time = std::nullopt) = 0;
|
||||
virtual void on_book_increment(const IMarketDataListener::BookOp &) {}
|
||||
virtual void on_trades(
|
||||
const std::vector<domain::TradeRecord const *> &trades) = 0;
|
||||
virtual void on_rfqs(const std::vector<domain::RFQ const *> &rfqs) = 0;
|
||||
|
||||
Reference in new issue
Block a user