Algorithm.Python/readme.md
This document contains information regarding how to use Python with the Lean engine, this includes how to use Python Autocomplete, setting up Lean for Python algorithms, PythonNet compilation for devs, and what imports to use to replicate the web IDE experience in your local development.
To enable autocomplete for your local Python IDE, install the quantconnect-stubs package from PyPI using the following command:
pip install quantconnect-stubs
To update your autocomplete to the latest version, you can run the following command:
pip install --upgrade quantconnect-stubs
Copy and paste the imports found here to the top of your project file to enable autocomplete.
In addition, you can use Skylight to automatically sync local changes to the cloud.
Before setting up python support, follow the installation instructions to get LEAN running C# algorithms on your machine.
Next we must prepare a Python installation for Lean to use. Follow the instructions for your OS.
PYTHONNET_PYDLL environment variable to the location of your python dll in your installation (e.g. C:\Dev\Python311\python311.dll or C:\Anaconda3\python311.dll):
PYTHONNET_PYDLL{python dll location}PYTHONNET_PYDLL environment variable to the location of your python dll in your installation directory (e.g. /Users/{your_user_name}/anaconda3/lib/libpython3.11.dylib):
~/.bash-profile with a text editor of your choice.export PYTHONNET_PYDLL="/{your}/{path}/{here}/libpython3.11.dylib"
source ~/.bash-profile
$HOME):export PATH="$HOME/miniconda3/bin:$PATH"
wget https://cdn.quantconnect.com/miniconda/Miniconda3-py311_24.9.2-0-Linux-x86_64.sh
bash Miniconda3-py311_24.9.2-0-Linux-x86_64.sh -b -p /opt/miniconda3
rm -rf Miniconda3-py311_24.9.2-0-Linux-x86_64.sh
conda create -n qc_lean python=3.11.11 pandas=2.2.3 wrapt=1.16.0
PYTHONNET_PYDLL environment variable to location of your python dll in your installation directory (e.g. /home/{your_user_name}/miniconda3/envs/qc_lean/lib/libpython3.11.so):
/etc/environment with a text editor of your choice.PYTHONNET_PYDLL="/home/{your_user_name}/miniconda3/envs/qc_lean/lib/libpython3.11.so"
"algorithm-type-name": "BasicTemplateAlgorithm",
"algorithm-language": "Python",
"algorithm-location": "../../../Algorithm.Python/BasicTemplateAlgorithm.py",
LEAN users do not need to compile Python.Runtime.dll. The information below is targeted to developers who wish to improve it. Download QuantConnect/pythonnet github clone or downloading the zip. If downloading the zip - unzip to a local pathway.
Note: QuantConnect's version of pythonnet is an enhanced version of pythonnet with added support for System.Decimal and System.DateTime.
Below are some examples of build commands that create a suitable Python.Runtime.dll.
msbuild pythonnet.sln /nologo /v:quiet /t:Clean;Rebuild
OR
dotnet build pythonnet.sln
Adding from AlgorithmImports import * to the top of your Python file is enough to enable autocomplete and import the required types for the algorithm at runtime.
site-packages directory is not present in the python path. If you have the required modules installed and are seeing errors about them not being found, please try the following steps: