Files
spark/src/app_sparkd/sparkd/md/okx/OkxSecuritySubscription.cpp
T

342 lines
12 KiB
C++

#include "OkxSecuritySubscription.h"
#include "spark/domain/SecurityCriteria.h"
#include "spark/io/JsonReader.h"
#include "sparkd/md/ccapi/CCApiCorrelationID.h"
#include "sparkd/md/ccapi/CCApiHttpParams.h"
#include "sparkd/md/ccapi/CCApiMessageHandler.h"
#include "sparkd/md/ccapi/CCApiReader.h"
#include "sparkd/md/ccapi/CCApiSession.h"
#include "sparkd/md/ccapi/ICCApiMessageHandler.h"
#include "sparkd/md/okx/OkxChannel.h"
#include "sparkd/md/okx/OkxChannelDemuxer.h"
#include "sparkd/md/okx/OkxInstrument.h"
#include "sparkd/md/okx/OkxInstrumentStore.h"
#include "sparkd/md/okx/OkxInstrumentType.h"
#include "sparkd/md/okx/OkxMessageType.h"
#include "sparkd/md/okx/OkxRequest.h"
#include "system/collections/contains.h"
#include "system/collections/remove.h"
#include <ccapi_cpp/ccapi_macro.h>
#include <ccapi_cpp/ccapi_request.h>
#include <ccapi_cpp/ccapi_subscription.h>
#include <nlohmann/json.hpp>
#include <optional>
#include <range/v3/view.hpp>
#include <unordered_set>
#include <vector>
namespace bts::md::okx {
#define VENUE bts::api::domain::VENUE_OKX
#define VENUE_NAME \
std::string { bts::api::domain::Venue_Name(bts::api::domain::VENUE_OKX) }
#define CCAPI_VENUE_NAME CCAPI_EXCHANGE_NAME_OKX
#define OKX_MSG_TYPE(x) \
OkxMessageType { OkxMessageType::x }
#define _LOG(level, ...) LOG(md, level, "OkxSecuritySubscription::" __VA_ARGS__)
#define _DIAG(...) _LOG(Diag, __VA_ARGS__)
#define _INFO(...) _LOG(Info, __VA_ARGS__)
#define _WARN(...) _LOG(Warn, __VA_ARGS__)
#define _ERROR(...) _LOG(Error, __VA_ARGS__)
using bts::domain::Security;
using bts::domain::SecurityCriteria;
using bts::io::JsonReader;
using nlohmann::json;
using std::optional;
using std::string;
using std::unique_ptr;
using std::unordered_set;
using std::vector;
static std::optional<OkxInstrumentType> to_okx_type(
api::domain::SecurityType type) {
switch (type) {
case api::domain::SecurityType::SECURITY_TYPE_FUTURE:
return OkxInstrumentType::FUTURES;
case api::domain::SECURITY_TYPE_SPOT:
return OkxInstrumentType::SPOT;
case api::domain::SECURITY_TYPE_OPTION:
return OkxInstrumentType::OPTION;
default:
return {};
}
}
static unordered_set<OkxInstrumentType> get_subscription_types(
const SecurityCriteria &criteria) {
// Determine the set of types to subscribe
unordered_set<OkxInstrumentType> types;
for (const auto &clause : criteria.clauses) {
if (clause.types.empty()) {
types.insert(OkxInstrumentType::SPOT);
types.insert(OkxInstrumentType::FUTURES);
types.insert(OkxInstrumentType::OPTION);
break;
}
for (const auto type : clause.types) {
const auto okx_type = to_okx_type(type);
if (okx_type) {
types.insert(*okx_type);
}
}
}
return types;
}
static ccapi::Subscription instruments_sub(const vector<OkxChannel> &channels) {
const OkxRequest req{"subscribe", channels};
const auto corr_id = CCApiCorrelationID::from_parts(
VENUE_NAME, OKX_MSG_TYPE(INSTRUMENTS_SUB));
ccapi::Subscription sub{
CCAPI_VENUE_NAME, "", CCAPI_GENERIC_PUBLIC_SUBSCRIPTION, req.to_string(),
corr_id.to_string()};
return sub;
}
static ccapi::Request underlying_req(OkxInstrumentType type) {
const auto corr_id = CCApiCorrelationID::from_parts(
VENUE_NAME, OKX_MSG_TYPE(UNDERLYING), type);
ccapi::Request req{
ccapi::Request::Operation::GENERIC_PUBLIC_REQUEST, CCAPI_VENUE_NAME, "",
corr_id.to_string()};
CCApiHttpParams http_params;
http_params.query_params["instType"] = type.name();
http_params.path = OKX_UNDERLYING_PATH;
req.appendParam(http_params.to_map());
return req;
}
/**
* @brief Get http rest instruments request
* @a https://www.okx.com/docs-v5/en/#rest-api-public-data-get-instruments
*/
static ccapi::Request instruments_req(
OkxInstrumentType type, const string &uly = {}) {
const auto corr_id =
CCApiCorrelationID::from_parts(OKX_MSG_TYPE(INSTRUMENTS_REQ), type);
ccapi::Request req{
ccapi::Request::Operation::GENERIC_PUBLIC_REQUEST, CCAPI_VENUE_NAME, "",
corr_id.to_string()};
CCApiHttpParams http_params;
http_params.query_params["instType"] = type.name();
if (!uly.empty()) {
http_params.query_params["uly"] = uly;
}
http_params.path = OKX_INSTRUMENTS_PATH;
req.appendParam(http_params.to_map());
return req;
}
struct OkxSecuritySubscription::PImpl {
CCApiSession *session;
SecurityCriteria criteria;
OkxInstrumentStore *instrument_store;
unordered_set<OkxInstrumentType> instrument_types;
unordered_set<string> option_ulys;
unordered_set<OkxChannel> subscribed_channels;
OkxChannelDemuxer demuxer;
vector<unique_ptr<CCApiMessageHandler>> handlers;
std::function<void(vector<const Security *>)> callback;
void start() {
if (CONTAINS(instrument_types, OkxInstrumentType::OPTION)) {
// Instrument reqs for options require a 'uly' (underlying) param. We
// must obtain the list of 'uly' syms before we can request the insts.
request_underlying_(OkxInstrumentType::OPTION);
return;
}
request_snapshot_();
subscribe_incremental_();
}
private:
void request_underlying_(OkxInstrumentType type) {
auto &hnd =
*handlers.emplace_back(std::make_unique<CCApiMessageHandler>());
hnd.callback().bind<&PImpl::on_underlying_>(*this);
const auto req = underlying_req(type);
session->request(req, &hnd);
}
void request_snapshot_() {
auto &hnd =
*handlers.emplace_back(std::make_unique<CCApiMessageHandler>());
hnd.callback().bind<&PImpl::on_instruments_snapshot_>(*this);
CCApiSession::BatchRequest reqs;
for (const auto type : instrument_types) {
switch (type.value) {
case OkxInstrumentType::OPTION:
for (const auto &uly : option_ulys) {
reqs.push_back({instruments_req(type, uly), &hnd});
}
break;
case OkxInstrumentType::FUTURES:
case OkxInstrumentType::SPOT:
reqs.push_back({instruments_req(type), &hnd});
break;
default:
_WARN("subscribe_incremental_. Ignoring type '{}'", type);
break;
}
session->request(reqs);
}
}
void subscribe_incremental_() {
vector<OkxChannel> new_channels;
auto add_channel = [&](const OkxChannel &ch) {
if (subscribed_channels.insert(ch).second) {
new_channels.emplace_back(ch);
}
};
for (const auto type : instrument_types) {
switch (type.value) {
case OkxInstrumentType::OPTION:
for (const auto &uly : option_ulys) {
add_channel({"instruments", type, uly});
}
break;
case OkxInstrumentType::FUTURES:
case OkxInstrumentType::SPOT:
add_channel({"instruments", type});
break;
default:
_WARN("subscribe_incremental_. Ignoring type '{}'", type);
break;
}
}
if (new_channels.empty()) {
return;
}
for (const auto &ch : new_channels) {
if (ch.instType == OkxInstrumentType::OPTION && ch.uly) {
// For some reason, 'uly' is removed from the channel params in
// OPTION responses, so add a handler for the channel without it.
auto res_ch = ch;
res_ch.uly = std::nullopt;
demuxer[res_ch].bind<&PImpl::on_instruments_update_>(*this);
}
demuxer[ch].bind<&PImpl::on_instruments_update_>(*this);
}
const auto sub = instruments_sub(new_channels);
session->subscribe(instruments_sub(new_channels), &demuxer);
}
void on_instruments_(
const CCApiCorrelationID &corr_id, const json &payload) {
_INFO("on_instruments_. corr_id: {}", corr_id);
vector<OkxInstrument> instruments;
try {
payload.get_to(instruments);
} catch (const std::exception &ex) {
_ERROR(
"on_instruments. Parse error: {}\n***payload***:\n{}",
string{ex.what()}, payload.dump(2));
return;
}
_INFO("on_instruments. Received {} OkxInstruments", instruments.size());
if (instruments.empty()) {
return;
}
vector<const Security *> new_secs;
instrument_store->on_instruments(instruments, new_secs);
if (criteria.is_defined()) {
REMOVE_IF(
new_secs, [&](const auto *s) { return !criteria.includes(*s); });
}
_INFO("on_instruments. Generated {} Securities.", new_secs.size());
// Fire callback with new_secs
if (!new_secs.empty()) {
callback(new_secs);
}
}
/**
* @brief Handles an instruments rest request update (snapshot)
*/
bool on_underlying_(
const ccapi::Event &event,
ccapi::Session * /*session*/,
const std::vector<const ccapi::Message *> &msgs,
const CCApiCorrelationID &corr_id) {
_INFO("on_underlying_. corr_id: {}", corr_id);
auto type_name = corr_id.peek_string_at(corr_id.size() - 1);
OkxInstrumentType type;
if (!OkxInstrumentType::parse(type_name, &type)) {
_ERROR("on_underlying_. Failed to extract inst type from corr_id");
return true;
}
vector<string> unds;
try {
const auto payload = CCApiReader::read_http_json(msgs);
JsonReader jr{&payload};
payload.at("data").at(0).get_to(unds);
} catch (const std::exception &ex) {
_ERROR(
"on_underlying_. Parse error: {}\n***Event***:\n{}",
string{ex.what()}, event.toStringPretty());
return true;
}
option_ulys = {unds.begin(), unds.end()};
request_snapshot_();
subscribe_incremental_();
return true;
}
/**
* @brief Handles an instruments rest request update (snapshot)
*/
bool on_instruments_snapshot_(
const ccapi::Event &event,
ccapi::Session * /*session*/,
const std::vector<const ccapi::Message *> &msgs,
const CCApiCorrelationID &corr_id) {
_INFO("on_instruments_snapshot_. corr_id: {}", corr_id);
try {
const auto payload = CCApiReader::read_http_json(msgs);
on_instruments_(corr_id, payload.at("data"));
} catch (const std::exception &ex) {
_ERROR(
"on_instruments_snapshot_. Failed to read instruments. Exception: "
"{}.\n***Event***\n{}",
string{ex.what()}, event.toStringPretty());
}
return true;
}
/**
* @brief Handles an instruments subscription update (incremental)
*
* @param instrument_arrays An vector of instrument arrays
*/
void on_instruments_update_(
const vector<json> &instrument_arrays,
const CCApiCorrelationID &corr_id) {
bts_assert(instrument_arrays.size() == 1);
on_instruments_(corr_id, instrument_arrays.at(0));
}
};
OkxSecuritySubscription::OkxSecuritySubscription(
CCApiSession *const session,
OkxInstrumentStore *const instrument_store,
SecurityCriteria criteria,
std::function<void(std::vector<const Security *>)> callback)
: me_(std::make_unique<PImpl>()) {
_INFO("Initializing OkxSecuritySubscription:\n{}", criteria);
me_->session = session;
me_->instrument_store = instrument_store;
me_->criteria = std::move(criteria);
me_->callback = callback;
me_->instrument_types = get_subscription_types(me_->criteria);
me_->start();
}
OkxSecuritySubscription::~OkxSecuritySubscription() = default;
} // namespace bts::md::okx