Files
spark/src/app_sparkd/sparkd/md/ftx/FTXSubscription.cpp
T
John Colvin 0aaa6e18fd refactor: Split MarketDataGateway up into providers/sources
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).
2024-11-25 19:21:48 +00:00

250 lines
9.2 KiB
C++

#include "FTXSubscription.h"
#include "spark/api/domain/Venue.pb.h"
#include "spark/api/md/FTXMarketDataConfig.pb.h"
#include "spark/domain/Security.h"
#include "spark/domain/SecurityCriteria.h"
#include "spark/io/ProtoPrinter.h"
#include "spark/logging/logging.h"
#include "spark/md/IMarketDataListener.h"
#include "spark/persistence/IDGenerator.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 "sparkd/md/ftx/FTXHistoricalPriceAdaptor.h"
#include "sparkd/md/ftx/FTXHistoricalPriceRecord.h"
#include "sparkd/md/ftx/FTXMessageType.h"
#include "sparkd/md/ftx/FTXSecurityRequestor.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_subscription.h>
#include <string>
#include <unordered_map>
namespace bts::md::ftx {
using bts::api::domain::SecurityType;
using bts::api::domain::Venue;
using bts::api::domain::Venue_Name;
using bts::api::md::FTXMarketDataConfig;
using bts::domain::Security;
using bts::domain::SecurityCriteria;
using bts::domain::SecurityID;
using bts::io::ProtoPrinter;
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;
#define VENUE Venue::VENUE_FTX
#define VENUE_NAME (Venue_Name(Venue::VENUE_FTX))
#define FTX_MSG_TYPE_NAME(x) \
std::string { FTXMessageType{x}.name() }
static ccapi::Request create_stats_req(const string &sec_desc) {
const int SECS_PER_DAY = 86400;
auto corr_id = CCApiCorrelationID::from_parts(
VENUE_NAME, FTX_MSG_TYPE_NAME(FTXMessageType::STATISTICS), sec_desc);
ccapi::Request req{
ccapi::Request::Operation::GENERIC_PUBLIC_REQUEST,
CCAPI_EXCHANGE_NAME_FTX};
req.appendParam(
{{CCAPI_HTTP_PROTOCOL, "https"},
{CCAPI_HTTP_HOST, "ftx.com"},
{CCAPI_HTTP_PORT, "443"},
{CCAPI_HTTP_PATH, fmt::format("/api/markets/{}/candles", sec_desc)},
{CCAPI_HTTP_QUERY_STRING, fmt::format("resolution={}", SECS_PER_DAY)},
{CCAPI_HTTP_METHOD, "GET"}});
req.setCorrelationId(corr_id.to_string());
return req;
}
static ccapi::Subscription create_depth_sub(
const string &sec_desc, const string &opts) {
auto corr_id = CCApiCorrelationID::from_parts(
VENUE_NAME, FTXMessageType::DEPTH, sec_desc);
return ccapi::Subscription{
CCAPI_EXCHANGE_NAME_FTX, sec_desc, CCAPI_MARKET_DEPTH, opts,
corr_id.to_string()};
}
static ccapi::Subscription create_ticker_sub(
const string &sec_desc, const string &opts) {
const auto corr_id = CCApiCorrelationID::from_parts(
VENUE_NAME, FTXMessageType::TICKER, sec_desc);
return ccapi::Subscription{
CCAPI_EXCHANGE_NAME_FTX, sec_desc, CCAPI_TRADE, opts,
corr_id.to_string()};
}
struct FTXSubscription::PImpl {
// General Notes:
// FTX securities must be obtained from 2 different urls:
// 1) https://ftx.com/api/markets
// 2) https://ftx.com/api/futures
// Although all securities are listed in the response from (1), not all
// information is provided for futures (notably, the expiration date is
// missing!). This missing information is obtained from (2). Therefore,
// futures are published when (2) is received, while all other securities
// are published when (1) is received.
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.)
FTXMarketDataConfig cfg;
SecurityCriteria criteria;
FTXSecurityRequestor sec_requestor;
// The session automatically starts/stops in its ctr/dtr
CCApiSession session;
shared_ptr<IMarketDataListener> listener;
// 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 = FTXHistoricalPriceAdaptor;
unordered_map<string, DepthHandler> depth_handlers;
unordered_map<string, TickerHandler> ticker_handlers;
unordered_map<string, StatsHandler> stats_handlers;
explicit PImpl(
ccapi::EventDispatcher *dispatcher,
FTXMarketDataConfig cfg,
persistence::IDGenerator *sec_id_store)
: sec_id_store(sec_id_store), cfg(std::move(cfg)),
criteria(domain::SecurityCriteria::from_proto(
this->cfg.security_criteria())),
sec_requestor(dispatcher, criteria, sec_id_store),
session(dispatcher) {
LOG(md, Info, "Initializing FTXSubscription:\n{}",
ProtoPrinter::to_string(this->cfg));
sec_requestor.callback().bind<&PImpl::on_securities>(*this);
}
void start(shared_ptr<IMarketDataListener> listener) {
bts_assert(listener != nullptr);
this->listener = std::move(listener);
sec_requestor.request();
}
DepthHandler &create_depth_handler(
SecurityType sec_type, const string &sec_desc) {
SecurityID sec_id{sec_id_store->get_or_add(VENUE, sec_type, sec_desc)};
auto [it, is_new] =
depth_handlers.try_emplace(sec_desc, sec_id, listener.get());
bts_assert(is_new);
return it->second;
}
TickerHandler &create_ticker_handler(
SecurityType sec_type, const string &sec_desc) {
SecurityID sec_id{sec_id_store->get_or_add(VENUE, sec_type, 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 &create_stats_handler(
SecurityType sec_type, const string &sec_desc) {
SecurityID sec_id{sec_id_store->get_or_add(VENUE, sec_type, sec_desc)};
auto [it, is_new] =
stats_handlers.try_emplace(sec_desc, sec_id, listener.get());
bts_assert_msgf(
is_new, "Statistics handler already created for '{}'", sec_desc);
return it->second;
}
/**
* @brief Subscribes to depth, ticker, and price data for a list of
* securities
*/
template <class SecDescs>
void subscribe_market_data(SecDescs securities) {
const string depth_opts =
fmt::format(CCAPI_MARKET_DEPTH_MAX "={}", cfg.num_depth_levels());
const string ticker_opts = "";
// Batch subscribe/request ticker, depth, and stats
CCApiSession::BatchSubscription depth_subs;
CCApiSession::BatchSubscription ticker_subs;
CCApiSession::BatchRequest stats_reqs;
for (const auto &[sec_type, sec_desc] : securities) {
depth_subs.push_back(
{create_depth_sub(sec_desc, depth_opts),
&create_depth_handler(sec_type, sec_desc)});
ticker_subs.push_back(
{create_ticker_sub(sec_desc, ticker_opts),
&create_ticker_handler(sec_type, sec_desc)});
stats_reqs.push_back(
{create_stats_req(sec_desc),
&create_stats_handler(sec_type, sec_desc)});
}
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<domain::Security> secs) {
// 'sec_descs' is used to make depth subscriptions
vector<pair<SecurityType, string>> sec_descs;
sec_descs.reserve(secs.size());
// 'sec_ptrs' is passed to IMarketDataListeners
vector<const domain::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<domain::Security>(std::move(s)));
bts_assert_msgf(is_new, "Received duplicate FTX security '{}'", desc);
sec_descs.push_back(std::make_pair(s.type(), 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);
}
};
FTXSubscription::FTXSubscription(
ccapi::EventDispatcher *dispatcher,
FTXMarketDataConfig cfg,
IDGenerator *sec_id_store)
: me_(std::make_unique<PImpl>(dispatcher, std::move(cfg), sec_id_store)) {}
FTXSubscription::FTXSubscription(FTXSubscription &&) noexcept = default;
FTXSubscription &FTXSubscription::operator=(FTXSubscription &&) noexcept =
default;
FTXSubscription::~FTXSubscription() = default;
void FTXSubscription::start(shared_ptr<IMarketDataListener> listener) {
me_->start(std::move(listener));
}
} // namespace bts::md::ftx