Optimization
Deployment
Introduction
The Lean CLI supports optimizing a project's parameters on your local machine or in the cloud 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. If your run optimizations in the cloud, you don't need your own powerful machine or data library.
Run Local Optimizations
Follow these steps to run a local optimization:
 Set up your local data for all the data required by your project.
 Convert your project to use project parameters instead of constants for all values that must be optimized.
 Open a terminal in the workspace that contains the project.
 Run
lean optimize "<projectName>"
to start optimizing the project in . / <projectName>. 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:
 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 Eulerlike 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
 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
 For each parameter, enter whether you want to optimize it and what its values can be.
$ lean optimize "My Project" Should the 'emafast' parameter be optimized? [Y/n]: y Minimum value for 'emafast': 5 Maximum value for 'emafast': 10 Step size for 'emafast' [1.0]: 1 Should the 'emamedium' parameter be optimized? [Y/n]: y Minimum value for 'emamedium': 25 Maximum value for 'emamedium': 30 Step size for 'emamedium' [1.0]: 1 Should the 'emaslow' parameter be optimized? [Y/n]: y Minimum value for 'emaslow': 45 Maximum value for 'emaslow': 50 Step size for 'emaslow' [1.0]: 1
 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
 View the results in the terminal after the optimizer finished. The logs contains the optimal parameter combination.
$ lean optimize "My Project" 20220223 18:26:20.000 TRACE:: Program.Main(): Exiting Lean... 20220223 18:26:20.079 TRACE:: LeanOptimizer.TriggerOnEndEvent(OID 2313bbba9c714b9e8b91c70cc117b0c7): Optimization has ended. Result for Target: ['TotalPerformance'].['PortfolioStatistics'].['SharpeRatio'] at: 3.6205: was reached using ParameterSet: (emaslow:47,emamedium:26,emafast:5) backtestId '21c30000dc5a4decb75ada5b1796ccba'. Constraints: (['TotalPerformance'].['PortfolioStatistics'].['Drawdown'] 'LessOrEqual' 0.25) Optimal parameters: emaslow: 47, emamedium: 26, emafast: 5 Successfully optimized 'My Project' and stored the output in 'My Project/optimizations/20210324_002215'

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/customoutput" 20220223 18:28:20.000 TRACE:: Program.Main(): Exiting Lean... 20220223 18:28:20.079 TRACE:: LeanOptimizer.TriggerOnEndEvent(OID 1ac5e638aae04aa980d402c51bb7b84d): Optimization has ended. Result for Target: ['TotalPerformance'].['PortfolioStatistics'].['SharpeRatio'] at: 3.6205: was reached using ParameterSet: (emaslow:47,emamedium:26,emafast:5) backtestId 'e2aa3abfbb604e91a28159c882ada62f'. Constraints: (['TotalPerformance'].['PortfolioStatistics'].['Drawdown'] 'LessOrEqual' 0.25) Optimal parameters: emaslow: 47, emamedium: 26, emafast: 5 Successfully optimized 'My Project' and stored the output in 'My Project/customoutput'
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 Project Libraries to learn how to use project libraries, and Custom Docker Images to learn how to build and use custom Docker images.
Run Cloud Optimizations
Follow these steps to run a cloud optimization:
 Log in to the CLI if you haven't done so already.
 Convert your project to use project parameters instead of constants for all values that must be optimized.
 Open a terminal in the workspace that contains the project.
 Run
lean cloud optimize "<projectName>" push
to push . / <projectName> to the cloud and start optimizing the project in the cloud.$ lean cloud optimize "My Project" push [1/1] Pushing 'My Project' Successfully updated cloud file 'My Project/main.py' Started compiling project 'My Project' Successfully compiled project '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:
 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 cloud optimize "My Project" push 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
 For each parameter, enter whether you want to optimize it and what its values can be.
$ lean cloud optimize "My Project" push Should the 'emafast' parameter be optimized? [Y/n]: y Minimum value for 'emafast': 1 Maximum value for 'emafast': 10 Step size for 'emafast' [1.0]: 1 Should the 'emaslow' parameter be optimized? [Y/n]: y Minimum value for 'emaslow': 21 Maximum value for 'emaslow': 30 Step size for 'emaslow' [1.0]: 1
 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 cloud optimize "My Project" push 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
 Enter the number of the optimization node type to use.
$ lean cloud optimize "My Project" push Select the optimization node type: 1) O28 (2 cores, 8 GB RAM) @ $0.15 per hour 2) O412 (4 cores, 12 GB RAM) @ $0.30 per hour 3) O816 (8 cores, 16 GB RAM) @ $0.60 per hour Enter an option: 2
 Enter the number of nodes that should run in parallel.
$ lean cloud optimize "My Project" push How many nodes should run in parallel (112) [6]: 10
 Confirm the given input to start the optimizer.
$ lean cloud optimize "My Project" push Estimated number of backtests: 100 Estimated batch time: 8 minutes Estimated batch cost: $0.38 Organization balance: 173,368 QCC ($1,733.68) Do you want to start the optimization on the selected node type? [Y/n]: y
 Inspect the optimal parameter combination and the full statistics of the backtest that ran with this combination at the bottom of the logs when the optimizer has finished.