feat: Add support for python md plugins
This commit is contained in:
1 parent
2ee020d505
commit
81b94a3857
47 files changed
+2276
-10
No files matched your search
@@ -0,0 +1,146 @@
|
||||
#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 <pybind11/pybind11.h>
|
||||
|
||||
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 <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)
|
||||
// namespace blah {
|
||||
// typedef bts::md::py_interop::PyBookSide & (bts::md::py_interop::PyBook::*bids());
|
||||
// }
|
||||
|
||||
// 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_<bts::md::py_interop::PySpot>(m, "PySpot")
|
||||
.def(py::init<>())
|
||||
.def("__str__", &PySpot::to_string);
|
||||
py::class_<bts::md::py_interop::PyFuture>(m, "PyFuture")
|
||||
.def(py::init<>())
|
||||
.PROP(PyFuture, expiration)
|
||||
.def("__str__", &PyFuture::to_string);
|
||||
py::class_<bts::md::py_interop::PyOption>(m, "PyOption")
|
||||
.def(py::init<>())
|
||||
.PROP(PyOption, expiration)
|
||||
.PROP(PyOption, is_call)
|
||||
.PROP(PyOption, strike)
|
||||
.def("__str__", &PyOption::to_string);
|
||||
py::class_<bts::md::py_interop::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, spot)
|
||||
.PROP(PySecurity, future)
|
||||
.PROP(PySecurity, option)
|
||||
.def("__str__", &PySecurity::to_string);
|
||||
py::class_<PySecurityList>(m, "PySecurityList")
|
||||
.def(py::init<>())
|
||||
.DEF(PySecurityList, append)
|
||||
.DEF(PySecurityList, clear)
|
||||
.def("__len__", &PySecurityList::size)
|
||||
.def("__str__", &PySecurityList::to_string);
|
||||
// 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_<bts::md::py_interop::PyBook>(m, "PyBook")
|
||||
.def(py::init<>())
|
||||
.PROP(PyBook, venue)
|
||||
.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_<PyBookList>(m, "PyBookList")
|
||||
.def(py::init<>())
|
||||
.DEF(PyBookList, append)
|
||||
.DEF(PyBookList, clear)
|
||||
.def("__len__", &PyBookList::size)
|
||||
.def("__str__", &PyBookList::to_string);
|
||||
// trade
|
||||
py::class_<PyTrade>(m, "PyTrade")
|
||||
.def(py::init<>())
|
||||
.PROP(PyTrade, venue)
|
||||
.PROP(PyTrade, exchange_security_id)
|
||||
.PROP(PyTrade, price)
|
||||
.PROP(PyTrade, quantity)
|
||||
.PROP(PyTrade, aggressor_side)
|
||||
.PROP(PyTrade, timestamp);
|
||||
py::class_<PyTradeList>(m, "PyTradeList")
|
||||
.def(py::init<>())
|
||||
.DEF(PyTradeList, append)
|
||||
.DEF(PyTradeList, clear)
|
||||
.def("__len__", &PyTradeList::size)
|
||||
.def("__str__", &PyTradeList::to_string);
|
||||
// md sink
|
||||
py::class_<bts::md::py_interop::MdSink>(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_<bts::md::py_interop::MdSinkImpl, bts::md::py_interop::MdSink>(
|
||||
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 DEF
|
||||
#undef _STR
|
||||
Reference in new issue
Block a user