ITCHCPP 1.6.0
High-Performance NASDAQ TotalView-ITCH 5.0 Parser & Market-Data Platform
Loading...
Searching...
No Matches
overlay.hpp
Go to the documentation of this file.
1#pragma once
2
12
13#include <array>
14#include <cstddef>
15#include <cstdint>
16#include <cstring>
17#include <functional>
18#include <span>
19#include <string_view>
20
21#include "itch/detail/wire.hpp"
22#include "itch/parser.hpp"
23
24namespace itch::overlay {
25
26namespace detail {
27
36template <typename FieldType>
37[[nodiscard]] inline auto read_field(const std::byte* base, std::size_t offset) noexcept
38 -> FieldType {
39 FieldType value {};
40 std::memcpy(&value, base + offset, sizeof(FieldType));
41 if constexpr (std::is_integral_v<FieldType> && sizeof(FieldType) > 1) {
42 return utils::from_big_endian(value);
43 } else {
44 return value;
45 }
46}
47
53[[nodiscard]] inline auto read_timestamp(const std::byte* base, std::size_t offset) noexcept
54 -> std::uint64_t {
55 const auto high = read_field<std::uint16_t>(base, offset);
56 const auto low = read_field<std::uint32_t>(base, offset + 2);
57 constexpr int LOWER_SHIFT = 32;
58 return (static_cast<std::uint64_t>(high) << LOWER_SHIFT) | low;
59}
60
66[[nodiscard]] inline auto read_stock(const std::byte* base, std::size_t offset) noexcept
67 -> std::string_view {
68 const void* raw = base + offset;
69 return std::string_view {static_cast<const char*>(raw), 8};
70}
71
72// Common field offsets shared by every message after the 1-byte type.
73constexpr std::size_t STOCK_LOCATE_OFFSET = 1;
74constexpr std::size_t TRACKING_NUMBER_OFFSET = 3;
75constexpr std::size_t TIMESTAMP_OFFSET = 5;
76
77} // namespace detail
78
89 public:
91 constexpr MessageView() noexcept = default;
92
97 explicit MessageView(const std::byte* data, std::size_t size) noexcept
98 : m_data {data}, m_size {size} {}
99
102 [[nodiscard]] auto type() const noexcept -> char {
103 return static_cast<char>(static_cast<unsigned char>(m_data[0]));
104 }
105
108 [[nodiscard]] auto stock_locate() const noexcept -> std::uint16_t {
109 return detail::read_field<std::uint16_t>(m_data, detail::STOCK_LOCATE_OFFSET);
110 }
111
114 [[nodiscard]] auto tracking_number() const noexcept -> std::uint16_t {
115 return detail::read_field<std::uint16_t>(m_data, detail::TRACKING_NUMBER_OFFSET);
116 }
117
120 [[nodiscard]] auto timestamp() const noexcept -> std::uint64_t {
122 }
123
126 [[nodiscard]] auto size() const noexcept -> std::size_t { return m_size; }
127
130 [[nodiscard]] auto data() const noexcept -> const std::byte* { return m_data; }
131
138 template <typename FieldType>
139 [[nodiscard]] auto read(std::size_t offset) const noexcept -> FieldType {
140 return detail::read_field<FieldType>(m_data, offset);
141 }
142
143 protected:
144 const std::byte* m_data {nullptr};
145 std::size_t m_size {0};
146};
147
149class AddOrderView : public MessageView {
150 public:
152
155 [[nodiscard]] auto order_reference_number() const noexcept -> std::uint64_t {
156 return read<std::uint64_t>(11);
157 }
158
161 [[nodiscard]] auto buy_sell_indicator() const noexcept -> char { return read<char>(19); }
162
165 [[nodiscard]] auto shares() const noexcept -> std::uint32_t { return read<std::uint32_t>(20); }
166
169 [[nodiscard]] auto stock() const noexcept -> std::string_view {
170 return detail::read_stock(m_data, 24);
171 }
172
175 [[nodiscard]] auto price() const noexcept -> std::uint32_t { return read<std::uint32_t>(32); }
176};
177
180 public:
182
185 [[nodiscard]] auto order_reference_number() const noexcept -> std::uint64_t {
186 return read<std::uint64_t>(11);
187 }
188
191 [[nodiscard]] auto executed_shares() const noexcept -> std::uint32_t {
192 return read<std::uint32_t>(19);
193 }
194
197 [[nodiscard]] auto match_number() const noexcept -> std::uint64_t {
198 return read<std::uint64_t>(23);
199 }
200};
201
204 public:
206
209 [[nodiscard]] auto order_reference_number() const noexcept -> std::uint64_t {
210 return read<std::uint64_t>(11);
211 }
212
215 [[nodiscard]] auto executed_shares() const noexcept -> std::uint32_t {
216 return read<std::uint32_t>(19);
217 }
218
221 [[nodiscard]] auto match_number() const noexcept -> std::uint64_t {
222 return read<std::uint64_t>(23);
223 }
224
227 [[nodiscard]] auto printable() const noexcept -> char { return read<char>(31); }
228
231 [[nodiscard]] auto execution_price() const noexcept -> std::uint32_t {
232 return read<std::uint32_t>(32);
233 }
234};
235
238 public:
240
243 [[nodiscard]] auto order_reference_number() const noexcept -> std::uint64_t {
244 return read<std::uint64_t>(11);
245 }
246
249 [[nodiscard]] auto cancelled_shares() const noexcept -> std::uint32_t {
250 return read<std::uint32_t>(19);
251 }
252};
253
256 public:
258
261 [[nodiscard]] auto order_reference_number() const noexcept -> std::uint64_t {
262 return read<std::uint64_t>(11);
263 }
264};
265
268 public:
270
273 [[nodiscard]] auto original_order_reference_number() const noexcept -> std::uint64_t {
274 return read<std::uint64_t>(11);
275 }
276
279 [[nodiscard]] auto new_order_reference_number() const noexcept -> std::uint64_t {
280 return read<std::uint64_t>(19);
281 }
282
285 [[nodiscard]] auto shares() const noexcept -> std::uint32_t { return read<std::uint32_t>(27); }
286
289 [[nodiscard]] auto price() const noexcept -> std::uint32_t { return read<std::uint32_t>(31); }
290};
291
294 public:
296
299 [[nodiscard]] auto order_reference_number() const noexcept -> std::uint64_t {
300 return read<std::uint64_t>(11);
301 }
302
305 [[nodiscard]] auto buy_sell_indicator() const noexcept -> char { return read<char>(19); }
306
309 [[nodiscard]] auto shares() const noexcept -> std::uint32_t { return read<std::uint32_t>(20); }
310
313 [[nodiscard]] auto stock() const noexcept -> std::string_view {
314 return detail::read_stock(m_data, 24);
315 }
316
319 [[nodiscard]] auto price() const noexcept -> std::uint32_t { return read<std::uint32_t>(32); }
320
323 [[nodiscard]] auto match_number() const noexcept -> std::uint64_t {
324 return read<std::uint64_t>(36);
325 }
326};
327
329using ViewCallback = std::function<void(const MessageView&)>;
330
336[[nodiscard]] consteval auto build_size_table() -> std::array<std::uint16_t, 256> {
337 std::array<std::uint16_t, 256> table {};
338 auto add = [&table]<typename MsgType>(char type) {
339 table[static_cast<unsigned char>(type)] =
340 static_cast<std::uint16_t>(itch::detail::WIRE_SIZE<MsgType>);
341 };
343 return table;
344}
345
346inline constexpr auto SIZE_TABLE = build_size_table();
347
359inline auto for_each_message(std::span<const std::byte> data, const ViewCallback& callback)
360 -> std::uint64_t {
361 std::uint64_t delivered = 0;
362 std::size_t offset = 0;
363 while (offset + sizeof(std::uint16_t) <= data.size()) {
364 std::uint16_t length {};
365 std::memcpy(&length, data.data() + offset, sizeof(length));
366 length = utils::from_big_endian(length);
367 offset += sizeof(std::uint16_t);
368 if (length == 0) {
369 continue;
370 }
371 if (offset + length > data.size()) {
372 break;
373 }
374 const std::byte* frame = data.data() + offset;
375 const auto message_type = static_cast<unsigned char>(frame[0]);
376 offset += length;
377
378 const std::uint16_t expected = SIZE_TABLE[message_type];
379 if (expected == 0 || length < expected) {
380 continue; // Unknown type or undersized frame.
381 }
382 callback(MessageView {frame, length});
383 ++delivered;
384 }
385 return delivered;
386}
387
388} // namespace itch::overlay
Lazy view of an Add Order (A) message.
Definition overlay.hpp:149
auto buy_sell_indicator() const noexcept -> char
'B' buy, 'S' sell.
Definition overlay.hpp:161
auto shares() const noexcept -> std::uint32_t
Displayed share quantity.
Definition overlay.hpp:165
auto stock() const noexcept -> std::string_view
Stock symbol, right padded with spaces.
Definition overlay.hpp:169
auto price() const noexcept -> std::uint32_t
Display price (4 implied decimals).
Definition overlay.hpp:175
auto order_reference_number() const noexcept -> std::uint64_t
Day-unique reference number for the order.
Definition overlay.hpp:155
A zero-copy typed view over one raw ITCH frame.
Definition overlay.hpp:88
auto timestamp() const noexcept -> std::uint64_t
The message timestamp (nanoseconds past midnight).
Definition overlay.hpp:120
auto type() const noexcept -> char
The one-byte message type.
Definition overlay.hpp:102
auto size() const noexcept -> std::size_t
The raw frame size in bytes.
Definition overlay.hpp:126
auto read(std::size_t offset) const noexcept -> FieldType
Reads an arbitrary integral field at a byte offset, big-endian.
Definition overlay.hpp:139
constexpr MessageView() noexcept=default
Constructs an empty view with no underlying frame.
const std::byte * m_data
Definition overlay.hpp:144
auto stock_locate() const noexcept -> std::uint16_t
The locate code identifying the security.
Definition overlay.hpp:108
auto data() const noexcept -> const std::byte *
Pointer to the raw frame (the type byte).
Definition overlay.hpp:130
auto tracking_number() const noexcept -> std::uint16_t
The Nasdaq internal tracking number.
Definition overlay.hpp:114
Lazy view of a Trade (P, non-cross) message.
Definition overlay.hpp:293
auto order_reference_number() const noexcept -> std::uint64_t
Reference number of the non-displayed order.
Definition overlay.hpp:299
auto match_number() const noexcept -> std::uint64_t
Day-unique match number for the trade.
Definition overlay.hpp:323
auto price() const noexcept -> std::uint32_t
Trade price (4 implied decimals).
Definition overlay.hpp:319
auto shares() const noexcept -> std::uint32_t
Number of shares traded.
Definition overlay.hpp:309
auto buy_sell_indicator() const noexcept -> char
'B' buy, 'S' sell.
Definition overlay.hpp:305
auto stock() const noexcept -> std::string_view
Stock symbol, right padded with spaces.
Definition overlay.hpp:313
Lazy view of an Order Cancel (X) message.
Definition overlay.hpp:237
auto order_reference_number() const noexcept -> std::uint64_t
Reference number of the cancelled order.
Definition overlay.hpp:243
auto cancelled_shares() const noexcept -> std::uint32_t
Number of shares cancelled.
Definition overlay.hpp:249
Lazy view of an Order Delete (D) message.
Definition overlay.hpp:255
auto order_reference_number() const noexcept -> std::uint64_t
Reference number of the deleted order.
Definition overlay.hpp:261
Lazy view of an Order Executed (E) message.
Definition overlay.hpp:179
auto executed_shares() const noexcept -> std::uint32_t
Number of shares executed.
Definition overlay.hpp:191
auto order_reference_number() const noexcept -> std::uint64_t
Reference number of the executed order.
Definition overlay.hpp:185
auto match_number() const noexcept -> std::uint64_t
Day-unique match number for the execution.
Definition overlay.hpp:197
Lazy view of an Order Executed With Price (C) message.
Definition overlay.hpp:203
auto printable() const noexcept -> char
'Y' if the trade is printable to the tape, else 'N'.
Definition overlay.hpp:227
auto match_number() const noexcept -> std::uint64_t
Day-unique match number for the execution.
Definition overlay.hpp:221
auto execution_price() const noexcept -> std::uint32_t
Price at which the order executed (4 decimals).
Definition overlay.hpp:231
auto order_reference_number() const noexcept -> std::uint64_t
Reference number of the executed order.
Definition overlay.hpp:209
auto executed_shares() const noexcept -> std::uint32_t
Number of shares executed.
Definition overlay.hpp:215
Lazy view of an Order Replace (U) message.
Definition overlay.hpp:267
auto new_order_reference_number() const noexcept -> std::uint64_t
New reference number for the order.
Definition overlay.hpp:279
auto original_order_reference_number() const noexcept -> std::uint64_t
Reference number being replaced.
Definition overlay.hpp:273
auto price() const noexcept -> std::uint32_t
New display price (4 implied decimals).
Definition overlay.hpp:289
auto shares() const noexcept -> std::uint32_t
New displayed share quantity.
Definition overlay.hpp:285
char message_type
Definition csv_sink.cpp:15
constexpr auto for_each_message_type(Visitor &&visitor) -> void
Invokes visitor.template operator()<MsgType>(type_byte) once for each ITCH 5.0 message type,...
Definition wire.hpp:48
constexpr std::size_t STOCK_LOCATE_OFFSET
Definition overlay.hpp:73
auto read_timestamp(const std::byte *base, std::size_t offset) noexcept -> std::uint64_t
Reads the 48-bit ITCH timestamp at offset into a 64-bit value.
Definition overlay.hpp:53
constexpr std::size_t TIMESTAMP_OFFSET
Definition overlay.hpp:75
auto read_field(const std::byte *base, std::size_t offset) noexcept -> FieldType
Reads an integral field of width sizeof(T) at offset, converting from network (big-endian) order on a...
Definition overlay.hpp:37
auto read_stock(const std::byte *base, std::size_t offset) noexcept -> std::string_view
Views an 8-byte fixed-width stock field as a string_view (untrimmed).
Definition overlay.hpp:66
constexpr std::size_t TRACKING_NUMBER_OFFSET
Definition overlay.hpp:74
consteval auto build_size_table() -> std::array< std::uint16_t, 256 >
Builds the per-type expected wire-size table at compile time, mirroring the eager parser's so the ove...
Definition overlay.hpp:336
auto for_each_message(std::span< const std::byte > data, const ViewCallback &callback) -> std::uint64_t
Frames a buffer and invokes callback with a zero-copy MessageView for each well-formed message.
Definition overlay.hpp:359
std::function< void(const MessageView &)> ViewCallback
The signature for the overlay framing callback.
Definition overlay.hpp:329
constexpr auto SIZE_TABLE
Definition overlay.hpp:346
Public parsing interface for NASDAQ TotalView-ITCH 5.0 feeds, plus the low-level byte-unpacking utili...
Single source of truth for the ITCH 5.0 wire layout metadata shared by the eager parser,...