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.
This commit is contained in:
1 parent
ae1e9ecb29
commit
00649e0c14
6 files changed
+132
-17
No files matched your search
@@ -10,7 +10,8 @@ class ExecutionGatewayBridge {
|
||||
virtual ~ExecutionGatewayBridge();
|
||||
// Python interface
|
||||
virtual void on_execution_report(std::shared_ptr<PyExecutionReport>) {}
|
||||
virtual void on_position_info(std::shared_ptr<PyPositionInfo>) {}
|
||||
virtual void on_position_info(std::vector<std::shared_ptr<PyPositionInfo>>) {
|
||||
}
|
||||
virtual void on_is_ready(bool) {}
|
||||
};
|
||||
|
||||
|
||||
@@ -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<PyPositionInfo> pi) {
|
||||
INFO_TRACE_PARAMS("on_execution_report", pi->to_string());
|
||||
incoming_pos_infos.push(std::move(pi));
|
||||
std::vector<std::shared_ptr<PyPositionInfo>> pos_infos) {
|
||||
std::string pos_info_str = FMT_JOIN_FN(
|
||||
pos_infos, ", ", [&](const std::shared_ptr<PyPositionInfo> &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<shared_ptr<PyPositionInfo>> &result) {
|
||||
auto res = incoming_pos_infos.pop();
|
||||
if (res) {
|
||||
result = std::move(*res);
|
||||
return true;
|
||||
}
|
||||
result = {};
|
||||
return false;
|
||||
}
|
||||
|
||||
void ExecutionGatewayBridgeImpl::on_securities(
|
||||
std::vector<std::shared_ptr<bts::md::py_interop::PySecurity>> securities) {
|
||||
INFO_TRACE_PARAMS(
|
||||
|
||||
@@ -20,7 +20,7 @@ class ExecutionGatewayBridgeImpl : public ExecutionGatewayBridge {
|
||||
|
||||
// Python interface
|
||||
void on_execution_report(std::shared_ptr<PyExecutionReport>) override;
|
||||
void on_position_info(std::shared_ptr<PyPositionInfo>) override;
|
||||
void on_position_info(std::vector<std::shared_ptr<PyPositionInfo>>) 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<const PyExecutionReport> &result);
|
||||
bool pop_position_info(std::vector<std::shared_ptr<PyPositionInfo>> &result);
|
||||
std::shared_ptr<const PyExecutionReport> pop_exe_report();
|
||||
int run_py(
|
||||
volatile bool *keep_going,
|
||||
@@ -43,7 +44,8 @@ class ExecutionGatewayBridgeImpl : public ExecutionGatewayBridge {
|
||||
bts::utils::ConcurrentValueStore<bool> is_ready;
|
||||
bts::collections::ConcurrentQueue<std::shared_ptr<PyExecutionReport>>
|
||||
incoming_exe_reports;
|
||||
bts::collections::ConcurrentQueue<std::shared_ptr<PyPositionInfo>>
|
||||
bts::collections::ConcurrentQueue<
|
||||
std::vector<std::shared_ptr<PyPositionInfo>>>
|
||||
incoming_pos_infos;
|
||||
bts::collections::ConcurrentQueue<PyOrderRequest> outgoing_reqs;
|
||||
std::mutex securities_mx;
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <fmt/format.h>
|
||||
#include <limits>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
@@ -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<float>::quiet_NaN();
|
||||
float short_quantity_ = 0;
|
||||
float short_avg_cost_ = std::numeric_limits<float>::quiet_NaN();
|
||||
std::int64_t transact_time_ = 0;
|
||||
|
||||
friend void from_json(const nlohmann::json &j, PyPositionInfo &req);
|
||||
};
|
||||
|
||||
@@ -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<std::vector<shared_ptr<PyPositionInfo>>>(m, "PyPositionInfoList")
|
||||
.PY_LIST_TO_STRING(PyPositionInfo);
|
||||
py::class_<ExecutionGatewayBridge>(m, "ExecutionGatewayBridge")
|
||||
.def(py::init<>())
|
||||
.DEF(ExecutionGatewayBridge, on_execution_report)
|
||||
|
||||
Reference in new issue
Block a user