Exchange Open orders are routed all the way to EMExecutionGateway

This commit is contained in:
taha afzal committed 2022-07-20 20:13:56 -04:00
1 parent 4b5ff8cad4
commit 5eca2cd3ac
4 files changed
+103 -34

No files matched your search

@@ -21,7 +21,7 @@
#include "lib/tracing/bts_trace.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/datetime.h"
#include "lib/utils/helper_macros.h"
//#include "lib/utils/helper_macros.h"
#include "server/sparkd/md/platforms/emumba/EmMDListener.h"
#include "server/sparkd/md/platforms/emumba/IEMOrderMuxer.h"
@@ -66,6 +66,10 @@ struct EMExchangePositionCallbackMsg {
std::vector<EMPosition> positions;
};
struct EMExchangeOpenOrdersCallbackMsg {
std::vector<EMOpenOrder> orders;
};
struct EMExecutionGateway::PImpl {
PImpl(md::SecurityOracle o) : sec_oracle(o) {
LOG(OrderMan, Info, "EMExecutionGateway::PImpl constructor");
@@ -99,6 +103,9 @@ struct EMExecutionGateway::PImpl {
tbb::concurrent_queue<EMCallbackMsg>();
tbb::concurrent_queue<EMExchangePositionCallbackMsg> exch_pos_queue =
tbb::concurrent_queue<EMExchangePositionCallbackMsg>();
tbb::concurrent_queue<EMExchangeOpenOrdersCallbackMsg>
exch_open_orders_queue =
tbb::concurrent_queue<EMExchangeOpenOrdersCallbackMsg>();
// periodic query (temporary)
bts::datetime::PTime last_pos_fire_time = bts::datetime::now();
uint32_t pos_fire_period_ms = 5000;
@@ -222,6 +229,10 @@ void EMExecutionGateway::dispatch_incoming_fix_messages() {
while (me->exch_pos_queue.try_pop(pos_cm_msg)) {
_process_pos_cb_and_fire(pos_cm_msg);
}
EMExchangeOpenOrdersCallbackMsg open_order_cb_msg = {};
while (me->exch_open_orders_queue.try_pop(open_order_cb_msg)) {
_process_open_order_cb_and_fire(open_order_cb_msg);
}
_send_position_request();
}
@@ -302,7 +313,7 @@ bool EMExecutionGateway::_init() {
me->exch_pos_queue.push(pos_cb);
};
me->order_mux->exchange_open_orders_update_cb() =
[](const auto &orders) {
[this](const auto &orders) {
LOG(OrderMan, Info,
"EMExecutionGateway exchange_open_orders_update_cb() "
"open_orders: {}",
@@ -312,6 +323,8 @@ bool EMExecutionGateway::_init() {
"EMExecutionGateway exchange_open_orders_update_cb {}",
oord);
}
EMExchangeOpenOrdersCallbackMsg open_order_cb = {orders};
me->exch_open_orders_queue.push(open_order_cb);
};
}
}
@@ -474,43 +487,18 @@ void EMExecutionGateway::_process_cb_and_fire_er(const EMCallbackMsg &em_cb) {
// ExchangePosition (resolved security and account)
void EMExecutionGateway::_process_pos_cb_and_fire(
const EMExchangePositionCallbackMsg &pos_cb_msg) {
// resoves the account from sec, sub_account
auto get_account = [this](
api::Security const *sec,
const std::string &exch_account) {
auto *entry =
me->subaccount_resolver.find_entry(sec, exch_account, false);
if (entry == nullptr) {
LOG(OrderMan, Error,
"Could not resolve internal account for sec: {}, sub_account: {}",
utils::pb::to_string(*sec), exch_account);
return std::tuple<std::string, api::admin::BTSAccount *>("", nullptr);
}
auto account_name = entry->bts_account_name();
auto bts_account_ptr =
admin::AdminServiceUtils::find_account(me->admin_data, account_name);
return std::tuple<std::string, api::admin::BTSAccount *>(
account_name, bts_account_ptr);
};
auto get_sec = [this](const auto &exch_sec_desc) {
for (auto sec : me->sec_oracle.get_securities()) {
if (sec->has_deribit()) {
if (sec->deribit().security_desc() == exch_sec_desc) {
return sec;
}
}
}
return static_cast<const api::Security *>(nullptr);
};
me->exchange_position.total_exchange_positions.clear();
// TODO: need to fix this whem EM connects to multiple exchanges
me->exchange_position.exchange = api::VENUE_DERIBIT;
for (const auto &pos : pos_cb_msg.positions) {
const std::string &exch_sec_desc =
std::get<std::string>(pos.position.sec_id.value());
auto sec = get_sec(exch_sec_desc);
auto sec = _get_security(exch_sec_desc);
double pos_qty = std::get<double>(pos.position.qty.value());
auto [account_name, bts_account_ptr] = get_account(sec, pos.sub_account);
auto bts_account_ptr = _get_account(sec, pos.sub_account);
auto account_name = [bts_account_ptr]() {
return bts_account_ptr == nullptr ? "" : bts_account_ptr->name();
};
std::optional<uint32_t> account_id =
bts_account_ptr == nullptr
? std::nullopt
@@ -518,9 +506,9 @@ void EMExecutionGateway::_process_pos_cb_and_fire(
LOG(OrderMan, Info,
"EMExecutionGateway::_process_pos_cb_and_fire pos: {} bts_account: "
"{}",
pos, account_name);
pos, account_name());
me->exchange_position.total_exchange_positions.push_back(
{exch_sec_desc, sec, account_name, account_id, pos_qty,
{exch_sec_desc, sec, account_name(), account_id, pos_qty,
Price(pos.position.average_price.to_double())});
}
LOG(OrderMan, Info, "EMExecutionGateway::_process_pos_cb_and_fire n_pos: {}",
@@ -528,6 +516,13 @@ void EMExecutionGateway::_process_pos_cb_and_fire(
me->on_exchange_position_state(me->exchange_position);
}
void EMExecutionGateway::_process_open_order_cb_and_fire(
const EMExchangeOpenOrdersCallbackMsg &open_order_cb) {
LOG(OrderMan, Info,
"EMExecutionGateway::_process_open_order_cb_and_fire n_orders: {}",
open_order_cb.orders.size());
}
void EMExecutionGateway::_send_position_request() {
if (me->order_mux == nullptr)
return;
@@ -542,5 +537,32 @@ void EMExecutionGateway::_send_position_request() {
me->order_mux->get_open_orders();
}
api::admin::BTSAccount *EMExecutionGateway::_get_account(
api::Security const *sec, const std::string &exch_account) {
auto *entry = me->subaccount_resolver.find_entry(sec, exch_account, false);
if (entry == nullptr) {
LOG(OrderMan, Error,
"Could not resolve internal account for sec: {}, sub_account: {}",
utils::pb::to_string(*sec), exch_account);
return nullptr;
}
auto account_name = entry->bts_account_name();
auto bts_account_ptr =
admin::AdminServiceUtils::find_account(me->admin_data, account_name);
return bts_account_ptr;
}
const api::Security *EMExecutionGateway::_get_security(
const std::string &exch_sec_desc) {
for (auto sec : me->sec_oracle.get_securities()) {
if (sec->has_deribit()) {
if (sec->deribit().security_desc() == exch_sec_desc) {
return sec;
}
}
}
return static_cast<const api::Security *>(nullptr);
}
} // namespace orderman
} // namespace bts
@@ -1,9 +1,11 @@
#pragma once
#include "IExecutionGateway.h"
#include "lib/api/services/AdminService.pb.h"
#include "lib/orderman/CancelOrderMsg.h"
#include "lib/orderman/CancelReplaceOrderMsg.h"
#include "lib/orderman/CreateOrderMsg.h"
#include "server/sparkd/md/platforms/emumba/IEMOrderMuxer.h"
#include <bits/stdint-uintn.h>
#include <cstdint>
@@ -47,6 +49,7 @@ namespace orderman {
struct EMCallbackMsg;
struct EMExchangePositionCallbackMsg;
struct EMExchangeOpenOrdersCallbackMsg;
// Wrapper around EM's order gateway. There are fix overtones to this, that are
// ignored
class EMExecutionGateway : public IExecutionGateway {
@@ -77,8 +80,13 @@ class EMExecutionGateway : public IExecutionGateway {
void _send_cxl(const CancelOrderMsg &);
void _process_cb_and_fire_er(const EMCallbackMsg &);
void _process_pos_cb_and_fire(const EMExchangePositionCallbackMsg &);
void _process_open_order_cb_and_fire(
const EMExchangeOpenOrdersCallbackMsg &);
// periodic polling of position and orders
void _send_position_request();
api::admin::BTSAccount *_get_account(
api::Security const *sec, const std::string &exch_account);
const api::Security *_get_security(const std::string &exch_sec_desc);
};
} // namespace orderman
@@ -6,6 +6,7 @@
#include "lib/md/SecurityOracle.h"
#include "lib/mercury/events/Update.h"
#include "lib/orderman/OrderMsg.h"
#include "lib/orderman/orderman_utils/ExchangeOpenOrder.h"
#include "lib/orderman/orderman_utils/ExchangePosition.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/utils/bts_no_copy.h"
@@ -0,0 +1,38 @@
#pragma once
#include "lib/api/services/OrderService.pb.h"
#include "lib/domain_model/Security.pb.h"
#include "lib/domain_model/common.h"
#include "lib/mercury/common/mercury_common.h"
#include "lib/orderman/orderman_utils/orderman_common.h"
#include "lib/utils/overloaded.h"
#include <cstdint>
#include <memory>
#include <optional>
#include <variant>
#include <vector>
namespace bts::orderman {
class ExchangeOpenOrder {
public:
class UnitOrder {
public:
std::string exchange_sec_desc;
api::Security const *security;
std::string exchange_order_id;
std::string account_name;
std::optional<uint32_t> account_id;
bts::api::orderman::TimeInForce tif;
bts::api::Side side;
Price price;
Quantity qty;
Price filled_price;
Quantity filled_qty;
bool api;
bool web;
};
api::Venue exchange;
std::vector<UnitOrder> total_exchange_positions;
};
} // namespace bts::orderman