ITCHCPP 1.6.0
High-Performance NASDAQ TotalView-ITCH 5.0 Parser & Market-Data Platform
Loading...
Searching...
No Matches
l3_book.hpp
Go to the documentation of this file.
1#pragma once
2
14
15#include <cstdint>
16#include <optional>
17#include <string>
18#include <vector>
19
21#include "itch/price.hpp"
22
23namespace itch::book {
24
26enum class Side : char {
27 buy = 'B',
28 sell = 'S',
29};
30
32struct DepthLevel {
34 std::uint64_t shares {0};
35 std::uint32_t order_count {0};
36};
37
39struct OrderView {
40 std::uint64_t reference_number {0};
41 std::uint32_t shares {0};
43};
44
46struct Bbo {
47 bool has_bid {false};
48 bool has_ask {false};
50 std::uint64_t bid_shares {0};
52 std::uint64_t ask_shares {0};
53
59 [[nodiscard]] friend auto operator==(const Bbo&, const Bbo&) noexcept -> bool = default;
60};
61
72class L3Book {
73 public:
77 explicit L3Book(std::string symbol = {});
78
81 auto set_symbol(std::string symbol) -> void { m_symbol = std::move(symbol); }
82
85 [[nodiscard]] auto symbol() const noexcept -> const std::string& { return m_symbol; }
86
93 auto add_order(
94 std::uint64_t reference_number, Side side, std::uint32_t shares, std::uint32_t price
95 ) -> void;
96
104 auto execute_order(std::uint64_t reference_number, std::uint32_t shares) -> std::uint32_t;
105
113 auto reduce_order(std::uint64_t reference_number, std::uint32_t shares) -> std::uint32_t;
114
118 auto delete_order(std::uint64_t reference_number) -> void;
119
128 auto replace_order(
129 std::uint64_t old_reference_number,
130 std::uint64_t new_reference_number,
131 std::uint32_t shares,
132 std::uint32_t price
133 ) -> void;
134
139 [[nodiscard]] auto contains(std::uint64_t reference_number) const -> bool;
140
145 [[nodiscard]] auto order_price(std::uint64_t reference_number
146 ) const -> std::optional<std::uint32_t>;
147
152 [[nodiscard]] auto order_side(std::uint64_t reference_number) const -> std::optional<Side>;
153
156 [[nodiscard]] auto bbo() const -> Bbo;
157
163 [[nodiscard]] auto depth(Side side, std::size_t max_levels = 0) const
164 -> std::vector<DepthLevel>;
165
170 [[nodiscard]] auto orders_at(Side side, std::uint32_t price) const -> std::vector<OrderView>;
171
175 [[nodiscard]] auto level_count(Side side) const noexcept -> std::size_t;
176
179 [[nodiscard]] auto empty() const noexcept -> bool { return m_index.empty(); }
180
181 private:
183 static constexpr std::uint32_t NIL = 0xFFFFFFFFU;
184
186 struct OrderNode {
187 std::uint64_t reference_number {0};
188 std::uint32_t shares {0};
189 std::uint32_t price {0};
191 std::uint32_t next {NIL};
192 std::uint32_t prev {NIL};
193 };
194
196 struct Level {
197 std::uint32_t price {0};
198 std::uint64_t total_shares {0};
199 std::uint32_t order_count {0};
200 std::uint32_t head {NIL};
201 std::uint32_t tail {NIL};
202 };
203
207 [[nodiscard]] auto side_levels(Side side) noexcept -> std::vector<Level>&;
208
212 [[nodiscard]] auto side_levels(Side side) const noexcept -> const std::vector<Level>&;
213
217 auto allocate_node() -> std::uint32_t;
218
221 auto free_node(std::uint32_t node_index) -> void;
222
228 [[nodiscard]] auto find_level(Side side, std::uint32_t price) const -> std::uint32_t;
229
235 auto find_or_create_level(Side side, std::uint32_t price) -> std::uint32_t;
236
240 auto unlink_node(std::uint32_t node_index) -> void;
241
242 std::string m_symbol;
243 std::vector<OrderNode> m_pool;
244 std::uint32_t m_free_head {NIL};
245 std::vector<Level> m_bids;
246 std::vector<Level> m_asks;
247 // Reference-number -> pool index for O(1), allocation-free order lookup.
248 OrderIndex m_index;
249};
250
251} // namespace itch::book
A single-symbol, order-level (L3) limit order book with allocation-light internals.
Definition l3_book.hpp:72
auto execute_order(std::uint64_t reference_number, std::uint32_t shares) -> std::uint32_t
Removes shares from an order on execution (ITCH E/C), deleting it when fully filled.
Definition l3_book.cpp:150
auto replace_order(std::uint64_t old_reference_number, std::uint64_t new_reference_number, std::uint32_t shares, std::uint32_t price) -> void
Replaces an order with a new reference number, size, and price on the same side (ITCH U).
Definition l3_book.cpp:188
auto orders_at(Side side, std::uint32_t price) const -> std::vector< OrderView >
The resting orders at a given price on a side, in time priority.
Definition l3_book.cpp:253
auto delete_order(std::uint64_t reference_number) -> void
Deletes an order in its entirety (ITCH D).
Definition l3_book.cpp:178
auto level_count(Side side) const noexcept -> std::size_t
The number of active price levels on a side.
Definition l3_book.cpp:273
auto order_side(std::uint64_t reference_number) const -> std::optional< Side >
The side of a resting order, if present.
Definition l3_book.cpp:215
auto contains(std::uint64_t reference_number) const -> bool
Whether an order with the given reference number is resting.
Definition l3_book.cpp:203
auto empty() const noexcept -> bool
Whether the book has no resting orders on either side.
Definition l3_book.hpp:179
auto set_symbol(std::string symbol) -> void
Sets the stock symbol associated with this book.
Definition l3_book.hpp:81
auto order_price(std::uint64_t reference_number) const -> std::optional< std::uint32_t >
The raw limit price of a resting order, if present.
Definition l3_book.cpp:207
auto reduce_order(std::uint64_t reference_number, std::uint32_t shares) -> std::uint32_t
Removes shares from an order on a partial cancel (ITCH X), deleting it when it reaches zero.
Definition l3_book.cpp:173
auto symbol() const noexcept -> const std::string &
The stock symbol associated with this book (may be empty).
Definition l3_book.hpp:85
auto add_order(std::uint64_t reference_number, Side side, std::uint32_t shares, std::uint32_t price) -> void
Adds a new resting order to the book (ITCH A/F).
Definition l3_book.cpp:93
auto depth(Side side, std::size_t max_levels=0) const -> std::vector< DepthLevel >
Aggregated L2 depth for a side, best level first.
Definition l3_book.cpp:238
auto bbo() const -> Bbo
The current best bid and offer.
Definition l3_book.cpp:223
auto empty() const noexcept -> bool
Whether the map holds no keys.
std::string reference_number
Definition csv_sink.cpp:20
std::string price
Definition csv_sink.cpp:23
char side
Definition csv_sink.cpp:21
std::string shares
Definition csv_sink.cpp:22
Side
Which side of the book an order rests on.
Definition l3_book.hpp:26
@ sell
An offer/ask.
Allocation-light, open-addressed hash map from order reference number to order-pool index.
Strongly typed, fixed-point price representation for ITCH price fields.
Best bid and offer of a single book.
Definition l3_book.hpp:46
StandardPrice ask_price
Best (lowest) ask price.
Definition l3_book.hpp:51
friend auto operator==(const Bbo &, const Bbo &) noexcept -> bool=default
Compares two BBO snapshots for equality of all fields.
std::uint64_t bid_shares
Shares at the best bid.
Definition l3_book.hpp:50
StandardPrice bid_price
Best (highest) bid price.
Definition l3_book.hpp:49
bool has_bid
Whether a bid side exists.
Definition l3_book.hpp:47
bool has_ask
Whether an ask side exists.
Definition l3_book.hpp:48
std::uint64_t ask_shares
Shares at the best ask.
Definition l3_book.hpp:52
Aggregated state of one price level, for L2 depth snapshots.
Definition l3_book.hpp:32
std::uint32_t order_count
Number of resting orders at the level.
Definition l3_book.hpp:35
std::uint64_t shares
Total displayed shares resting at the level.
Definition l3_book.hpp:34
StandardPrice price
The level's limit price.
Definition l3_book.hpp:33
A single resting order, for L3 order-level snapshots.
Definition l3_book.hpp:39
std::uint32_t shares
Shares still resting.
Definition l3_book.hpp:41
StandardPrice price
Limit price.
Definition l3_book.hpp:42
std::uint64_t reference_number
Exchange order reference number.
Definition l3_book.hpp:40