#include "CCApiMarketDataProvider.h" #include "domain/protobufs/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 namespace bts::md { #define VENUE(x) bts::domain::protobufs::VENUE_##x using bts::api::md::MarketDataConfig; using bts::md::binance_usds_futures::BinanceUsdsFuturesSubscription; 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 & /*errs*/) { return true; } struct CCApiMarketDataProvider::PImpl { struct VenueSink { GeneralSecurityIndex sec_index; MDProducer md_producer; GeneralMarketDataListener listener; VenueSink(api::Venue venue, shared_ptr sec_id_store) : sec_index(), listener(sec_index, md_producer, venue, sec_id_store.get()) {} }; MarketDataConfig config; ccapi::EventDispatcher dispatcher; shared_ptr sec_id_store; unordered_map> venue_sinks; vector< std::pair>> sources; PImpl(MarketDataConfig config, shared_ptr sec_id_store) : config(std::move(config)), sec_id_store(std::move(sec_id_store)) {} 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(venue, sec_id_store)) .first->second.get(); } }; CCApiMarketDataProvider::CCApiMarketDataProvider( MarketDataConfig config, shared_ptr sec_id_store) : me_(std::make_unique(std::move(config), std::move(sec_id_store))) { } CCApiMarketDataProvider::~CCApiMarketDataProvider() = default; vector>> CCApiMarketDataProvider::get_sources() { auto &cfg = me_->config; vector cfg_errs; if (!validate(cfg, cfg_errs)) { LOG(md, Error, "CCApiMarketDataProvider::get_sources failed. Invalid config: {}", fmt::to_string(fmt::join(cfg_errs, "\n"))); } // Binance for (const auto &binance : cfg.binance()) { me_->sources.emplace_back( VENUE(BINANCE), std::make_shared( &me_->dispatcher, binance, me_->sec_id_store.get())); } // Binance US for (const auto &binance_us : cfg.binance_us()) { me_->sources.emplace_back( VENUE(BINANCE_US), std::make_shared( &me_->dispatcher, binance_us, me_->sec_id_store.get())); } // Binance USDS Futures for (const auto &binance_ufs : cfg.binance_usds_futures()) { auto sub = std::make_shared< binance_usds_futures::BinanceUsdsFuturesSubscription>( &me_->dispatcher, binance_ufs, me_->sec_id_store.get()); me_->sources.emplace_back(VENUE(BINANCE_USDS_FUTURES), std::move(sub)); } // Coinbase for (const auto &coinbase : cfg.coinbase()) { auto sub = std::make_shared( &me_->dispatcher, coinbase, me_->sec_id_store.get()); me_->sources.emplace_back(VENUE(COINBASE), std::move(sub)); } // Deribit for (const auto &deribit : cfg.deribit()) { auto sub = std::make_shared( &me_->dispatcher, deribit, me_->sec_id_store.get()); me_->sources.emplace_back(VENUE(DERIBIT), std::move(sub)); } // FTX for (const auto &ftx : cfg.ftx()) { auto sub = std::make_shared( &me_->dispatcher, ftx, me_->sec_id_store.get()); me_->sources.emplace_back(VENUE(FTX), std::move(sub)); } // Okx for (const auto &okx : cfg.okx()) { me_->sources.emplace_back( VENUE(OKX), std::make_shared( &me_->dispatcher, okx, me_->sec_id_store.get())); } return me_->sources; } } // namespace bts::md