ITCHCPP 1.6.0
High-Performance NASDAQ TotalView-ITCH 5.0 Parser & Market-Data Platform
Loading...
Searching...
No Matches
replay.cpp
Go to the documentation of this file.
1#include "itch/replay.hpp"
2
3#include <chrono>
4#include <thread>
5#include <variant>
6
7namespace itch {
8
9namespace {
10
11// Extracts the nanoseconds-past-midnight timestamp from any message.
12auto timestamp_of(const Message& message) -> std::uint64_t {
13 return std::visit([](const auto& concrete) { return concrete.timestamp; }, message);
14}
15
16} // namespace
17
18auto ReplayEngine::replay(std::span<const std::byte> data, const MessageCallback& callback) const
19 -> std::uint64_t {
20 using clock = std::chrono::steady_clock;
21 const bool paced = m_speed_multiplier > 0.0;
22
23 std::uint64_t replayed = 0;
24 bool have_base = false;
25 std::uint64_t base_timestamp = 0;
26 clock::time_point base_wall {};
27
28 Parser parser;
29 parser.parse(data, [&](const Message& message) {
30 if (paced) {
31 const std::uint64_t feed_timestamp = timestamp_of(message);
32 if (!have_base) {
33 base_timestamp = feed_timestamp;
34 base_wall = clock::now();
35 have_base = true;
36 } else if (feed_timestamp > base_timestamp) {
37 // Scale the elapsed feed time by the speed multiplier and sleep
38 // until that point relative to the first message's wall time.
39 const double elapsed_ns =
40 static_cast<double>(feed_timestamp - base_timestamp) / m_speed_multiplier;
41 const auto target =
42 base_wall + std::chrono::nanoseconds {static_cast<std::int64_t>(elapsed_ns)};
43 std::this_thread::sleep_until(target);
44 }
45 }
46 callback(message);
47 ++replayed;
48 });
49 return replayed;
50}
51
52} // namespace itch
A high-performance parser for the NASDAQ TotalView-ITCH 5.0 protocol.
Definition parser.hpp:78
auto parse(const char *data, size_t size, const MessageCallback &callback) -> void
Parses messages from a memory buffer and invokes a callback for each.
Definition parser.cpp:343
auto replay(std::span< const std::byte > data, const MessageCallback &callback) const -> std::uint64_t
Parses data and invokes callback for each message, paced by the message timestamps.
Definition replay.cpp:18
std::variant< SystemEventMessage, StockDirectoryMessage, StockTradingActionMessage, RegSHOMessage, MarketParticipantPositionMessage, MWCBDeclineLevelMessage, MWCBStatusMessage, IPOQuotingPeriodUpdateMessage, LULDAuctionCollarMessage, OperationalHaltMessage, AddOrderMessage, AddOrderMPIDAttributionMessage, OrderExecutedMessage, OrderExecutedWithPriceMessage, OrderCancelMessage, OrderDeleteMessage, OrderReplaceMessage, NonCrossTradeMessage, CrossTradeMessage, BrokenTradeMessage, NOIIMessage, RetailPriceImprovementIndicatorMessage, DLCRMessage > Message
A variant able to hold any one of the ITCH 5.0 message structs.
Definition messages.hpp:483
std::function< void(const Message &)> MessageCallback
The signature for the callback function used in streaming parse methods.
Definition parser.hpp:40
A timestamp-paced replay engine for previously captured ITCH streams.