94 lines
2.4 KiB
C++
94 lines
2.4 KiB
C++
#pragma once
|
|
|
|
#include "python_md/py_book_level.h"
|
|
#include "spark/core/FMT_IMPL.h"
|
|
#include "spark/io/json_formatter.h"
|
|
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
namespace bts::md::py_interop {
|
|
|
|
/**
|
|
* @brief L2 Order Book for one side of the market (i.e., bids or offers).
|
|
*
|
|
* @details Bid market levels should be in DESCENDING order by price, while Ask
|
|
* levels (offers) should be in ASCENDING order. Another way to think about it
|
|
* is the price levels are sorted by their distance from the opposite market.
|
|
*/
|
|
class PyBookSide {
|
|
public:
|
|
constexpr bool is_ascending();
|
|
void set_is_ascending(bool is_ascending);
|
|
|
|
constexpr PyBookLevel get_level(int index);
|
|
constexpr PyBookLevel get_level(int index) const;
|
|
void set_level(PyBookLevel level);
|
|
|
|
constexpr int size() const;
|
|
|
|
std::string to_string() const;
|
|
std::string to_string_pretty() const;
|
|
|
|
private:
|
|
std::vector<PyBookLevel> levels_;
|
|
bool is_ascending_;
|
|
|
|
friend void to_json(nlohmann::json &j, const PyBookSide &bs);
|
|
friend void from_json(const nlohmann::json &j, PyBookSide &bs);
|
|
};
|
|
|
|
void to_json(nlohmann::json &j, const PyBookSide &bs);
|
|
void from_json(const nlohmann::json &j, PyBookSide &bs);
|
|
|
|
} // namespace bts::md::py_interop
|
|
|
|
//////////////////////////////////////////////////
|
|
// Definition
|
|
//////////////////////////////////////////////////
|
|
|
|
FMT_IMPL(
|
|
bts::md::py_interop::PyBookSide, bs, "{}", bts::io::json_formatter(bs));
|
|
|
|
namespace bts::md::py_interop {
|
|
|
|
constexpr bool PyBookSide::is_ascending() { return is_ascending_; }
|
|
|
|
inline void PyBookSide::set_is_ascending(bool is_ascending) {
|
|
is_ascending_ = is_ascending;
|
|
}
|
|
|
|
constexpr PyBookLevel PyBookSide::get_level(int index) {
|
|
if (!is_ascending_) {
|
|
index = levels_.size() - 1 - index;
|
|
}
|
|
return levels_.at(index);
|
|
}
|
|
|
|
constexpr PyBookLevel PyBookSide::get_level(int index) const {
|
|
if (!is_ascending_) {
|
|
index = levels_.size() - 1 - index;
|
|
}
|
|
return levels_.at(index);
|
|
}
|
|
|
|
constexpr int PyBookSide::size() const { return levels_.size(); }
|
|
|
|
inline std::string PyBookSide::to_string() const {
|
|
return fmt::format("{}", *this);
|
|
}
|
|
|
|
inline void to_json(nlohmann::json &j, const PyBookSide &bs) { j = bs.levels_; }
|
|
|
|
inline void from_json(const nlohmann::json &j, PyBookSide &bs) {
|
|
j.get_to(bs.levels_);
|
|
}
|
|
|
|
inline std::ostream &operator<<(
|
|
std::ostream &out, const PyBookSide &bs) noexcept {
|
|
out << fmt::format("{}", bs);
|
|
return out;
|
|
}
|
|
|
|
} // namespace bts::md::py_interop
|