Files
spark/src/lib/md/binance_us/BinanceUsSecurityRequestor.cpp
T

248 lines
8.8 KiB
C++

#if defined(SPARK_ENABLE_CCAPI) && defined(CCAPI_ENABLE_EXCHANGE_BINANCE_US)
#include "BinanceUsSecurityRequestor.h"
#include "lib/api/domain/SecurityType.pb.h"
#include "lib/api/domain/Venue.pb.h"
#include "lib/api/md/BinanceMarketDataConfig.pb.h"
#include "lib/collections/Contains.h"
#include "lib/collections/Remove.h"
#include "lib/domain/Security.h"
#include "lib/domain/SecurityCriteria.h"
#include "lib/logging/logging.h"
#include "lib/md/CCApiCorrelationID.h"
#include "lib/md/CCApiMessageHandler.h"
#include "lib/md/CCApiSession.h"
#include "lib/md/ICCApiMessageHandler.h"
#include "lib/md/binance/BinanceMessageType.h"
#include "lib/md/binance/BinanceSpot.h"
#include "lib/persistence/IDGenerator.h"
#include <algorithm>
#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 <string>
#include <unordered_set>
namespace bts::md::binance_us {
using api::domain::SecurityType;
using api::domain::Venue;
using api::domain::Venue_Name;
using bts::domain::Security;
using bts::domain::SecurityCriteria;
using bts::domain::SecurityID;
using bts::md::binance::BinanceMessageType;
using bts::md::binance::BinanceSpot;
using bts::persistence::IDGenerator;
using std::string;
using std::unordered_set;
using std::vector;
#define VENUE Venue::BINANCE_US
#define VENUE_NAME (Venue_Name(Venue::BINANCE_US))
#define CCAPI_VENUE_NAME CCAPI_EXCHANGE_NAME_BINANCE_US
#define BINANCE_MSG_TYPE_NAME(x) \
std::string { BinanceMessageType{x}.name() }
static ccapi::Request exchange_info_req() {
auto corr_id = CCApiCorrelationID::from_parts(
VENUE_NAME, BINANCE_MSG_TYPE_NAME(BinanceMessageType::EXCHANGE_INFO));
ccapi::Request req{
ccapi::Request::Operation::GENERIC_PUBLIC_REQUEST, CCAPI_VENUE_NAME};
req.appendParam(
{{CCAPI_HTTP_PROTOCOL, "https"},
{CCAPI_HTTP_PORT, "443"},
{CCAPI_HTTP_PATH, BINANCE_US_INSTRUMENTS_PATH},
{CCAPI_HTTP_METHOD, "GET"}});
req.setCorrelationId(corr_id.to_string());
return req;
}
static vector<string> get_sec_descs(const vector<Security> &sec_ptrs) {
vector<string> descs{sec_ptrs.size()};
std::transform(
sec_ptrs.begin(), sec_ptrs.end(), descs.begin(),
[](const auto &s) { return s.desc(); });
return descs;
}
static void validate_response(const ccapi::Event &event, bool is_expected) {
bts_assert_msgf(
event.getType() == ccapi::Event::Type::RESPONSE,
"Received event w/ unexpected type:\n{}", event.toStringPretty());
bts_assert_msgf(
is_expected, "Received unexpected response from Binance US:\n{}",
event.toStringPretty());
}
struct BinanceUsSecurityRequestor::PImpl {
// General Notes:
// Securities are in the 'symbols' field (list) in the json from
// https://api.binance.us/api/v3/exchangeInfo
// The session automatically starts/stops in its ctr/dtr
CCApiSession *const session;
// Maps security descs (strings) to numeric ids
IDGenerator *const sec_id_store;
// 'criteria' and 'excluded_securities' collectively form the security
// filter predicate. Securities are forwarded to the callback if and only if
// they are included by 'criteria', and absent from 'excluded_securities'.
const SecurityCriteria criteria;
const unordered_set<string> excluded_securities;
const unordered_set<string> securities;
// These proxy handlers don't actually process new securities. Member methods
// in this class process the securities; we simply bind them to proxy handler
// callbacks. This is easier than defining separate ICCApiMessageHandler
// impls for 'markets', and 'futures', esp since they would need access to
// state contained in this class.
CCApiMessageHandler exch_info_handler_proxy;
Callback callback;
// True when an 'exchange info' request is in flight
bool exch_info_req_pending = false;
// True when someone called BinanceUsSecurityRequestor::request while another
// request was already in flight. In this case, the 'external_req_pending'
// flag is used to signal that another request should be sent immediately
// after the current one finishes.
bool external_req_pending = false;
explicit PImpl(
CCApiSession *const session,
domain::SecurityCriteria criteria,
persistence::IDGenerator *const sec_id_store,
unordered_set<string> excluded_securities)
: session(session), sec_id_store(sec_id_store),
criteria(std::move(criteria)),
excluded_securities(std::move(excluded_securities)) {
exch_info_handler_proxy.callback().bind<&PImpl::on_exchange_info>(*this);
}
bool is_busy() const { return exch_info_req_pending; }
void request() {
external_req_pending = false;
exch_info_req_pending = true;
session->request(exchange_info_req(), &exch_info_handler_proxy);
}
/**
* @brief Parses securities from a ccapi::Message
*
* @details Only new securities that satisfy the 'SecurityCriteria' are added
* to \p added_secs
*
* @tparam TParser
* @param msg The ccapi::Message to parse
* @param types The types of securities to parse
* @param parser Ther parser to use
*/
template <class T>
void parse_secs(
const ccapi::Message &msg,
const vector<SecurityType> &types,
vector<Security> &added_secs) {
T::parse(msg, added_secs, VENUE);
// Exclude secs that don't satisfy the SecurityCriteria
auto is_sec_excluded = [this, &types](const Security &sec) {
return !CONTAINS(types, sec.type()) ||
!criteria.includes_or_undefined(sec) ||
CONTAINS(excluded_securities, sec.desc()) ||
securities.count(sec.desc()) > 0;
};
REMOVE_IF(added_secs, is_sec_excluded);
// Assign security ids
for (auto &sec : added_secs) {
// Since the ids haven't been set yet, we should expect them to be 0
bts_assert(sec.id().value() == 0);
SecurityID sec_id{sec_id_store->get_or_add(VENUE_NAME, sec.desc())};
sec.security_common.id = sec_id;
}
}
/**
* @brief Parses securities from ccapi messages, updates the securities set,
* and subscribes depth for newly arrived securities
*
* @details Securities that don't pass the criteria filter are ignored
*
* @tparam TParser The type of parser
* @param msgs Vector of ccapi messages containing securities
* @param types The types of securities to update (types must be explicitly
* specified to be included)
*/
template <class T>
void update_securities(
const vector<const ccapi::Message *> &msgs,
const vector<SecurityType> &types) {
vector<Security> added_secs;
for (const auto *msg : msgs) {
bts_assert_msgf(
msg->getType() == ccapi::Message::Type::GENERIC_PUBLIC_REQUEST,
"Received msg w/ unexpected type:\n{}", msg->toStringPretty());
parse_secs<T>(*msg, types, added_secs);
}
auto added_sec_descs = get_sec_descs(added_secs);
LOG(md, Info, "Received Binance US securities:\n{}",
fmt::to_string(fmt::join(added_sec_descs, "\n")));
callback(added_secs);
}
/**
* Handles 'exchange info' update from CCApiSession
*/
bool on_exchange_info(
const ccapi::Event &event,
ccapi::Session * /*session*/,
const vector<const ccapi::Message *> &msgs,
const CCApiCorrelationID & /*corr_id*/) {
validate_response(event, exch_info_req_pending);
LOG(md, Diag, "Received Binance US exchange info (spots):\n{}",
event.toStringPretty());
update_securities<BinanceSpot>(msgs, {SecurityType::SPOT});
exch_info_req_pending = false;
if (external_req_pending && !is_busy()) {
request();
}
return true;
}
};
BinanceUsSecurityRequestor::BinanceUsSecurityRequestor(
CCApiSession *const session,
SecurityCriteria criteria,
IDGenerator *const sec_id_store,
unordered_set<string> excluded_securities)
: me_(std::make_unique<PImpl>(
session,
std::move(criteria),
sec_id_store,
std::move(excluded_securities))) {}
BinanceUsSecurityRequestor::BinanceUsSecurityRequestor(
BinanceUsSecurityRequestor &&) noexcept = default;
BinanceUsSecurityRequestor &BinanceUsSecurityRequestor::operator=(
BinanceUsSecurityRequestor &&) noexcept = default;
BinanceUsSecurityRequestor::~BinanceUsSecurityRequestor() = default;
void BinanceUsSecurityRequestor::request() {
if (is_busy()) {
me_->external_req_pending = true;
return;
}
me_->request();
}
bool BinanceUsSecurityRequestor::is_busy() const { return me_->is_busy(); }
BinanceUsSecurityRequestor::Callback &BinanceUsSecurityRequestor::callback() {
return me_->callback;
}
} // namespace bts::md::binance_us
#endif