Before we enable python support, follow the installation instructions to get LEAN running C# algorithms in your machine.
1. Install Python 3.6:
- Use the Windows x86-64 MSI installer from https://www.python.org/downloads/release/python-364/.
- When asked to select the features to be installed, make sure you select "Add python.exe to Path"
- Once installed confirm the python36.dll is in place in C:\Windows\System32.
- Add python36.dll location to the system path:
- Right mouse button on My Computer. Click Properties.
- Click Advanced System Settings -> Environment Variables -> System Variables
- On the "Path" section click New and enter the python36.dll path, in our example this was C:\Windows\System32
- Create two new system variables: PYTHONHOME and PYTHONPATH which values must be, respectively, the location of your python installation (e.g. C:\Python36amd64) and its Lib folder (e.g. C:\Python36amd64\Lib).
- C:\Windows\System32 is normally in the path, thus this procedure is not necessary.
- Install pandas and its dependencies.
2. Run python algorithm:
- Prepare Python.Runtime.dll. This is needed to run Python algorithms in LEAN.
- Delete the existing files in \Lean\packages\QuantConnect.pythonnet.1.0.5.5\lib
- Using windows you'll need to copy the \Lean\packages\QuantConnect.pythonnet.1.0.5.5\build\ Python.Runtime.win file into the ..\lib\ directory and rename it to Python.Runtime.dll.
- Update the config to run the python algorithm:
"algorithm-type-name": "BasicTemplateAlgorithm",
"algorithm-language": "Python",
"algorithm-location": "../../../Algorithm.Python/BasicTemplateAlgorithm.py",- Rebuild LEAN. This step will ensure that the Python.Runtime.dll you set in 2.1 will be used.
- Run LEAN. You should see the same result of the C# algorithm you tested earlier.
Known issue with macOS: Python.Runtime.dll cannot import CLR modules after their reference is added. Please follow this Github Issue for updates.
1. Install Python 3.6 with Miniconda:
export PATH="/opt/miniconda3/bin:$PATH"
wget http://cdn.quantconnect.com.s3.amazonaws.com/miniconda/Miniconda3-4.3.31-Linux-x86_64.sh
bash Miniconda3-4.3.31-Linux-x86_64.sh -b
rm -rf Miniconda3-4.3.31-Linux-x86_64.sh
ln -s /opt/miniconda3/lib/libpython3.6m.so /usr/lib/libpython3.6.so
conda update -y python conda pip
conda install -y cython pandas
2 Run python algorithm:
- Update the config to run the python algorithm:
"algorithm-type-name": "BasicTemplateAlgorithm",
"algorithm-language": "Python",
"algorithm-location": "../../../Algorithm.Python/BasicTemplateAlgorithm.py",- Run LEAN. You should see the same result of the C# algorithm you tested earlier.
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 of pythonnet with support to System.Decimal and System.DateTime.
Below we can find the compilation flags that create a suitable Python.Runtime.dll for each operating system.
Windows
msbuild pythonnet.sln /nologo /v:quiet /t:Clean;Rebuild /p:Platform=x64 /p:PythonInteropFile="interop36.cs" /p:Configuration=ReleaseWin /p:DefineConstants="PYTHON36,PYTHON3,UCS2"
macOS UCS2
msbuild pythonnet.sln /nologo /v:quiet /t:Clean;Rebuild /p:Platform=x64 /p:PythonInteropFile="interop36.cs" /p:Configuration=ReleaseMono /p:DefineConstants="PYTHON36,PYTHON3,UCS2,MONO_OSX"
macOS UCS4
msbuild pythonnet.sln /nologo /v:quiet /t:Clean;Rebuild /p:Platform=x64 /p:PythonInteropFile="interop36.cs" /p:Configuration=ReleaseMono /p:DefineConstants="PYTHON36,PYTHON3,UCS4,MONO_OSX"
Linux
msbuild pythonnet.sln /nologo /v:quiet /t:Clean;Rebuild /p:Platform=x64 /p:PythonInteropFile="interop36.cs" /p:Configuration=ReleaseMono /p:DefineConstants="PYTHON36,PYTHON3,UCS4,MONO_LINUX"