Getting Started

Lean CLI

Introduction

The Lean CLI is a cross-platform CLI which makes it easier to develop with the LEAN engine locally and in the cloud.

Prerequisites

The Lean CLI is distributed as a Python package, so it requires pip to be installed. See Installing pip to learn how to install pip on your operating system. Please note that the Python distribution from the Microsoft Store is not supported, we recommend using the Anaconda distribution instead.

The commands which run the LEAN engine locally also depend on Docker being installed and running. See Installing Docker to learn how to install Docker on your operating system.

Installation

Run pip install lean in a terminal to install the latest version of the CLI.

After installing the CLI, open a terminal in an empty directory and run lean init. This command downloads the latest configuration file and sample data from the QuantConnect/Lean repository. We recommend running all Lean CLI commands in the same directory lean init was ran in.

$ lean init
Downloading latest sample data from the Lean repository...
The following objects have been created:
- lean.json contains the configuration used when running the LEAN engine locally
- data/ contains the data that is used when running the LEAN engine locally
...

If you are running Docker on Windows using the legacy Hyper-V backend instead of the new WSL 2 backend, you also need to enable file sharing for your temporary directories and for the directory you ran lean init in. To do so, open your Docker settings, go to Resources > File sharing and add C:/Users/<username>/AppData/Local/Temp and the directory you ran lean init in to the list. Click Apply & Restart after making the required changes.

Basic usage

The CLI contains a lot of commands to make working on LEAN algorithms easier and more productive. Below we list some of the most common tasks, see the tutorials in the sidebar and the API reference for a complete overview of the supported features.

Authenticating with the cloud

Before using the CLI to perform tasks in the cloud it is required to login with your QuantConnect API credentials. Run lean login to open an interactive wizard which asks you for your user id and API token. Both of these credentials can be requested on your QuantConnect Account page, after which they'll be sent to your email address.

$ lean login
Your user id and API token are needed to make authenticated requests to the QuantConnect API
You can request these credentials on https://www.quantconnect.com/account
Both will be saved in /home/<username>/.lean/credentials
User id: <user id>
API token: <api token>
Successfully logged in

Pulling projects from the cloud

Run lean cloud pull to pull your QuantConnect projects to your local drive. This command pulls all your cloud projects to your local drive while preserving your QuantConnect directory structure. If you have a lot of projects and only want to work locally on a few of them you can run this command with the --project "Project Name" option, which makes the command pull a single project instead.

$ lean cloud pull
[1/3] Pulling 'Creative Red Mule'
Successfully pulled 'Creative Red Mule/main.py'
[2/3] Pulling 'Determined Yellow-Green Duck'
Successfully pulled 'Determined Yellow-Green Duck/main.py'
Successfully pulled 'Determined Yellow-Green Duck/research.ipynb'
[3/3] Pulling 'Halloween Strategy'
Successfully pulled 'Halloween Strategy/benchmark.py'
Successfully pulled 'Halloween Strategy/main.py'
Successfully pulled 'Halloween Strategy/research.ipynb'

Sourcing data

Run lean data generate --start 20180101 --symbol-count 100 to generate realistic fake market data to test with. You can also choose to download data from QuantConnect Datasets, or to convert your own data into LEAN-compatible data.

$ lean data generate --start 20180101 --symbol-count 100
Begin data generation of 100 randomly generated Equity assets...
...

Running a local backtest

Run lean backtest "Project Name" to run a local backtest for the specified project. This command runs a backtest in a Docker container containing the same packages as the ones used on QuantConnect.com, but with your own data.

$ lean backtest "Project Name"
20210308 23:58:35.354 TRACE:: Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v2.5.0.0 Mode: DEBUG (64bit)
20210308 23:58:35.360 TRACE:: Engine.Main(): Started 11:58 PM
...

Pushing local changes to the cloud

Run lean cloud push to push local changes to the QuantConnect. This command pushes all your local projects to the cloud and creates new cloud projects when necessary. If you only want to push a single project you can run this command with the --project "Project Name" option.

$ lean cloud push
[1/3] Pushing 'Creative Red Mule'
Successfully updated cloud file 'Creative Red Mule/main.py'
[2/3] Pushing 'Determined Yellow-Green Duck'
[3/3] Pushing 'Halloween Strategy'

Running a cloud backtest

Run lean cloud backtest "Project Name" to run a cloud backtest for the specified project. By default, a summary of the results and a link to the full results are shown in the terminal. Running this command with the --open flag automatically opens the full results in the browser once the backtest is finished. Additionally, you can run this command with the --push flag to push all local changes to the project to the cloud before running the backtest.

$ lean cloud backtest "Project Name"
Started compiling project 'Project Name'
Detected parameters (2):
- main.py:19 :: 1 Order Event parameter detected near "SetHoldings(self.spy, 1)".
- main.py:21 :: 1 Order Event parameter detected near "SetHoldings(self.spy, 0)".
Build Request Successful for Project ID: 4882833, with CompileID: eaf9b677c91cfadd0a9032eb95918beb-c3b92b55d26a6d610e9b792ce561a687, Lean Version: 2.5.0.0.11058
Successfully compiled project 'Project Name'
Started backtest named 'Swimming Orange Lemur' for project 'Project Name'
...

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: