Optimization

Cloud optimizations

Introduction

The Lean CLI supports optimizing a project's parameters in the cloud, which allows you to optimize your algorithm without needing your own powerful machine or data library. 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 cloud optimizations

Follow these steps to run a cloud optimization:

  1. Log in to the CLI if you haven't done so already.
  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 cloud optimize "My Project" --push to push ./My Project to the cloud, 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:
  5. 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
  6. For each parameter, enter whether you want to optimize it and what its values can be.
    $ lean cloud optimize "My Project" --push
    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
  7. 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
  8. Enter the number of the optimization node type to use.
    $ lean cloud optimize "My Project" --push
    Select the optimization node type:
    1) O2-8 (2 cores, 8 GB RAM) @ $0.15 per hour
    2) O4-12 (4 cores, 12 GB RAM) @ $0.30 per hour
    3) O8-16 (8 cores, 16 GB RAM) @ $0.60 per hour
    Enter an option: 2
  9. Enter the number of nodes that should run in parallel.
    $ lean cloud optimize "My Project" --push
    How many nodes should run in parallel (1-12) [6]: 10
  10. Confirm the given input to start the optimizer.
    $ lean cloud optimize "My Project" --push
    Estimated number of backtests: 10
    Estimated batch time: 4 minutes
    Estimated batch cost: $0.06
    Organization balance: 173,368 QCC ($1,733.68)
    Do you want to start the optimization on the selected node type? [Y/n]: y
    
  11. 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.

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: