17#include "fastmm/core/engine_runner.hpp"
18#include "fastmm/sim/recorded_session.hpp"
19#include "fastmm/sim/sim_backend.hpp"
32class BacktestSession {
39 std::string_view strategy_meta = {});
45 [[nodiscard]] sim::SimBackend&
backend() noexcept {
return *backend_; }
56 void enable_ab(
const std::function<std::unique_ptr<ParamPublisher>(
ParamSink)>& make_publisher);
62 IEngineRunner* runner,
63 std::string_view strategy_name);
72 void begin(
const sim::EngineHooks& hooks, IEngineRunner* runner,
const Timestamp* world);
88 [[nodiscard]]
bool stopped() const noexcept;
107 bool advance(sim::SimKey limit);
108 [[nodiscard]]
Timestamp next_stop() const noexcept;
109 std::unique_ptr<Loop> loop_;
115 std::unique_ptr<sim::MarketGenerator> generator_;
116 std::unique_ptr<sim::SimBackend> backend_;
120 std::
string start_note_;
122 std::
string state_what_ = "the state to restore";
126 std::uint32_t trade_venues_ = 0;
127 std::string staged_state_;
128 std::uint32_t staged_version_ = 0;
129 bool staged_empty_ =
false;
132 std::vector<AccountFlowMsg> flows_;
134 std::vector<sim::TransferScope> scopes_;
135 std::vector<std::pair<VenueId, Timestamp>> refreshes_;
137 std::vector<std::vector<std::uint64_t>> controls_;
140 std::vector<ParamUpdateMsg> journal_params_;
141 [[nodiscard]]
bool journal_param(
ParamUpdateMsg& m)
const noexcept;
147 [[nodiscard]]
bool replays_control(
const EventHeader& h)
const;
148 void wake_slow_tier();
149 std::vector<int> param_map_;
151 std::vector<std::pair<VenueId, Timestamp>> account_origin_;
152 bool following_ =
false;
153 std::unique_ptr<sim::ParamSchedule> own_params_;
154 std::unique_ptr<Impl> impl_;
157 sim::SlowHooks slow_{};
159 std::unique_ptr<AbRun> ab_;
162template <StrategyLike S>
163BacktestResult
run_backtest(
const BacktestConfig& cfg, MdSource* source =
nullptr) {
164 BacktestSession session(cfg, source, &S::schema());
165 if (cfg.publishes_params()) session.enable_ab(detail::publisher_factory<S>());
166 std::unique_ptr<IEngineRunner> runner = session.backend().template make_runner<S>(session.deps());
167 return session.run(session.backend().hooks, runner.get(), S::name());
173 std::string_view strategy,
174 MdSource* source =
nullptr);
177template <StrategyLike S>
203 std::span<const std::uint64_t> by_instrument);
209[[nodiscard]] std::unique_ptr<MdSource>
open_data(std::string_view spec,
BacktestResult (8.4): structure-of-arrays rows (numpy-friendly: each column is one contiguous vector)...
BacktestConfig: everything a run needs, assembled from the TOML sections [engine] [[instruments]] [st...
Dense-by-id table built at startup; symbol lookup is a sorted FlatMap keyed by (venue,...
Definition instrument.hpp:147
Definition params.hpp:102
Convenience wrapper that owns the config.
Definition backtest_runner.hpp:180
BacktestResult run(MdSource *source=nullptr)
Definition backtest_runner.hpp:185
BacktestConfig & config() noexcept
Definition backtest_runner.hpp:183
Definition backtest_runner.hpp:34
void world_event(const EventHeader &md) noexcept
void reach(Timestamp t) noexcept
The clock at the last event the run shared (which it may not have had to take).
const FillRows & fill_rows() const noexcept
What the run has so far: the fills up to the event it last took and the equity bars it has passed.
void take_record(const EventHeader &record)
A run over a journal that is being followed (backtest/follow_source.hpp): a record of the session it ...
void set_slow_hooks(const sim::SlowHooks &hooks) noexcept
Code run at simulated times during run() (sim::SimDriver::set_slow_hooks).
Definition backtest_runner.hpp:60
bool stopped() const noexcept
The engine asked to stop: the run takes no further event.
void enable_ab(StrategyEntry::PublisherFactory publisher)
[ab] and [strategy.overrides]: builds the run's publisher with publisher (StrategyEntry::publisher),...
std::size_t forget_rows(std::size_t fills, std::size_t bars)
Returns how many fills it forgot.
void begin(const sim::EngineHooks &hooks, IEngineRunner *runner, const Timestamp *world)
run() in steps, for a multiverse (multiverse.hpp), whose runs share the recorded events and the marke...
void shared_market_data(const EventHeader &md) noexcept
sim::SimKey step_key()
After shared_market_data(): the earliest event the engine's step added (SimDriver::step_key).
RunnerDeps & deps() noexcept
Definition backtest_runner.hpp:46
BacktestResult run(const sim::EngineHooks &hooks, IEngineRunner *runner, std::string_view strategy_name)
Runs to the end of the data / horizon.
BacktestSession & operator=(const BacktestSession &)=delete
const EquityRows & equity_rows() const noexcept
bool advance_before(sim::SimKey k)
void world_venue_event(const EventHeader &md) noexcept
BacktestResult end(std::string_view strategy_name)
sim::SimBackend & backend() noexcept
Definition backtest_runner.hpp:47
void world_feed_event(const EventHeader &md) noexcept
sim::SimKey next_key()
The key of this run's own next event or stop (equity bar, markout); never() once it is over.
void set_param_schedule(sim::ParamSchedule *schedule) noexcept
Parameter updates delivered at simulated times during run() (not owned; may be null).
Definition backtest_runner.hpp:49
void credit_market_data(std::uint64_t n) noexcept
Market data the multiverse did not hand the run, which its engine did not need (SimDriver::credit_mar...
std::uint64_t orders_sent() const noexcept
The order messages the run has sent.
Definition synthetic_source.hpp:31
Definition param_schedule.hpp:38
DataSource (8.4): a timestamp-ordered stream of normalized market-data events consumed by the simulat...
Strategy modules (ADR-0012, section 5): one registration function per strategy library.
Definition backtest_config.hpp:44
std::unique_ptr< MdSource > open_data(std::string_view spec, const InstrumentTable *instruments=nullptr)
Resolves --data through the data-source registry (data_registry.hpp): "<name>:<args>",...
void warn_unfed_instruments(const InstrumentTable &instruments, std::span< const std::uint64_t > by_instrument)
Logs a warning for each instrument the data gave no events, and for the events that named an instrume...
BacktestConfig shared_start(BacktestConfig cfg)
cfg for runs that start from one place: the points of a sweep, the folds of a walk-forward,...
SyntheticSourceConfig synthetic_source_config(const BacktestConfig &cfg)
The SyntheticSource a run with source == nullptr replays under the L2 queue fill model.
BacktestResult run_backtest(const BacktestConfig &cfg, MdSource *source=nullptr)
Definition backtest_runner.hpp:165
std::unique_ptr< MdSource > open_source(const BacktestConfig &cfg)
Source for [backtest] source / path.
Strategy registry (8.6, ADR-0012): name -> parameter schema + one factory per transport kind,...
Where a built update goes; push returns false when it was not accepted (full).
Definition param_publisher.hpp:49
New strategy parameter values (ADR-0013): up to kMaxFields (field index, raw value) pairs,...
Definition messages.hpp:795
Control thread injects the venue's view after a reconnect (5.8).
Definition messages.hpp:668
Everything a factory needs that is backend-independent.
Definition registry.hpp:51
std::unique_ptr< ParamPublisher >(*)(ParamSink, const ParamMap &, std::size_t instruments) PublisherFactory
Builds a publisher for this strategy's parameters over sink, its copy seeded with the values the sess...
Definition registry.hpp:74
Definition backtest_config.hpp:80
Definition result.hpp:116
Definition synthetic_source.hpp:20
SyntheticSource: a MarketGenerator run against its own MatchingEngine, published through MdAggregator...