Files
spark/src/lib/domain/Book.h
T

166 lines
4.2 KiB
C++

#pragma once
#include "BookLevel.h"
#include "BookLevelComp.h"
#include "BookSide.h"
#include "SecurityID.h"
#include "Side.h"
#include <boost/container/small_vector.hpp>
namespace bts::domain {
template <class BS>
class BookT {
public:
[[nodiscard]] static constexpr BookT<BS> empty() noexcept;
[[nodiscard]] static constexpr BookT<BS> empty(
SecurityID security_id) noexcept;
[[nodiscard]] static constexpr size_t max_depth();
[[nodiscard]] constexpr std::size_t size() const noexcept;
[[nodiscard]] SecurityID security_id() const;
void set_security_id(SecurityID);
template <class OtherBook>
void merge_with(OtherBook const &b);
void clear_to_end(std::size_t level = 0);
void clear_after_empty();
[[nodiscard]] bool is_sorted() const;
template <class OtherBook>
static BookT merge(BookT const & /*b1*/, OtherBook const & /*b2*/);
void reset_merged_quantity();
void reset_n_orders();
template <class Container>
void apply_changes(Side side, const Container &changes);
/**
* Removes price levels of the non-dominant side that cross the dominant
* side
*/
void remove_crosses(Side dominant_side);
BS &operator[](Side s);
const BS &operator[](Side s) const;
bool operator==(BookT const &rhs) const;
private:
SecurityID _security_id;
std::array<BS, 2> _books;
};
using Book = BookT<BookSide>;
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
template <class BS>
constexpr BookT<BS> BookT<BS>::empty() noexcept {
// Be sure to change the impl if BookLevel is optimized such that
// the quantity no longer defaults to 0.
return {};
}
template <class BS>
constexpr BookT<BS> BookT<BS>::empty(SecurityID security_id) noexcept {
// Be sure to change the impl if BookLevel is optimized such that
// the quantity no longer defaults to 0.
auto rv = BookT<BS>::empty();
rv.set_security_id(security_id);
return rv;
}
template <class BS>
constexpr std::size_t BookT<BS>::max_depth() {
return BS::max_depth();
}
template <class BS>
constexpr std::size_t BookT<BS>::size() const noexcept {
return _books.size();
}
template <class BS>
SecurityID BookT<BS>::security_id() const {
return _security_id;
}
template <class BS>
void BookT<BS>::set_security_id(SecurityID id) {
_security_id = id;
}
template <class BS>
inline BS &BookT<BS>::operator[](Side s) {
return _books[s.as_index()];
}
template <class BS>
inline const BS &BookT<BS>::operator[](Side s) const {
return _books[s.as_index()];
}
template <class BS>
inline bool BookT<BS>::operator==(BookT const &rhs) const {
return _books == rhs._books;
}
template <class BS>
template <class OtherBook>
void BookT<BS>::merge_with(OtherBook const &b) {
operator[](Side::Buy).merge_with(Side::Buy, b[Side::Buy]);
operator[](Side::Sell).merge_with(Side::Sell, b[Side::Sell]);
}
template <class BS>
template <class OtherBook>
BookT<BS> BookT<BS>::merge(BookT const &b1, OtherBook const &b2) {
auto result = b1;
result.merge_with(b2);
return result;
}
template <class BS>
void BookT<BS>::reset_merged_quantity() {
Side::foreach ([&](Side s) { operator[](s).reset_merged_quantity(); });
}
template <class BS>
void BookT<BS>::reset_n_orders() {
Side::foreach ([&](Side s) { operator[](s).reset_n_orders(); });
}
template <class BS>
void BookT<BS>::remove_crosses(Side dominant_side) {
(*this)[dominant_side.other()].remove_crosses(
dominant_side.other(), (*this)[dominant_side][0].price);
}
template <class BS>
void BookT<BS>::clear_to_end(std::size_t level) {
Side::foreach ([&](Side s) {
operator[](s).clear_to_end(static_cast<std::uint8_t>(level));
});
}
template <class BS>
void BookT<BS>::clear_after_empty() {
Side::foreach ([&](Side s) { operator[](s).clear_after_empty(); });
}
template <class BS>
bool BookT<BS>::is_sorted() const {
return Side::forboth([&](Side s) { return operator[](s).is_sorted(s); });
}
template <class BS>
template <class Container>
void BookT<BS>::apply_changes(Side side, const Container &changes) {
_books[static_cast<std::size_t>(side.value)].apply_changes(side, changes);
}
} // namespace bts::domain