#include "md_provider.h" #include "python_md/md_sink.h" #include "python_md/md_sink_impl.h" #include "python_md/py_book_level.h" #include "python_md/py_book_list.h" #include "python_md/py_book_side.h" #include "python_md/py_md_provider.h" #include "python_md/py_security.h" #include "python_md/py_security_list.h" #include "python_md/py_trade.h" #include using bts::md::py_interop::MdSinkImpl; using bts::md::py_interop::PyBook; using bts::md::py_interop::PyBookLevel; using bts::md::py_interop::PyBookList; using bts::md::py_interop::PyBookSide; using bts::md::py_interop::PyFuture; using bts::md::py_interop::PyOption; using bts::md::py_interop::PySecurity; using bts::md::py_interop::PySecurityList; using bts::md::py_interop::PySpot; using bts::md::py_interop::PyTrade; using bts::md::py_interop::PyTradeList; 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 EXPLICIT_DEF(type, member, member_type) \ def(_STR(member), (member_type) & type::member) // The PROP macro defines getter and setter methods according to // Binds a pseudo property with getter/setter methods. // Binds getter -> , and setter -> set_. #define PROP(type, name) \ def(_STR(name), &type::name).def(_STR(set_##name), &type::set_##name) // Define python module with c++ classes. PYBIND11_MODULE(python_md_module, m) { using BookSideGetter = PyBookSide &(PyBook::*)(); using BookLevelGetter = PyBookLevel (PyBookSide::*)(int) const; // security py::class_(m, "PySpot") .def(py::init<>()) .def("__str__", &PySpot::to_string); py::class_(m, "PyFuture") .def(py::init<>()) .PROP(PyFuture, expiration) .def("__str__", &PyFuture::to_string); py::class_(m, "PyOption") .def(py::init<>()) .PROP(PyOption, expiration) .PROP(PyOption, is_call) .PROP(PyOption, strike) .def("__str__", &PyOption::to_string); py::class_(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, spot) .PROP(PySecurity, future) .PROP(PySecurity, option) .def("__str__", &PySecurity::to_string); py::class_(m, "PySecurityList") .def(py::init<>()) .DEF(PySecurityList, append) .DEF(PySecurityList, clear) .def("__len__", &PySecurityList::size) .def("__str__", &PySecurityList::to_string); // book py::class_(m, "PyBookLevel") .def(py::init<>()) .PROP(PyBookLevel, price) .PROP(PyBookLevel, quantity); py::class_(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_(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("__len__", &PyBook::size) .def("__str__", &PyBook::to_string); py::class_(m, "PyBookList") .def(py::init<>()) .DEF(PyBookList, append) .DEF(PyBookList, clear) .def("__len__", &PyBookList::size) .def("__str__", &PyBookList::to_string); // trade py::class_(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::class_(m, "PyTradeList") .def(py::init<>()) .DEF(PyTradeList, append) .DEF(PyTradeList, clear) .def("__len__", &PyTradeList::size) .def("__str__", &PyTradeList::to_string); // md sink py::class_(m, "MdSink") .DEF(bts::md::py_interop::MdSink, on_securities) .DEF(bts::md::py_interop::MdSink, on_books) .DEF(bts::md::py_interop::MdSink, on_trades); py::class_( m, "MdSinkImpl") .def(py::init<>()) .DEF(bts::md::py_interop::MdSinkImpl, on_securities) .DEF(bts::md::py_interop::MdSinkImpl, on_books) .DEF(bts::md::py_interop::MdSinkImpl, on_trades); // md provider py::class_< bts::md::py_interop::MdProvider, bts::md::py_interop::PyMdProvider>( m, "MdProvider") .def(py::init<>()) .DEF(bts::md::py_interop::MdProvider, start) .DEF(bts::md::py_interop::MdProvider, stop); } #undef PROP #undef EXPLICIT_DEF #undef DEF #undef _STR