23using ParamGrid = std::vector<std::pair<std::string, std::vector<std::string>>>;
26using SourceFactory = std::function<std::unique_ptr<MdSource>()>;
55std::vector<SweepPoint>
sweep(
const BacktestConfig& base,
62template <StrategyLike S>
63std::vector<SweepPoint>
sweep(
const BacktestConfig& base,
71 [](
const BacktestConfig& c, MdSource* s) {
return run_backtest<S>(c, s); },
78std::vector<SweepPoint>
sweep_by_name(
const BacktestConfig& base,
83 bool separately =
false,
85std::vector<SweepPoint>
sweep_by_name(
const BacktestConfig& base,
90 bool separately =
false,
98using ScoreFn = std::function<double(
const BacktestResult&)>;
110 double in_sample = std::numeric_limits<double>::quiet_NaN();
112 double hindsight = std::numeric_limits<double>::quiet_NaN();
126 [[nodiscard]] std::string
label(
const ParamMap& params)
const;
128 [[nodiscard]] std::string
table()
const;
142 std::unique_ptr<MdSource> inner,
166 std::string_view metric =
"net_pnl",
173 std::string_view metric =
"net_pnl",
180 std::string strategy,
184 std::string_view metric =
"net_pnl",
188 std::string strategy,
192 std::string_view metric =
"net_pnl",
BacktestRunner (8.4): wires Engine<S, SimClock, SimTransport, InlineFeed> to a SimDriver and collects...
Definition backtest_config.hpp:44
std::vector< SweepPoint > sweep_by_name(const BacktestConfig &base, std::string strategy, const SweepPoints &points, const SourceFactory &source_factory={}, int threads=0, bool separately=false, const ResultHook &done={})
A registered strategy's sweep.
SweepPoints expand_grid(const ParamGrid &grid)
Expands the grid in order: [{k1: v11, k2: v21}, {k1: v11, k2: v22}, ...].
void score_walk_forward(WalkForwardReport &rep, const ScoreFn &score_fn={})
Scores the points of every fold of rep by score_fn, or by the metric named rep.metric when score_fn i...
std::unique_ptr< MdSource > fold_source(const BacktestConfig &base, std::unique_ptr< MdSource > inner, Timestamp t0, Timestamp t1)
The cursor a walk-forward fold reads: inner from t0 to t1, owning it.
std::function< BacktestResult(const BacktestConfig &, MdSource *)> RunFn
Definition sweep.hpp:29
std::function< void(std::size_t index, BacktestResult &result)> ResultHook
Called with each point's result as it is done (index in the list of points or configurations),...
Definition sweep.hpp:39
std::function< std::unique_ptr< MdSource >()> SourceFactory
may return nullptr
Definition sweep.hpp:28
std::vector< Timestamp > fold_edges(const BacktestConfig &base, const SourceFactory &source_factory, int folds)
The k + 1 edges of folds equal slices of the data's event-time range: fold i is [edges[i],...
std::vector< std::pair< std::string, std::vector< std::string > > > ParamGrid
Definition sweep.hpp:25
std::vector< SweepPoint > sweep(const BacktestConfig &base, const SweepPoints &points, const SourceFactory &source_factory, const RunFn &run, int threads=0, const ResultHook &done={})
Generic driver; run is called once per point on a worker thread.
std::function< double(const BacktestResult &)> ScoreFn
Ranking metric of a run, higher is better.
Definition sweep.hpp:99
WalkForwardReport walk_forward_by_name(const BacktestConfig &base, std::string strategy, const SweepPoints &points, const SourceFactory &source_factory, int folds, std::string_view metric="net_pnl", int threads=0, const ResultHook &done={})
walk_forward() of a registered strategy.
WalkForwardReport walk_forward(const BacktestConfig &base, const SweepPoints &points, const SourceFactory &source_factory, const RunFn &run, int folds, std::string_view metric="net_pnl", int threads=0)
Splits the data's event-time range into folds equal slices and runs the whole grid on each one (a fre...
std::vector< ParamMap > SweepPoints
The points of a sweep, in order: a grid expanded (expand_grid) or any list.
Definition sweep.hpp:27
BacktestConfig point_config(const BacktestConfig &start, const ParamMap &point)
The configuration a point runs: start (shared_start() of the sweep's base) with each key of point set...
ScoreFn score_metric(std::string_view name)
std::map< std::string, std::string > ParamMap
Definition params.hpp:119
Definition backtest_config.hpp:80
Definition result.hpp:116
BacktestResult result
Definition sweep.hpp:33
ParamMap params
the grid values applied for this point
Definition sweep.hpp:32
double in_sample
Definition sweep.hpp:111
std::int64_t start_ts
[start_ts, end_ts) of event time, ns
Definition sweep.hpp:103
std::int64_t end_ts
Definition sweep.hpp:104
std::size_t best
in-sample best of this fold (lowest grid index on ties)
Definition sweep.hpp:107
double hindsight
Definition sweep.hpp:113
std::vector< double > scores
the metric of each point (NaN ranks last)
Definition sweep.hpp:106
std::vector< SweepPoint > points
every grid point run on this fold alone, grid order
Definition sweep.hpp:105
double out_of_sample
Definition sweep.hpp:112
std::size_t chosen
Folds >= 1: the point chosen on the previous fold, its score there (in-sample), its score here (out-o...
Definition sweep.hpp:110
std::string label(const ParamMap ¶ms) const
"k1=v1 k2=v2" of one grid point, in grid order.
std::vector< std::string > param_names
grid order; of a list of points, every key they set
Definition sweep.hpp:118
double mean_in_sample
Means over folds 1..K-1 (NaN with one fold).
Definition sweep.hpp:121
std::size_t choice_changes
folds i >= 2 whose chosen point differs from fold i-1's
Definition sweep.hpp:124
double mean_hindsight
Definition sweep.hpp:123
std::string table() const
One line per fold plus the summary.
double mean_out_of_sample
Definition sweep.hpp:122
std::string metric
Definition sweep.hpp:117
std::vector< WalkForwardFold > folds
Definition sweep.hpp:119