Files
quantconnect--lean/Algorithm.Python
Jhonathan Abreu 539011274c
Python Virtual Environments / build (push) Has been cancelled
Benchmarks / build (push) Has been cancelled
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
Support extended market hours for futures (#6522)
* Support extended market hours in AddFuture()

* Support extended market hours in AddFutureContract()

* Add C# regression algorithm

* Add Python regression algorithm

* Add regression algorithm for future contracts

* Add regression algorithm checking market hour ranges

* Fixed future regression algorithms to use extended market hours

* Fixed future regression algorithms to use extended market hours

* Fixed future regression algorithms to use extended market hours

* Fixed AddFutureOptionContractFromFutureChainRegressionAlgorithm to use extended market hours

* Update future market hours to include extended in market hours database

* Fixed AddFutureOptionContractDataStreamingRegressionAlgorithm to use extended market hours

* Fixed AddFutureOptionContractFromFutureChainRegressionAlgorithm to use extended market hours

* Fixed AddFutureContractWithContinuousRegressionAlgorithm to use extended market hours

* Fixed BasicTemplateContinuousFutureAlgorithm to use extended market hours

* Fixed BasicTemplateFuturesAlgorithm to use extended market hours

* Fix BasicTemplateFuturesDailyAlgorithm to use extended market hours

* Fixed BasicTemplateFuturesFrameworkAlgorithm to use extended market hours

* Fixed BasicTemplateFuturesHistoryAlgorithm to use extended market hours

* Fixed ContinuousBackMonthRawFutureRegressionAlgorithm to use extended market hours

* Fixed ContinuousFutureBackMonthRegressionAlgorithm to use extended market hours

* Fixed ContinuousFutureHistoryRegressionAlgorithm to use extended market hours

* Fixed ContinuousFutureLimitIfTouchedOrderRegressionAlgorithm to use extended market hours

* Fixed ContinuousFutureRegressionAlgorithm to use extended market hours

* Fixed DelistedFutureLiquidateRegressionAlgorithm to use extended market hours

* Fixed AutomaticIndicatorWarmupDataTypeRegressionAlgorithm to use extended market hours

* Fixed ConsolidateRegressionAlgorithm to use extended market hours

* Fixed DelistingFutureOptionRegressionAlgorithm to use extended market hours

* Fixed EqualWeightingPortfolioConstructionModelFutureRegressionAlgorithm to use extended market hours

* Fixed FutureContractsExtendedMarketHoursRegressionAlgorithm to use extended market hours

* Fixed FutureMarketOpenAndCloseRegressionAlgorithm to use extended market hours

* Fixed FutureMarketOpenConsolidatorRegressionAlgorithm to use extended market hours

* Fixed FutureOptionBuySellCallIntradayRegressionAlgorithm to use extended market hours

* Fixed FutureOptionCallITMExpiryRegressionAlgorithm to use extended market hours

* Fixed FutureOptionCallITMGreeksExpiryRegressionAlgorithm to use extended market hours

* Fixed FutureOptionCallOTMExpiryRegressionAlgorithm to use extended market hours

* Fixed FutureOptionDailyRegressionAlgorithm to use extended market hours

* Fixed FutureOptionHourlyRegressionAlgorithm to use extended market hours

* Fixed FutureOptionMultipleContractsInDifferentContractMonthsWithSameUnderlyingFutureRegressionAlgorithm to use extended market hours

* Fixed FutureOptionPutITMExpiryRegressionAlgorithm to use extended market hours

* Fixed FutureOptionPutOTMExpiryRegressionAlgorithm to use extended market hours

* Fixed FutureOptionShortCallITMExpiryRegressionAlgorithm to use extended market hours

* Fixed FutureOptionShortCallOTMExpiryRegressionAlgorithm to use extended market hours

* Fixed FutureOptionShortPutITMExpiryRegressionAlgorithm to use extended market hours

* Fixed FutureOptionShortPutOTMExpiryRegressionAlgorithm to use extended market hours

* Fixed FuturesAndFuturesOptionsExpiryTimeAndLiquidationRegressionAlgorithm to use extended market hours

* Fixed FuturesExpiredContractRegression to use extended market hours

* Fixed FutureSharingTickerRegressionAlgorithm to use extended market hours

* Fixed HistoryWithDifferentContinuousContractDepthOffsetsRegressionAlgorithm to use extended market hours

* Fixed HistoryWithDifferentDataMappingModeRegressionAlgorithm to use extended market hours

* Fixed HistoryWithDifferentDataNormalizationModeRegressionAlgorithm to use extended market hours

* Fixed LimitOrdersAreFilledAfterHoursForFuturesRegressionAlgorithm to use extended market hours

* Fixed OpenInterestFuturesRegressionAlgorithm to use extended market hours

* Fixed RegisterIndicatorRegressionAlgorithm to use extended market hours

* Fixed SetHoldingsFutureRegressionAlgorithm to use extended market hours

* Fixed WarmupFutureRegressionAlgorithm to use extended market hours

* Fixed AddFutureOptionSingleOptionChainSelectedInUniverseFilterRegressionAlgorithm to use extended market hours

* Fixed AlgorithmHistoryTests to use extended market hours for futures

* Fixed AlgorithmTradingTests to use extended market hours for futures

* Fixed BrokerageSetupHandlerTests to use extended market hours for futures

* Fixed TimeRulesTests to use extended market hours for futures

* Fixed FutureOptionMarginBuyingPowerModelTests to use extended market hours for futures

* Fixed FutureMarginBuyingPowerModelTests to use extended market hours for futures

* Fixed FileSystemDataFeedTests to use extended market hours for futures

* Fixed QuantBookHistoryTests to use extended market hours for futures

* Split BasicTemplateContinuousFutureAlgorithm to have an extended market version

* Fixed FutureMarketOpenAndCloseRegressionAlgorithm to use extended market hours

* Split BasicTemplateFuturesAlgorithm to have an extended market version

* Split BasicTemplateFuturesAlgorithm to have an extended market version

* Split BasicTemplateFuturesFrameworkAlgorithm to have an extended market version

* Split BasicTemplateFuturesHistoryAlgorithm to have an extended market version

* Revert AddFutureContractWithContinuousRegressionAlgorithm

* Revert AddFutureOptionContractDataStreamingRegressionAlgorithm and added data

* Revert AddFutureOptionContractFromFutureChainRegressionAlgorithm

* Revert AddFutureOptionSingleOptionChainSelectedInUniverseFilterRegressionAlgorithm

* Revert ConsolidateRegressionAlgorithm

* Revert Algorithm.CSharp/ContinuousBackMonthRawFutureRegressionAlgorithm.cs

* Revert ContinuousFutureBackMonthRegressionAlgorithm

* Revert ContinuousFutureHistoryRegressionAlgorithm

* Revert ContinuousFutureLimitIfTouchedOrderRegressionAlgorithm

* Revert ContinuousFutureRegressionAlgorithm

* Revert Algorithm.CSharp/DelistedFutureLiquidateRegressionAlgorithm.cs

* Revert EqualWeightingPortfolioConstructionModelFutureRegressionAlgorithm

* Split FutureMarketOpenAndCloseRegressionAlgorithm to have an extended market version

* Split FutureMarketOpenConsolidatorRegressionAlgorithm to have an extended market version

* Revert FutureOptionBuySellCallIntradayRegressionAlgorithm

* Revert FutureOptionCallITMExpiryRegressionAlgorithm

* Revert FutureOptionDailyRegressionAlgorithm

* Revert FutureOptionPutITMExpiryRegressionAlgorithm

* Revert FutureSharingTickerRegressionAlgorithm

* Revert FuturesAndFuturesOptionsExpiryTimeAndLiquidationRegressionAlgorithm

* Revert FuturesExpiredContractRegression

* Revert HistoryWithDifferentContinuousContractDepthOffsetsRegressionAlgorithm

* Revert HistoryWithDifferentDataMappingModeRegressionAlgorithm

* Revert HistoryWithDifferentDataNormalizationModeRegressionAlgorithm

* Revert OpenInterestFuturesRegressionAlgorithm

* Revert RegisterIndicatorRegressionAlgorithm

* Revert SetHoldingsFutureRegressionAlgorithm

* Revert WarmupFutureRegressionAlgorithm

* Revert AutomaticIndicatorWarmupDataTypeRegressionAlgorithm

* Some cleanup

* Address changes request

* Address changes request

* Add more Class III Milk data to fix DelistingFutureOptionDailyRegressionAlgorithm
2022-08-18 10:56:44 -03:00
..
2020-06-02 12:52:28 -03:00
2020-06-02 12:52:28 -03:00
2015-09-01 22:17:35 -04:00

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

  1. 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
  2. When asked to select the features to be installed, make sure you select "Add python.exe to Path"
  3. Create PYTHONNET_PYDLL environment variable to the location of your python dll in your installation (e.g. C:\Dev\Python368\python36.dll or C:\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}
  4. Install pandas=0.25.3 and its dependencies.
  5. Install wrapt=1.11.2 module.
  6. Reboot computer to ensure changes are propagated.

macOS

  1. Use the macOS x86-64 package installer from Anaconda and follow "Installing on macOS" instructions from Anaconda documentation page.
  2. Set PYTHONNET_PYDLL environment 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-profile with 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
    
  3. Install pandas=0.25.3 and its dependencies.
  4. Install wrapt=1.11.2 module.

Linux

  1. 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
  1. 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
  1. Set 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.6m.so):
    • Open /etc/environment with 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

Run Python Algorithms

  1. Update the config to run a python algorithm:
    "algorithm-type-name": "BasicTemplateAlgorithm",
    "algorithm-language": "Python",
    "algorithm-location": "../../../Algorithm.Python/BasicTemplateAlgorithm.py",
    
  2. Rebuild LEAN.
  3. 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-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:
    • remove stubs -> pip uninstall quantconnect-stubs
    • reinstall stubs -> pip install quantconnect-stubs