* Use lean data key as param for request * key -> filePath rename and some cleanup * Refactor * Add Organizations Endpoints * Add some organization api wrapper objects * Address namespace issue * Reorganize Api Test into seperate files using one ApiTestBase * Add Organization tests * Use capitalized "API" test namespace to reduce amount of file changes * Add License to test base * Update /data endpoint functions and response objects * Update ApiDataProvider Logic * Handle deserialization of organization products * Simplify converter * Only throw for equity requests when not subscribed to map/factor files * Add missing header * Make arguement exception * Api adjustments * Add Zip factor and map file providers - Common project will now reference Compression project and not the other way round. - Adding Zip FactorFile and MapFile providers * Refactor FactorFileProvider to use DataProvider to fetch files * Use resulting MinimumDate in construction of FactorFile * Nit FactorFile comments and arrangement * Refactor MapFileProviders to use DataProvider for fetching files * Refactor ZipFileProvider * Clean up * Refactor Backtesting Future/Option chain providers to use dataprovider * Fixes for data/ endpoints and test adjustments * Response objects adjustments/cleanups * ApiDateProvider fixes and testing * Add LocalZipFactorFileTests * Update ApiDataProvider download test to verify stream is not null * Implement posting of agreement summary and signed time * Mark all Api related tests as explicit and document details on running * Clarify default token on ApiTestBase * Adjust summary * Update Api responses for QCC, except org products which are sold in USD * Implement cache expiration for zip MapFile and FactorFiles. Adding unit tests * Fix multiple markets for ZipFactorFile provider * Use Symbol as cache key * Api.cs review * Dispose of factorFileStream after reading * Use zip.EntryFileNames * Address a few reviews * Few more fixes * Address Api Review * Add Job Org id to config * Minor tweaks * Compare with invariant culture * Fixes Option Universe selection * ZipEntryNameSubscriptionDataSourceReader will use IDataProvider * Fix research * Fix null reference exception * Make duplicate log debug Co-authored-by: Martin-Molinero <martin@quantconnect.com>
QuantConnect Python Algorithm Project
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.
Local Python Autocomplete
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.
Setup Lean Locally with Python
Before setting up python support, follow the installation instructions to get LEAN running C# algorithms on your machine.
Installing Python 3.6:
Next we must prepare a Python installation for Lean to use. Follow the instructions for your OS.
Windows
- Use the Windows x86-64 MSI Python 3.6.8 installer from python.org or Anaconda for Windows installer. "Anaconda 5.2" installs 3.5.2 by default, after installation of Anaconda you will need to upgrade python to make it work as expected:
conda install -y python=3.6.8 - When asked to select the features to be installed, make sure you select "Add python.exe to Path"
- Create
PYTHONNET_PYDLLenvironment variable to the location of your python dll in your installation (e.g.C:\Dev\Python368\python36.dllorC:\Anaconda3\python36.dll):- Right mouse button on My Computer. Click Properties.
- Click Advanced System Settings -> Environment Variables -> System Variables
- Click New.
- Name:
PYTHONNET_PYDLL - Value:
{python dll location}
- Name:
- Install pandas=0.25.3 and its dependencies.
- Install wrapt=1.11.2 module.
- Reboot computer to ensure changes are propagated.
macOS
- Use the macOS x86-64 package installer from Anaconda and follow "Installing on macOS" instructions from Anaconda documentation page.
- Set
PYTHONNET_PYDLLenvironment variable to the location of your python dll in your installation directory (e.g./Users/{your_user_name}/anaconda3/lib/libpython3.6m.dylib):- Open
~/.bash-profilewith a text editor of your choice. - Add a new line to the file containing
export PYTHONNET_PYDLL="/{your}/{path}/{here}/libpython3.6m.dylib"- Save your changes, and either restart your terminal or execute
source ~/.bash-profile - Open
- Install pandas=0.25.3 and its dependencies.
- Install wrapt=1.11.2 module.
Linux
- Install Python using miniconda by following these commands; by default, miniconda is installed in the users home directory (
$HOME):
export PATH="$HOME/miniconda3/bin:$PATH"
wget https://cdn.quantconnect.com/miniconda/Miniconda3-4.5.12-Linux-x86_64.sh
bash Miniconda3-4.5.12-Linux-x86_64.sh -b
rm -rf Miniconda3-4.5.12-Linux-x86_64.sh
conda update -y python conda pip
- Create a new Python environment with the needed dependencies
conda create -n qc_lean python=3.6.8 cython=0.29.11 pandas=0.25.3 wrapt=1.11.2
- Set
PYTHONNET_PYDLLenvironment variable to location of your python dll in your installation directory (e.g./home/{your_user_name}/miniconda3/envs/qc_lean/lib/libpython3.6m.so):- Open
/etc/environmentwith a text editor of your choice. - Add a new line to the file containing
PYTHONNET_PYDLL="/home/{your_user_name}/miniconda3/envs/qc_lean/lib/libpython3.6m.so"- Save your changes, and logout or reboot to reflect these changes
- Open
Run Python Algorithms
- Update the config to run a python algorithm:
"algorithm-type-name": "BasicTemplateAlgorithm", "algorithm-language": "Python", "algorithm-location": "../../../Algorithm.Python/BasicTemplateAlgorithm.py", - Rebuild LEAN.
- Run LEAN. You should see the same result of the C# algorithm you tested earlier.
Python.NET development - Python.Runtime.dll compilation
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
Python Autocomplete Imports
Copy and paste these imports to the top of your Python file to enable a development experience equal to the cloud (these imports are exactly the same as the ones used in the QuantConnect Terminal).
from QuantConnect import *
from QuantConnect.Parameters import *
from QuantConnect.Benchmarks import *
from QuantConnect.Brokerages import *
from QuantConnect.Util import *
from QuantConnect.Interfaces import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Selection import *
from QuantConnect.Algorithm.Framework.Alphas import *
from QuantConnect.Algorithm.Framework.Portfolio import *
from QuantConnect.Algorithm.Framework.Execution import *
from QuantConnect.Algorithm.Framework.Risk import *
from QuantConnect.Indicators import *
from QuantConnect.Data import *
from QuantConnect.Data.Consolidators import *
from QuantConnect.Data.Custom import *
from QuantConnect.Data.Fundamental import *
from QuantConnect.Data.Market import *
from QuantConnect.Data.UniverseSelection import *
from QuantConnect.Notifications import *
from QuantConnect.Orders import *
from QuantConnect.Orders.Fees import *
from QuantConnect.Orders.Fills import *
from QuantConnect.Orders.Slippage import *
from QuantConnect.Scheduling import *
from QuantConnect.Securities import *
from QuantConnect.Securities.Equity import *
from QuantConnect.Securities.Forex import *
from QuantConnect.Securities.Interfaces import *
from datetime import date, datetime, timedelta
from QuantConnect.Python import *
from QuantConnect.Storage import *
QCAlgorithmFramework = QCAlgorithm
QCAlgorithmFrameworkBridge = QCAlgorithm