Backtesting

Running backtests

Introduction

Backtesting is a way to test your algorithm on historic data. The CLI makes backtesting easier by providing simple commands to backtest your algorithms locally with your own data, or in the cloud with QuantConnect's data.

Running local backtests

Follow these steps to run a local backtest:

  1. Set up your local data for all the data required by your project.
  2. Open a terminal in the directory you ran lean init in.
  3. Run lean backtest "My Project" to run a local backtest for the project in ./My Project.
    $ lean backtest "My Project"
    20210322 17:27:46.658 TRACE:: Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v2.5.0.0 Mode: DEBUG (64bit)
    20210322 17:27:46.664 TRACE:: Engine.Main(): Started 5:27 PM
    Successfully ran 'My Project' in the 'backtesting' environment and stored the output in 'My Project/backtests/2021-03-22_18-51-28'
  4. View the result in the <project>/backtests/<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 3.
    $ lean backtest "My Project" --output "My Project/custom-output"
    20210322 17:27:46.658 TRACE:: Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v2.5.0.0 Mode: DEBUG (64bit)
    20210322 17:27:46.664 TRACE:: Engine.Main(): Started 5:27 PM
    Successfully ran 'My Project' in the 'backtesting' environment and stored the output in 'My Project/custom-output'

Follow these steps to run a backtest with a custom version of the LEAN engine:

  1. View the available versions on the quantconnect/lean Docker Hub tags page.
  2. Copy the name of the tag that you want to run.
  3. Follow the "How to run a local backtest" steps above and append the --image quantconnect/lean:<tag from step 2> option when running the lean backtest command.
    $ lean backtest "My Project" --image quantconnect/lean:11154
    Pulling quantconnect/lean:11154...
    20210322 17:27:46.658 TRACE:: Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v2.5.0.0 Mode: DEBUG (64bit)
    20210322 17:27:46.664 TRACE:: Engine.Main(): Started 5:27 PM

By default, local backtests 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.

Because algorithms run in a Docker container, localhost does not point to your computer's localhost. Substitute localhost with host.docker.internal if your algorithm needs to connect to other services running on your computer. In other words, instead of connecting to http://localhost:<port>/ you need to connect to http://host.docker.internal:<port>/.

Running cloud backtests

Follow these steps to run a cloud backtest:

  1. Log in to the CLI if you haven't done so already.
  2. Open a terminal in the directory you ran lean init in.
  3. Run lean cloud backtest "My Project" --push --open to push ./My Project to the cloud, run a cloud backtest for the project, and open the results in the browser.
    $ lean cloud backtest "My Project" --push --open
    [1/1] Pushing 'My Project'
    Successfully updated cloud file 'My Project/main.py'
    Started compiling project 'My Project'
    Successfully compiled project 'My Project'
    Started backtest named 'Muscular Black Chinchilla' for project 'My Project'
  4. Inspect the result in the browser, which opens automatically after the backtest finishes.

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: