Slick OrderBook 1.1.0
High-Performance C++23 OrderBook Library
Loading...
Searching...
No Matches
events.hpp
Go to the documentation of this file.
1// Copyright 2026 Slick Quant
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
8#include <array>
9
11
13enum ChangeFlag : uint8_t {
14 PriceChanged = 0x01, // Price level was added/moved
15 QuantityChanged = 0x02, // Quantity at level changed
16 LastInBatch = 0x04, // Last update in external batch
17};
18
22 Timestamp timestamp; // Update timestamp
23 uint64_t seq_num; // Exchange sequence number (0 = not tracked)
24 Price price; // Price level
25 Quantity quantity; // New total quantity at this level (0 = delete)
26 SymbolId symbol; // Symbol identifier
27 uint16_t level_index; // 0-based index in sorted order (0 = best, 1 = second best, etc.)
28 uint16_t num_orders; // number of orders on the level
29 uint8_t change_flags; // Bitset of ChangeFlag
30 Side side; // Buy or Sell
31
32 PriceLevelUpdate() noexcept = default;
33
34 PriceLevelUpdate(Timestamp ts, SymbolId sym, Side s, Price p, Quantity q, uint16_t order_count = 0,
35 uint16_t idx = 0, uint8_t flags = 0, uint64_t seq = 0) noexcept
36 : timestamp(ts), seq_num(seq), price(p), quantity(q), symbol(sym),
37 level_index(idx), num_orders(order_count), change_flags(flags), side(s) {}
38
40 [[nodiscard]] constexpr bool isDelete() const noexcept {
41 return quantity == 0;
42 }
43
45 [[nodiscard]] constexpr bool priceChanged() const noexcept {
46 return (change_flags & PriceChanged) != 0;
47 }
48
50 [[nodiscard]] constexpr bool quantityChanged() const noexcept {
51 return (change_flags & QuantityChanged) != 0;
52 }
53
55 [[nodiscard]] constexpr bool isTopN(uint16_t n) const noexcept {
56 return level_index < n;
57 }
58
60 [[nodiscard]] constexpr bool isLastInBatch() const noexcept {
61 return (change_flags & LastInBatch) != 0;
62 }
63};
64
68 Timestamp timestamp; // Update timestamp
69 uint64_t seq_num; // Exchange sequence number (0 = not tracked)
70 OrderId order_id; // Unique order identifier
71 Price price; // Order price
72 Quantity quantity; // Order quantity (0 = delete)
73 Price old_price; // Old order price
74 Quantity old_qty; // Old order quantity
75 SymbolId symbol; // Symbol identifier
76 uint16_t price_level_index; // Index of the price level this order belongs to
77 uint64_t priority; // Order priority (typically timestamp or sequence number)
78 Side side; // Buy or Sell
79 uint8_t change_flags; // Bitset of ChangeFlag
80
81 OrderUpdate() noexcept = default;
82
84 uint16_t idx = 0, uint64_t prio = 0, uint8_t flags = 0, uint64_t seq = 0) noexcept
85 : timestamp(ts), seq_num(seq), order_id(id), price(p), quantity(q), old_price(op), old_qty(oq), symbol(sym),
86 price_level_index(idx), priority(prio), side(s), change_flags(flags) {}
87
89 [[nodiscard]] constexpr bool isDelete() const noexcept {
90 return quantity == 0;
91 }
92
94 [[nodiscard]] constexpr bool priceChanged() const noexcept {
95 return (change_flags & PriceChanged) != 0;
96 }
97
99 [[nodiscard]] constexpr bool quantityChanged() const noexcept {
100 return (change_flags & QuantityChanged) != 0;
101 }
102
104 [[nodiscard]] constexpr bool isTopN(uint16_t n) const noexcept {
105 return price_level_index < n;
106 }
107
109 [[nodiscard]] constexpr bool isLastInBatch() const noexcept {
110 return (change_flags & LastInBatch) != 0;
111 }
112};
113
115struct Trade {
116 SymbolId symbol; // Symbol identifier
117 Price price; // Trade price
118 Quantity quantity; // Trade quantity
119 Timestamp timestamp; // Trade timestamp
120 OrderId aggressive_order_id; // OrderId of the aggressive (incoming) order
121 OrderId passive_order_id; // OrderId of the passive (resting) order
122 Side aggressor_side; // Side that initiated the trade (Buy = uptick, Sell = downtick)
123 uint8_t change_flags; // Bitset of ChangeFlag
124
125 Trade() noexcept : symbol(0), price(0), quantity(0), timestamp(0),
126 aggressive_order_id(0), passive_order_id(0),
127 aggressor_side(Side::Buy), change_flags(0) {}
128
129 Trade(SymbolId sym, Price p, Quantity q, Timestamp ts, Side aggressor,
130 OrderId passive = 0, OrderId aggressive = 0, uint8_t flags = 0) noexcept
131 : symbol(sym), price(p), quantity(q), timestamp(ts),
132 aggressive_order_id(aggressive), passive_order_id(passive),
133 aggressor_side(aggressor), change_flags(flags) {}
134
136 [[nodiscard]] constexpr bool isLastInBatch() const noexcept {
137 return (change_flags & LastInBatch) != 0;
138 }
139};
140
142struct TopOfBook {
143 SymbolId symbol; // Symbol identifier
144 Price best_bid; // Best bid price (highest buy price)
145 Quantity bid_quantity; // Total quantity at best bid
146 Price best_ask; // Best ask price (lowest sell price)
147 Quantity ask_quantity; // Total quantity at best ask
148 Timestamp timestamp; // Snapshot timestamp
149 std::array<uint8_t, 2> change_flags {0, 0}; // Bitset of ChangeFlag
150
151 TopOfBook() noexcept
152 : symbol(0), best_bid(0), bid_quantity(0), best_ask(0), ask_quantity(0), timestamp(0) {}
153
154 TopOfBook(SymbolId sym, Price bid, Quantity bid_qty, Price ask, Quantity ask_qty, Timestamp ts) noexcept
155 : symbol(sym), best_bid(bid), bid_quantity(bid_qty), best_ask(ask), ask_quantity(ask_qty), timestamp(ts) {}
156
158 [[nodiscard]] Price spread() const noexcept {
159 return (best_ask > best_bid) ? (best_ask - best_bid) : 0;
160 }
161
163 [[nodiscard]] Price midPrice() const noexcept {
164 return (best_bid + best_ask) / 2;
165 }
166
168 [[nodiscard]] bool isCrossed() const noexcept {
169 return best_bid >= best_ask && best_bid > 0 && best_ask > 0;
170 }
171
173 [[nodiscard]] bool hasBid() const noexcept {
174 return best_bid > 0 && bid_quantity > 0;
175 }
176
178 [[nodiscard]] bool hasAsk() const noexcept {
179 return best_ask > 0 && ask_quantity > 0;
180 }
181
183 [[nodiscard]] bool isValid() const noexcept {
184 return hasBid() && hasAsk();
185 }
186};
187
#define SLICK_NAMESPACE_END
Definition config.hpp:127
#define SLICK_NAMESPACE_BEGIN
Definition config.hpp:126
int64_t Quantity
Quantity type - volume or size.
Definition types.hpp:21
int64_t Price
Price type - fixed-point representation Example: For 4 decimal places, price = actual_price * 10000 T...
Definition types.hpp:18
uint64_t OrderId
Unique order identifier.
Definition types.hpp:24
Side
Order side - using enum for easy array indexing.
Definition types.hpp:35
ChangeFlag
Change flags for price level and order updates (regular enum for bitwise operations)
Definition events.hpp:13
uint16_t SymbolId
Symbol identifier - can be hash or sequential ID.
Definition types.hpp:27
uint64_t Timestamp
Timestamp in nanoseconds since epoch.
Definition types.hpp:30
Level 3 order update event Note: quantity = 0 implies the order should be deleted.
Definition events.hpp:67
constexpr bool quantityChanged() const noexcept
Check if quantity changed.
Definition events.hpp:99
constexpr bool priceChanged() const noexcept
Check if price changed (order was added or moved to different price)
Definition events.hpp:94
constexpr bool isLastInBatch() const noexcept
Check if this is the last update in a batch.
Definition events.hpp:109
constexpr bool isDelete() const noexcept
Check if this is a delete action.
Definition events.hpp:89
constexpr bool isTopN(uint16_t n) const noexcept
Check if this order's price level is in the top N levels.
Definition events.hpp:104
OrderUpdate() noexcept=default
Level 2 price level update event Note: quantity = 0 implies the price level should be deleted.
Definition events.hpp:21
constexpr bool priceChanged() const noexcept
Check if price changed (level was added or moved)
Definition events.hpp:45
PriceLevelUpdate() noexcept=default
constexpr bool quantityChanged() const noexcept
Check if quantity changed.
Definition events.hpp:50
constexpr bool isTopN(uint16_t n) const noexcept
Check if this update affects the top N levels.
Definition events.hpp:55
constexpr bool isDelete() const noexcept
Check if this is a delete action.
Definition events.hpp:40
constexpr bool isLastInBatch() const noexcept
Check if this is the last update in a batch.
Definition events.hpp:60
Top-of-book snapshot - best bid and ask.
Definition events.hpp:142
bool hasBid() const noexcept
Check if book has valid bid.
Definition events.hpp:173
bool isValid() const noexcept
Check if book has both valid bid and ask.
Definition events.hpp:183
bool hasAsk() const noexcept
Check if book has valid ask.
Definition events.hpp:178
Price spread() const noexcept
Calculate spread.
Definition events.hpp:158
bool isCrossed() const noexcept
Check if book is crossed (bid >= ask, should not happen in normal conditions)
Definition events.hpp:168
Price midPrice() const noexcept
Calculate mid price.
Definition events.hpp:163
TopOfBook(SymbolId sym, Price bid, Quantity bid_qty, Price ask, Quantity ask_qty, Timestamp ts) noexcept
Definition events.hpp:154
Trade event - represents an executed trade.
Definition events.hpp:115
Trade(SymbolId sym, Price p, Quantity q, Timestamp ts, Side aggressor, OrderId passive=0, OrderId aggressive=0, uint8_t flags=0) noexcept
Definition events.hpp:129
constexpr bool isLastInBatch() const noexcept
Check if this is the last update in a batch.
Definition events.hpp:136