FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
backtest_config.hpp
Go to the documentation of this file.
1#pragma once
2// BacktestConfig: everything a run needs, assembled from the TOML sections
3// [engine] [[instruments]] [strategy] [strategy.params] [risk] [venues.<x>.fees] [sim]
4// [backtest] (8.4, 8.7) or built by hand (examples, tests, Python).
5//
6// [backtest] [sim] (synthetic market)
7// source = "synthetic" | "journal" | "csv" seed, duration_s, start_mid,
8// path = "data.fmj" | "data.csv" depth_update_ms, book_ticker,
9// seed = 1 limit_rate_per_s, market_rate_per_s,
10// duration_s = 60 (synthetic horizon) mid_step_rate_per_s,
11// fill_model = "matching" | "l2_queue" cancel_rate_per_order_s, offset_p,
12// queue_conservatism = 0..1 base_spread_ticks, limit_qty_median_lots,
13// latency_fixed_us, latency_jitter_us, market_qty_median_lots, regimes,
14// latency_ack_us, latency_ack_jitter_us,
15// latency_md_us, latency_md_jitter_us, p_drop volatile_mult, seed_levels
16// latency_cancel_us, latency_cancel_jitter_us, order_service_us
17// md_arrival = "venue" | "recorded", reorder_window_ms = 1000
18// balances_from_journal = false, own_orders_in_feed = true, transfer_latency_ms = 0
19// initial_state = "state_file" | "journal" | "none", params_from_journal = false
20// [backtest.balances] <ASSET> = "<amount>" (and [backtest.venues.<name>.balances])
21// equity_bar_s = 1, initial_capital = 0
22// markout_horizons_s = "1,10,60"
23// output_dir = "runs/backtest", journal_out = ""
25#include "fastmm/core/engine.hpp"
27#include "fastmm/core/treasury.hpp"
28#include "fastmm/sim/market_generator.hpp"
29#include "fastmm/sim/sim_transport.hpp"
31
32#include <cstdint>
33#include <string>
34#include <string_view>
35#include <vector>
36
37namespace fastmm::bt {
38
39// Where the strategy's starting state comes from ([backtest] initial_state).
40enum class InitialState : std::uint8_t {
41 StateFile = 0, // [strategy] state_file, when set
42 Journal = 1, // the data journal's (journal_state.hpp), with the session's positions
43 None = 2, // nothing: the strategy starts fresh
44};
45
46struct BacktestConfig {
49 std::string strategy; // registry name (apps / Python); ignored by run_backtest<S>()
51 sim::SimTransportConfig transport;
52 sim::MarketGeneratorParams generator;
53 std::uint64_t seed = 1; // synthetic market + latency model (engine.rng_seed is separate)
54 Timestamp start{seconds(1'700'000'000).ns}; // synthetic start; data sources use their own
55 Duration duration = seconds(60); // synthetic horizon
57 double initial_capital = 0.0; // reporting only (drawdown %)
58 // [accounting] and the venue names its sources name (Config::venues order). from_config() builds
59 // engine.fx from them for `instruments`; replay builds it again for the journal's table.
60 AccountingSpec accounting;
61 std::vector<std::string> venue_names;
62 // [risk.underlying]: from_config() builds engine.underlying from it for `instruments`; replay
63 // builds it again for the journal's table.
64 UnderlyingSpec underlying;
65 // from_config: Config::warnings plus unknown [backtest] keys, each naming the key and its line.
66 std::vector<std::string> warnings;
67 int generator_seed_levels = 20;
68 std::string source; // "synthetic" | "journal" | "csv" | "" (caller supplies the source)
69 std::string path;
70 std::string output_dir;
71 std::string journal_out; // record the run to this .fmj (empty = no journal)
72 // Effective configuration embedded in journal_out (Config::effective_toml(); from_config() sets
73 // it, hand-built configs leave it empty). Callers that override fields after from_config()
74 // must refresh or clear it.
75 std::string config_toml;
76 bool measure_wall_clock = true;
77 // Post-fill markout horizons in simulated time; empty turns markouts off. The run stops the
78 // simulated clock at every fill time + horizon to read the venue mid there, so the shortest
79 // horizon also bounds how often the run loop is entered.
80 std::vector<Duration> markout_horizons = {seconds(1), seconds(10), seconds(60)};
81 // Start each venue's account (transport.accounts) from the first balance snapshot the journal
82 // source recorded for it; a venue it has none for keeps the configured balances.
83 bool balances_from_journal = false;
84 // The pool treasuries ([venues.<primary>.treasury] with enabled): each runs on simulated time
85 // against its pool's simulated accounts (sim/sim_treasury.hpp), without a ledger file. Empty:
86 // none, and the run is what it was without them.
87 std::vector<TreasuryConfig> treasuries;
88 // [backtest] transfer_latency_ms: a simulated transfer is carried out this long after it is sent.
90 // The strategy's starting state; Journal also starts the engine's positions where the recorded
91 // session had them (sim::SessionStart).
93 // The data journal's parameter updates reach the strategy at the times the session received
94 // them (sim::RecordedParams), mapped by name and type onto its schema.
95 bool params_from_journal = false;
96 // Recorded data reaches the simulated venue in venue-time order (sim/venue_order.hpp), assuming
97 // no event was received more than this after its venue time; zero keeps the recorded order.
99
100 // Throws ConfigError on invalid values.
101 [[nodiscard]] static BacktestConfig from_config(const Config& cfg);
102 // One instrument (venue 0) with the given tick/lot; generator tick/lot follow (examples).
103 [[nodiscard]] static BacktestConfig single_instrument(std::string_view symbol,
104 Price tick,
105 Qty lot);
106
107 // The FX plan for `table` under `accounting` (throws ConfigError when a limit reads the totals
108 // and the table is not covered; otherwise a warning goes to `warnings` and nothing converts).
109 [[nodiscard]] FxPlan fx_plan(const InstrumentTable& table);
110 // The [risk.underlying] plan for `table` (throws ConfigError when the spec does not fit it).
111 [[nodiscard]] UnderlyingPlan underlying_plan(const InstrumentTable& table) const;
112
113 // "key=value" strategy parameter override (CLI --param).
114 void set_param(const std::string& key, const std::string& value) { params[key] = value; }
115 // Seeds the synthetic market and the latency model.
116 void set_seed(std::uint64_t s) noexcept {
117 seed = s;
118 transport.seed = s;
119 }
120};
121
122} // namespace fastmm::bt
Definition config.hpp:302
Dense-by-id table built at startup; symbol lookup is a sorted FlatMap keyed by (venue,...
Definition instrument.hpp:141
Typed configuration (8.7).
Instrument: the single definition of a tradable contract (5.3 / 5.15).
Definition backtest_config.hpp:39
InitialState
Where the strategy's starting state comes from ([backtest] initial_state).
Definition backtest_config.hpp:42
@ StateFile
[strategy] state_file, when set
@ None
nothing: the strategy starts fresh
@ Journal
the data journal's (journal_state.hpp), with the session's positions
constexpr Duration milliseconds(std::int64_t v) noexcept
Definition time.hpp:38
constexpr Duration seconds(std::int64_t v) noexcept
Definition time.hpp:41
std::map< std::string, std::string > ParamMap
Definition params.hpp:119
Strategy parameter schema (8.6, ADR-0012).
Definition time.hpp:22
std::int64_t ns
Definition time.hpp:23
Definition engine_config.hpp:21
Definition time.hpp:45
Definition backtest_config.hpp:48
double initial_capital
reporting only (drawdown %)
Definition backtest_config.hpp:59
AccountingSpec accounting
[accounting] and the venue names its sources name (Config::venues order).
Definition backtest_config.hpp:62
UnderlyingPlan underlying_plan(const InstrumentTable &table) const
The [risk.underlying] plan for table (throws ConfigError when the spec does not fit it).
std::string path
Definition backtest_config.hpp:71
std::vector< TreasuryConfig > treasuries
The pool treasuries ([venues.
Definition backtest_config.hpp:89
std::string strategy
registry name (apps / Python); ignored by run_backtest<S>()
Definition backtest_config.hpp:51
std::string config_toml
Effective configuration embedded in journal_out (Config::effective_toml(); from_config() sets it,...
Definition backtest_config.hpp:77
InstrumentTable instruments
Definition backtest_config.hpp:50
std::string output_dir
Definition backtest_config.hpp:72
bool balances_from_journal
Start each venue's account (transport.accounts) from the first balance snapshot the journal source re...
Definition backtest_config.hpp:85
Duration reorder_window
Recorded data reaches the simulated venue in venue-time order (sim/venue_order.hpp),...
Definition backtest_config.hpp:100
static BacktestConfig single_instrument(std::string_view symbol, Price tick, Qty lot)
One instrument (venue 0) with the given tick/lot; generator tick/lot follow (examples).
std::string source
"synthetic" | "journal" | "csv" | "" (caller supplies the source)
Definition backtest_config.hpp:70
bool params_from_journal
The data journal's parameter updates reach the strategy at the times the session received them (sim::...
Definition backtest_config.hpp:97
InitialState initial_state
The strategy's starting state; Journal also starts the engine's positions where the recorded session ...
Definition backtest_config.hpp:94
static BacktestConfig from_config(const Config &cfg)
Throws ConfigError on invalid values.
bool measure_wall_clock
Definition backtest_config.hpp:78
Timestamp start
Definition backtest_config.hpp:56
std::string journal_out
record the run to this .fmj (empty = no journal)
Definition backtest_config.hpp:73
FxPlan fx_plan(const InstrumentTable &table)
The FX plan for table under accounting (throws ConfigError when a limit reads the totals and the tabl...
ParamMap params
Definition backtest_config.hpp:52
int generator_seed_levels
Definition backtest_config.hpp:69
sim::SimTransportConfig transport
Definition backtest_config.hpp:53
std::vector< std::string > warnings
from_config: Config::warnings plus unknown [backtest] keys, each naming the key and its line.
Definition backtest_config.hpp:68
void set_seed(std::uint64_t s) noexcept
Seeds the synthetic market and the latency model.
Definition backtest_config.hpp:118
std::uint64_t seed
synthetic market + latency model (engine.rng_seed is separate)
Definition backtest_config.hpp:55
Duration transfer_latency
[backtest] transfer_latency_ms: a simulated transfer is carried out this long after it is sent.
Definition backtest_config.hpp:91
sim::MarketGeneratorParams generator
Definition backtest_config.hpp:54
UnderlyingSpec underlying
[risk.underlying]: from_config() builds engine.underlying from it for instruments; replay builds it a...
Definition backtest_config.hpp:66
EngineConfig engine
Definition backtest_config.hpp:49
Duration duration
synthetic horizon
Definition backtest_config.hpp:57
std::vector< Duration > markout_horizons
Post-fill markout horizons in simulated time; empty turns markouts off.
Definition backtest_config.hpp:82
Duration equity_bar
Definition backtest_config.hpp:58
std::vector< std::string > venue_names
Definition backtest_config.hpp:63
void set_param(const std::string &key, const std::string &value)
"key=value" strategy parameter override (CLI --param).
Definition backtest_config.hpp:116