LEAN Engine

Getting Started

Introduction

Lean Engine is an open-source algorithmic trading engine built for easy strategy research, backtesting and live trading. We integrate with common data providers and brokerages so you can quickly deploy algorithmic trading strategies.

The core of the LEAN Engine is written in C#; but it operates seamlessly on Linux, Mac and Windows operating systems. It supports algorithms written in Python 3.8 or C#. Lean drives the web-based algorithmic trading platform QuantConnect.

System Overview

The Engine is broken into many modular pieces which can be extended without touching other files. The modules are configured in config.json as set "environments". Through these environments, you can control LEAN to operate in the mode required.

The most important plugins are:

Result Processing

An IResultHandler that handle all messages from the algorithmic trading engine. Decide what should be sent, and where the messages should go. The result processing system can send messages to a local GUI, or the web interface.

Datafeed Sourcing

An IDataFeed that connect and download the data required for the algorithmic trading engine. For backtesting this sources files from the disk, for live trading, it connects to a stream and generates the data objects.

Transaction Processing

An ITransactionHandler that process new order requests; either using the fill models provided by the algorithm or with an actual brokerage. Send the processed orders back to the algorithm's portfolio to be filled.

Realtime Event Management

An IRealtimeHandler that generate real-time events - such as the end of day events. Trigger callbacks to real-time event handlers. For backtesting, this is mocked-up a works on simulated time.

Algorithm State Setup

An ISetupHandler that configure the algorithm cash, portfolio and data requested. Initialize all state parameters required.

These are all configurable from the config.json file in the Launcher Project.

Developing with Lean CLI

QuantConnect recommends using Lean CLI for local algorithm development. This is because it is a great tool for working with your algorithms locally while still being able to deploy to the cloud and have access to Lean data. It is also able to run algorithms on your local machine with your data through our official docker images.

Reference QuantConnects documentation on Lean CLI here.

Installation Instructions

This section will cover how to install lean locally for you to use in your own environment.

Refer to the following readme files for a detailed guide regarding using your local IDE with Lean:

To install locally, download the zip file with the latest master and unzip it to your favorite location. Alternatively, install Git and clone the repo:

git clone https://github.com/QuantConnect/Lean.git
cd Lean

Mac OS

  1. Install Visual Studio for Mac
  2. Open QuantConnect.Lean.sln in Visual Studio

Visual Studio will automatically start to restore the Nuget packages. If not, in the menu bar,

  1. click Project > Restore NuGet Packages
  2. In the menu bar, click Run > Start Debugging

Alternatively, run the compiled dll file:

  1. click Build > Build All
  2. run the following code:
  3. cd Lean/Launcher/bin/Debug
    dotnet QuantConnect.Lean.Launcher.dll

Linux (Debian, Ubuntu)

  1. Install dotnet 6
  2. Compile Lean Solution
  3. dotnet build QuantConnect.Lean.sln
  4. Run Lean
  5. cd Launcher/bin/Debug
    dotnet QuantConnect.Lean.Launcher.dll

To set up Interactive Brokers integration, make sure you fix the ib-tws-dir and ib-controller-dir fields in the config.json file with the actual paths to the TWS and the IBController folders respectively. If after all you still receive connection refuse error, try changing the ib-port field in the config.json file from 4002 to 4001 to match the settings in your IBGateway/TWS.

Windows

  1. Install Visual Studio
  2. Open QuantConnect.Lean.sln in Visual Studio
  3. Build the solution by clicking Build Menu -> Build Solution
  4. Press F5 to run

Python Support

A full explanation of the Python installation process can be found in the Algorithm.Python project.

Local-Cloud Hybrid Development

Seamlessly develop locally in your favorite development environment, with full autocomplete and debugging support to quickly and easily identify problems with your strategy. For more information please see the CLI documentation.

Sponsorships

Sponsor QuantConnect to support our developers as we improve a revolutionary quantitative trading platform LEAN, in an open, collaborative way. We will continue to level the playing field with industry-grade tools and data accessibility. We use sponsorship funds to achieve the following goals:

  • To continue the development of LEAN’s infrastructure
  • To create free, high-quality research and educational material
  • To provide continued support for our community
  • To make terabytes of data accessible in the Dataset Market
  • To bring LEAN to global financial markets
  • To increase live trading brokerage connections
  • To connect more individuals with financial institutions so individuals can gain income for their ideas at scale

To become a QuantConnect sponsor, see the Sponsorship page on GitHub.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: