# quantconnect-lean > Build algorithmic trading strategies with QuantConnect LEAN engine. Use when creating trading algorithms, backtesting strategies, implementing technical indicators, setting up live trading, or using the LEAN CLI. Covers Python algorithm development with support for equities, options, futures, forex, and crypto. - Author: py77 - Repository: py77/quantio - Version: 20260125194137 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-06 - Source: https://github.com/py77/quantio - Web: https://mule.run/skillshub/@@py77/quantio~quantconnect-lean:20260125194137 --- --- name: quantconnect-lean description: Build algorithmic trading strategies with QuantConnect LEAN engine. Use when creating trading algorithms, backtesting strategies, implementing technical indicators, setting up live trading, or using the LEAN CLI. Covers Python algorithm development with support for equities, options, futures, forex, and crypto. --- # QuantConnect LEAN Trading Build algorithmic trading strategies using the QuantConnect LEAN engine. ## Quick Start Create a new algorithm project: ```bash pip install lean lean init lean project-create "My Strategy" --language python ``` ## Workflow Decision Tree 1. **Classic Algorithm** → Simple, direct control - Override `Initialize()` and `OnData()` - Good for straightforward strategies 2. **Framework Algorithm** → Modular, reusable components - Use Universe Selection, Alpha, Portfolio, Risk, Execution modules - Good for complex, multi-asset strategies Choose Classic unless you need component reusability or are building institutional-grade systems. ## Core Workflow ``` Research → Backtest → Optimize → Live ↓ ↓ ↓ ↓ lean lean lean lean research backtest optimize live ``` ## Classic Algorithm Structure ```python from AlgorithmImports import * class MyAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2020, 1, 1) self.SetEndDate(2023, 1, 1) self.SetCash(100000) self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol self.sma = self.SMA(self.symbol, 20) def OnData(self, data): if not self.sma.IsReady: return if self.Securities[self.symbol].Price > self.sma.Current.Value: self.SetHoldings(self.symbol, 1) else: self.Liquidate(self.symbol) ``` ## Key Patterns ### Scheduled Events ```python def Initialize(self): self.Schedule.On( self.DateRules.EveryDay(self.symbol), self.TimeRules.AfterMarketOpen(self.symbol, 30), self.Rebalance ) def Rebalance(self): # Execute at scheduled time pass ``` ### Multiple Indicators ```python def Initialize(self): self.sma_fast = self.SMA(self.symbol, 10) self.sma_slow = self.SMA(self.symbol, 30) self.rsi = self.RSI(self.symbol, 14) self.macd = self.MACD(self.symbol, 12, 26, 9) ``` ### Risk Management ```python def OnData(self, data): # Position sizing based on volatility atr = self.ATR(self.symbol, 14).Current.Value risk_per_trade = self.Portfolio.TotalPortfolioValue * 0.01 position_size = risk_per_trade / atr # Stop loss if self.Portfolio[self.symbol].UnrealizedProfitPercent < -0.02: self.Liquidate(self.symbol) ``` ## References - **CLI Commands**: See [cli-commands.md](references/cli-commands.md) for full LEAN CLI reference - **Algorithm Templates**: See [algorithm-templates.md](references/algorithm-templates.md) for strategy patterns - **Framework Components**: See [framework-components.md](references/framework-components.md) for modular components ## Asset Templates Use the templates in `assets/templates/` as starting points: - `basic_algorithm.py` - Minimal starter template - `momentum_strategy.py` - SMA crossover momentum strategy