From 5eca2cd3ac418113552f8cd41e09cb715c385648 Mon Sep 17 00:00:00 2001 From: taha afzal Date: Wed, 20 Jul 2022 20:13:56 -0400 Subject: [PATCH] Exchange Open orders are routed all the way to EMExecutionGateway --- .../orderman/gateways/EMExecutionGateway.cpp | 90 ++++++++++++------- .../orderman/gateways/EMExecutionGateway.h | 8 ++ src/lib/orderman/gateways/IExecutionGateway.h | 1 + .../orderman_utils/ExchangeOpenOrder.h | 38 ++++++++ 4 files changed, 103 insertions(+), 34 deletions(-) create mode 100644 src/lib/orderman/orderman_utils/ExchangeOpenOrder.h diff --git a/src/lib/orderman/gateways/EMExecutionGateway.cpp b/src/lib/orderman/gateways/EMExecutionGateway.cpp index 296fae605..f2f0b91cd 100644 --- a/src/lib/orderman/gateways/EMExecutionGateway.cpp +++ b/src/lib/orderman/gateways/EMExecutionGateway.cpp @@ -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 positions; }; +struct EMExchangeOpenOrdersCallbackMsg { + std::vector 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(); tbb::concurrent_queue exch_pos_queue = tbb::concurrent_queue(); + tbb::concurrent_queue + exch_open_orders_queue = + tbb::concurrent_queue(); // 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("", nullptr); - } - auto account_name = entry->bts_account_name(); - auto bts_account_ptr = - admin::AdminServiceUtils::find_account(me->admin_data, account_name); - return std::tuple( - 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(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(pos.position.sec_id.value()); - auto sec = get_sec(exch_sec_desc); + auto sec = _get_security(exch_sec_desc); double pos_qty = std::get(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 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(nullptr); +} + } // namespace orderman } // namespace bts \ No newline at end of file diff --git a/src/lib/orderman/gateways/EMExecutionGateway.h b/src/lib/orderman/gateways/EMExecutionGateway.h index 7245bc718..41c128331 100644 --- a/src/lib/orderman/gateways/EMExecutionGateway.h +++ b/src/lib/orderman/gateways/EMExecutionGateway.h @@ -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 #include @@ -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 diff --git a/src/lib/orderman/gateways/IExecutionGateway.h b/src/lib/orderman/gateways/IExecutionGateway.h index a161a5539..1d3bee2cb 100644 --- a/src/lib/orderman/gateways/IExecutionGateway.h +++ b/src/lib/orderman/gateways/IExecutionGateway.h @@ -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" diff --git a/src/lib/orderman/orderman_utils/ExchangeOpenOrder.h b/src/lib/orderman/orderman_utils/ExchangeOpenOrder.h new file mode 100644 index 000000000..9cacab727 --- /dev/null +++ b/src/lib/orderman/orderman_utils/ExchangeOpenOrder.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 +#include +#include +#include +#include + +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 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 total_exchange_positions; +}; +} // namespace bts::orderman \ No newline at end of file