API Reference
lean backtest
Description
Runs a local backtest in a Docker container using the quantconnect/lean Docker image.
The logs of the backtest are shown in real-time and the full results are stored in the <project> / backtest / <timestamp> directory.
You can use the --output
option to change the output directory.
The given <project>
argument must be either a project directory or a file containing the algorithm to backtest.
If it is a project directory, the CLI looks for a main.py or Main.cs file, assuming the first file it finds to be the algorithm to run.
If the --debug
option is given, this command configures the Docker container in such a way to allow debugging using your editor's debugger.
The exact ways to get local debugging to work depends on your editor and language, see Debugging for more information on how to set this up.
You can use the --data-provider
option to change where the data is retrieved.
This option updates the Lean configuration file, so you don't need to use this option multiple times for the same data provider if you are not switching between them. If you use the Terminal Link data provider, you must also provide the following options:
--terminal-link-environment
--terminal-link-server-host
--terminal-link-server-port
--terminal-link-openfigi-api-key
You can use the --download-data
flag as an alias for --data-provider QuantConnect
and the --data-purchase-limit
option to set the maximum amount of QuantConnect Credit (QCC) to spend during the backtest when using QuantConnect as data provider.
The --data-purchase-limit
option is not persistent.
The Docker image that's used contains the same libraries as the ones available on QuantConnect. If the selected project is a C# project, it is compiled before starting the backtest.
By default, the official LEAN engine image is used.
You can override this using the --image <value>
option.
Alternatively, you can set the default engine image for all commands using lean config set engine-image <value>
.
The image is pulled before running the backtest if it doesn't exist locally yet or if you pass the --update
flag.
Options
The lean backtest
command supports the following options:
Option | Description |
---|---|
--output <path> | Directory to store results in (defaults to <project> / backtests / <timestamp>). |
--detach , -d | Run the backtest in a detached Docker container and return immediately. The name of the Docker container is shown before the command ends. You can use Docker's own commands to manage the detached container. |
--debug | Enable a certain debugging method, see Debugging for more information. |
--data-provider <value> | Update the Lean configuration file to retrieve data from the given provider, which must be Local , QuantConnect , or Terminal Link . |
--download-data | Update the Lean configuration file to download data from the QuantConnect API, alias for --data-provider QuantConnect . |
--data-purchase-limit <value> | The maximum amount of QCC to spend on downloading data during the backtest when using QuantConnect as data provider. |
--terminal-link-environment <value> | The environment to run in, which must be Production or Beta . |
--terminal-link-server-host <value> | The host on which the Terminal Link server is running. |
--terminal-link-server-port <value> | The port on which the Terminal Link server is running. |
--terminal-link-openfigi-api-key <value> | The Open FIGI API key to use for mapping Options. |
--image <value> | The LEAN engine image to use (defaults to quantconnect/lean:latest ). |
--python-venv <value> | The path of the python virtual environment to use. |
--update | Pull the LEAN engine image before running the backtest. |
--no-update | Use the local LEAN engine image instead of pulling the latest version. |
--lean-config <path> | The Lean configuration file that should be used (defaults to the nearest lean.json file). |
--release | Compile C# projects in release configuration instead of debug. |
--verbose | Enable debug logging. |
--help | Display the help text of the lean backtest command and exit. |