348 lines
12 KiB
C++
348 lines
12 KiB
C++
#include "BinanceSubscription.h"
|
|
#include "spark/api/domain/Venue.pb.h"
|
|
#include "spark/api/md/BinanceMarketDataConfig.pb.h"
|
|
#include "spark/core/FMT.h"
|
|
#include "spark/domain/Security.h"
|
|
#include "spark/domain/SecurityCriteria.h"
|
|
#include "spark/io/ProtoPrinter.h"
|
|
#include "spark/io/QueryParams.h"
|
|
#include "spark/logging/logging.h"
|
|
#include "spark/md/IMarketDataListener.h"
|
|
#include "spark/persistence/IDGenerator.h"
|
|
#include "sparkd/md/binance/BinanceMessageType.h"
|
|
#include "sparkd/md/binance/BinancePriceStatistics.h"
|
|
#include "sparkd/md/binance/BinancePriceStatisticsAdaptor.h"
|
|
#include "sparkd/md/binance/BinanceSecurityRequestor.h"
|
|
#include "sparkd/md/ccapi/CCApiCorrelationID.h"
|
|
#include "sparkd/md/ccapi/CCApiMarketDepthAdaptor.h"
|
|
#include "sparkd/md/ccapi/CCApiMessageLogger.h"
|
|
#include "sparkd/md/ccapi/CCApiSession.h"
|
|
#include "sparkd/md/ccapi/CCApiTradeAdaptor.h"
|
|
#include "system/collections/unordered_set_factory.h"
|
|
|
|
#include <ccapi_cpp/ccapi_event_dispatcher.h>
|
|
#include <ccapi_cpp/ccapi_macro.h>
|
|
#include <ccapi_cpp/ccapi_message.h>
|
|
#include <ccapi_cpp/ccapi_request.h>
|
|
#include <ccapi_cpp/ccapi_session.h>
|
|
#include <ccapi_cpp/ccapi_session_configs.h>
|
|
#include <ccapi_cpp/ccapi_subscription.h>
|
|
#include <range/v3/view.hpp>
|
|
#include <range/v3/view/transform.hpp>
|
|
#include <string>
|
|
#include <unordered_map>
|
|
|
|
namespace bts::md::binance {
|
|
|
|
using bts::api::domain::Venue;
|
|
using bts::api::domain::Venue_Name;
|
|
using bts::api::md::BinanceMarketDataConfig;
|
|
using bts::domain::Security;
|
|
using bts::domain::SecurityCriteria;
|
|
using bts::domain::SecurityID;
|
|
using bts::io::ProtoPrinter;
|
|
using bts::io::QueryParams;
|
|
using bts::persistence::IDGenerator;
|
|
using std::pair;
|
|
using std::string;
|
|
using std::uint32_t;
|
|
using std::unique_ptr;
|
|
using std::unordered_map;
|
|
using std::vector;
|
|
|
|
#define VENUE_NAME (Venue_Name(Venue::VENUE_BINANCE))
|
|
#define CCAPI_VENUE_NAME CCAPI_EXCHANGE_NAME_BINANCE
|
|
|
|
/**
|
|
* @brief Apply overrides from our config to a ccapi::SessionConfigs object
|
|
* that is otherwise populated with defaults
|
|
*/
|
|
static ccapi::SessionConfigs mk_session_cfg(
|
|
const BinanceMarketDataConfig &cfg) {
|
|
ccapi::SessionConfigs rv;
|
|
if (!cfg.url_rest_base().empty()) {
|
|
rv.urlRestBase[CCAPI_VENUE_NAME] = cfg.url_rest_base();
|
|
}
|
|
if (!cfg.url_ws_base().empty()) {
|
|
rv.urlWebsocketBase[CCAPI_VENUE_NAME] = cfg.url_ws_base();
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
static string mk_depth_opts(const BinanceMarketDataConfig &cfg) {
|
|
// For details on supported opts, read the docs
|
|
// https://binance-docs.github.io/apidocs/spot/en/#partial-book-depth-streams
|
|
QueryParams opts;
|
|
switch (cfg.num_depth_levels()) {
|
|
case 5:
|
|
case 10:
|
|
case 20:
|
|
// The exchange only supports these num levels
|
|
opts[CCAPI_MARKET_DEPTH_MAX] = std::to_string(cfg.num_depth_levels());
|
|
break;
|
|
case 0:
|
|
// Leave num levels unspecified (let exchange choose default)
|
|
break;
|
|
default:
|
|
LOG(md, Warn, "Invalid num depth levels '{}'. Must be one of [5, 10, 20]",
|
|
cfg.num_depth_levels());
|
|
break;
|
|
}
|
|
|
|
switch (cfg.depth_throttle_ms()) {
|
|
case 100:
|
|
case 1000:
|
|
// The exchange only supports these throttle speeds
|
|
opts[CCAPI_MARKET_DEPTH] = std::to_string(cfg.depth_throttle_ms());
|
|
break;
|
|
case 0:
|
|
// Leave throttle unspecified (let exchange choose default)
|
|
break;
|
|
default:
|
|
LOG(md, Warn,
|
|
"Invalid depth throttle '{}ms'. Must be one of [100, 250, 500])",
|
|
cfg.depth_throttle_ms());
|
|
break;
|
|
}
|
|
|
|
return opts.to_string();
|
|
}
|
|
|
|
static ccapi::Subscription mk_depth_sub(
|
|
const string &sec_desc, const string &opts) {
|
|
auto corr_id = CCApiCorrelationID::from_parts(
|
|
VENUE_NAME, BinanceMessageType::DEPTH, sec_desc);
|
|
return ccapi::Subscription{
|
|
CCAPI_VENUE_NAME, sec_desc, CCAPI_MARKET_DEPTH, opts,
|
|
corr_id.to_string()};
|
|
}
|
|
|
|
static CCApiCorrelationID mk_stats_corr_id(const string &desc) {
|
|
return CCApiCorrelationID::from_parts(
|
|
VENUE_NAME, BinanceMessageType(BinanceMessageType::STATISTICS), desc);
|
|
}
|
|
|
|
static ccapi::Request mk_stats_req(
|
|
const vector<string> &sec_descs, uint32_t batch_id) {
|
|
const auto corr_id = mk_stats_corr_id(FMT("BATCH_{}", batch_id));
|
|
ccapi::Request req{
|
|
ccapi::Request::Operation::GENERIC_PUBLIC_REQUEST, CCAPI_VENUE_NAME};
|
|
req.setCorrelationId(corr_id.to_string());
|
|
auto quoted_sec_descs =
|
|
sec_descs |
|
|
ranges::views::transform([](const auto &s) { return "\"" + s + "\""; });
|
|
const string symbols_query_arg = FMT_JOIN(quoted_sec_descs, ",");
|
|
req.appendParam(
|
|
{{CCAPI_HTTP_PATH, BINANCE_PRICE_STATS_PATH},
|
|
{CCAPI_HTTP_QUERY_STRING, FMT("symbols=[{}]", symbols_query_arg)},
|
|
{CCAPI_HTTP_METHOD, "GET"}});
|
|
return req;
|
|
}
|
|
|
|
static string mk_ticker_opts(const BinanceMarketDataConfig & /*cfg*/) {
|
|
// Placeholder (for consistency's sake)
|
|
return "";
|
|
}
|
|
|
|
static ccapi::Subscription mk_ticker_sub(
|
|
const string &sec_desc, const string &opts) {
|
|
auto ticker_corr_id = CCApiCorrelationID::from_parts(
|
|
VENUE_NAME, BinanceMessageType::TICKER, sec_desc);
|
|
return ccapi::Subscription{
|
|
CCAPI_VENUE_NAME, sec_desc, CCAPI_TRADE, opts,
|
|
ticker_corr_id.to_string()};
|
|
}
|
|
|
|
struct BinanceSubscription::PImpl {
|
|
// General Notes:
|
|
// All securities are listed in the 'symbols' field of the json response
|
|
// to the 'exchangeInfo' request.
|
|
|
|
unordered_map<SecurityID, unique_ptr<Security>> securities;
|
|
IDGenerator *sec_id_store;
|
|
|
|
// Don't actually need this, but keeping it around for diagnostic purposes
|
|
// (i.e., inspection in debugger, temp logs, etc.)
|
|
const BinanceMarketDataConfig cfg;
|
|
const SecurityCriteria criteria;
|
|
|
|
// The session automatically starts/stops in its ctr/dtr
|
|
CCApiSession session;
|
|
std::shared_ptr<IMarketDataListener> listener;
|
|
BinanceSecurityRequestor sec_requestor;
|
|
|
|
// These aliases are useful for quickly switching the handler types (e.g.,
|
|
// a special handler that logs messages for testing/diagnostic purposes)
|
|
using DepthHandler = CCApiMarketDepthAdaptor;
|
|
using TickerHandler = CCApiTradeAdaptor;
|
|
using StatsHandler = BinancePriceStatisticsAdaptor;
|
|
|
|
unordered_map<string, DepthHandler> depth_handlers;
|
|
unordered_map<string, TickerHandler> ticker_handlers;
|
|
// 'sec_stats_handler_map' maps security (description) to the StatsHandler
|
|
// subscribed to that security's data. Since each StatsHandler processes
|
|
// data for multiple securities, the actual instances are contained in a
|
|
// different map, 'stats_handlers' (unique ids are generated w/ a counter,
|
|
// 'stats_handlers_id_counter').
|
|
unordered_map<string, StatsHandler *> sec_stats_handler_map;
|
|
unordered_map<uint32_t, unique_ptr<StatsHandler>> stats_handlers;
|
|
uint32_t stats_handlers_id_counter = 0;
|
|
|
|
explicit PImpl(
|
|
ccapi::EventDispatcher *dispatcher,
|
|
BinanceMarketDataConfig cfg,
|
|
IDGenerator *sec_id_store)
|
|
: sec_id_store(sec_id_store), cfg(std::move(cfg)),
|
|
criteria(SecurityCriteria::from_proto(this->cfg.security_criteria())),
|
|
session(dispatcher, mk_session_cfg(this->cfg), {}),
|
|
sec_requestor(
|
|
&session,
|
|
this->criteria,
|
|
sec_id_store,
|
|
collections::UnorderedSetFactory<string>::create(
|
|
this->cfg.excluded_securities().begin(),
|
|
this->cfg.excluded_securities().end())) {
|
|
LOG(md, Info, "Initializing BinanceSubscription:\n{}",
|
|
ProtoPrinter::to_string(this->cfg));
|
|
sec_requestor.callback().bind<&PImpl::on_securities>(*this);
|
|
}
|
|
|
|
void start(std::shared_ptr<IMarketDataListener> listener) {
|
|
bts_assert(listener != nullptr);
|
|
this->listener = std::move(listener);
|
|
sec_requestor.request();
|
|
}
|
|
|
|
DepthHandler &mk_depth_handler(const string &sec_desc) {
|
|
SecurityID sec_id{sec_id_store->get_or_add(VENUE_NAME, sec_desc)};
|
|
auto [it, is_new] =
|
|
depth_handlers.try_emplace(sec_desc, sec_id, listener.get());
|
|
bts_assert(is_new);
|
|
return it->second;
|
|
}
|
|
|
|
TickerHandler &mk_ticker_handler(const string &sec_desc) {
|
|
SecurityID sec_id{sec_id_store->get_or_add(VENUE_NAME, sec_desc)};
|
|
auto [it, is_new] =
|
|
ticker_handlers.try_emplace(sec_desc, sec_id, listener.get());
|
|
bts_assert_msgf(
|
|
is_new, "Ticker handler already created for '{}'", sec_desc);
|
|
return it->second;
|
|
}
|
|
|
|
StatsHandler &mk_stats_handler(
|
|
const vector<string> &sec_descs, uint32_t hnd_id) {
|
|
vector<pair<string, SecurityID>> sec_desc_id_pairs;
|
|
sec_desc_id_pairs.reserve(sec_descs.size());
|
|
for (const auto &sec_desc : sec_descs) {
|
|
sec_desc_id_pairs.emplace_back(
|
|
sec_desc,
|
|
SecurityID{sec_id_store->get_or_add(VENUE_NAME, sec_desc)});
|
|
}
|
|
auto [it, is_new] = stats_handlers.try_emplace(
|
|
hnd_id,
|
|
std::make_unique<StatsHandler>(sec_desc_id_pairs, listener.get()));
|
|
bts_assert_msgf(
|
|
is_new, "StatsHandler already created with id '{}'", hnd_id);
|
|
return *it->second;
|
|
}
|
|
|
|
void add_stats_handler(
|
|
const vector<string> &sec_descs, CCApiSession::BatchRequest &reqs) {
|
|
if (sec_descs.empty()) {
|
|
return;
|
|
}
|
|
const auto hnd_id = stats_handlers_id_counter++;
|
|
reqs.push_back(
|
|
{mk_stats_req(sec_descs, hnd_id),
|
|
&mk_stats_handler(sec_descs, hnd_id)});
|
|
}
|
|
|
|
/**
|
|
* @brief Subscribes to depth, ticker, and price data for a list of
|
|
* securities
|
|
*/
|
|
template <class SecDescs>
|
|
void subscribe_market_data(SecDescs securities) {
|
|
const auto depth_opts = mk_depth_opts(cfg);
|
|
const auto ticker_opts = mk_ticker_opts(cfg);
|
|
|
|
// Batch subscribe/request ticker, depth, and stats
|
|
CCApiSession::BatchSubscription depth_subs;
|
|
CCApiSession::BatchSubscription ticker_subs;
|
|
CCApiSession::BatchRequest stats_reqs;
|
|
|
|
const int N_SECS_PER_STATS_REQ = 16;
|
|
vector<string> curr_stats_sec_descs;
|
|
for (const auto &sec_desc : securities) {
|
|
depth_subs.push_back(
|
|
{mk_depth_sub(sec_desc, depth_opts), &mk_depth_handler(sec_desc)});
|
|
ticker_subs.push_back(
|
|
{mk_ticker_sub(sec_desc, ticker_opts),
|
|
&mk_ticker_handler(sec_desc)});
|
|
curr_stats_sec_descs.push_back(sec_desc);
|
|
if (curr_stats_sec_descs.size() == N_SECS_PER_STATS_REQ) {
|
|
add_stats_handler(curr_stats_sec_descs, stats_reqs);
|
|
curr_stats_sec_descs.clear();
|
|
}
|
|
}
|
|
if (!curr_stats_sec_descs.empty()) {
|
|
add_stats_handler(curr_stats_sec_descs, stats_reqs);
|
|
}
|
|
|
|
session.subscribe(
|
|
depth_subs, {ccapi::Message::Type::MARKET_DATA_EVENTS_MARKET_DEPTH});
|
|
session.subscribe(
|
|
ticker_subs, {ccapi::Message::Type::MARKET_DATA_EVENTS_TRADE});
|
|
session.request(
|
|
stats_reqs, {ccapi::Message::Type::GENERIC_PUBLIC_REQUEST});
|
|
}
|
|
|
|
/**
|
|
* Updates the securities map and subscribes market data for newly arrived
|
|
* securities
|
|
*/
|
|
void on_securities(vector<Security> secs) {
|
|
// 'sec_descs' is used to make depth subscriptions
|
|
vector<string> sec_descs;
|
|
sec_descs.reserve(secs.size());
|
|
// 'sec_ptrs' is passed to IMarketDataListeners
|
|
vector<const Security *> sec_ptrs;
|
|
sec_ptrs.reserve(secs.size());
|
|
|
|
for (auto &s : secs) {
|
|
auto id = s.id();
|
|
auto desc = s.desc();
|
|
auto [it, is_new] = securities.try_emplace(
|
|
id, std::make_unique<Security>(std::move(s)));
|
|
bts_assert_msgf(
|
|
is_new, "Received duplicate Binance security '{}'", desc);
|
|
sec_descs.push_back(std::move(desc));
|
|
// add ptr to entry in 'securities' (not the loop var, we moved that)
|
|
sec_ptrs.push_back(it->second.get());
|
|
}
|
|
|
|
listener->on_securities(sec_ptrs);
|
|
subscribe_market_data(sec_descs);
|
|
}
|
|
};
|
|
|
|
BinanceSubscription::BinanceSubscription(
|
|
ccapi::EventDispatcher *dispatcher,
|
|
BinanceMarketDataConfig cfg,
|
|
IDGenerator *sec_id_store)
|
|
: me_(std::make_unique<PImpl>(dispatcher, std::move(cfg), sec_id_store)) {}
|
|
|
|
BinanceSubscription::BinanceSubscription(BinanceSubscription &&) noexcept =
|
|
default;
|
|
BinanceSubscription &BinanceSubscription::operator=(
|
|
BinanceSubscription &&) noexcept = default;
|
|
|
|
BinanceSubscription::~BinanceSubscription() = default;
|
|
|
|
void BinanceSubscription::start(std::shared_ptr<IMarketDataListener> listener) {
|
|
me_->start(listener);
|
|
}
|
|
|
|
} // namespace bts::md::binance
|