Deribit spread market data is now written to shared memory

This commit is contained in:
Taha Afzal authored and John Colvin committed 2022-09-22 23:51:48 -04:00
1 parent c688657d53
commit 83de5fac75
4 files changed
+141 -14

No files matched your search

+1
View File
@@ -139,6 +139,7 @@ Symbols {
deribit {
future: @SymbolLists.list[DERIBIT].symbol
option: @SymbolLists.list[DERIBIT].symbol
spread: @SymbolLists.list[DERIBIT].symbol
}
}
}
+5
View File
@@ -228,6 +228,11 @@ size_t get_system_md_symbols(
e.option(), api::VENUE_DERIBIT, api::SECURITYTYPE_OPTION,
&result);
}
if (type == api::SECURITYTYPE_SPREAD ||
type == api::SECURITYTYPE_UNSET)
add_symbols_to_result(
e.spread(), api::VENUE_DERIBIT, api::SECURITYTYPE_SPREAD,
&result);
}
}
for (const auto &u : result) {
@@ -217,6 +217,18 @@ const std::string &extract_security_desc(
return extract_security_desc(s.sec_common.sec_id);
}
std::string get_spread_def(const emumba::market_data::security &sec) {
const auto &sec_desc = extract_security_desc(sec.sec_common.sec_id);
std::string spr_str = sec_desc + ":";
const auto &spr = std::get<emumba::market_data::spread>(sec.sec);
for (const auto &l : spr.legs) {
auto l_qty = extract_qty(l.leg_mult);
spr_str = spr_str + " " + std::to_string(l_qty) + "*" +
extract_security_desc(l.sec_id);
}
return spr_str;
}
void get_date_from_epoch_secs(
long int epoch_msecs, uint32_t &year, uint32_t &month, uint32_t &day) {
// timezone)
@@ -229,12 +241,16 @@ void get_date_from_epoch_secs(
}
struct SecurityAuxData {
const uint64_t locked_key;
// Deribit (and other crypto exchanges) do not have numeric ids for the
// securities. Since the market data infrastructure requires numeric exchange
// id's we keep a map of exchange, exchange_string_id -> generated numeric id
// in the database.
const uint64_t exchange_id;
const std::string symbol;
bool subsribed = false;
double volume = 0.0;
SecurityAuxData(uint64_t locked_key, const std::string &symbol)
: locked_key(locked_key), symbol(symbol) {}
: exchange_id(locked_key), symbol(symbol) {}
};
// Encapsulates the code that queries the trading session for position
@@ -315,6 +331,7 @@ struct EmMDListener::PImpl {
std::vector<EMTradingSession> trading_sessions;
EmumbaSecurityIndex &index;
MDProducer &md_producer;
// map of exchange's sec desc vs
std::unordered_map<std::string, SecurityAuxData> sec_map = {};
MDArray bids = {};
MDArray asks = {};
@@ -324,6 +341,7 @@ struct EmMDListener::PImpl {
std::map<int64_t, EMTradingSession *> order_acc_map;
std::unordered_map<int64_t, bts::orderman::EMOrderState> order_state_map =
{};
std::set<emumba::market_data::security const *> pending_spreads = {};
AsyncRequestHelper<emumba::order_entry::position, EMPosition> pos_helper = {
&trading_sessions};
AsyncRequestHelper<emumba::order_entry::open_order, EMOpenOrder>
@@ -566,6 +584,10 @@ void EmMDListener::on_securities(
case (api::SecurityType::SECURITYTYPE_OPTION):
_add_option(sec);
break;
case (api::SecurityType::SECURITYTYPE_SPREAD): {
_add_spread(sec);
break;
}
default: {
bts_assert_msg(false, "Unknown security type");
}
@@ -584,7 +606,7 @@ void EmMDListener::on_security_statistics(
__attribute__((unused)) const void *ptr) {
EmumbaSecurityData *sec_data = _find_security(market_data_stats.sec_id);
if (sec_data == nullptr) {
LOG(md, Error, "EmMDListener::on_security_state could not find desc:{}",
LOG(md, Error, "EmMDListener::on_security_state could not find desc: {}",
extract_security_desc(market_data_stats.sec_id));
return;
}
@@ -617,7 +639,7 @@ void EmMDListener::on_quote(
__attribute__((unused)) const void *ptr) {
EmumbaSecurityData *sec_data = _find_security(quote.sec_id);
if (sec_data == nullptr) {
LOG(md, Error, "EmMDListener::on_quote could not find desc:{}",
LOG(md, Error, "EmMDListener::on_quote could not find desc: {}",
extract_security_desc(quote.sec_id));
return;
}
@@ -645,7 +667,7 @@ void EmMDListener::on_book(
__attribute__((unused)) const void *ptr) {
EmumbaSecurityData *sec_data = _find_security(book.sec_id);
if (sec_data == nullptr) {
LOG(md, Error, "EmMDListener::on_security_state could not find desc:{}",
LOG(md, Error, "EmMDListener::on_security_state could not find desc: {}",
extract_security_desc(book.sec_id));
return;
}
@@ -663,6 +685,7 @@ void EmMDListener::on_book(
copy_books(book.bids, me->bids);
copy_books(book.asks, me->asks);
sec_data->handle.update_direct(utils::TimeStamp::now(), me->bids, me->asks);
_check_pending_spreads();
}
void EmMDListener::on_security_state(
@@ -670,7 +693,7 @@ void EmMDListener::on_security_state(
__attribute__((unused)) const void *ptr) {
EmumbaSecurityData *sec_data = _find_security(sec_state.sec_id);
if (sec_data == nullptr) {
LOG(md, Error, "EmMDListener::on_security_state could not find desc:{}",
LOG(md, Error, "EmMDListener::on_security_state could not find desc: {}",
extract_security_desc(sec_state.sec_id));
return;
}
@@ -798,6 +821,75 @@ void EmMDListener::_add_option(const emumba::market_data::security &sec) {
_add_security(osec);
}
bool EmMDListener::_add_spread(const emumba::market_data::security &sec) {
bts_assert(
extract_security_type(sec) == api::SecurityType::SECURITYTYPE_SPREAD);
std::string spread_def = get_spread_def(sec);
LOG(md, Info, "EmMDListener::_add_spread attemping to add spread: {}",
spread_def);
// if spread exists return else
// if not
// if all legs exist add it
// else add it to the pending queue
const std::string &desc = extract_security_desc(sec);
auto search_result = _find_by_desc(sec.sec_common.symbol, desc, false);
if (search_result.existed_before) {
// already exists, nothing to do
LOG(md, Warn, "EmMDListener::_add_spread spread: {} already exists",
spread_def);
bts_assert(search_result.exists_now);
return false;
}
// does not exist, check if all legs exist
const auto &spr = std::get<emumba::market_data::spread>(sec.sec);
for (const auto &l : spr.legs) {
const auto &l_id = extract_security_desc(l.sec_id);
auto l_search_result = _find_by_desc(l_id, l_id, false);
if (!l_search_result.existed_before) {
LOG(md, Warn,
"EmMDListener::_add_spread could not add spread: {} because leg: "
"{} does not exist",
spread_def, l_id);
auto sec_p = &sec;
if (me->pending_spreads.find(sec_p) == me->pending_spreads.end()) {
LOG(md, Info,
"EmMDListener::_add_spread adding spread: {} to pending "
"spreads",
desc);
me->pending_spreads.insert(sec_p);
}
return false;
}
}
// all legs exist, add the spread
LOG(md, Info,
"EmMDListener::_add_spread all legs resolved, adding spread: {}",
spread_def);
// actually add spread
search_result = _find_by_desc(sec.sec_common.symbol, desc, true);
bts_assert(!search_result.existed_before);
bts_assert(search_result.exists_now);
FixedSpreadSecurityS fspread = {};
_copy_security_common(sec, search_result.key, fspread);
fspread.set_leg_count(spr.legs.size());
size_t i = 0;
for (const auto &l : spr.legs) {
const auto &l_desc = extract_security_desc(l.sec_id);
auto l_search_result = _find_by_desc(l_desc, l_desc, false);
bts_assert(l_search_result.existed_before);
double l_mult = extract_qty(l.leg_mult);
// assining values ot leg
FixedSpreadLeg f_leg = {};
f_leg.key = l_search_result.key;
f_leg.exchange_key = ExchangeKey(l_search_result.key);
f_leg.leg_side = l_mult < 0 ? Side::SIDE_BUY : Side::SIDE_SELL;
f_leg.leg_quantity = l_mult;
fspread.legs[i++] = f_leg;
}
_add_security(fspread);
return true;
}
void EmMDListener::_add_spot(const emumba::market_data::security &) {
// TODO
}
@@ -806,13 +898,14 @@ void EmMDListener::_copy_security_common(
const emumba::market_data::security &from,
uint64_t key,
FixedSecurity &to) const {
to.key = key; // key;
// TODO, uint64 -> uint32
to.key = key;
to.numeric_id = key;
auto symbol = from.sec_common.symbol;
to.symbol(symbol);
const auto desc = extract_security_desc(from);
to.security_desc(desc);
to.exchange = Venue::VENUE_DERIBIT; // TODO: hc'ing to SIM
to.exchange = Venue::VENUE_DERIBIT;
to.display_factor = 0.0;
double min_tick = from.sec_common.min_price_increment.to_double();
to.low_tick = to.high_tick = min_tick;
@@ -821,7 +914,8 @@ void EmMDListener::_copy_security_common(
EmMDListener::~EmMDListener() {}
// Private functions
// Private functions:
// adds a security to the shared memory
void EmMDListener::_add_security(const FixedSecurity &security) {
// Set up the shared memory for this security
bts_assert(security.numeric_id != 0);
@@ -830,12 +924,15 @@ void EmMDListener::_add_security(const FixedSecurity &security) {
me->index.add_security(security.numeric_id, handle);
}
// Finds a security by its exchange given desc (or id, in the case of Deribit).
// Adds it if add_if_absent is true. The returned type conatains the system sec
// id along with its 'addition' information
DescSearchResult EmMDListener::_find_by_desc(
const std::string &symbol, const std::string &desc, bool add_if_absent) {
auto sec_it = me->sec_map.find(desc);
if (sec_it != me->sec_map.end()) {
// exists already
auto key = sec_it->second.locked_key;
auto key = sec_it->second.exchange_id;
return {key, true, true};
} else {
// does not exist
@@ -843,9 +940,8 @@ DescSearchResult EmMDListener::_find_by_desc(
auto new_key_opt = me->id_gen.get_id("exchange_key", desc, true);
bts_assert(new_key_opt.has_value());
auto new_key = new_key_opt.value();
// me->sec_map.insert({desc, SecuritySubStatus(new_key, symbol)});
me->sec_map.insert({desc, {new_key, symbol}});
LOG(md, Info, "Adding new sec desc:{} key:{}", desc, new_key);
LOG(md, Info, "Adding new sec desc: {} key: {}", desc, new_key);
return {new_key, false, true};
} else {
return {0, false, false};
@@ -887,8 +983,8 @@ void EmMDListener::_subsribe() {
if (sec_sub.subsribed) {
return;
}
me->index.has_security(sec_sub.locked_key);
const auto &val = me->index.get_security(sec_sub.locked_key);
me->index.has_security(sec_sub.exchange_id);
const auto &val = me->index.get_security(sec_sub.exchange_id);
const auto &handle = val->handle;
const FixedSecurity &sec = handle.security();
if (sec.type == api::SECURITYTYPE_FUTURE) {
@@ -930,6 +1026,29 @@ EMTradingSession *EmMDListener::_find_trading_sessions(
return nullptr;
}
// tries to add the pending spreads that have not been added earlier
void EmMDListener::_check_pending_spreads() {
if (me->pending_spreads.empty()) {
return;
}
std::vector<const emumba::market_data::security *> added_spreads = {};
for (auto sec_p : me->pending_spreads) {
if (_add_spread(*sec_p)) {
added_spreads.push_back(sec_p);
}
}
for (auto sec_p : added_spreads) {
LOG(md, Info,
"EmMDListener::_check_pending_spreads removing spread: {} from "
"pending "
"spreads",
extract_security_desc(*sec_p));
me->pending_spreads.erase(sec_p);
}
LOG(md, Info,
"EmMDListener::_check_pending_spreads pending spreads is now empty");
}
/* #endregion */
} // namespace bts::md
@@ -133,6 +133,7 @@ class EmMDListener : public emumba::deribit_connector::deribit_listener,
SecurityKey _get_key(bts::api::Security &security) const;
void _add_future(const emumba::market_data::security &);
void _add_option(const emumba::market_data::security &);
bool _add_spread(const emumba::market_data::security &);
void _add_spot(const emumba::market_data::security &);
void _add_security(const FixedSecurity &security);
void _copy_security_common(
@@ -147,6 +148,7 @@ class EmMDListener : public emumba::deribit_connector::deribit_listener,
void _subsribe();
bts::orderman::EMTradingSession *_find_trading_sessions(
const std::string &account);
void _check_pending_spreads();
public:
// the handler and the listener both need each other, however the handler