Parameter Optimization for Lean Algorithms
Parameter optimization for Lean Trading Algorithms
This toolset allows you to execute multiple parallel backtests using a local Lean clone. It is possible to configure several different optimization methods to fit your trading algorithm to an array of different success measures.
You must edit the config file optimization.json to define parameters and other settings. The parameter values are fed into the Lean config and can be accessed in an algorithm using the QuantConnect.Configuration.Config methods.
An example algorithm is provided here: ParameterizedAlgorithm.cs and here: ParameterizedSharedAppDomainAlgorithm.py
Full documentation is provided in comments: OptimizerConfiguration
A few important options:
The default OptimizerFitness is a simple Sharpe Ratio tournament. There is also CompoundingAnnualReturnFitness to maximize raw returns. It is possible to optimize any Lean statistic using ConfiguredFitness.
Specifying the SharpeMaximizer fitness allows access to all of the optimization methods provided by the SharpLearning library. These include Random Search. Grid Search, Particle Swarm, Smac and several others.
The simple SharpeMaximizer has been extended in NFoldCrossSharpeMaximizer so that the success score is measured over N-fold periods. This will prevent overfitting to a single in-sample period.
Also now available is an experimental release of N-fold Walk Forward optimization.
An attempt to extend the single iteration walk forward optimizer has now evolved into a specialized ensemble machine learning method.
An optimization algorithm must be selected as iteration parent. This optimizer will improve a weighted metric cost function composed of the following: i) The out-sample score of each walk forward period fold, which is derived from an optimization of the in-sample fold using a separately selected optimization algorithm. ii) The standard deviation of the parameter search ranges (the genes) drawn from the best performer in each fold (the alpha).
After each parent iteration, the fold optimization range is constrained with the highest and lowest parameter values of the fold alphas. The effect of this gradual tightening of constraints is analogous to synthesized annealing, allowing convergence on a minimized range of successful parameters.
The parent optimizer will stop early for cases in which all out-sample scores return a failure. This indicates that the in-sample does not generalize and can often be resolved with a longer optimization period.
If it possible to run each parallel backtest in it's own context, or in a single context. The latter option can be useful for training a machine learning model and tends to execute more quickly. For Python algorithms this setting is ignored as only a single context is supported.
Setting this value correctly will prevent fitting to a small number of high-success events that are unlikely to generalize. Any backtest not meeting the minimum trades will be ignored.
When training a non-deterministic machine learning model (such as QLearning), this will allow executing the same parameters multiple times on a single period. By default, duplicate parameters are not executed more than once.
The optimizers support multiple parallel executions of a Lean algorithm as standard. Currently, the following methods are available:
These methods can target several fitness and maximization goals:
Now also provided are several Blazor interfaces: