#pragma once #include "python_md/py_book_side.h" #include "spark/core/FMT_IMPL.h" #include "spark/io/json_formatter.h" #include #include namespace bts::md::py_interop { /** * @brief An L2 Order Book (both sides of the market). */ class PyBook { public: /** * Gets the exchange security id. This is typically the security's name * (e.g., "BTS-PERPETUAL", or "ESH5"). * * @note Should be unique for each security at a particular venue (i.e., a single * venue cannot have 2 securities with the same name). MUST be unique for each security * of the same type (e.g., SPOT, FUTURE, OPTION) at a particular venue. * * @note There is no "security_id" member because that is an internal id that * we generate elsewhere in c++ code. The python plugins just need to set the * venue and exchange_security_id. */ const std::string &exchange_security_id() const; /** * @brief Sets the exchange security id. * * @param value The new exchange security id. */ void set_exchange_security_id(std::string value); /** * @brief Gets the name of the venue (i.e., exchange). * * @return const std::string& The name of the venue. */ const std::string &venue() const; void set_venue(std::string value); /** * @brief Gets the name of the security type (e.g., "SPOT", "FUTURE", "OPTION"). * * @return const std::string& The name of the security type. */ const std::string &security_type() const; /** * @brief Sets the security type. * * @param value The new security type (should be "SPOT", "FUTURE", or "OPTION"). */ void set_security_type(std::string value); PyBookSide &bids(); const PyBookSide &bids() const; void set_bids(PyBookSide bids); PyBookSide &asks(); const PyBookSide &asks() const; void set_asks(PyBookSide asks); /** * @brief Gets the number of levels in the book (i.e., the number of prices * with resting orders). */ constexpr int size() const; std::string to_string() const; private: std::string venue_; std::string security_type_; std::string exchange_security_id_; PyBookSide bids_; PyBookSide asks_; }; void to_json(nlohmann::json &j, const PyBook &bk); void from_json(const nlohmann::json &j, PyBook &bk); } // namespace bts::md::py_interop ////////////////////////////////////////////////// // Definition ////////////////////////////////////////////////// FMT_IMPL(bts::md::py_interop::PyBook, bs, "{}", bts::io::json_formatter(bs)); namespace bts::md::py_interop { inline const std::string &PyBook::venue() const { return venue_; } inline void PyBook::set_venue(std::string value) { venue_ = std::move(value); } inline const std::string &PyBook::security_type() const { return security_type_; } inline void PyBook::set_security_type(std::string value) { security_type_ = std::move(value); } inline const std::string &PyBook::exchange_security_id() const { return exchange_security_id_; } inline void PyBook::set_exchange_security_id(std::string value) { exchange_security_id_ = std::move(value); } inline PyBookSide &PyBook::bids() { return bids_; } inline const PyBookSide &PyBook::bids() const { return bids_; } inline void PyBook::set_bids(PyBookSide bids) { bids_ = std::move(bids); } inline PyBookSide &PyBook::asks() { return asks_; } inline const PyBookSide &PyBook::asks() const { return asks_; } inline void PyBook::set_asks(PyBookSide asks) { asks_ = std::move(asks); } constexpr int PyBook::size() const { return bids_.size() + asks_.size(); } inline std::string PyBook::to_string() const { return fmt::format("{}", *this); } inline void to_json(nlohmann::json &j, const PyBook &bk) { j["bids"] = bk.bids(); j["asks"] = bk.asks(); } inline void from_json(const nlohmann::json &j, PyBook &bk) { bk.set_exchange_security_id(j.at("sec").get()); bk.set_venue(j.at("venue").get()); bk.set_security_type(j.at("typ").get()); bk.set_bids(j.at("bids").get()); bk.set_asks(j.at("asks").get()); } } // namespace bts::md::py_interop