16#include "fastmm/core/config_macros.hpp"
17#include "fastmm/core/containers/static_vector.hpp"
28template <std::
size_t kMaxDepth = 256>
30 static_assert(kMaxDepth >= 2);
33 static constexpr std::size_t
kDepth = kMaxDepth;
40 void clear() noexcept {
43 truncated_[0] = truncated_[1] =
false;
44 have_snapshot_ =
false;
58 auto& v = levels(side);
60 const std::size_t i = locate(v, side, price, found);
70 if (qty.is_zero())
return true;
71 if (FASTMM_UNLIKELY(v.size() >= kMaxDepth)) {
72 truncated_[
static_cast<std::size_t
>(side)] =
true;
73 if (i == 0)
return false;
76 std::copy(v.begin() + 1, v.begin() + i, v.begin());
77 v[i - 1] = Level{price, qty};
80 v.insert_at(i, Level{price, qty});
91 auto& v = levels(side);
93 const std::size_t i = locate(v, side, price, found);
99 return v.insert_at(i,
Level{price, qty});
102 auto& v = levels(side);
104 const std::size_t i = locate(v, side, price, found);
116 std::span<const Level> asks,
122 have_snapshot_ =
true;
132 if (d.is_snapshot()) {
139 if (FASTMM_UNLIKELY((d.hdr.flags & kCut) != 0)) trim_truncated(d);
140 seq_ = d.last_update_id;
145 void set_seq(std::uint64_t s)
noexcept { seq_ = s; }
152 return bids_.empty() ?
Level{} : bids_.back();
155 return asks_.empty() ?
Level{} : asks_.back();
158 if (bids_.empty() || asks_.empty())
return Price{};
159 return Price::from_raw((bids_.back().price.raw + asks_.back().price.raw) / 2);
162 if (bids_.empty() || asks_.empty())
return Price{};
163 return asks_.back().price - bids_.back().price;
167 const auto& v = levels(side);
168 return i < v.size() ? v[v.size() - 1 - i] :
Level{};
170 [[nodiscard]] std::size_t
depth(
Side side)
const noexcept {
return levels(side).size(); }
171 [[nodiscard]] std::size_t
depth() const noexcept {
return bids_.size() + asks_.size(); }
175 const auto& v = levels(side);
177 for (std::size_t i = v.size(); i > 0; --i) {
178 const Level& l = v[i - 1];
187 const auto& v = levels(side);
189 for (std::size_t i = v.size(); i > 0; --i) {
191 if (cum >= qty)
return v[i - 1].price;
198 const auto& v = levels(side);
199 for (std::size_t i = v.size(); i > 0; --i) {
200 if constexpr (std::is_same_v<std::invoke_result_t<F, const Level&>,
bool>) {
201 if (!f(v[i - 1]))
return;
207 template <std::
size_t N>
210 const auto& v = levels(side);
211 const std::size_t n = v.size() < N ? v.size() : N;
212 for (std::size_t i = 0; i < n; ++i) out.
levels[i] = v[v.size() - 1 - i];
213 out.
count =
static_cast<std::uint32_t
>(n);
218 return !bids_.empty() && !asks_.empty() && bids_.back().price >= asks_.back().price;
222 return crossed_since_.
valid() ? now - crossed_since_ :
Duration{};
225 return truncated_[
static_cast<std::size_t
>(side)];
227 [[nodiscard]]
bool truncated() const noexcept {
return truncated_[0] || truncated_[1]; }
228 [[nodiscard]]
bool has_snapshot() const noexcept {
return have_snapshot_; }
230 return have_snapshot_ && !bids_.empty() && !asks_.empty() && !
crossed();
232 [[nodiscard]] std::uint64_t
seq() const noexcept {
return seq_; }
236 using Levels = StaticVector<Level, kMaxDepth + kOwnLevels>;
237 [[nodiscard]]
const Levels&
raw(
Side side)
const noexcept {
return levels(side); }
240 FASTMM_FORCE_INLINE
Levels& levels(
Side s)
noexcept {
return s ==
Side::Buy ? bids_ : asks_; }
241 FASTMM_FORCE_INLINE
const Levels& levels(
Side s)
const noexcept {
247 FASTMM_FORCE_INLINE
static std::size_t locate(
const Levels& v,
250 bool& found)
noexcept {
251 std::size_t i = v.size();
255 const Price p = v[i - 1].price;
260 if (
better(side, price, p))
return i;
263 if (stop == 0)
return 0;
265 std::size_t hi = stop;
267 const std::size_t
mid = lo + (hi - lo) / 2;
274 found = lo < v.size() && v[lo].price == price;
281 [[gnu::noinline, gnu::cold]]
void trim_truncated(
const BookDeltaMsg& d)
noexcept {
285 void trim_behind(
Side side, std::span<const Level> carried)
noexcept {
286 if (carried.empty())
return;
287 Price bound = carried[0].price;
288 for (
const Level& l : carried) {
289 if (
better(side, bound, l.price)) bound = l.price;
291 auto& v = levels(side);
293 while (k < v.size() &&
better(side, bound, v[k].price)) ++k;
296 truncated_[
static_cast<std::size_t
>(side)] =
true;
300 void load_side(
Side side, std::span<const Level> in)
noexcept {
301 auto& v = levels(side);
302 const std::size_t n = in.size();
304 if (n > 0 && in[0].qty.is_positive()) {
305 order = n == 1 ? 1 :
better(side, in[0].price, in[1].price) ? 1 : -1;
306 for (std::size_t k = 1; k < n && order != 0; ++k) {
308 in[k].qty.is_positive() && (order == 1 ?
better(side, in[k - 1].price, in[k].price)
309 :
better(side, in[k].price, in[k - 1].price));
314 for (
const Level& l : in)
apply_level(side, l.price, l.qty);
317 const std::size_t m = n < kMaxDepth ? n : kMaxDepth;
318 v.resize_uninitialized(m);
320 for (std::size_t k = 0; k < m; ++k) v[m - 1 - k] = in[k];
322 for (std::size_t k = 0; k < m; ++k) v[k] = in[n - m + k];
324 if (n > m) truncated_[
static_cast<std::size_t
>(side)] =
true;
326 void update_crossed(Timestamp ts)
noexcept {
328 if (!crossed_since_.
valid()) crossed_since_ = ts;
330 crossed_since_ = Timestamp{};
336 std::uint64_t seq_ = 0;
337 Timestamp last_update_{};
338 Timestamp crossed_since_{};
339 bool truncated_[2] = {
false,
false};
340 bool have_snapshot_ =
false;
343static_assert(BookView<L2Book<256>>);
BookView: the read interface every book implementation (L2Book, L3Book::to_l2 view,...
void mark_snapshot() noexcept
Definition l2_book.hpp:148
std::size_t depth() const noexcept
Definition l2_book.hpp:171
Price price_for_qty(Side side, Qty qty) const noexcept
Price of the level at which cumulative quantity from the best reaches qty; Price{} if the side is too...
Definition l2_book.hpp:186
void clear() noexcept
Definition l2_book.hpp:41
bool add_own(Side side, Price price, Qty qty, bool &inserted) noexcept
Definition l2_book.hpp:91
Price mid() const noexcept
Definition l2_book.hpp:157
std::uint64_t seq() const noexcept
Definition l2_book.hpp:232
bool is_valid() const noexcept
Definition l2_book.hpp:229
Qty qty_at_or_better(Side side, Price limit) const noexcept
Total quantity on side at prices at-or-better than limit.
Definition l2_book.hpp:174
Level level(Side side, std::size_t i) const noexcept
i == 0 is best. Level{} if out of range.
Definition l2_book.hpp:166
Timestamp last_update() const noexcept
Definition l2_book.hpp:233
StaticVector< Level, kMaxDepth+kOwnLevels > Levels
Raw access (worst..best order) for tests/tools.
Definition l2_book.hpp:236
void for_each_level(Side side, F &&f) const noexcept
Visits levels best-first. F(const Level&) may return bool (false stops) or void.
Definition l2_book.hpp:197
static constexpr std::size_t kLinearScan
Definition l2_book.hpp:36
Price spread() const noexcept
Definition l2_book.hpp:161
std::size_t depth(Side side) const noexcept
Definition l2_book.hpp:170
bool truncated(Side side) const noexcept
Definition l2_book.hpp:224
bool crossed() const noexcept
Definition l2_book.hpp:217
bool truncated() const noexcept
Definition l2_book.hpp:227
void set_seq(std::uint64_t s) noexcept
Definition l2_book.hpp:146
void apply_snapshot(std::span< const Level > bids, std::span< const Level > asks, std::uint64_t seq, Timestamp ts) noexcept
O(levels) for a snapshot sorted best-first or worst-first with no empty level (what every parser send...
Definition l2_book.hpp:116
FASTMM_FORCE_INLINE Level best_ask() const noexcept
Definition l2_book.hpp:154
FASTMM_FORCE_INLINE bool apply_level(Side side, Price price, Qty qty) noexcept
Sets/updates/deletes (qty == 0) one level.
Definition l2_book.hpp:52
static constexpr std::size_t kDepth
Definition l2_book.hpp:35
void remove_own(Side side, Price price, Qty qty, bool inserted) noexcept
Definition l2_book.hpp:102
const Levels & raw(Side side) const noexcept
Definition l2_book.hpp:237
void set_last_update(Timestamp t) noexcept
Definition l2_book.hpp:147
Duration crossed_for(Timestamp now) const noexcept
How long the book has been crossed as of now (zero if not crossed).
Definition l2_book.hpp:221
static constexpr std::size_t kOwnLevels
Our own quantity shown in a book that is otherwise the market's (the engine's view of a simulated fee...
Definition l2_book.hpp:90
bool has_snapshot() const noexcept
Definition l2_book.hpp:228
FASTMM_FORCE_INLINE Level best_bid() const noexcept
Definition l2_book.hpp:151
L2Book() noexcept=default
void apply_delta(const BookDeltaMsg &d) noexcept
Applies a BookDeltaMsg (or snapshot with kSnapshot).
Definition l2_book.hpp:131
TopLevels< N > top(Side side) const noexcept
Definition l2_book.hpp:208
Event bus message structs (5.5).
Definition strategy.hpp:23
Side
Definition enums.hpp:12
Fixed< struct QtyTag > Qty
Definition fixed_point.hpp:333
constexpr bool at_or_better(Side s, Price p, Price q) noexcept
true if p is at least as good as q from the point of view of side s.
Definition book_view.hpp:45
Fixed< struct PriceTag > Price
Definition fixed_point.hpp:332
constexpr bool better(Side s, Price p, Price q) noexcept
Definition book_view.hpp:48
Bids then asks follow the fixed part as a flexible array of Level (bid_count + ask_count).
Definition messages.hpp:100
static constexpr Fixed from_raw(std::int64_t r) noexcept
Definition fixed_point.hpp:155
One price level. Shared by book messages and the L2 book.
Definition messages.hpp:31
Price price
Definition messages.hpp:32
Qty qty
Definition messages.hpp:33
constexpr bool valid() const noexcept
Definition time.hpp:55
Fixed-size result of top<N>().
Definition book_view.hpp:20
std::uint32_t count
Definition book_view.hpp:22
Level levels[N]
Definition book_view.hpp:21