Optimization

Local optimizations

Introduction

The Lean CLI supports optimizing a project's parameters locally using LEAN's powerful optimization strategies. Optimization is helpful when you want to find the best combination of parameters to minimize or maximize a certain statistic, like the algorithm's Sharpe ratio or drawdown.

Running local optimizations

Follow these steps to run a local optimization:

  1. Set up your local data for all the data required by your project.
  2. Convert your project to use project parameters instead of constants for all values that must be optimized.
  3. Open a terminal in the directory you ran lean init in.
  4. Run lean optimize "My Project" to start optimizing the project in ./My Project. This command starts an interactive wizard which lets you configure the optimizer.
    $ lean optimize "My Project"
    Select the optimization strategy to use:
    1) Grid Search
    2) Euler Search
    Enter an option:
  5. Enter the number of the optimization strategy to use. You can either choose for Grid Search, which runs through all possible combinations of parameters, or for Euler Search, which performs an Euler-like which gradually works towards smaller optimizations.
    $ lean optimize "My Project"
    Select the optimization strategy to use:
    1) Grid Search
    2) Euler Search
    Enter an option: 1
  6. Enter the number of the optimization target to use. The target specifies what statistic you want to optimize and whether you want to minimize or maximize it.
    $ lean optimize "My Project"
    Select an optimization target:
    1) Sharpe Ratio (min)
    2) Sharpe Ratio (max)
    3) Compounding Annual Return (min)
    4) Compounding Annual Return (max)
    5) Probabilistic Sharpe Ratio (min)
    6) Probabilistic Sharpe Ratio (max)
    7) Drawdown (min)
    8) Drawdown (max)
    Enter an option: 2
  7. For each parameter, enter whether you want to optimize it and what its values can be.
    $ lean optimize "My Project"
    Should the 'ema-fast' parameter be optimized? [Y/n]: y
    Minimum value for 'ema-fast': 1
    Maximum value for 'ema-fast': 10
    Step size for 'ema-fast' [1.0]: 1
  8. Enter the constraints of the optimization. An example optimization is "Drawdown <= 0.25", which discards all parameter combinations resulting in a drawdown higher than 25%.
    $ lean optimize "My Project"
    Current constraints: None
    Do you want to add a constraint? [y/N]: y
    Select a constraint target:
    1) Sharpe Ratio
    2) Compounding Annual Return
    3) Probabilistic Sharpe Ratio
    4) Drawdown
    Enter an option: 4
    Select a constraint operator (<value> will be asked after this):
    1) Less than <value>
    2) Less than or equal to <value>
    3) Greater than <value>
    4) Greater than or equal to <value>
    5) Equal to <value>
    6) Not equal to <value>
    Enter an option: 2
    Set the <value> for the selected operator: 0.25
    Current constraints: TotalPerformance.PortfolioStatistics.Drawdown <= 0.25
    Do you want to add a constraint? [y/N]: n
    After configuring the constraints the optimizer starts running.
  9. View the results in the terminal after the optimizer finished. The last line of LEAN's logs contains the optimal parameter combination.
    $ lean optimize "My Project"
    20210323 23:24:21.796 TRACE:: Program.Main(): Exiting Lean...
    20210323 23:24:22.020 TRACE:: LeanOptimizer.TriggerOnEndEvent(OID 26f0efc9-c7d2-46ad-864e-f036edd04a04): Optimization has ended. Result for Target: ['TotalPerformance'].['PortfolioStatistics'].['SharpeRatio']
    at: 0: was reached using ParameterSet: (ema-fast:6) backtestId 'bd9ad642-1d08-42ef-81fd-9dc3708c9619'. Constraints: (['TotalPerformance'].['PortfolioStatistics'].['Drawdown'] 'LessOrEqual' 0.25)
    Successfully optimized 'My Project' and stored the output in 'My Project/optimizations/2021-03-24_00-22-15'
    
  10. View the individual backtest results in the <project>/optimizations/<timestamp> directory. Results are stored in JSON files and can be analyzed in a local research environment. You can save results to a different directory by providing the --output <path> option in step 4.
    $ lean optimize "My Project" --output "My Project/custom-output"
    20210323 23:24:21.796 TRACE:: Program.Main(): Exiting Lean...
    20210323 23:24:22.020 TRACE:: LeanOptimizer.TriggerOnEndEvent(OID 26f0efc9-c7d2-46ad-864e-f036edd04a04): Optimization has ended. Result for Target: ['TotalPerformance'].['PortfolioStatistics'].['SharpeRatio']
    at: 0: was reached using ParameterSet: (ema-fast:6) backtestId 'bd9ad642-1d08-42ef-81fd-9dc3708c9619'. Constraints: (['TotalPerformance'].['PortfolioStatistics'].['Drawdown'] 'LessOrEqual' 0.25)
    Successfully optimized 'My Project' and stored the output in 'My Project/custom-output'

By default, local optimizations run in the LEAN engine in the quantconnect/lean Docker image. This Docker image contains all the libraries available on QuantConnect, meaning your algorithm also has access to those libraries. If the specified project is a C# project it is first compiled using the same Docker image. See Custom libraries to learn how to use custom libraries, and Custom Docker images to learn how to build and use custom Docker images.

You can also see our Videos. You can also get in touch with us via Chat.

Did you find this page helpful?

Contribute to the documentation: