FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
replay.hpp
Go to the documentation of this file.
1#pragma once
2// Deterministic replay (5.11): runs Engine<S, SimClock, ReplayTransport, JournalFeed> over a
3// session journal recorded by fastmm-live, a sim or a backtest run (BacktestConfig::journal_out)
4// and proves that the engine reproduces the recorded outbound stream bit for bit.
5//
6// A format v2 journal carries what a replay needs besides the events: the engine clock of every
7// event, the session epoch, quoting enabled (dry run), the RNG seed, per-venue cancel-replace and
8// the effective configuration. replay_journal(path) uses all of it; replay_journal(path, cfg)
9// takes the engine and strategy configuration from `cfg` (what-if runs) and still restores the
10// session settings from the header.
11//
12// journal (inbound events + Out* copies) --JournalFeed--> engine --ReplayTransport--> sha256
13//
14// A replay passes when the replayed and recorded message counts and SHA-256 hashes are equal
15// and no message differed (ReplayResult::ok()). A different strategy or parameters turn the
16// same call into a what-if run (ok() is then expected to be false).
18#include "fastmm/core/engine_runner.hpp"
21
22#include <cstdint>
23#include <functional>
24#include <memory>
25#include <string>
26
27namespace fastmm::bt {
28
29struct ReplayOptions {
30 std::string strategy; // empty -> the strategy recorded in the journal header
31 bool verify = true; // compare every outbound message with the journal's Out* records
32 // Restore session epoch, quoting enabled, RNG seed and per-venue cancel-replace from a v2
33 // header (a journal without them replays with the given configuration's values).
34 bool session_from_journal = true;
35 // Apply the journal's ParamUpdate records, their fields matched to the strategy's parameters by
36 // name (false: skip them and keep the configured parameters, a what-if run).
37 bool param_updates = true;
38 // Replay a journal whose writer never closed it (no trailer) or whose last block is corrupt.
39 // The events before the damage are intact, so a replay of them runs and the outbound comparison
40 // stops early; by default such a file is refused rather than compared against a stream that was
41 // cut off. fastmm-replay --allow-incomplete sets it and warns instead.
42 bool allow_incomplete = false;
43 // Where the replayed engine puts its records (core/record_stream.hpp): the same fills, decision
44 // records included, orders and positions the recorded session handed its store. The caller's
45 // ring, drained by the caller, large enough for the journal (a full ring drops records). Null:
46 // none.
47 MsgRing* record_ring = nullptr;
48};
50struct ReplayResult {
51 std::string strategy;
52 std::string outbound_sha256; // what the replayed engine sent
53 std::string recorded_sha256; // the journal's Out* records
54 std::uint64_t outbound_messages = 0;
55 std::uint64_t recorded_messages = 0;
56 std::uint64_t events = 0; // inbound events replayed
57 std::int64_t first_mismatch = -1; // index of the first differing message (verify only)
58 std::string expected_message; // summaries at first_mismatch ("(none)" past the end)
59 std::string actual_message;
60 bool session_restored = false; // the header's session settings were applied
61 bool incomplete = false; // the journal has no trailer or a damaged tail block
62 [[nodiscard]] bool ok() const noexcept {
68struct JournalInfo {
69 // The file was closed cleanly: it ends in a trailer and no block was discarded. False means the
70 // writer died or ran out of space and the tail of the session is missing.
71 bool complete = true;
72 std::string strategy;
73 std::uint32_t version = 0;
74 std::uint64_t rng_seed = 0;
75 std::uint64_t config_hash = 0;
76 std::int64_t start_ts = 0;
77 std::uint64_t messages = 0;
78 std::uint64_t outbound_messages = 0; // > 0 == session journal, 0 == market data only
79 std::uint64_t dropped_outbound = 0; // of which the transport did not accept
80 std::uint64_t market_data_messages = 0;
81 bool has_session = false; // v2 session settings below are valid
82 std::uint16_t session_epoch = 0;
83 bool quoting_enabled = true;
84 std::uint64_t replace_venues = 0;
85 bool engine_time = false; // the events carry the engine clock
86 std::string config_toml; // embedded effective configuration (empty: none)
87 std::string strategy_meta; // `key=value` lines about the strategy (empty: none)
88};
90// A strategy outside the registry (a Python hot strategy): the name for the result, the parameter
91// schema that ParamUpdate fields are matched against, and a factory that builds the runner on the
92// sim::ReplayBackend that `deps.backend` points at.
93struct ReplayStrategy {
94 std::string name;
95 const ParamSchema* schema = nullptr;
96 std::function<std::unique_ptr<IEngineRunner>(RunnerDeps& deps)> make;
97};
99// Throws std::runtime_error when the file cannot be opened / validated.
100[[nodiscard]] JournalInfo inspect_journal(const std::string& path);
101
102// The configuration embedded in the journal. Throws std::runtime_error when there is none (v1
103// journals, market-data journals, hand-built backtest configurations) or it does not parse.
104[[nodiscard]] BacktestConfig journal_config(const std::string& path);
105
106// One-line description of an Out* message (type, ids, side, price, quantity, stamp).
107[[nodiscard]] std::string describe_outbound(const EventHeader& h);
108
109// `cfg` supplies the engine configuration and strategy parameters (it must match the
110// recording run for a verifying replay); the instrument table is taken from the journal.
111// Throws std::runtime_error / std::invalid_argument on unreadable journals or unknown
112// strategies.
113[[nodiscard]] ReplayResult replay_journal(const std::string& path,
114 const BacktestConfig& cfg,
115 const ReplayOptions& opt = {});
116// Replays `strategy` instead of a registered one (opt.strategy is ignored).
117[[nodiscard]] ReplayResult replay_journal(const std::string& path,
118 const BacktestConfig& cfg,
119 const ReplayOptions& opt,
120 const ReplayStrategy& strategy);
121// Replays with the journal's embedded configuration (journal_config(path)).
122[[nodiscard]] ReplayResult replay_journal(const std::string& path, const ReplayOptions& opt = {});
123
124} // namespace fastmm::bt
BacktestConfig: everything a run needs, assembled from the TOML sections [engine] [[instruments]] [st...
Definition params.hpp:102
Definition backtest_config.hpp:44
JournalInfo inspect_journal(const std::string &path)
Throws std::runtime_error when the file cannot be opened / validated.
std::string describe_outbound(const EventHeader &h)
One-line description of an Out* message (type, ids, side, price, quantity, stamp).
BacktestConfig journal_config(const std::string &path)
The configuration embedded in the journal.
ReplayResult replay_journal(const std::string &path, const BacktestConfig &cfg, const ReplayOptions &opt={})
cfg supplies the engine configuration and strategy parameters (it must match the recording run for a ...
Strategy parameter schema (8.6, ADR-0012).
Strategy registry (8.6, ADR-0012): name -> parameter schema + one factory per transport kind,...
Definition messages.hpp:38
Everything a factory needs that is backend-independent.
Definition registry.hpp:51
Definition backtest_config.hpp:80
Definition replay.hpp:70
std::uint16_t session_epoch
Definition replay.hpp:84
std::uint64_t outbound_messages
> 0 == session journal, 0 == market data only
Definition replay.hpp:80
std::string strategy
Definition replay.hpp:74
std::uint64_t replace_venues
Definition replay.hpp:86
std::uint32_t version
Definition replay.hpp:75
std::uint64_t messages
Definition replay.hpp:79
std::uint64_t market_data_messages
Definition replay.hpp:82
std::string strategy_meta
key=value lines about the strategy (empty: none)
Definition replay.hpp:89
bool complete
The file was closed cleanly: it ends in a trailer and no block was discarded.
Definition replay.hpp:73
bool has_session
v2 session settings below are valid
Definition replay.hpp:83
bool engine_time
the events carry the engine clock
Definition replay.hpp:87
std::int64_t start_ts
Definition replay.hpp:78
std::uint64_t rng_seed
Definition replay.hpp:76
bool quoting_enabled
Definition replay.hpp:85
std::uint64_t config_hash
Definition replay.hpp:77
std::string config_toml
embedded effective configuration (empty: none)
Definition replay.hpp:88
std::uint64_t dropped_outbound
of which the transport did not accept
Definition replay.hpp:81
Definition replay.hpp:31
bool param_updates
Apply the journal's ParamUpdate records, their fields matched to the strategy's parameters by name (f...
Definition replay.hpp:39
bool allow_incomplete
Replay a journal whose writer never closed it (no trailer) or whose last block is corrupt.
Definition replay.hpp:44
bool verify
compare every outbound message with the journal's Out* records
Definition replay.hpp:33
MsgRing * record_ring
Where the replayed engine puts its records (core/record_stream.hpp): the same fills,...
Definition replay.hpp:49
bool session_from_journal
Restore session epoch, quoting enabled, RNG seed and per-venue cancel-replace from a v2 header (a jou...
Definition replay.hpp:36
std::string strategy
empty -> the strategy recorded in the journal header
Definition replay.hpp:32
Definition replay.hpp:52
std::string expected_message
summaries at first_mismatch ("(none)" past the end)
Definition replay.hpp:60
bool incomplete
the journal has no trailer or a damaged tail block
Definition replay.hpp:63
bool session_restored
the header's session settings were applied
Definition replay.hpp:62
std::string strategy
Definition replay.hpp:53
std::uint64_t events
inbound events replayed
Definition replay.hpp:58
std::string outbound_sha256
what the replayed engine sent
Definition replay.hpp:54
bool ok() const noexcept
Definition replay.hpp:64
std::string recorded_sha256
the journal's Out* records
Definition replay.hpp:55
std::uint64_t outbound_messages
Definition replay.hpp:56
std::int64_t first_mismatch
index of the first differing message (verify only)
Definition replay.hpp:59
std::string actual_message
Definition replay.hpp:61
std::uint64_t recorded_messages
Definition replay.hpp:57
A strategy outside the registry (a Python hot strategy): the name for the result, the parameter schem...
Definition replay.hpp:95
std::string name
Definition replay.hpp:96
const ParamSchema * schema
Definition replay.hpp:97
std::function< std::unique_ptr< IEngineRunner >(RunnerDeps &deps)> make
Definition replay.hpp:98