Skip to content
Daniel Esambu
Work
Work
Projects
Things I've built
Leka
A C++20 matching engine
Latency Lab
(opens in a new tab)
A C++ course I wrote
Resume
The formal version
Writing
Writing
Poetry
Poems, shelved by what they're about
Essays
Notes and essays
Math
Math
Notes
Notes I've written up
IB papers
Worked exam questions
Calculus lessons
(opens in a new tab)
On YouTube
Life
Life
Photos
Things I stopped to look at
Training
Reps, not phases
Modeling
Measurements and photos
Resume
Say hi
Say hi
Resume
Menu
Leka
A low-latency C++20 price-time-priority limit order book and matching engine
Replay viewer
Toggle main menu visibility
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
12
namespace
lob {
13
14
class
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. */
32
struct
BookSnapshotLevel
{
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
*/
48
struct
BookSnapshot
{
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
lob::OrderBook
Owns resting limit orders and maintains their book indexes.
Definition
order_book.hpp:29
lob::BookSnapshotLevel
One aggregated price level as seen from outside the engine.
Definition
order_book_snapshot.hpp:32
lob::BookSnapshotLevel::orderCount
std::size_t orderCount
Number of distinct resting orders at this level.
Definition
order_book_snapshot.hpp:38
lob::BookSnapshotLevel::priceRaw
std::uint64_t priceRaw
Price in raw integer units (1/10000, matching Price's own encoding).
Definition
order_book_snapshot.hpp:34
lob::BookSnapshotLevel::quantity
std::uint64_t quantity
Total resting quantity at this level.
Definition
order_book_snapshot.hpp:36
lob::BookSnapshot
Top-of-book depth plus the counters a viewer needs for context.
Definition
order_book_snapshot.hpp:48
lob::BookSnapshot::bestBidRaw
std::uint64_t bestBidRaw
Best bid price in raw units, or 0 when no bids rest.
Definition
order_book_snapshot.hpp:55
lob::BookSnapshot::sequence
std::uint64_t sequence
Number of events applied to the book before this snapshot.
Definition
order_book_snapshot.hpp:50
lob::BookSnapshot::bidLevelCount
std::size_t bidLevelCount
Occupied level count per side, which may exceed the captured depth.
Definition
order_book_snapshot.hpp:64
lob::BookSnapshot::tsNs
std::uint64_t tsNs
Engine timestamp of the most recently applied event, in nanoseconds.
Definition
order_book_snapshot.hpp:52
lob::BookSnapshot::bestAskRaw
std::uint64_t bestAskRaw
Best ask price in raw units, or 0 when no asks rest.
Definition
order_book_snapshot.hpp:57
lob::BookSnapshot::bids
std::vector< BookSnapshotLevel > bids
Top levels, best first.
Definition
order_book_snapshot.hpp:60
lob::BookSnapshot::reset
void reset()
Clears the vectors without releasing their capacity, so a caller reusing one snapshot across captures...
Definition
order_book_snapshot.hpp:69
include
lob
book
order_book_snapshot.hpp
Generated by
1.18.0