refactor: Consolidate all py interop into single lib

It was just too hard trying to make it work with separate libs. The
main issue was getting the python md and execution modules to
recognize PySecurityList and other types defined in the core module.
This commit is contained in:
John Colvin committed 2025-06-09 06:06:50 +00:00
1 parent 44948ab4db
commit 2174162081
87 files changed
+1125 -1181

No files matched your search

@@ -0,0 +1,153 @@
#include "py_cancel_order_request.h"
#include "py_cancel_replace_order_request.h"
#include "py_create_order_request.h"
#include "python_interop/execution/execution_gateway.h"
#include "python_interop/execution/execution_gateway_bridge.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/md/md_bridge.h"
#include "python_interop/md/md_provider.h"
#include "python_interop/md/py_book.h"
#include "python_interop/md/py_book_level.h"
#include "python_interop/md/py_book_side.h"
#include "python_interop/md/py_md_provider.h"
#include "python_interop/md/py_trade.h"
#include <memory>
#include <pybind11/detail/common.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>
using bts::md::py_interop::MdBridge;
using bts::md::py_interop::MdProvider;
using bts::md::py_interop::PyBook;
using bts::md::py_interop::PyBookLevel;
using bts::md::py_interop::PyBookSide;
using bts::md::py_interop::PyMdProvider;
using bts::md::py_interop::PyTrade;
using python_execution::ExecutionGateway;
using python_execution::ExecutionGatewayBridge;
using python_execution::PyCancelOrderRequest;
using python_execution::PyCancelReplaceOrderRequest;
using python_execution::PyCreateOrderRequest;
using python_execution::PyExecutionGateway;
using python_execution::PyExecutionReport;
using python_execution::PyPositionInfo;
using std::shared_ptr;
using std::shared_ptr;
PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>)
namespace py = pybind11;
#define PY_ARG(arg) pybind11::arg(#arg)
#define _STR(x) #x
// In general, we bind c++ methods to python methods with the same name verbatim.
// The DEF macro avoids typos in the python method name for this common case.
#define DEF(type, member) def(_STR(member), &type::member)
#define DEF_ARGS(type, member, ...) \
def(_STR(member), &type::member, __VA_ARGS__)
#define EXPLICIT_DEF(type, member, member_type) \
def(_STR(member), (member_type) & type::member)
// The PROP macro defines getter and setter methods according to <name>
// Binds a pseudo property with getter/setter methods.
// Binds getter -> <name>, and setter -> set_<name>.
#define PROP(type, name) \
def(_STR(name), &type::name) \
.def(_STR(set_##name), &type::set_##name, py::arg(#name))
PYBIND11_MODULE(python_execution_module, m) {
py::class_<PyCreateOrderRequest, shared_ptr<PyCreateOrderRequest>>(
m, "PyCreateOrderRequest")
.def(py::init<>())
.PROP(PyCreateOrderRequest, key)
.PROP(PyCreateOrderRequest, exchange_security_id)
.PROP(PyCreateOrderRequest, venue)
.PROP(PyCreateOrderRequest, is_buy)
.PROP(PyCreateOrderRequest, price)
.PROP(PyCreateOrderRequest, quantity)
.PROP(PyCreateOrderRequest, order_type)
.PROP(PyCreateOrderRequest, time_in_force)
.def("__str__", &PyCreateOrderRequest::to_string);
py::class_<PyCancelOrderRequest, shared_ptr<PyCancelOrderRequest>>(
m, "PyCancelOrderRequest")
.def(py::init<>())
.PROP(PyCancelOrderRequest, key)
.PROP(PyCancelOrderRequest, exchange_order_id)
.PROP(PyCancelOrderRequest, exchange_security_id)
.PROP(PyCancelOrderRequest, venue)
.PROP(PyCancelOrderRequest, is_buy)
.def("__str__", &PyCancelOrderRequest::to_string);
py::class_<
PyCancelReplaceOrderRequest, shared_ptr<PyCancelReplaceOrderRequest>>(
m, "PyCancelReplaceOrderRequest")
.def(py::init<>())
.PROP(PyCancelReplaceOrderRequest, key)
.PROP(PyCancelReplaceOrderRequest, exchange_order_id)
.PROP(PyCancelReplaceOrderRequest, exchange_security_id)
.PROP(PyCancelReplaceOrderRequest, venue)
.PROP(PyCancelReplaceOrderRequest, is_buy)
.PROP(PyCancelReplaceOrderRequest, price)
.PROP(PyCancelReplaceOrderRequest, quantity)
.def("__str__", &PyCancelReplaceOrderRequest::to_string);
py::class_<PyExecutionReport, shared_ptr<PyExecutionReport>>(
m, "PyExecutionReport")
.def(py::init<>())
.PROP(PyExecutionReport, key)
.PROP(PyExecutionReport, account)
.PROP(PyExecutionReport, exchange_order_id)
.PROP(PyExecutionReport, exchange_security_id)
.PROP(PyExecutionReport, order_status)
.PROP(PyExecutionReport, time_in_force)
.PROP(PyExecutionReport, exec_type)
.PROP(PyExecutionReport, venue)
.PROP(PyExecutionReport, security_type)
.PROP(PyExecutionReport, is_buy)
.PROP(PyExecutionReport, price)
.PROP(PyExecutionReport, quantity)
.PROP(PyExecutionReport, filled_quantity)
.PROP(PyExecutionReport, last_filled_quantity)
.PROP(PyExecutionReport, last_filled_price)
.PROP(PyExecutionReport, avg_filled_price)
.PROP(PyExecutionReport, order_type)
.PROP(PyExecutionReport, transact_time)
.def(
"set_data", &PyExecutionReport::set_data, py::arg("key"),
py::arg("account"), py::arg("exchange_order_id"),
py::arg("exchange_security_id"), py::arg("venue"),
py::arg("security_type"), py::arg("order_type"),
py::arg("order_status"), py::arg("time_in_force"),
py::arg("exec_type"), py::arg("is_buy"), py::arg("price"),
py::arg("quantity"), py::arg("filled_quantity"),
py::arg("last_filled_quantity"), py::arg("last_filled_price"),
py::arg("avg_filled_price"), py::arg("transact_id"),
py::arg("transact_time"))
.def("__str__", &PyExecutionReport::to_string);
py::class_<PyPositionInfo, shared_ptr<PyPositionInfo>>(m, "PyPositionInfo")
.def(py::init<>())
.PROP(PyPositionInfo, account)
.PROP(PyPositionInfo, exchange_security_id)
.PROP(PyPositionInfo, venue)
.PROP(PyPositionInfo, security_type)
.PROP(PyPositionInfo, long_quantity)
.PROP(PyPositionInfo, long_avg_cost)
.PROP(PyPositionInfo, short_quantity)
.PROP(PyPositionInfo, short_avg_cost)
.PROP(PyPositionInfo, transact_time)
.def("__str__", &PyPositionInfo::to_string);
py::class_<ExecutionGatewayBridge>(m, "ExecutionGatewayBridge")
.def(py::init<>())
.DEF(ExecutionGatewayBridge, on_execution_report)
.DEF(ExecutionGatewayBridge, on_position_info)
.DEF(ExecutionGatewayBridge, on_is_ready);
py::class_<ExecutionGateway, PyExecutionGateway>(m, "ExecutionGateway")
.def(py::init<>())
.DEF_ARGS(ExecutionGateway, start, py::arg("bridge"))
.DEF_ARGS(ExecutionGateway, create_order, py::arg("req"))
.DEF_ARGS(ExecutionGateway, cancel_order, py::arg("req"))
.DEF_ARGS(ExecutionGateway, cancel_replace_order, py::arg("req"))
.DEF(ExecutionGateway, stop);
}