Install
openclaw skills install lean-engineRun QuantConnect LEAN backtests and manage US equity algorithm development. Use when asked to backtest a trading strategy, run a LEAN algorithm, analyze back...
openclaw skills install lean-engine| Variable | Purpose | Example |
|---|---|---|
LEAN_ROOT | Path to cloned LEAN repository | /home/user/lean |
DOTNET_ROOT | Path to .NET SDK installation | /home/user/.dotnet |
PYTHONNET_PYDLL | Path to Python shared library (required by LEAN's pythonnet) | $LEAN_ROOT/.libs/libpython3.11.so.1.0 |
All three must be set before using this skill. Add to your shell profile:
export LEAN_ROOT="$HOME/lean"
export DOTNET_ROOT="$HOME/.dotnet"
export PATH="$PATH:$DOTNET_ROOT"
export PYTHONNET_PYDLL="$LEAN_ROOT/.libs/libpython3.11.so.1.0"
Note: LEAN bundles its own Python shared library in
$LEAN_ROOT/.libs/. If you built LEAN from source, the library should be there afterdotnet build. If not, installlibpython3.11-devand pointPYTHONNET_PYDLLto your system'slibpython3.11.so.
Install .NET 8 SDK:
# Linux/macOS
wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh
chmod +x dotnet-install.sh
./dotnet-install.sh --channel 8.0
export DOTNET_ROOT="$HOME/.dotnet"
export PATH="$PATH:$DOTNET_ROOT"
Clone and build LEAN:
git clone https://github.com/QuantConnect/Lean.git "$LEAN_ROOT"
cd "$LEAN_ROOT"
dotnet build QuantConnect.Lean.sln -c Debug
Download initial market data:
pip install yfinance pandas
python3 {baseDir}/scripts/download_us_universe.py --symbols sp500 --start 2020-01-01 --data-dir "$LEAN_ROOT/Data"
Verify setup:
ls "$LEAN_ROOT/Data/equity/usa/daily/" # Should list .zip files
ls "$LEAN_ROOT/Launcher/bin/Debug/" # Should contain QuantConnect.Lean.Launcher.dll
$LEAN_ROOT/$LEAN_ROOT/Launcher/bin/Debug/$LEAN_ROOT/Launcher/config.json$LEAN_ROOT/Algorithm.Python/$LEAN_ROOT/Data/$DOTNET_ROOT/dotnet (add to PATH: export PATH="$PATH:$DOTNET_ROOT")$LEAN_ROOT/Algorithm.Python/YourAlgo.py# Update config.json — set these fields:
# "algorithm-type-name": "YourClassName"
# "algorithm-language": "Python"
# "algorithm-location": "../../../Algorithm.Python/YourAlgo.py"
export PATH="$PATH:$DOTNET_ROOT"
cd "$LEAN_ROOT/Launcher/bin/Debug"
dotnet QuantConnect.Lean.Launcher.dll
$LEAN_ROOT/Results/Or use the helper script:
bash {baseDir}/scripts/run_backtest.sh YourClassName YourAlgo.py
Edit $LEAN_ROOT/Launcher/config.json with these key fields:
| Field | Purpose | Example |
|---|---|---|
algorithm-type-name | Python class name | "MyStrategy" |
algorithm-language | Language | "Python" |
algorithm-location | Path to .py file | "../../../Algorithm.Python/MyStrategy.py" |
data-folder | Market data path | "../Data/" |
environment | Mode | "backtesting" or "live-interactive" |
For IB live trading, set environment to "live-interactive" and configure the
ib-* fields (account, username, password, host, port, trading-mode).
Check available data:
ls "$LEAN_ROOT/Data/equity/usa/daily/"
Data format: ZIP files containing CSV. Each line:
YYYYMMDD HH:MM,Open*10000,High*10000,Low*10000,Close*10000,Volume
Prices are stored as integers (multiply by 10000). LEAN handles conversion internally.
Download more data:
python3 {baseDir}/scripts/download_us_universe.py --symbols sp500 --data-dir "$LEAN_ROOT/Data"
See {baseDir}/references/data-download.md for additional methods to expand the universe.
LEAN Python algorithms inherit from QCAlgorithm:
from AlgorithmImports import *
class MyAlgo(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2024, 1, 1)
self.SetEndDate(2025, 1, 1)
self.SetCash(100_000)
self.AddEquity("SPY", Resolution.Daily)
self.SetBenchmark("SPY")
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage,
AccountType.Margin)
def OnData(self, data):
if not self.Portfolio.Invested:
self.SetHoldings("SPY", 1.0)
Key API patterns:
self.History(symbol, periods, resolution) — get historical barsself.SetHoldings(symbol, weight) — target portfolio weightself.Liquidate(symbol) — close positionself.AddUniverse(coarse_fn, fine_fn) — dynamic universe selectionself.Schedule.On(date_rule, time_rule, action) — scheduled eventsself.Debug(msg) — log outputAfter a backtest run, check:
ls "$LEAN_ROOT/Results/"
# Key files: *-log.txt, *-order-log.txt, *.json (statistics)
export PATH="$PATH:$DOTNET_ROOT"
cd "$LEAN_ROOT"
dotnet build QuantConnect.Lean.sln -c Debug
The run_backtest.sh script does NOT modify your original config.json. Instead, it:
config.backtest.json with only algorithm fields changed (class name, file path, language, environment=backtesting)trap cleanup handlerThe configure_algo.py helper performs the field substitution in an isolated output file. Your original config — including any Interactive Brokers credentials for live trading — is never modified.
Modified fields (in the temp copy only):
algorithm-type-name — set to the requested class namealgorithm-language — set to Pythonalgorithm-location — set to the requested .py file pathenvironment — set to backtestingThe setup instructions involve network downloads:
git clone from GitHub (QuantConnect/Lean repository)dotnet build may restore NuGet packagespip install yfinance pandas installs Python packages from PyPIdownload_us_universe.py fetches market data from Yahoo FinanceAll downloads are from well-known public sources. For maximum isolation, run setup in a container or VM.
This skill requires the following environment variables at runtime:
LEAN_ROOT — path to your cloned LEAN repositoryDOTNET_ROOT — path to your .NET SDK installationPYTHONNET_PYDLL — path to Python shared library (auto-detected from $LEAN_ROOT/.libs/ if not set)These are declared in the skill metadata and must be set before use.
data-folder in config.json points to correct pathpython-venv config if using custom packagesLEAN_ROOT not set → Add export LEAN_ROOT="$HOME/lean" to your shell profileexport PATH="$PATH:$DOTNET_ROOT" to your shell profileRuntime.PythonDLL was not set → Set PYTHONNET_PYDLL to the Python shared library path (see env var table above)