185 lines
5.6 KiB
C++
185 lines
5.6 KiB
C++
#include "OkxInstrumentStore.h"
|
|
#include "lib/core/TimeStamp.h"
|
|
#include "lib/domain/Security.h"
|
|
#include "lib/api/domain/SecurityType.h"
|
|
#include "lib/domain_model/Security.pb.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/persistence/IDGenerator.h"
|
|
#include "lib/utils/bts_fatal.h"
|
|
#include "server/sparkd/md/okx/OkxInstrument.h"
|
|
#include "server/sparkd/md/okx/OkxInstrumentType.h"
|
|
|
|
#include <ccapi_cpp/ccapi_message.h>
|
|
#include <nlohmann/json.hpp>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
namespace bts::md::okx {
|
|
|
|
using bts::api::domain::CallPut;
|
|
using bts::api::domain::SecurityType;
|
|
using bts::domain::FutureSecurity;
|
|
using bts::domain::OptionSecurity;
|
|
using bts::domain::Price;
|
|
using bts::domain::Quantity;
|
|
using bts::domain::Security;
|
|
using bts::domain::SecurityID;
|
|
using bts::domain::SpotSecurity;
|
|
using bts::persistence::IDGenerator;
|
|
using nlohmann::json;
|
|
using std::function;
|
|
using std::string;
|
|
using std::unordered_map;
|
|
using std::vector;
|
|
|
|
#define SEC_TYPE(x) bts::api::domain::SECURITY_TYPE_##x
|
|
#define VENUE bts::api::domain::VENUE_OKX
|
|
|
|
static void instrument_to_sec(const OkxInstrument &oi, Security &sec) {
|
|
auto &scomm = sec.security_common;
|
|
scomm.venue = VENUE;
|
|
scomm.exchange_sec_id = oi.instId;
|
|
scomm.desc = oi.instId;
|
|
scomm.min_price_increment = oi.tickSz;
|
|
scomm.min_quantity = oi.minSz;
|
|
scomm.min_quantity_increment = oi.lotSz;
|
|
scomm.currency = oi.quoteCcy;
|
|
|
|
switch (oi.instType.value) {
|
|
case OkxInstrumentType::SPOT:
|
|
sec.security_common.symbol = oi.baseCcy;
|
|
sec.security.emplace<SpotSecurity>();
|
|
break;
|
|
case bts::md::okx::OkxInstrumentType::SWAP:{
|
|
auto &fut = sec.security.emplace<FutureSecurity>();
|
|
core::TimeStamp::from_ymd(3000, 1, 1,&fut.expiration);
|
|
if(oi.instId.find("USDT")!=std::string::npos || oi.instId.find("USDC")!=std::string::npos){
|
|
sec.security_common.symbol = oi.instId.substr(0,oi.instId.find_first_of("-"));
|
|
//tickSz 0.1, lotSz 0.01, minSz = 0.01
|
|
if(oi.instId.find("BTC")!=std::string::npos){// so that config overrides them
|
|
scomm.min_quantity = 0;
|
|
scomm.min_quantity_increment = 0;
|
|
}
|
|
break;
|
|
}
|
|
sec.security_common.symbol = oi.settleCcy;
|
|
break;
|
|
}
|
|
case OkxInstrumentType::FUTURES: {
|
|
sec.security_common.symbol = oi.settleCcy;
|
|
auto &fut = sec.security.emplace<FutureSecurity>();
|
|
fut.expiration = *oi.expTime;
|
|
break;
|
|
}
|
|
case OkxInstrumentType::OPTION: {
|
|
sec.security_common.symbol = oi.settleCcy;
|
|
auto &opt = sec.security.emplace<OptionSecurity>();
|
|
if (!oi.expTime || !oi.stk || !oi.optType) {
|
|
LOG(md, Error,
|
|
"Invalid OkxInstrument. Missing key Option field(s):\n{}",
|
|
json{oi}.dump(2));
|
|
return;
|
|
}
|
|
opt.expiration = *oi.expTime;
|
|
opt.strike = Price::from_double(*oi.stk);
|
|
opt.call_put = *oi.optType == "P" ? CallPut::PUT : CallPut::CALL;
|
|
break;
|
|
}
|
|
default:
|
|
BTS_FATAL_MSGF("Unexpected instrument kind at Okx '{}'", oi.instType);
|
|
break;
|
|
}
|
|
scomm.asset = scomm.symbol;
|
|
scomm.group = scomm.symbol;
|
|
}
|
|
|
|
struct OkxInstrumentStore::PImpl {
|
|
PImpl(IDGenerator *sec_id_store, function<bool(const Security &)> filter)
|
|
: sec_id_store_(sec_id_store), filter_(filter) {}
|
|
|
|
Security *find(SecurityID id) {
|
|
auto it = securities_.find(id);
|
|
if (it == securities_.end()) {
|
|
return nullptr;
|
|
}
|
|
return &it->second;
|
|
}
|
|
|
|
const Security *find(SecurityID id) const {
|
|
auto it = securities_.find(id);
|
|
if (it == securities_.end()) {
|
|
return nullptr;
|
|
}
|
|
return &it->second;
|
|
}
|
|
|
|
Security *find(SecurityType type, const string &name) {
|
|
auto id = sec_id_store_->get_id(VENUE, type, name);
|
|
if (!id) {
|
|
return nullptr;
|
|
}
|
|
return find(SecurityID{*id});
|
|
}
|
|
|
|
void on_instruments(
|
|
const vector<OkxInstrument> &instruments,
|
|
vector<const Security *> &new_secs) {
|
|
for (const auto &oi : instruments) {
|
|
Security sec;
|
|
instrument_to_sec(oi, sec);
|
|
auto type = sec.type();
|
|
switch (sec.type()) {
|
|
case SEC_TYPE(SPOT):
|
|
case SEC_TYPE(FUTURE):
|
|
case SEC_TYPE(OPTION):
|
|
break;
|
|
default:
|
|
bts_assert_msgf(
|
|
false, "Parse result has invalid security type '{}'", type);
|
|
continue;
|
|
}
|
|
// Only set id if sec passes filter to avoid flooding id store w/
|
|
// irrelevant secs.
|
|
if (!filter_(sec)) {
|
|
continue;
|
|
}
|
|
SecurityID id{sec_id_store_->get_or_add(VENUE, sec.type(), oi.instId)};
|
|
sec.security_common.id = id;
|
|
auto [it, is_new] = securities_.try_emplace(id, std::move(sec));
|
|
if (!is_new) {
|
|
continue;
|
|
}
|
|
new_secs.push_back(&it->second);
|
|
}
|
|
}
|
|
|
|
private:
|
|
IDGenerator *sec_id_store_;
|
|
function<bool(const Security &)> filter_;
|
|
unordered_map<SecurityID, Security> securities_;
|
|
};
|
|
|
|
OkxInstrumentStore::OkxInstrumentStore(
|
|
IDGenerator *sec_id_store, function<bool(const Security &)> filter)
|
|
: me_(std::make_unique<PImpl>(sec_id_store, filter)) {}
|
|
|
|
OkxInstrumentStore::~OkxInstrumentStore() = default;
|
|
|
|
Security *OkxInstrumentStore::find(SecurityID id) { return me_->find(id); }
|
|
|
|
const Security *OkxInstrumentStore::find(SecurityID id) const {
|
|
return me_->find(id);
|
|
}
|
|
|
|
Security *OkxInstrumentStore::find(SecurityType type, const string &name) {
|
|
return me_->find(type, name);
|
|
}
|
|
|
|
void OkxInstrumentStore::on_instruments(
|
|
const vector<OkxInstrument> &instruments,
|
|
vector<const Security *> &new_secs) {
|
|
me_->on_instruments(instruments, new_secs);
|
|
}
|
|
|
|
} // namespace bts::md::okx
|