FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
sweep.hpp File Reference

Parameter sweep (8.4/8.5): a list of points, or the cartesian grid that expands to one, over strategy parameters and backtest settings, run on a std::jthread pool; every worker owns its own data cursor (source_factory) and backend, results come back in the points' order (a grid's first parameter varies slowest). More...

#include "fastmm/backtest/backtest_runner.hpp"
#include <cstddef>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

Go to the source code of this file.

Classes

struct  fastmm::bt::SweepPoint
 
struct  fastmm::bt::WalkForwardFold
 
struct  fastmm::bt::WalkForwardReport
 

Namespaces

namespace  fastmm
 
namespace  fastmm::bt
 

Typedefs

using fastmm::bt::ParamGrid = std::vector< std::pair< std::string, std::vector< std::string > > >
 
using fastmm::bt::SweepPoints = std::vector< ParamMap >
 The points of a sweep, in order: a grid expanded (expand_grid) or any list.
 
using fastmm::bt::SourceFactory = std::function< std::unique_ptr< MdSource >()>
 may return nullptr
 
using fastmm::bt::RunFn = std::function< BacktestResult(const BacktestConfig &, MdSource *)>
 
using fastmm::bt::ResultHook = std::function< void(std::size_t index, BacktestResult &result)>
 Called with each point's result as it is done (index in the list of points or configurations), one call at a time, before it is kept: a caller that needs less of it (the CLI prints the metrics) can drop the rest there, so a sweep of thousands of points holds no more than that.
 
using fastmm::bt::ScoreFn = std::function< double(const BacktestResult &)>
 Ranking metric of a run, higher is better.
 

Functions

SweepPoints fastmm::bt::expand_grid (const ParamGrid &grid)
 Expands the grid in order: [{k1: v11, k2: v21}, {k1: v11, k2: v22}, ...].
 
BacktestConfig fastmm::bt::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, a key of kSweepSettings as that backtest setting (BacktestConfig::set_setting) and any other as a strategy parameter; no output directory, journal or order rows.
 
std::vector< SweepPoint > fastmm::bt::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::vector< SweepPoint > fastmm::bt::sweep (const BacktestConfig &base, const ParamGrid &grid, const SourceFactory &source_factory, const RunFn &run, int threads=0, const ResultHook &done={})
 
template<StrategyLike S>
std::vector< SweepPoint > fastmm::bt::sweep (const BacktestConfig &base, const ParamGrid &grid, const SourceFactory &source_factory={}, int threads=0)
 
std::vector< SweepPoint > fastmm::bt::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.
 
std::vector< SweepPoint > fastmm::bt::sweep_by_name (const BacktestConfig &base, std::string strategy, const ParamGrid &grid, const SourceFactory &source_factory={}, int threads=0, bool separately=false, const ResultHook &done={})
 
ScoreFn fastmm::bt::score_metric (std::string_view name)
 
std::vector< Timestamp > fastmm::bt::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], edges[i + 1]).
 
std::unique_ptr< MdSource > fastmm::bt::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.
 
void fastmm::bt::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 is empty, and fills in best, chosen, the in-sample / out-of-sample / hindsight scores, the means and the choice changes.
 
WalkForwardReport fastmm::bt::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 fresh backtest per point and fold, all on one pool of threads workers).
 
WalkForwardReport fastmm::bt::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)
 
WalkForwardReport fastmm::bt::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 fastmm::bt::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, const ResultHook &done={})
 

Detailed Description

Parameter sweep (8.4/8.5): a list of points, or the cartesian grid that expands to one, over strategy parameters and backtest settings, run on a std::jthread pool; every worker owns its own data cursor (source_factory) and backend, results come back in the points' order (a grid's first parameter varies slowest).

walk_forward() runs the same grid on K consecutive time slices (folds) of the data and scores the in-sample winner of fold i-1 on fold i: an out-of-sample check of the sweep's best row.