* Initial options universe with greeks implementation * Options universe improvements * Address peer review * File based options universe fixes and improvements. - Adjust OptionUniverse start-end times and period. - Adapt unit tests and some algorithms to pass with new options universe selection. * Updated options regression algorithms stats for new universe data * Updated options regression algorithms stats for new universe data * Updated options regression algorithms stats for new universe data * Updated options regression algorithms stats for new universe data * Updated options regression algorithms stats for new universe data * Option chain provider with new options universe * Allow canonical option history requests * Address peer review * Address peer review * Fix symbols parsing in OptionUniverse * Fix universe selection subscriptions start time to not include extended market hours * Minor changes * Minor changes * Peer recommended changes and fixes * Update regression algorithm stats * Update regression algorithms stats and minor fixes * Fix option chain provider history request * Round option indicators values * Added option universe csv header property * Update regression algorithms stats * Update regression algorithms stats * Data fixes and regression algos stats update * Unit test fixes * Minor changes * Option chain handling in live trading data feed * Minor changes * Added processed data provider * Fix thread-safety violation in Slice class * Minor change * Update options filter universe API to use OptionUniverse data Add new filter methods for greeks, IV and open interest * Option filter universe api updates * Add OptionUniverse history regression algorithms * Add regression algorithms for new options filter universe api methods * Added options greeks data and updated regression algorithms * Address peer review * Address peer review * Add more assertions to new options filter api regression algorithms * Minor performance improvement. Reduce greeks binomial model steps to 140 * Minor tests updates * Greeks numerical models performance improvements * Greeks numerical models performance improvements * Revert array pool change for option pricing numerical models * Update default dividend yield provider depending on option type * [TEST] * Add helper method con calculate time till expiration * Use double in price option numerical models * Implied volatility calculation improvements - Adjust root finding method accuracy as a factor of the option price - Use BSM to get a first guess * Cleanup * Some regression algorithms and unit tests cleanup * Regression tests updates after rebasing from master * Add universe files * Self review and cleanup * Minor regression tests updates after rebase * Fix: set data time zone to same as exchange tz for options universes * Minor change * Minor change * Fix for live trading options universe selection * Keep underlying when aggregating collections in BaseDataCollectionAggregatorEnumerator * Update index options regression algorithms stats * Minor change * Address peer review * Memory usage improvements * Minor build fix * Minor changes and test fixes * Cache symbols in OptionUniverse * Cleanup * Fix index option creation in OptionUniverse * Use cached underlying SID when parsing from string * Abstract symbols cache to BaseDataCollection * Return actual underlying symbol when mapping decomposing ICO ticker * Address peer review * Minor performance improvements reduce garbage * Limit Symbols and SIDs cache size to help with memory usage * Minor fix in symbols and sid cache cleanup * Build fix * Lazily parse greeks on individual access * Cleanup and tests * Address peer review * Minor greeks fix --------- Co-authored-by: Martin Molinero <martin.molinero1@gmail.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.8:
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.8.13 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.8.13 - 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\Python38\python38.dllorC:\Anaconda3\python38.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=1.4.3 and its dependencies.
- Install wrapt=1.14.1 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.8.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.8.dylib"- Save your changes, and either restart your terminal or execute
source ~/.bash-profile - Open
- Install pandas=1.4.3 and its dependencies.
- Install wrapt=1.14.1 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.8.13 pandas=1.4.3 wrapt=1.14.1
- 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.8.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.8.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", - Build 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
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.
Known Issues
- Python can sometimes have issues when paired with our quantconnect stubs package on Windows. This issue can cause modules not to be found because
site-packagesdirectory 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:- remove stubs -> pip uninstall quantconnect-stubs
- reinstall stubs -> pip install quantconnect-stubs