22using ParamGrid = std::vector<std::pair<std::string, std::vector<std::string>>>;
23using SourceFactory = std::function<std::unique_ptr<MdSource>()>;
42template <StrategyLike S>
51 [](
const BacktestConfig& c, MdSource* s) {
return run_backtest<S>(c, s); },
55inline std::vector<SweepPoint>
sweep_by_name(
const BacktestConfig& base,
64 [strategy = std::move(strategy)](
const BacktestConfig& c, MdSource* s) {
74using ScoreFn = double (*)(
const BacktestResult&);
86 double in_sample = std::numeric_limits<double>::quiet_NaN();
88 double hindsight = std::numeric_limits<double>::quiet_NaN();
94 std::vector<WalkForwardFold>
folds;
102 [[nodiscard]] std::string
label(
const ParamMap& params)
const;
104 [[nodiscard]] std::string
table()
const;
119 std::string_view metric =
"net_pnl",
123 std::string strategy,
127 std::string_view metric =
"net_pnl",
133 [strategy = std::move(strategy)](
const BacktestConfig& c, MdSource* s) {
BacktestRunner (8.4): wires Engine<S, SimClock, SimTransport, InlineFeed> to a SimDriver and collects...
Definition backtest_config.hpp:39
WalkForwardReport walk_forward_by_name(const BacktestConfig &base, std::string strategy, const ParamGrid &grid, const SourceFactory &source_factory, int folds, std::string_view metric="net_pnl", int threads=0)
Definition sweep.hpp:123
std::vector< ParamMap > expand_grid(const ParamGrid &grid)
Expands the grid in order: [{k1: v11, k2: v21}, {k1: v11, k2: v22}, ...].
WalkForwardReport walk_forward(const BacktestConfig &base, const ParamGrid &grid, 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::function< BacktestResult(const BacktestConfig &, MdSource *)> RunFn
Definition sweep.hpp:26
std::function< std::unique_ptr< MdSource >()> SourceFactory
may return nullptr
Definition sweep.hpp:25
double(*)(const BacktestResult &) ScoreFn
Ranking metric of a run, higher is better: "net_pnl", "realized_pnl", "sharpe_bar" or "spread_capture...
Definition sweep.hpp:75
std::vector< std::pair< std::string, std::vector< std::string > > > ParamGrid
Definition sweep.hpp:24
std::vector< SweepPoint > sweep_by_name(const BacktestConfig &base, std::string strategy, const ParamGrid &grid, const SourceFactory &source_factory={}, int threads=0)
Definition sweep.hpp:57
BacktestResult run_backtest(const BacktestConfig &cfg, MdSource *source=nullptr)
Definition backtest_runner.hpp:74
std::vector< SweepPoint > sweep(const BacktestConfig &base, const ParamGrid &grid, const SourceFactory &source_factory, const RunFn &run, int threads=0)
Generic driver; run is called once per point on a worker thread.
ScoreFn score_metric(std::string_view name)
std::map< std::string, std::string > ParamMap
Definition params.hpp:119
Definition backtest_config.hpp:48
Definition result.hpp:113
BacktestResult result
Definition sweep.hpp:30
ParamMap params
the grid values applied for this point
Definition sweep.hpp:29
double in_sample
Definition sweep.hpp:87
std::int64_t start_ts
[start_ts, end_ts) of event time, ns
Definition sweep.hpp:79
std::int64_t end_ts
Definition sweep.hpp:80
std::size_t best
in-sample best of this fold (lowest grid index on ties)
Definition sweep.hpp:83
double hindsight
Definition sweep.hpp:89
std::vector< double > scores
the metric of each point (NaN ranks last)
Definition sweep.hpp:82
std::vector< SweepPoint > points
every grid point run on this fold alone, grid order
Definition sweep.hpp:81
double out_of_sample
Definition sweep.hpp:88
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:86
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
Definition sweep.hpp:94
double mean_in_sample
Means over folds 1..K-1 (NaN with one fold).
Definition sweep.hpp:97
std::size_t choice_changes
folds i >= 2 whose chosen point differs from fold i-1's
Definition sweep.hpp:100
double mean_hindsight
Definition sweep.hpp:99
std::string table() const
One line per fold plus the summary.
double mean_out_of_sample
Definition sweep.hpp:98
std::string metric
Definition sweep.hpp:93
std::vector< WalkForwardFold > folds
Definition sweep.hpp:95