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:
1 parent
44948ab4db
commit
2174162081
87 files changed
+1125
-1181
No files matched your search
@@ -0,0 +1,276 @@
|
||||
#include "python_interop/execution/execution_gateway.h"
|
||||
#include "python_interop/execution/execution_gateway_bridge.h"
|
||||
#include "python_interop/execution/py_cancel_order_request.h"
|
||||
#include "python_interop/execution/py_cancel_replace_order_request.h"
|
||||
#include "python_interop/execution/py_create_order_request.h"
|
||||
#include "python_interop/execution/py_execution_gateway.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 "python_interop/py_contract_price_info.h"
|
||||
#include "python_interop/py_contract_quantity_info.h"
|
||||
#include "python_interop/py_contract_specification.h"
|
||||
#include "python_interop/py_security.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::PyContractPriceInfo;
|
||||
using bts::md::py_interop::PyContractQuantityInfo;
|
||||
using bts::md::py_interop::PyContractSpecification;
|
||||
using bts::md::py_interop::PyFuture;
|
||||
using bts::md::py_interop::PyMdProvider;
|
||||
using bts::md::py_interop::PyOption;
|
||||
using bts::md::py_interop::PySecurity;
|
||||
using bts::md::py_interop::PySpot;
|
||||
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;
|
||||
|
||||
PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>)
|
||||
|
||||
namespace py = pybind11;
|
||||
|
||||
#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)
|
||||
// Defines get/set methods for a pseudo property
|
||||
#define PROP(type, name) \
|
||||
def(_STR(name), &type::name) \
|
||||
.def(_STR(set_##name), &type::set_##name, py::arg(#name))
|
||||
|
||||
// Define python module with c++ classes.
|
||||
PYBIND11_MODULE(python_interop_module, m) {
|
||||
// price info
|
||||
py::class_<PyContractPriceInfo>(m, "PyContractPriceInfo")
|
||||
.def(py::init<>())
|
||||
.PROP(PyContractPriceInfo, tick_unit)
|
||||
.PROP(PyContractPriceInfo, tick_base)
|
||||
.PROP(PyContractPriceInfo, multiplier)
|
||||
.PROP(PyContractPriceInfo, cash_per_point)
|
||||
.PROP(PyContractPriceInfo, max_price)
|
||||
.def("__str__", &PyContractPriceInfo::to_string);
|
||||
py::bind_vector<std::vector<PyContractPriceInfo>>(
|
||||
m, "PyContractPriceInfoList");
|
||||
// quantity info
|
||||
py::class_<PyContractQuantityInfo>(m, "PyContractQuantityInfo")
|
||||
.def(py::init<>())
|
||||
.PROP(PyContractQuantityInfo, min_value)
|
||||
.PROP(PyContractQuantityInfo, max_value)
|
||||
.PROP(PyContractQuantityInfo, tick_unit)
|
||||
.PROP(PyContractQuantityInfo, tick_base)
|
||||
.PROP(PyContractQuantityInfo, multiplier)
|
||||
.PROP(PyContractQuantityInfo, max_price)
|
||||
.def("__str__", &PyContractQuantityInfo::to_string);
|
||||
py::bind_vector<std::vector<PyContractQuantityInfo>>(
|
||||
m, "PyContractQuantityInfoList");
|
||||
// contract spec
|
||||
py::class_<PyContractSpecification>(m, "PyContractSpecification")
|
||||
.def(py::init<>())
|
||||
.PROP(PyContractSpecification, price_infos)
|
||||
.PROP(PyContractSpecification, quantity_infos)
|
||||
.PROP(PyContractSpecification, notional_quantity_infos)
|
||||
.def("__str__", &PyContractSpecification::to_string);
|
||||
// security
|
||||
py::class_<PySpot>(m, "PySpot")
|
||||
.def(py::init<>())
|
||||
.def("__str__", &PySpot::to_string);
|
||||
py::class_<PyFuture>(m, "PyFuture")
|
||||
.def(py::init<>())
|
||||
.PROP(PyFuture, expiration)
|
||||
.def("__str__", &PyFuture::to_string);
|
||||
py::class_<PyOption>(m, "PyOption")
|
||||
.def(py::init<>())
|
||||
.PROP(PyOption, expiration)
|
||||
.PROP(PyOption, is_call)
|
||||
.PROP(PyOption, strike)
|
||||
.def("__str__", &PyOption::to_string);
|
||||
py::class_<PySecurity, shared_ptr<PySecurity>>(m, "PySecurity")
|
||||
.def(py::init<>())
|
||||
.PROP(PySecurity, id)
|
||||
.PROP(PySecurity, exchange_id)
|
||||
.PROP(PySecurity, venue)
|
||||
.PROP(PySecurity, name)
|
||||
.PROP(PySecurity, currency)
|
||||
.PROP(PySecurity, group)
|
||||
.PROP(PySecurity, asset)
|
||||
.PROP(PySecurity, contract_spec)
|
||||
.PROP(PySecurity, spot)
|
||||
.PROP(PySecurity, future)
|
||||
.PROP(PySecurity, option)
|
||||
.def("__str__", &PySecurity::to_string);
|
||||
py::bind_vector<std::vector<shared_ptr<PySecurity>>>(m, "PySecurityList");
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Md
|
||||
//////////////////////////////////////////////////
|
||||
using BookSideGetter = PyBookSide &(PyBook::*)();
|
||||
using BookLevelGetter = PyBookLevel (PyBookSide::*)(int) const;
|
||||
// book
|
||||
py::class_<PyBookLevel>(m, "PyBookLevel")
|
||||
.def(py::init<>())
|
||||
.PROP(PyBookLevel, price)
|
||||
.PROP(PyBookLevel, quantity);
|
||||
py::class_<PyBookSide>(m, "PyBookSide")
|
||||
.def(py::init<>())
|
||||
.EXPLICIT_DEF(PyBookSide, get_level, BookLevelGetter)
|
||||
.DEF(PyBookSide, set_level)
|
||||
.PROP(PyBookSide, is_ascending)
|
||||
.def("__len__", &PyBookSide::size)
|
||||
.def("__str__", &PyBookSide::to_string);
|
||||
py::class_<PyBook, shared_ptr<PyBook>>(m, "PyBook")
|
||||
.def(py::init<>())
|
||||
.PROP(PyBook, venue)
|
||||
.PROP(PyBook, security_type)
|
||||
.PROP(PyBook, exchange_security_id)
|
||||
.EXPLICIT_DEF(PyBook, bids, BookSideGetter)
|
||||
.DEF(PyBook, set_bids)
|
||||
.EXPLICIT_DEF(PyBook, asks, BookSideGetter)
|
||||
.DEF(PyBook, set_asks)
|
||||
.def(
|
||||
"set_data", &PyBook::set_data, py::arg("exch_sec_id"),
|
||||
py::arg("venue"), py::arg("sec_type"), py::arg("bids"),
|
||||
py::arg("asks"))
|
||||
.def("__len__", &PyBook::size)
|
||||
.def("__str__", &PyBook::to_string);
|
||||
py::bind_vector<std::vector<shared_ptr<PyBook>>>(m, "PyBookList");
|
||||
// trade
|
||||
py::class_<PyTrade, shared_ptr<PyTrade>>(m, "PyTrade")
|
||||
.def(py::init<>())
|
||||
.PROP(PyTrade, venue)
|
||||
.PROP(PyTrade, security_type)
|
||||
.PROP(PyTrade, exchange_security_id)
|
||||
.PROP(PyTrade, price)
|
||||
.PROP(PyTrade, quantity)
|
||||
.PROP(PyTrade, aggressor_side)
|
||||
.PROP(PyTrade, timestamp);
|
||||
py::bind_vector<std::vector<shared_ptr<PyTrade>>>(m, "PyTradeList");
|
||||
// md bridge
|
||||
py::class_<MdBridge>(m, "MdBridge")
|
||||
.DEF_ARGS(MdBridge, on_securities, py::arg("securities"))
|
||||
.DEF_ARGS(MdBridge, on_books, py::arg("books"))
|
||||
.DEF_ARGS(MdBridge, on_trades, py::arg("trades"));
|
||||
// md provider
|
||||
py::class_<MdProvider, PyMdProvider>(m, "MdProvider")
|
||||
.def(py::init<>())
|
||||
.DEF_ARGS(MdProvider, start, py::arg("bridge"))
|
||||
.DEF(MdProvider, stop);
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Execution
|
||||
//////////////////////////////////////////////////
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user