From 00649e0c14b79d300f206bb4477eba6d7f3d8e83 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Wed, 13 Aug 2025 15:00:45 +0000 Subject: [PATCH] chore: Hook up py execution gw position callback Refactor the py bridge placeholder function to accept a vector of position objs (instead of a single one) to more accurately reflects its c++ counterpart. It also allows us to pass the account's entire position (for all securities) in a single call. --- .../py_execution_gateway_adapter.cpp | 104 +++++++++++++++++- .../execution/execution_gateway_bridge.h | 3 +- .../execution_gateway_bridge_impl.cpp | 23 +++- .../execution/execution_gateway_bridge_impl.h | 6 +- .../execution/py_position_info.h | 11 +- .../python_interop/python_interop_module.cpp | 2 + 6 files changed, 132 insertions(+), 17 deletions(-) diff --git a/src/app_sparkd/sparkd/execution/py_execution_gateway_adapter.cpp b/src/app_sparkd/sparkd/execution/py_execution_gateway_adapter.cpp index 8b4bc9ff7..b98f3db6d 100644 --- a/src/app_sparkd/sparkd/execution/py_execution_gateway_adapter.cpp +++ b/src/app_sparkd/sparkd/execution/py_execution_gateway_adapter.cpp @@ -7,6 +7,7 @@ #include "python_interop/execution/py_create_order_request.h" #include "python_interop/execution/py_execution_gateway.h" #include "python_interop/execution/py_execution_report.h" +#include "python_interop/execution/py_position_info.h" #include "python_interop/plugin_manager.h" #include "python_interop/py_contract_price_info.h" #include "python_interop/py_contract_quantity_info.h" @@ -60,6 +61,7 @@ #include #include +#include #include #include #include @@ -92,6 +94,8 @@ namespace bts::orderman { params) #define DIAG_TRACE_PARAMS(name, params) \ _LOG_TRACE_PARAMS(bts::Diag, name, params) +#define WARN_TRACE_PARAMS(name, params) \ + _LOG_TRACE_PARAMS(bts::Warn, name, params) #define ERROR_TRACE_PARAMS(name, params) \ _LOG_TRACE_PARAMS(bts::Error, name, params) @@ -132,6 +136,7 @@ using python_execution::ExecutionGatewayBridgeImpl; using python_execution::PyCancelOrderRequest; using python_execution::PyCancelReplaceOrderRequest; using python_execution::PyCreateOrderRequest; +using python_execution::PyPositionInfo; using python_interop::PluginManager; using python_interop::PythonObjectT; using std::int64_t; @@ -195,6 +200,7 @@ struct PyExecutionGatewayAdapter::PImpl { bool is_ready_dirty = false; volatile bool py_keep_going = false; uint32_t n_active = 0; + core::TimeStamp last_unresolved_account_log_time; StartStopStateMachine sm; // SimpleOrderKeyMap key_map; @@ -312,12 +318,6 @@ struct PyExecutionGatewayAdapter::PImpl { bridge.on_securities(std::move(py_secs)); } - void get_position_info() { - sm.invoke_if_active([&](auto) { - // - }); - } - void dispatch_is_ready() { BTS_LOCK_GUARD(sm.mutex()); if (is_ready_dirty) { @@ -478,6 +478,38 @@ struct PyExecutionGatewayAdapter::PImpl { py_req.set_is_buy(msg.side == bts::api::SIDE_BUY); } + void from_py( + const python_execution::PyPositionInfo &py_pos_info, + ExchangePosition::UnitPosition &upos) { + upos.exchange_sec_desc = py_pos_info.exchange_security_id(); + double long_qty = py_pos_info.long_quantity(); + if (std::isnan(long_qty)) { + long_qty = 0; + } + double long_avg_cost = py_pos_info.long_avg_cost(); + if (std::isnan(long_avg_cost)) { + long_avg_cost = 0; + } + double short_qty = py_pos_info.short_quantity(); + if (std::isnan(short_qty)) { + short_qty = 0; + } + double short_avg_cost = py_pos_info.short_avg_cost(); + if (std::isnan(short_avg_cost)) { + short_avg_cost = 0; + } + upos.position = long_qty - short_qty; + // We shouldn't combine long/short positions like this. Avg cost is + // lost/meaningless when the net qty is 0. + if (upos.position == 0) { + *upos.avg_filled_price = std::numeric_limits::quiet_NaN(); + } else { + *upos.avg_filled_price = + (long_avg_cost * long_qty - short_avg_cost * short_qty) / + (long_qty - short_qty); + } + } + void dispatch_order_updates() { BTS_LOCK_GUARD(sm.mutex()); std::shared_ptr py_er_p; @@ -531,6 +563,65 @@ struct PyExecutionGatewayAdapter::PImpl { TimeStamp::now_ns()); } } + + void dispatch_position_updates() { + BTS_LOCK_GUARD(sm.mutex()); + if (account.id() == 0 || account.name().empty()) { + const auto now = core::TimeStamp::now(); + if ((now - last_unresolved_account_log_time) > + core::TimeSpan::from_seconds(1)) { + WARN_TRACE_PARAMS( + "dispatch_position_updates", + fmt::format( + R"({{ "venue": "{}", "cfg_acc": "{}", "msg": "Waiting for admin data to get account id" }})", + api::to_pretty(pos.exchange), this->config.account)); + last_unresolved_account_log_time = now; + } + return; + } + vector> py_pos_infos; + utils::Stopwatch sw; + uint32_t n_pos_changes = 0; + sw.start(); + while (bridge.pop_position_info(py_pos_infos)) { + for (const shared_ptr &py_pos_info_ptr : + py_pos_infos) { + PyPositionInfo &py_pos_info = *py_pos_info_ptr; + const string &exch_sec_id = py_pos_info.exchange_security_id(); + const api::Security *sec_ptr = + sec_oracle.get_security(pos.exchange, exch_sec_id); + if (sec_ptr == nullptr) { + WARN_TRACE_PARAMS( + "dispatch_position_updates", + fmt::format( + R"({{ "venue": "{}", "acc": "{}", "accid": "{}", "esid": "{}", "msg": "Unresolved security" }})", + api::to_pretty(pos.exchange), account.name(), + account.id(), exch_sec_id)); + continue; + } + ExchangePosition::UnitPosition *upos_ptr = nullptr; + for (auto &upos : pos.total_exchange_positions) { + bts_assert(upos.security != nullptr); + if (upos.security->id() == sec_ptr->id()) { + upos_ptr = &upos; + break; + } + } + if (upos_ptr == nullptr) { + upos_ptr = &pos.total_exchange_positions.emplace_back(); + upos_ptr->security = sec_ptr; + upos_ptr->exchange_sec_desc = exch_sec_id; + upos_ptr->account_id = account.id(); + upos_ptr->account_name = account.name(); + } + from_py(py_pos_info, *upos_ptr); + n_pos_changes++; + } + } + if (n_pos_changes > 0) { + callbacks.exchange_position_state(pos); + } + } }; PyExecutionGatewayAdapter::PyExecutionGatewayAdapter( @@ -616,6 +707,7 @@ void PyExecutionGatewayAdapter::send_fix( void PyExecutionGatewayAdapter::dispatch_incoming_fix_messages() { me->dispatch_is_ready(); me->dispatch_order_updates(); + me->dispatch_position_updates(); } // Callbacks diff --git a/src/lib_python_interop/python_interop/execution/execution_gateway_bridge.h b/src/lib_python_interop/python_interop/execution/execution_gateway_bridge.h index 68a7107f6..e2985f3d2 100644 --- a/src/lib_python_interop/python_interop/execution/execution_gateway_bridge.h +++ b/src/lib_python_interop/python_interop/execution/execution_gateway_bridge.h @@ -10,7 +10,8 @@ class ExecutionGatewayBridge { virtual ~ExecutionGatewayBridge(); // Python interface virtual void on_execution_report(std::shared_ptr) {} - virtual void on_position_info(std::shared_ptr) {} + virtual void on_position_info(std::vector>) { + } virtual void on_is_ready(bool) {} }; diff --git a/src/lib_python_interop/python_interop/execution/execution_gateway_bridge_impl.cpp b/src/lib_python_interop/python_interop/execution/execution_gateway_bridge_impl.cpp index e2e4e7818..067fbd072 100644 --- a/src/lib_python_interop/python_interop/execution/execution_gateway_bridge_impl.cpp +++ b/src/lib_python_interop/python_interop/execution/execution_gateway_bridge_impl.cpp @@ -4,8 +4,10 @@ #include "python_interop/execution/py_create_order_request.h" #include "python_interop/execution/py_position_info.h" #include "python_interop/python_object.h" +#include "spark/core/FMT.h" #include "spark/core/TimeSpan.h" #include "spark/domain_model/Venue.h" +#include "spark/io/JsonDumper.h" #include "spark/io/json_formatter.h" #include "spark/logging/logging.h" #include "spark/logging/trace_log.h" @@ -69,9 +71,13 @@ void ExecutionGatewayBridgeImpl::on_execution_report( } void ExecutionGatewayBridgeImpl::on_position_info( - std::shared_ptr pi) { - INFO_TRACE_PARAMS("on_execution_report", pi->to_string()); - incoming_pos_infos.push(std::move(pi)); + std::vector> pos_infos) { + std::string pos_info_str = FMT_JOIN_FN( + pos_infos, ", ", [&](const std::shared_ptr &pi) { + return pi->to_string(); + }); + INFO_TRACE_PARAMS("on_position_info", fmt::format("[{}]", pos_info_str)); + incoming_pos_infos.push(std::move(pos_infos)); } void ExecutionGatewayBridgeImpl::on_is_ready(bool value) { @@ -95,6 +101,17 @@ bool ExecutionGatewayBridgeImpl::pop_exe_report( return false; } +bool ExecutionGatewayBridgeImpl::pop_position_info( + vector> &result) { + auto res = incoming_pos_infos.pop(); + if (res) { + result = std::move(*res); + return true; + } + result = {}; + return false; +} + void ExecutionGatewayBridgeImpl::on_securities( std::vector> securities) { INFO_TRACE_PARAMS( diff --git a/src/lib_python_interop/python_interop/execution/execution_gateway_bridge_impl.h b/src/lib_python_interop/python_interop/execution/execution_gateway_bridge_impl.h index c455ccd07..931975c00 100644 --- a/src/lib_python_interop/python_interop/execution/execution_gateway_bridge_impl.h +++ b/src/lib_python_interop/python_interop/execution/execution_gateway_bridge_impl.h @@ -20,7 +20,7 @@ class ExecutionGatewayBridgeImpl : public ExecutionGatewayBridge { // Python interface void on_execution_report(std::shared_ptr) override; - void on_position_info(std::shared_ptr) override; + void on_position_info(std::vector>) override; void on_is_ready(bool) override; // C++ interface @@ -32,6 +32,7 @@ class ExecutionGatewayBridgeImpl : public ExecutionGatewayBridge { securities); bool wait_for_ready(bts::core::TimeSpan timeout); bool pop_exe_report(std::shared_ptr &result); + bool pop_position_info(std::vector> &result); std::shared_ptr pop_exe_report(); int run_py( volatile bool *keep_going, @@ -43,7 +44,8 @@ class ExecutionGatewayBridgeImpl : public ExecutionGatewayBridge { bts::utils::ConcurrentValueStore is_ready; bts::collections::ConcurrentQueue> incoming_exe_reports; - bts::collections::ConcurrentQueue> + bts::collections::ConcurrentQueue< + std::vector>> incoming_pos_infos; bts::collections::ConcurrentQueue outgoing_reqs; std::mutex securities_mx; diff --git a/src/lib_python_interop/python_interop/execution/py_position_info.h b/src/lib_python_interop/python_interop/execution/py_position_info.h index 74f1d4c9f..33a1ad4dc 100644 --- a/src/lib_python_interop/python_interop/execution/py_position_info.h +++ b/src/lib_python_interop/python_interop/execution/py_position_info.h @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -46,11 +47,11 @@ class PyPositionInfo { std::string exchange_security_id_; std::string venue_; std::string security_type_; - float long_quantity_; - float long_avg_cost_; - float short_quantity_; - float short_avg_cost_; - std::int64_t transact_time_; + float long_quantity_ = 0; + float long_avg_cost_ = std::numeric_limits::quiet_NaN(); + float short_quantity_ = 0; + float short_avg_cost_ = std::numeric_limits::quiet_NaN(); + std::int64_t transact_time_ = 0; friend void from_json(const nlohmann::json &j, PyPositionInfo &req); }; diff --git a/src/lib_python_interop/python_interop/python_interop_module.cpp b/src/lib_python_interop/python_interop/python_interop_module.cpp index ae764621f..0578e7a0b 100644 --- a/src/lib_python_interop/python_interop/python_interop_module.cpp +++ b/src/lib_python_interop/python_interop/python_interop_module.cpp @@ -289,6 +289,8 @@ PYBIND11_MODULE(python_interop_module, m) { .PROP(PyPositionInfo, short_avg_cost) .PROP(PyPositionInfo, transact_time) .def("__str__", &PyPositionInfo::to_string); + py::bind_vector>>(m, "PyPositionInfoList") + .PY_LIST_TO_STRING(PyPositionInfo); py::class_(m, "ExecutionGatewayBridge") .def(py::init<>()) .DEF(ExecutionGatewayBridge, on_execution_report)