The WFOOptimizer class performs Walk-Forward Optimization (WFO). A WFO divides the available historical data into a sequence of in-sample and out-of-sample intervals. For each interval, WealthLab optimizes the Strategy using the in-sample period, selects the best parameter set using a WFOResultSelectorBase, and then applies those parameters to the corresponding out-of-sample period. WFOOptimizer collects the resulting WFOResult instances for use by Walk-Forward Optimization Visualizers and other extension code. WFOOptimizer implements IOptimizerHost because each in-sample interval is executed internally by a StrategyOptimizer.
Returns the BacktestSettings used for the optimization.
Returns the compiled StrategyBase instance being optimized.
Returns the Optimizer selected for the optimization.
Returns the IOptimizerHost that hosts the WFO optimization.
Returns the PositionSize used during the optimization.
Allows the optimization process to report an estimated completion percentage. The value parameter is expressed from 0 to 100.
Returns the ScoreCard selected for the optimization.
Contains the historical BarHistory instances being optimized.
Creates a list of WFOResult instances containing the date ranges for a Walk-Forward Optimization. The returned objects define the in-sample and out-of-sample periods but do not yet contain optimization performance results. lst supplies the DateTimes spanning the data being optimized. intervals specifies the number of walk-forward intervals. oosPercent specifies the percentage of each window allocated to out-of-sample testing. wt specifies whether the WFO uses Sliding or Expanding windows. If preLive is true, an additional Pre-Live interval is added after the final standard WFO interval. For an Expanding WFO, all in-sample intervals use the same initial start date while their end dates advance through time.
Gets or sets the DataSet associated with the Walk-Forward Optimization. WealthLab uses this when loading benchmark and optimization data for individual WFO intervals.
Contains the name of the DataSet associated with persisted WFO results. This property is populated when a saved WFOOptimizer is restored using Parse.
Creates a WFOParametersList from the completed WFO results. The returned list contains the parameter values selected for each out-of-sample interval. For each applicable WFOResult, WealthLab stores:
- OutOfSampleStartDate
- OutOfSampleEndDate
- ParameterValues
- whether the result represents a Pre-Live run Normal in-sample results are excluded. This method is useful when applying the optimized parameter schedule to subsequent Strategy execution.
Returns the percentage of each WFO window allocated to in-sample optimization. It is calculated as:
100.0 - OutOfSamplePercent
Gets or sets the number of intervals in the Walk-Forward Optimization. Each normal interval consists of an in-sample optimization period followed by an out-of-sample test period.
Gets or sets whether the Walk-Forward Optimization has been canceled. The optimization checks this property between interval runs and stops processing when it becomes true. The default value is false.
Gets or sets the Selector mode used to choose the best OptimizationResult for each in-sample interval. The exact meaning of Mode depends on the selected Selector. For example, a Selector based on ScoreCard metrics can use Mode to identify which metric should be maximized or minimized when selecting the best optimization result.
Gets or sets the percentage of each WFO window allocated to out-of-sample testing. InSamplePercent is automatically calculated from this value.
Executes the Walk-Forward Optimization. For each interval, WFOOptimizer:
- Filters the optimization data to the in-sample date range.
- Runs a StrategyOptimizer over the in-sample data.
- Uses Selector and Mode to select the best OptimizationResult.
- Applies the selected parameter values to the corresponding out-of-sample period.
- Executes an out-of-sample backtest.
- Stores the in-sample and out-of-sample results in WFOResults.
- Calculates WFO Efficiency where possible. If resuming is true, WealthLab attempts to continue a previously saved incomplete WFO instead of rerunning completed intervals. WFO results are automatically saved as processing progresses.
Gets or sets whether WealthLab should perform an additional Pre-Live optimization after the normal WFO intervals.
The default value is false.
A Pre-Live run performs another in-sample optimization using the most recent available historical data but does not have a normal historical out-of-sample period.
Its resulting parameter values can be used as the parameters intended for current or subsequent live execution.
When a Pre-Live result is created, its out-of-sample range is represented using DateTime.MinValue and DateTime.MaxValue.
Gets or sets the WFOResultSelectorBase used to choose the best OptimizationResult from each in-sample optimization. The Selector works in conjunction with Mode to determine which result should be carried forward into the corresponding out-of-sample run.
Contains the WFOResult instances generated during the Walk-Forward Optimization. The collection contains both in-sample and out-of-sample results. After the optimization completes, results are sorted by interval number, with the in-sample result preceding the corresponding out-of-sample result. If Pre-Live processing is enabled, its result is also included.
Gets or sets the type of Walk-Forward Optimization window. Possible values include:
WFOWindowType.Sliding
WFOWindowType.Expanding
With a Sliding window, both the beginning and end of the in-sample period move forward for each interval. With an Expanding window, the in-sample start date remains fixed while the end date advances with each interval.
Returns the benchmark Backtester for the WFO interval currently being processed. The internal StrategyOptimizer uses this benchmark when calculating optimization results for the current window.
Returns the metric names exposed by the parent OptimizerHost.
Returns the Strategy's optimizable parameters from the parent OptimizerHost.
Forwards optimization progress from the StrategyOptimizer running the current WFO interval to the parent OptimizerHost.
Restores the WFOOptimizer state from a string previously created by Persist. The restored information includes:
- Intervals
- OutOfSamplePercent
- WindowType
- Selector
- Mode
- DataSetName
- WFOResults
- current resume state
- completed out-of-sample results
- PreLiveRun This state allows WealthLab to resume an incomplete Walk-Forward Optimization.
Serializes the current WFOOptimizer state to a string. In addition to the WFO configuration and completed WFOResults, the serialized data includes enough state to support resuming an interrupted optimization.