From bc0d606252060f9606a099dba092bb8071d32707 Mon Sep 17 00:00:00 2001 From: Mohammad Arham Date: Thu, 3 Oct 2024 12:03:48 +0000 Subject: [PATCH] Add: code to connect to Live databento --- src/lib/api/md/CboeMarketDataConfig.proto | 16 +-- .../api/md/DatabentoMarketDataConfig.proto | 16 +++ src/lib/api/md/GlbxMarketDataConfig.proto | 6 +- src/lib/config/profiles/default/jetfix.conf | 14 ++- src/lib/config/protos/jetfix.proto | 6 +- .../sim/SimExecutionGatewayFactory.cpp | 11 +- src/server/sparkd/md/MarketDataGateway.cpp | 28 +++-- .../sparkd/md/cboe/CboeMarketDataHandler.cpp | 31 ++--- .../sparkd/md/cboe/CboeMarketDataHandler.h | 8 +- ...taHanlder.cpp => DatabentoDataHandler.cpp} | 108 +++++++++++++++--- .../md/databento/DatabentoDataHandler.h | 2 + .../sparkd/md/glbx/GlbxMarketDataHandler.cpp | 30 +++-- .../sparkd/md/glbx/GlbxMarketDataHandler.h | 9 +- 13 files changed, 201 insertions(+), 84 deletions(-) create mode 100644 src/lib/api/md/DatabentoMarketDataConfig.proto rename src/server/sparkd/md/databento/{DatabentoDataHanlder.cpp => DatabentoDataHandler.cpp} (57%) diff --git a/src/lib/api/md/CboeMarketDataConfig.proto b/src/lib/api/md/CboeMarketDataConfig.proto index 9b6013d51..2cf87fa0b 100644 --- a/src/lib/api/md/CboeMarketDataConfig.proto +++ b/src/lib/api/md/CboeMarketDataConfig.proto @@ -1,6 +1,7 @@ syntax = "proto3"; import "lib/api/domain/SecurityCriteria.proto"; +import "lib/api/md/DatabentoMarketDataConfig.proto"; package bts.api.md; @@ -9,18 +10,7 @@ option csharp_namespace = "bts.api.md"; message CboeMarketDataConfig { uint32 unique_check_value = 1; - bool use_databento = 10; - bool read_from_file = 20; - string contracts_file_name = 30; - string bbo_file_name = 40; - //uint32 num_depth_levels = 1; - //uint32 depth_throttle_ms = 2; bts.api.domain.SecurityCriteria security_criteria = 3; - // Market data isn't subscribed for securities listed here (even if they are - // included by 'security_criteria'). There are some securities that don't - // fit into our system just yet, such as 'BTCBIDR', which trades in prices - // larger than our cpp 'domain::Price' class supports. - //repeated string excluded_securities = 4; - //string url_rest_base = 5; - //string url_ws_base = 6; + bool use_databento = 10; + DatabentoMarketDataConfig databento_settings = 20; } diff --git a/src/lib/api/md/DatabentoMarketDataConfig.proto b/src/lib/api/md/DatabentoMarketDataConfig.proto new file mode 100644 index 000000000..59a3c4af6 --- /dev/null +++ b/src/lib/api/md/DatabentoMarketDataConfig.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; + +package bts.api.md; + +option csharp_namespace = "bts.api.md"; +option cc_enable_arenas = true; + +message DatabentoMarketDataConfig { + uint32 unique_check_value = 1; + bool read_from_file = 20; + string contracts_file_name = 30; + string bbo_file_name = 40; + double replay_sleep_timer_sec = 50; + string key = 60; + string dataset = 70; +} diff --git a/src/lib/api/md/GlbxMarketDataConfig.proto b/src/lib/api/md/GlbxMarketDataConfig.proto index ee7b6ee98..486fd6cbc 100644 --- a/src/lib/api/md/GlbxMarketDataConfig.proto +++ b/src/lib/api/md/GlbxMarketDataConfig.proto @@ -1,7 +1,7 @@ syntax = "proto3"; import "lib/api/domain/SecurityCriteria.proto"; - +import "lib/api/md/DatabentoMarketDataConfig.proto"; package bts.api.md; option csharp_namespace = "bts.api.md"; @@ -9,9 +9,9 @@ option csharp_namespace = "bts.api.md"; message GlbxMarketDataConfig { uint32 unique_check_value = 1; - bool use_databento = 10; - bool read_from_file = 20; bts.api.domain.SecurityCriteria security_criteria = 3; + bool use_databento = 10; + DatabentoMarketDataConfig databento_settings =20; //uint32 num_depth_levels = 1; //uint32 depth_throttle_ms = 2; // Market data isn't subscribed for securities listed here (even if they are diff --git a/src/lib/config/profiles/default/jetfix.conf b/src/lib/config/profiles/default/jetfix.conf index aad2eec51..3e007a57a 100644 --- a/src/lib/config/profiles/default/jetfix.conf +++ b/src/lib/config/profiles/default/jetfix.conf @@ -4028,5 +4028,17 @@ JetFIX { name: "deribit_1" venue: VENUE_DERIBIT } + # CME EXECUTION NEEDS TO BE UPDATED, probably below + # connection { + # name: "onixs_sim" + # venue: VENUE_GLBX + # } } -} \ No newline at end of file +} +# NotSimExecution { +# value { +# entries { +# venue: VENUE_CBOE +# } +# } +# } \ No newline at end of file diff --git a/src/lib/config/protos/jetfix.proto b/src/lib/config/protos/jetfix.proto index 0a8ba56e9..46e8083da 100644 --- a/src/lib/config/protos/jetfix.proto +++ b/src/lib/config/protos/jetfix.proto @@ -105,10 +105,10 @@ message CMECannedSecureLogon { } -message SimExecution{ - message SimExecutionEntry { +message NotSimExecution{ + message NotSimExecutionEntry { bts.api.Venue venue = 100; bool incorporate_crosses_into_book = 200; } - repeated SimExecutionEntry entries = 100; + repeated NotSimExecutionEntry entries = 100; } diff --git a/src/lib/orderman/gateways/sim/SimExecutionGatewayFactory.cpp b/src/lib/orderman/gateways/sim/SimExecutionGatewayFactory.cpp index 0e90fb0bf..2342f959a 100644 --- a/src/lib/orderman/gateways/sim/SimExecutionGatewayFactory.cpp +++ b/src/lib/orderman/gateways/sim/SimExecutionGatewayFactory.cpp @@ -61,15 +61,14 @@ SimExecutionGatewayFactory *SimExecutionGatewayFactory::get() { } bool SimExecutionGatewayFactory::is_sim_execution_on(api::Venue venue) { - // TODO: Arham, fix this ! - return true; - const auto &sim_exec = config::get(); - for (const auto &e : sim_exec.entries()) { + // return true; + const auto ¬_sim_exec = config::get(); + for (const auto &e : not_sim_exec.entries()) { if (e.venue() == venue) { - return true; + return false; } } - return false; + return true; } } // namespace bts::orderman \ No newline at end of file diff --git a/src/server/sparkd/md/MarketDataGateway.cpp b/src/server/sparkd/md/MarketDataGateway.cpp index 5ce6c87f1..d35109916 100644 --- a/src/server/sparkd/md/MarketDataGateway.cpp +++ b/src/server/sparkd/md/MarketDataGateway.cpp @@ -254,7 +254,10 @@ void MarketDataGateway::PImpl::start_glbx(){ std::make_shared(bts::api::VENUE_GLBX)); std::string tid_name = "glbx"; bts::threading::make_thread(tid_name, [&]() { - bts::md::glbx::GlbxMarketDatahandler::run(glbx.read_from_file(), p->listener,sec_id_store,api::domain::VENUE_GLBX); + api::md::glbx::GlbxMarketDatahandler::run( + p->listener, + sec_id_store, + glbx); }).detach(); } else{ @@ -387,16 +390,19 @@ void MarketDataGateway::PImpl::start_taifex() { } void MarketDataGateway::PImpl::start_cboe(){ // cboe - for (const auto &cboe : md_config.cboe()) { - auto p = venue_listeners.emplace_back( - std::make_shared(bts::api::VENUE_CBOE)); - std::string tid_name = "CBOE"; - bts::threading::make_thread(tid_name, [&]() { - bts::md::cboe::CboeMarketDatahandler::run(cboe.read_from_file(), p->listener,sec_id_store,api::domain::VENUE_CBOE); - }).detach(); - - UNUSED(cboe); - } + for (const api::md::CboeMarketDataConfig &cboe : md_config.cboe()) { + auto p = venue_listeners.emplace_back( + std::make_shared(bts::api::VENUE_CBOE)); + std::string tid_name = "CBOE"; + bts::threading::make_thread(tid_name, [&]() { + api::md::cboe::CboeMarketDatahandler::run( + p->listener, + sec_id_store, + cboe); + }).detach(); + + UNUSED(cboe); + } } diff --git a/src/server/sparkd/md/cboe/CboeMarketDataHandler.cpp b/src/server/sparkd/md/cboe/CboeMarketDataHandler.cpp index 3ef7ecafc..6ecf637f6 100644 --- a/src/server/sparkd/md/cboe/CboeMarketDataHandler.cpp +++ b/src/server/sparkd/md/cboe/CboeMarketDataHandler.cpp @@ -2,31 +2,34 @@ #include "lib/api/domain/Venue.pb.h" #include "lib/config/config.h" #include "lib/config/protos/system_config.pb.h" +#include "lib/utils/bts_assert.h" #include "server/sparkd/md/databento/DatabentoDataHandler.h" #include +#include #include #include #include #include #include -namespace bts::md::cboe { +namespace bts::api::md::cboe { void CboeMarketDatahandler::run( - bool use_file, - std::shared_ptr listner, - std::shared_ptr sec_id_store, - bts::api::domain::Venue venue, - const std::string& contracts_file_name, - const std::string& bbo_file_name) { - std::string base_path = - config::get().data_path(); - DatabentoHandler::DatabentoDatahandler::run( - use_file, + std::shared_ptr<::bts::md::IMarketDataListener> listner, + std::shared_ptr sec_id_store, + const CboeMarketDataConfig &config) { + bts_assert_msg(config.use_databento(), "CboeMarketDatahandler only supports databento"); + std::string base_path = + config::get().data_path(); + bts_assert(config.has_databento_settings()); + bts::md::DatabentoHandler::DatabentoDatahandler::run( + config.databento_settings().read_from_file(), listner, sec_id_store, - venue, - base_path + contracts_file_name, - base_path + bbo_file_name); + domain::VENUE_CBOE, + base_path + config.databento_settings().contracts_file_name(), + base_path + config.databento_settings().bbo_file_name(), + config.databento_settings().key(), + config.databento_settings().dataset()); } } // namespace bts::md::cboe \ No newline at end of file diff --git a/src/server/sparkd/md/cboe/CboeMarketDataHandler.h b/src/server/sparkd/md/cboe/CboeMarketDataHandler.h index 1774e8ed0..f9db8becc 100644 --- a/src/server/sparkd/md/cboe/CboeMarketDataHandler.h +++ b/src/server/sparkd/md/cboe/CboeMarketDataHandler.h @@ -1,12 +1,12 @@ #pragma once +#include "lib/api/md/CboeMarketDataConfig.pb.h" #include "lib/md/IMarketDataListener.h" #include "lib/persistence/IDGenerator.h" -namespace bts::md::cboe { +namespace bts::api::md::cboe { class CboeMarketDatahandler { public: - static void run(bool use_file, - std::shared_ptr listner, + static void run(std::shared_ptr<::bts::md::IMarketDataListener> listner, std::shared_ptr sec_id_store, - bts::api::domain::Venue venue); + const CboeMarketDataConfig &config); }; } // namespace bts::md::cboe \ No newline at end of file diff --git a/src/server/sparkd/md/databento/DatabentoDataHanlder.cpp b/src/server/sparkd/md/databento/DatabentoDataHandler.cpp similarity index 57% rename from src/server/sparkd/md/databento/DatabentoDataHanlder.cpp rename to src/server/sparkd/md/databento/DatabentoDataHandler.cpp index 98d493d5a..80e806201 100644 --- a/src/server/sparkd/md/databento/DatabentoDataHanlder.cpp +++ b/src/server/sparkd/md/databento/DatabentoDataHandler.cpp @@ -1,5 +1,8 @@ #include "DatabentoDataHandler.h" #include "databento/datetime.hpp" +#include "databento/enums.hpp" +#include "databento/live_blocking.hpp" +#include "databento/log.hpp" #include "databento/timeseries.hpp" #include "lib/api/domain/Venue.pb.h" #include "lib/config/config.h" @@ -8,6 +11,8 @@ #include "lib/domain/Quantity.h" #include "lib/domain/Security.h" #include "lib/domain/SecurityID.h" +#include "lib/domain_model/Security.pb.h" +#include "lib/logging/logging.h" #include "lib/utils/TimeStamp.h" #include "lib/utils/bts_assert.h" #include "lib/utils/bts_chrono.h" @@ -16,6 +21,7 @@ #include #include #include +#include #include #include @@ -39,25 +45,37 @@ void DatabentoDatahandler::run( bts::api::domain::Venue venue, const std::string &securities_path, const std::string &books_path, + const std::string &key, + const std::string &dataset, const std::chrono::duration sleep_duration) { - auto client = databento::HistoricalBuilder{} - .SetKey("db-Q4eCbTQfrAHaG6sAcuFUYB9YqkF6j") - .Build(); + auto sleep_duration_updated = sleep_duration; + if (!use_file) { + sleep_duration_updated = std::chrono::duration::zero(); // Set to zero for live data + } auto process_security = [&](const databento::Record &record) { const auto &msg = record.Get(); std::string id = remove_spaces(msg.RawSymbol()); std::string symbol; - + switch (venue) { - case api::domain::Venue::VENUE_CBOE: - symbol = id.substr(0, 3); + case api::domain::Venue::VENUE_CBOE:{ + symbol = remove_spaces(std::string(msg.underlying.data(),msg.underlying.size())); + const auto exp_nanos = msg.expiration; + std::string exp_string = databento::ToIso8601(exp_nanos); + const auto expiration_time = bts::core::TimeStamp::from_iso_string(exp_string); + if(expiration_time.seconds() < expiration_time.now().seconds() || expiration_time.seconds() > expiration_time.now().seconds() + 8*30*24*60){ + return databento::KeepGoing::Continue; + } break; + } case api::domain::Venue::VENUE_GLBX: if (id.length() > 5) return databento::KeepGoing::Continue; symbol = id.substr(0, 2); + // sec.security_common.cpp = msg.unit_of_measure_qty; + break; default: bts_assert_msg(false, "Exchange not supported on databento"); @@ -71,13 +89,14 @@ void DatabentoDatahandler::run( sec.security_common.id = sec_id; sec.security_common.exchange_sec_id = msg.hd.instrument_id; sec.security_common.desc = msg.RawSymbol(); - sec.security_common.symbol = symbol; - sec.security_common.asset = symbol; - sec.security_common.group = symbol; + sec.security_common.symbol = msg.Asset(); + sec.security_common.asset = msg.Asset(); + sec.security_common.group = msg.Group(); sec.security_common.min_price_increment = 0.01; sec.security_common.min_quantity = 0.01; sec.security_common.min_quantity_increment = 0.01; sec.security_common.currency = {msg.currency.data(), msg.currency.size()}; + // sec.security_common.cpp = msg.hd. const std::string_view sec_type = {msg.security_type.data(), msg.security_type.size()}; const auto exp_nanos = msg.expiration; @@ -85,8 +104,15 @@ void DatabentoDatahandler::run( const auto expiration_time = bts::core::TimeStamp::from_iso_string(exp_string); if (sec_type.find("OPT") != std::string::npos) { + auto call_put = api::domain::CallPut::PUT; + if(msg.instrument_class == 'C' ){ + call_put=api::domain::CallPut::CALL; + } + else{ + bts_assert(msg.instrument_class == 'P'); + } sec.security.emplace( - expiration_time, bts::domain::Price::from_double(msg.strike_price)); + expiration_time, bts::domain::Price(msg.strike_price, -9),call_put); } else if (sec_type.find("FUT") != std::string::npos) { sec.security.emplace(expiration_time); } else { @@ -105,7 +131,7 @@ void DatabentoDatahandler::run( if (!sec_id_opt.has_value()) return databento::KeepGoing::Continue; - sleep_for(sleep_duration); + sleep_for(sleep_duration_updated); bts::domain::SecurityID sec_id = bts::domain::SecurityID(sec_id_opt.value()); domain::Book book; book.set_security_id(sec_id); @@ -126,15 +152,67 @@ void DatabentoDatahandler::run( utils::TimeStamp{datetime::ptime_to_timestamp(recv_time.to_ptime())}); return databento::KeepGoing::Continue; }; - if (use_file) { - std::string base_path = - config::get().data_path(); + auto client = databento::HistoricalBuilder{} + .SetKey(key) + .Build(); databento::DbnFileStore dbn_file_store(securities_path); dbn_file_store.Replay(process_security); - databento::DbnFileStore dbn_file_store2(books_path); dbn_file_store2.Replay(process_bbo); + }else { + bts_assert(dataset!=""); + // auto client = databento::LiveBuilder{} + // .SetKey(key) + // .SetDataset(dataset) + // .BuildThreaded(); + auto client = databento::LiveBlocking{ + databento::ILogReceiver::Default(), + key, + dataset, + true, + databento::VersionUpgradePolicy::Upgrade, + {} + }; + //hardcoding for now will update later + switch (venue) { + case api::domain::VENUE_CBOE: + client.Subscribe({"SPX.OPT","SPXW.OPT"}, databento::Schema::Definition, databento::SType::Parent); + client.Subscribe({"SPX.OPT","SPXW.OPT"}, databento::Schema::Mbp1, databento::SType::Parent); + break; + case api::domain::VENUE_GLBX: + client.Subscribe({"ES.FUT"}, databento::Schema::Definition, databento::SType::Parent); + client.Subscribe({"ES.FUT"}, databento::Schema::Mbp1, databento::SType::Parent); + break; + default: + bts_assert_msg(false, "ONLY GLBX AND CBOE SUPPORTED FOR NOW"); + + } + client.Start(); + while (true) { + try { + auto* record = client.NextRecord(std::chrono::seconds{5}); + if (record == nullptr) { + LOG(md, Error, "Timed out waiting for data"); + break; + } + if (auto* bbo = record->GetIf()) { + process_bbo(*record); + } + if (auto* def = record->GetIf()) { + process_security(*record); + } + } catch (const databento::TcpError& error) { + std::string err = error.what(); + LOG(md, Error, "Caught TCP error: {} \nAttempting reconnect\n",err); + client.Reconnect(); + } catch (const std::exception& exc) { + std::string err = exc.what(); + LOG(md, Error, "Caught unknown exception: {} \nShutting down\n",err); + client.Stop(); + } + + } } } diff --git a/src/server/sparkd/md/databento/DatabentoDataHandler.h b/src/server/sparkd/md/databento/DatabentoDataHandler.h index 1db36e81b..3eb0738ee 100644 --- a/src/server/sparkd/md/databento/DatabentoDataHandler.h +++ b/src/server/sparkd/md/databento/DatabentoDataHandler.h @@ -12,6 +12,8 @@ class DatabentoDatahandler { bts::api::domain::Venue venue, const std::string& securities_path, const std::string& books_path, + const std::string &dataset="", + const std::string& key = "db-Q4eCbTQfrAHaG6sAcuFUYB9YqkF6j", const std::chrono::duration sleep_duration = 0.04s); }; } // namespace bts::md::databento \ No newline at end of file diff --git a/src/server/sparkd/md/glbx/GlbxMarketDataHandler.cpp b/src/server/sparkd/md/glbx/GlbxMarketDataHandler.cpp index 35cb0f483..c0cf85f85 100644 --- a/src/server/sparkd/md/glbx/GlbxMarketDataHandler.cpp +++ b/src/server/sparkd/md/glbx/GlbxMarketDataHandler.cpp @@ -1,7 +1,9 @@ #include "GlbxMarketDataHandler.h" #include "lib/api/domain/Venue.pb.h" +#include "lib/api/md/GlbxMarketDataConfig.pb.h" #include "lib/config/config.h" #include "lib/config/protos/system_config.pb.h" +#include "lib/utils/bts_assert.h" #include "server/sparkd/md/databento/DatabentoDataHandler.h" #include #include @@ -9,18 +11,26 @@ #include #include #include -namespace bts::md::glbx { +namespace bts::api::md::glbx { void GlbxMarketDatahandler::run( - bool use_file, - std::shared_ptr listner, + std::shared_ptr<::bts::md::IMarketDataListener> listner, std::shared_ptr sec_id_store, - bts::api::domain::Venue venue) { - std::string contratcs_file_name = "/glbx-mdp3-20240815.definition.dbn.zst"; - std::string bbo_file_name = "/glbx-mdp3-20240815.mbp-1.dbn.zst"; - std::string base_path = - config::get().data_path(); - DatabentoHandler::DatabentoDatahandler::run(use_file, listner,sec_id_store,venue,base_path + contratcs_file_name,base_path + bbo_file_name); + const GlbxMarketDataConfig &config) { + bts_assert_msg(config.use_databento(), + "GlbxMarketDatahandler only supports databento"); + std::string base_path = + config::get().data_path(); + bts_assert(config.has_databento_settings()); + bts::md::DatabentoHandler::DatabentoDatahandler::run( + config.databento_settings().read_from_file(), + listner, + sec_id_store, + domain::VENUE_GLBX, + base_path + config.databento_settings().contracts_file_name(), + base_path + config.databento_settings().bbo_file_name(), + config.databento_settings().key(), + config.databento_settings().dataset()); } -} // namespace bts::md::glbx \ No newline at end of file +} // namespace bts::api::md::glbx \ No newline at end of file diff --git a/src/server/sparkd/md/glbx/GlbxMarketDataHandler.h b/src/server/sparkd/md/glbx/GlbxMarketDataHandler.h index 4a4b43989..7516dc69d 100644 --- a/src/server/sparkd/md/glbx/GlbxMarketDataHandler.h +++ b/src/server/sparkd/md/glbx/GlbxMarketDataHandler.h @@ -1,12 +1,13 @@ #pragma once +#include "lib/api/md/GlbxMarketDataConfig.pb.h" #include "lib/md/IMarketDataListener.h" #include "lib/persistence/IDGenerator.h" -namespace bts::md::glbx { +namespace bts::api::md::glbx { class GlbxMarketDatahandler { public: - static void run(bool use_file, - std::shared_ptr listner, + static void run( + std::shared_ptr<::bts::md::IMarketDataListener> listner, std::shared_ptr sec_id_store, - bts::api::domain::Venue venue); + const GlbxMarketDataConfig& config); }; } // namespace bts::md::glbx \ No newline at end of file