Leka
A low-latency C++20 price-time-priority limit order book and matching engine
▶ Replay viewer
Loading...
Searching...
No Matches
order_book_snapshot.hpp
Go to the documentation of this file.
1// include/lob/book/order_book_snapshot.hpp
2#ifndef ORDER_BOOK_SNAPSHOT_HPP
3#define ORDER_BOOK_SNAPSHOT_HPP
4
5#include "lob/types/price.hpp"
6#include "lob/types/quantity.hpp"
7
8#include <cstddef>
9#include <cstdint>
10#include <vector>
11
12namespace lob {
13
14class OrderBook;
15
16/**
17 * @file
18 * @brief Point-in-time view of book state for consumers outside the engine.
19 *
20 * This is the single data model shared by every out-of-engine consumer: the
21 * offline recorder that writes a replay to disk, and (later) a live server
22 * publishing to a browser. Both produce the same fields, so a visualization
23 * written against a recorded file works unchanged against a live feed.
24 *
25 * The engine never produces one of these on its own. A caller captures a
26 * snapshot *between* events, never inside MatchingEngine::processEvent(), so
27 * nothing here can appear on the matching hot path. See ARCH_DECISIONS.md
28 * ADR-010 for the measurement proving that separation holds.
29 */
30
31/** @brief One aggregated price level as seen from outside the engine. */
33 /** Price in raw integer units (1/10000, matching Price's own encoding). */
34 std::uint64_t priceRaw = 0;
35 /** Total resting quantity at this level. */
36 std::uint64_t quantity = 0;
37 /** Number of distinct resting orders at this level. */
38 std::size_t orderCount = 0;
39};
40
41/**
42 * @brief Top-of-book depth plus the counters a viewer needs for context.
43 *
44 * Levels are ordered best-first on each side: bids descending in price, asks
45 * ascending. A side with no resting liquidity yields an empty vector and a
46 * zero best price, zero being Price's own reserved invalid value.
47 */
49 /** Number of events applied to the book before this snapshot. */
50 std::uint64_t sequence = 0;
51 /** Engine timestamp of the most recently applied event, in nanoseconds. */
52 std::uint64_t tsNs = 0;
53
54 /** Best bid price in raw units, or 0 when no bids rest. */
55 std::uint64_t bestBidRaw = 0;
56 /** Best ask price in raw units, or 0 when no asks rest. */
57 std::uint64_t bestAskRaw = 0;
58
59 /** Top levels, best first. */
60 std::vector<BookSnapshotLevel> bids;
61 std::vector<BookSnapshotLevel> asks;
62
63 /** Occupied level count per side, which may exceed the captured depth. */
64 std::size_t bidLevelCount = 0;
65 std::size_t askLevelCount = 0;
66
67 /** Clears the vectors without releasing their capacity, so a caller
68 * reusing one snapshot across captures stops allocating after warmup. */
69 void reset() {
70 bids.clear();
71 asks.clear();
72 sequence = 0;
73 tsNs = 0;
74 bestBidRaw = 0;
75 bestAskRaw = 0;
76 bidLevelCount = 0;
77 askLevelCount = 0;
78 }
79};
80
81} // namespace lob
82
83#endif // ORDER_BOOK_SNAPSHOT_HPP
Owns resting limit orders and maintains their book indexes.
One aggregated price level as seen from outside the engine.
std::size_t orderCount
Number of distinct resting orders at this level.
std::uint64_t priceRaw
Price in raw integer units (1/10000, matching Price's own encoding).
std::uint64_t quantity
Total resting quantity at this level.
Top-of-book depth plus the counters a viewer needs for context.
std::uint64_t bestBidRaw
Best bid price in raw units, or 0 when no bids rest.
std::uint64_t sequence
Number of events applied to the book before this snapshot.
std::size_t bidLevelCount
Occupied level count per side, which may exceed the captured depth.
std::uint64_t tsNs
Engine timestamp of the most recently applied event, in nanoseconds.
std::uint64_t bestAskRaw
Best ask price in raw units, or 0 when no asks rest.
std::vector< BookSnapshotLevel > bids
Top levels, best first.
void reset()
Clears the vectors without releasing their capacity, so a caller reusing one snapshot across captures...