Search Framework:
WFOOptimizer
Namespace: WealthLab.Backtest
Parent: OptimizationRunnerBase

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.

Base Class Members
BacktestSettings
public BacktestSettings BacktestSettings

Returns the BacktestSettings used for the optimization.


CompiledStrategy
public StrategyBase CompiledStrategy

Returns the compiled StrategyBase instance being optimized.


OptimizationMethod
public OptimizerBase OptimizationMethod

Returns the Optimizer selected for the optimization.


OptimizerHost
public IOptimizerHost OptimizerHost

Returns the IOptimizerHost that hosts the WFO optimization.


PositionSize
public PositionSize PositionSize

Returns the PositionSize used during the optimization.


ReportEstimatedCompletion
public void ReportEstimatedCompletion(double value)

Allows the optimization process to report an estimated completion percentage. The value parameter is expressed from 0 to 100.


ScoreCard
public ScoreCardBase ScoreCard

Returns the ScoreCard selected for the optimization.


SymbolData
public List<BarHistory> SymbolData

Contains the historical BarHistory instances being optimized.



Members
CreateResultList
public static List<WFOResult> CreateResultList(
List<DateTime> lst,
int intervals,
double oosPercent,
WFOWindowType wt,
IOptimizerHost host,
bool preLive)

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.


DataSet
public DataSet DataSet

Gets or sets the DataSet associated with the Walk-Forward Optimization. WealthLab uses this when loading benchmark and optimization data for individual WFO intervals.


DataSetName
public string DataSetName

Contains the name of the DataSet associated with persisted WFO results. This property is populated when a saved WFOOptimizer is restored using Parse.


GenerateResultList
public WFOParametersList GenerateResultList()

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.

InSamplePercent
public double InSamplePercent

Returns the percentage of each WFO window allocated to in-sample optimization. It is calculated as:

100.0 - OutOfSamplePercent

Intervals
public int Intervals

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.


IsCancelled
public bool IsCancelled

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.


Mode
public string Mode

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.


OutOfSamplePercent
public double OutOfSamplePercent

Gets or sets the percentage of each WFO window allocated to out-of-sample testing. InSamplePercent is automatically calculated from this value.


PerformOptimization
public override void PerformOptimization(
IOptimizerHost host,
PositionSize posSize,
BacktestSettings bts,
bool resuming)

Executes the Walk-Forward Optimization. For each interval, WFOOptimizer:

  1. Filters the optimization data to the in-sample date range.
  2. Runs a StrategyOptimizer over the in-sample data.
  3. Uses Selector and Mode to select the best OptimizationResult.
  4. Applies the selected parameter values to the corresponding out-of-sample period.
  5. Executes an out-of-sample backtest.
  6. Stores the in-sample and out-of-sample results in WFOResults.
  7. 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.

PreLiveRun
public bool PreLiveRun

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.


Selector
public WFOResultSelectorBase Selector

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.


WFOResults
public List<WFOResult> WFOResults

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.


WindowType
public WFOWindowType WindowType

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.



Optimizer Host Members
BenchmarkBacktester
public Backtester BenchmarkBacktester

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.


MetricNames
public List<string> MetricNames

Returns the metric names exposed by the parent OptimizerHost.


OptimizableParameters
public new ParameterList OptimizableParameters

Returns the Strategy's optimizable parameters from the parent OptimizerHost.


ReportProgress
public void ReportProgress(double pctCompleted)

Forwards optimization progress from the StrategyOptimizer running the current WFO interval to the parent OptimizerHost.



Persistence
Parse
public void Parse(string s)

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.

Persist
public string Persist()

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.