Skip to content

Lean Quantitative Trading Tutorial

Click the link to switch to the 中文版

🇨🇳 本教程有中文版 → 切换到中文教程

A complete tutorial for the QuantConnect Lean quantitative trading engine, with runnable Python example code for every chapter.

Tutorial Structure

Part 1: Introduction

Chapter Document Example Code Description
Chapter 1: What is Lean 01-what-is-lean.md 01-hello-world.py The simplest Lean algorithm example
Chapter 2: Environment Setup 02-environment-setup.md - Lean CLI, local builds, Dev Container
Chapter 3: Quick Start 03-quick-start.md 03-sma-cross.py SMA crossover strategy

Part 2: Strategy Development Basics

Chapter Document Example Code Description
Chapter 4: QCAlgorithm Basics 04-qcalgorithm-basics.md 04-qcalgorithm-demo.py Algorithm lifecycle, basic configuration
Chapter 5: Order Management 05-order-management.md 05-order-management-demo.py Order types, OrderTicket
Chapter 6: Technical Indicators 06-technical-indicators.md 06-indicators-demo.py Built-in and custom indicators
Chapter 7: Data and History 07-data-and-history.md 07-data-demo.py History API, Universe selection

Part 3: Advanced Strategy Development

Chapter Document Example Code Description
Chapter 8: Algorithm Framework 08-algorithm-framework.md 08-framework-demo.py Modular strategy development
Chapter 9: Advanced Features 09-advanced-features.md 09-advanced-features-demo.py Options, futures, custom fees
Chapter 10: Research Environment 10-research-environment.md 10-research-demo.py Jupyter Notebook, QuantBook

Part 4: Backtesting and Optimization

Chapter Document Example Code Description
Chapter 11: Backtesting in Depth 11-backtesting.md - Backtest configuration, result analysis
Chapter 12: Parameter Optimization 12-optimization.md 12-optimization-demo.py Grid search, optimization strategies

Part 5: Live Trading

Chapter Document Example Code Description
Chapter 13: Live Trading Preparation 13-live-preparation.md - Brokerage integration, risk management
Chapter 14: Live Deployment 14-live-deployment.md - lean live command, monitoring

Part 6: Data Management

Chapter Document Example Code Description
Chapter 15: Data Management 15-data-management.md 15-data-demo.py Data formats, custom data

Part 7: Best Practices

Chapter Document Example Code Description
Chapter 16: Code Organization 16-code-organization.md - Project structure, design patterns
Appendix A: API Quick Reference 17-performance-optimization.md - Common method index, code snippets
Appendix B: Example Strategy Library 18-appendix-examples.md - Complete runnable strategies

Quick Start

Install Lean CLI

pip install lean

Create a Project

lean init
lean project-create "MyStrategy" --language python

Run an Example

Copy the example code into your project directory, then run:

lean backtest "MyStrategy"

Project Structure

Lean/
├── docs/                          # Tutorial documentation
│   ├── README.md                  # This file
│   ├── 01-introduction/           # Introduction
│   ├── 02-strategy-basics/        # Strategy development basics
│   └── 03-advanced/               # Advanced strategy development
├── Algorithm/                     # C# algorithm base classes
├── Algorithm.Python/              # Python algorithm examples
└── Launcher/                      # Program entry point

Learning Path

Beginner (Ch. 1-3)
    │
    ├── Understand Lean fundamentals
    ├── Set up the development environment
    └── Run your first strategy
    │
Intermediate (Ch. 4-7)
    │
    ├── Master the QCAlgorithm API
    ├── Learn order management
    ├── Use technical indicators
    └── Data queries and Universe selection
    │
Advanced (Ch. 8-10)
    │
    ├── Modular Framework development
    ├── Options and futures strategies
    └── Using the research environment
    │
Expert (Ch. 11-18)
    │
    ├── Backtesting and optimization
    ├── Live trading
    ├── Performance optimization
    └── Best practices

References

Contributing

Contributions of tutorials and example code are welcome! Please open an Issue / PR at the GitHub repository.

License

This tutorial follows the same Apache License 2.0 as the Lean project.