Search Framework:
StrategyRunner
Namespace: WealthLab.Backtest
Parent: Object

A helper class that provides a simplified way to run Strategy backtests and optimizations programmatically. StrategyRunner can be used from a WealthLab Extension or directly from Strategy code. Before running a backtest or optimization, configure its data source, scale, data range, position sizing, backtest settings, and benchmark as required. StrategyRunner implements IOptimizationHost and IOptimizerHost so that it can host and execute a StrategyOptimizer programmatically.

Members
BacktestSettings
public BacktestSettings BacktestSettings

Determines the BacktestSettings to use for backtests and optimizations. The default value is BacktestSettings.Instance.


BenchmarkData
public BarHistory BenchmarkData

Specifies a BarHistory instance to use for the benchmark backtest. Benchmark performance is used when calculating certain performance metrics such as Alpha and Beta. If this property is null or empty, StrategyRunner attempts to load benchmark data using the symbol specified in BenchmarkSymbol.


BenchmarkSymbol
public string BenchmarkSymbol

Specifies the symbol to use for the benchmark backtest. If BenchmarkData is null or empty, StrategyRunner attempts to load historical data for this symbol using the configured Scale and DataRange. The default value is "SPY".


Data
public List<BarHistory> Data

Contains the BarHistory instances to use as the source data for a backtest or optimization. You can populate this property directly when you already have the historical data you want StrategyRunner to use. If Data is null or empty, StrategyRunner attempts to obtain historical data from DataSet first, followed by Symbols.


DataRange
public DataRange DataRange

Determines the historical data range to use when StrategyRunner loads data. The default range is the most recent 10 years.


DataSet
public DataSet DataSet

Specifies a DataSet to use as the source of historical data. If Data is null or empty and this property is assigned, StrategyRunner loads historical data for the DataSet's symbols using the configured Scale and DataRange. If no data can be obtained from the DataSet, StrategyRunner next attempts to load the symbols contained in Symbols.


PerformOptimization
public OptimizationResultList PerformOptimization(
string optMethod,
string stratName,
OptimizationRunComplete runComplete = null,
List<object> optParamValues = null)
public OptimizationResultList PerformOptimization(
string optMethod,
Strategy s,
OptimizationRunComplete runComplete = null,
List<object> optParamValues = null)

Performs an optimization using the Optimization Method specified by name in optMethod. There are two overloads. The first specifies the Strategy by its saved Strategy name in stratName. The second accepts a Strategy instance in s. Before calling this method, configure the StrategyRunner's data source, scale, range, position sizing, backtest settings, and benchmark as required. The optional runComplete parameter can specify an OptimizationRunComplete delegate. StrategyRunner calls this delegate after each optimization run completes. The callback receives the completed OptimizationResult and a double containing the estimated percentage of the overall optimization that has completed. The optional optParamValues parameter is a List of objects containing values for the selected Optimizer's Parameters. The values correspond by index to the Parameters exposed by the Optimizer. Strategy parameters that are disabled for optimization remain disabled when the optimization is executed through StrategyRunner. Returns an OptimizationResultList containing the results of the optimization runs.


PositionSize
public PositionSize PositionSize

Determines the PositionSize to use for backtests and optimizations. The default position size is 10% of equity.


RunBacktest
public Backtester RunBacktest(string strategyName)
public Backtester RunBacktest(
StrategyBase sb,
Strategy s = null,
StrategyExecutionMode em = StrategyExecutionMode.Strategy)

Runs a Strategy backtest and returns the resulting Backtester instance. The first overload accepts the name of a saved WealthLab Strategy. StrategyRunner locates the Strategy, creates an executable instance, and runs the backtest. The second overload accepts an already compiled StrategyBase instance in sb. The optional s parameter specifies the Strategy associated with the compiled instance, while em specifies the StrategyExecutionMode. Before executing the Strategy, StrategyRunner obtains the required historical and benchmark data and creates a Backtester using the configured PositionSize and BacktestSettings. After the Strategy backtest completes, StrategyRunner performs the benchmark backtest and assigns the results to the Backtester's BenchmarkBacktestResults property. You can examine the returned Backtester, including its Metrics property, to obtain the performance results of the backtest.


Scale
public HistoryScale Scale

Determines the historical data scale, or frequency, to use when StrategyRunner loads data. The default value is HistoryScale.Daily. For intraday scales, StrategyRunner uses the scale's pre/post-market filtering setting when requesting historical data.


Symbols
public List<string> Symbols

Contains symbols that StrategyRunner can use as the source of historical data. If Data is null or empty and no usable data is obtained from DataSet, StrategyRunner attempts to load historical data for the symbols contained in this list using the configured Scale and DataRange.



Optimization Host Members
BenchmarkBacktester
public Backtester BenchmarkBacktester

Returns the Backtester containing the benchmark results used during the current optimization.


MetricNames
public List<string> MetricNames

Returns the currently selected ScoreCard metric names used during the optimization.


OptimizableParameters
public ParameterList OptimizableParameters

Returns the Strategy Parameters that are eligible for optimization.


OptimizationMethod
public OptimizerBase OptimizationMethod

Returns the OptimizerBase instance being used for the current optimization.


Strategy
public Strategy Strategy

Returns the Strategy currently being optimized.


StrategyOptimizer
public StrategyOptimizer StrategyOptimizer

Returns the StrategyOptimizer instance being used for the current optimization.