MarketDataGateway still exists, but is greatly simplified. It creates several `IMarketDataProvider`s, each of which generates a list of `IMarketDataSource`s. The market data sources provide market data to an `IMarketDataListener`. * EmumbaMarketDataProvider * CCApiMarketDataProvider * PyMarketDataProvider * OnixsMarketDataProvider * ReplayMarketDataProvider As a side effect, individual py market data sources are now explicitly limited to a single venue (it probably should have been this way all along, since the shared memory writers do not expect updates to involve multiple venues).
147 lines
4.9 KiB
C++
147 lines
4.9 KiB
C++
#include "CCApiMarketDataProvider.h"
|
|
#include "spark/api/domain/Venue.pb.h"
|
|
#include "spark/api/md/MarketDataConfig.pb.h"
|
|
#include "spark/config/config.h"
|
|
#include "spark/io/ProtoPrinter.h"
|
|
#include "spark/logging/logging.h"
|
|
#include "spark/md/GeneralMarketDataListener.h"
|
|
#include "spark/md/GeneralSecurityIndex.h"
|
|
#include "spark/md/MDProducer.h"
|
|
#include "spark/persistence/IDGenerator.h"
|
|
#include "sparkd/md/MarketDataSubscription.h"
|
|
#include "sparkd/md/binance/BinanceSubscription.h"
|
|
#include "sparkd/md/binance_us/BinanceUsSubscription.h"
|
|
#include "sparkd/md/binance_usds_futures/BinanceUsdsFuturesSubscription.h"
|
|
#include "sparkd/md/coinbase/CoinbaseSubscription.h"
|
|
#include "sparkd/md/deribit/DeribitSubscription.h"
|
|
#include "sparkd/md/ftx/FTXSubscription.h"
|
|
#include "sparkd/md/okx/OkxSubscription.h"
|
|
|
|
#include <ccapi_cpp/ccapi_event_dispatcher.h>
|
|
|
|
namespace bts::md {
|
|
|
|
using bts::api::md::MarketDataConfig;
|
|
using bts::persistence::IDGenerator;
|
|
using std::pair;
|
|
using std::shared_ptr;
|
|
using std::string;
|
|
using std::unique_ptr;
|
|
using std::unordered_map;
|
|
using std::vector;
|
|
|
|
static bool validate(
|
|
const MarketDataConfig & /*cfg*/, vector<string> & /*errs*/) {
|
|
return true;
|
|
}
|
|
|
|
struct CCApiMarketDataProvider::PImpl {
|
|
struct VenueSink {
|
|
GeneralSecurityIndex sec_index;
|
|
MDProducer md_producer;
|
|
GeneralMarketDataListener listener;
|
|
|
|
VenueSink(bts::api::Venue venue, IDGenerator *sec_id_store)
|
|
: sec_index(), listener(sec_index, md_producer, venue, sec_id_store) {
|
|
}
|
|
};
|
|
|
|
// TODO
|
|
api::md::MarketDataConfig config;
|
|
ccapi::EventDispatcher dispatcher;
|
|
IDGenerator &sec_id_store = IDGenerator::instance();
|
|
unordered_map<api::Venue, unique_ptr<VenueSink>> venue_sinks;
|
|
vector<std::pair<api::domain::Venue, std::shared_ptr<IMarketDataSource>>>
|
|
sources;
|
|
|
|
VenueSink *get_venue_sink(api::Venue venue) {
|
|
auto it = venue_sinks.find(venue);
|
|
if (it != venue_sinks.end()) {
|
|
return it->second.get();
|
|
}
|
|
return venue_sinks
|
|
.try_emplace(venue, std::make_unique<VenueSink>(venue, &sec_id_store))
|
|
.first->second.get();
|
|
}
|
|
};
|
|
|
|
CCApiMarketDataProvider::CCApiMarketDataProvider()
|
|
: bts::md::CCApiMarketDataProvider(bts::config::get<MarketDataConfig>()) {}
|
|
|
|
CCApiMarketDataProvider::CCApiMarketDataProvider(MarketDataConfig config)
|
|
: me_(std::make_unique<PImpl>()) {
|
|
me_->config = std::move(config);
|
|
}
|
|
|
|
CCApiMarketDataProvider::~CCApiMarketDataProvider() = default;
|
|
|
|
vector<pair<api::domain::Venue, shared_ptr<IMarketDataSource>>>
|
|
CCApiMarketDataProvider::get_sources() {
|
|
auto &cfg = me_->config;
|
|
LOG(md, Info, "CCApiMarketDataProvider::start t_id:{} t_name:{}",
|
|
os::get_thread_id(), os::get_thread_name());
|
|
LOG(md, Info, "CCApiMarketDataProvider subscribing to data:\n{}",
|
|
io::ProtoPrinter::to_string(cfg));
|
|
vector<string> cfg_errs;
|
|
if (!validate(cfg, cfg_errs)) {
|
|
LOG(md, Error,
|
|
"CCApiMarketDataProvider::start failed to start due to invalid "
|
|
"config: {}",
|
|
fmt::to_string(fmt::join(cfg_errs, "\n")));
|
|
}
|
|
|
|
// Binance
|
|
for (const auto &binance : cfg.binance()) {
|
|
me_->sources.emplace_back(
|
|
api::domain::VENUE_BINANCE,
|
|
std::make_shared<binance::BinanceSubscription>(
|
|
&me_->dispatcher, binance, &me_->sec_id_store));
|
|
}
|
|
// Binance US
|
|
for (const auto &binance_us : cfg.binance_us()) {
|
|
me_->sources.emplace_back(
|
|
api::domain::VENUE_BINANCE_US,
|
|
std::make_shared<binance_us::BinanceUsSubscription>(
|
|
&me_->dispatcher, binance_us, &me_->sec_id_store));
|
|
}
|
|
// Binance USDS Futures
|
|
for (const auto &binance_ufs : cfg.binance_usds_futures()) {
|
|
me_->sources.emplace_back(
|
|
api::domain::VENUE_BINANCE_USDS_FUTURES,
|
|
std::make_shared<
|
|
binance_usds_futures::BinanceUsdsFuturesSubscription>(
|
|
&me_->dispatcher, binance_ufs, &me_->sec_id_store));
|
|
}
|
|
// Coinbase
|
|
for (const auto &coinbase : cfg.coinbase()) {
|
|
me_->sources.emplace_back(
|
|
api::domain::VENUE_COINBASE,
|
|
std::make_shared<coinbase::CoinbaseSubscription>(
|
|
&me_->dispatcher, coinbase, &me_->sec_id_store));
|
|
}
|
|
// Deribit
|
|
for (const auto &deribit : cfg.deribit()) {
|
|
me_->sources.emplace_back(
|
|
api::domain::VENUE_DERIBIT,
|
|
std::make_shared<deribit::DeribitSubscription>(
|
|
&me_->dispatcher, deribit, &me_->sec_id_store));
|
|
}
|
|
// FTX
|
|
for (const auto &ftx : cfg.ftx()) {
|
|
me_->sources.emplace_back(
|
|
api::domain::VENUE_FTX,
|
|
std::make_shared<ftx::FTXSubscription>(
|
|
&me_->dispatcher, ftx, &me_->sec_id_store));
|
|
}
|
|
// Okx
|
|
for (const auto &okx : cfg.okx()) {
|
|
me_->sources.emplace_back(
|
|
api::domain::VENUE_OKX,
|
|
std::make_shared<okx::OkxSubscription>(
|
|
&me_->dispatcher, okx, &me_->sec_id_store));
|
|
}
|
|
return me_->sources;
|
|
}
|
|
|
|
} // namespace bts::md
|