* Refactor HasSufficientBuyingPowerForOrder implementations Adds Sufficient and Insufficient helper methods to HashSufficientbuyingPowerForOrderParameters enabling syntax like: return paraeeters.Sufficient() returnparameters.Insufficient(reason) The next change will add the initial margin required which will simply require updating both of these helper methods to accept the value. * IBuyingPowerModel: Add margin functions Maintenance/Initial/ForOrder These were originally hidden in an effort to only expose what's necessary for the engine to perform its work. Additionally, we encapsulated all of the method arguments into parameters classes to prevent having to break anyone in the future. Not including these foundational methods turns out to be an oversight. These methods are not required by the engine, but rather by other models. Another possible solution here is to add an additional abstraction and include these methods on this new abstraction. BuyingPowerModel would then explicitly implement these methods and models that depend on them would require two code paths, one for when the buying power model implements this interface and another for when it doesn't. Tests were additionally updated to remove test model implementations created for the sole purpose of exposing these private methods. * Add ConstantBuyingPowerModel Provides an implementation of IBuyingPowerModel that returns the same constant value * Update BuyingPowerModelPythonWrapper to use reflection for method names Having a bunch of hard-coded strings is a sure fire way for someone to overlook when changing methods. This change ensures that noone needs to remember that this code exists :) Cleans up the syntax around verifying a python object implements a particular C# interface via the ValidateImplementationOf<T> method by having it return a value since the only use cases are in constructors when setting the models. I was initially going to update ALL python wrappers to validate the passed in models, but such a change could break many things that are 'working' right now. Such an effort should be saved for its own dedicated PR. * Add Parameters/Result types for new buying power model methods * Support computing maintenance margin for arbitrary quantities The existing GetMaintenanceMargin function assumes that we're only interested in the maintenance margin for the entirety of the provided security's holdings. This makes it impossible to perform what-if analysis or to even ask how much maintenance margin is devoted to a particular subset of the security's holdings. This change adds the quantity to the MaintenanceMarginParameters class. Futures and Options models also depend on holdings cost and holdings value, so they have also been added to the parameters type. Finally, static factory methods were added to improve discernment of intent: ForCurrentHoldings provides the existing behavior and then ForQuantityAtCurrentPrice to support what-if scenarios where we're looking for the change in maintenance margin if we were to execute an order for the securiy at the current time step. Obviously a constructor is provided to set all of the values explicitly, using any price metric the caller desires. * Address review - Fix BPM xml documentation - Fix python unit tests and PythonWrapper validate method * Add SecurityHolding.QuantityChanged event Adding event handlers will allow us to orchestrate complex events from distant parts of the codebase through wiring them up. If we continue down this path, it will move us away from the current, very 'mechanical' data flows expressed in LEAN and towards a more modern, event processing based system. This is but a baby step in that direction and the initial use case is using this QuantityChanged event to trigger resolution of the algoritm's positions groups. This is part of an effort to improve the fidelity of options margin modeling where we'll model an OptionStrategy as an IPositionGroup. This will allow us to compute the margin requirements of an OptionStrategy as a unit instead of computing margin of each security individually in isolation. See #4065 * PortfolioManager: Group fields and remove unused field This codebase generally places fields as the first members, but this class had some fields at the top, then some properties, and then some more fields. This change brings all the fields together at the top of the file and also removes pointless comments placed directly above some of the fields. Additionally, an unused field was removed. * Remove unused _currencyConverter from Security Looks like at some point the only code using this member variable was removed and the necessary clean up was overlooked. * Add Parse.Enum functions * Support disabling regression algorithms by language via config.json Adds 'regression-test-languages' to config.json and filters regerssion algorithms to run based on this value. When cycling on a particular feature, it's nice to be able to run the entire regression set while ignoring the python algorithms. Once the C# algorithms are all passing, one can then go back and run C# and Python in a final run, since 99% of feature work doesn't impact python specifically. * Implement IComparable in SecurityIdentitfier This can be used to deterministically sort securities and symbols * Add .editorconfig to enforce common formatting for json/sh files * Fix typo in IBuyingPowerModel.GetBuyingPower xml docs * Add ListEquals/GetListHashCode and OrderDirection.Closes(PositionSide) ListEquals and GetListHashCode are designed to be used together as they complement each other according to C#'s requirements for Equals and GetHashCode functions. PositionSide.ToOrderDirection() extension simply converts a PositionSide to its logical equivalent OrderDirection. Long->Buy, Short->Sell, None->Hold OrderDirection.Closes(PositionSide) determines if a particular OrderDirection would have the effect of reducing a position's absolute size. This function greatly improves the readability of buying power functions that must provide adjustments when an order/contemplated trade reduces/closes an existing position. OrderDirection.Buy.Closes(PositionSide.Short) OrderDirection.Sell.Closes(PositionSide.Long) All other combinations return false Adds ToArray/ToImmutableArray convenience functions that combine a call to Select followed by To(Immutable)Array all in one function call. * Add decimal.DiscretelyRoundBy extension method Supports rounding a decimal value by an arbitrarily chosen maximum precision, or 'quanta' * Update FutureMarginBuyingPowerModelTests to respect the security's lot size * Add core position group classes and abstractions * Add initial/maintenance margin support, buying power model consistency tests * Add SufficientBuyingPower and GetReservedBuyingPower to position group model Includes update to BrokerageTransactionHandler to use position group BPM for sufficient buying power checks. * Resolve position groups on each fill We need to update the state of our position groups on each fill so that we can properly handle multiple orders within the same time step. We also limit the number of positions sent into the resolver by removing securities without any holdings. * fixup! Add SufficientBuyingPower and GetReservedBuyingPower to position group model * Add GetMaximumLotsFor{Target|Delta}BuyingPower Instead of computing order quantity, these functions compute the maximum number of position group lots, which is the position group quantity, and is guaranteed to be a whole number, for the provided target/delta buying power parameters. The SecurityPositionGroupBuyingPowerModel delegates to the security's IBuyingPowerModel by applying a scaling factor equal to the security's lot size. This change also updates references to IBuyingPowerModel.GetMaximum... to use the new position group model methods. * Convert remaining IBuyingPowerModel call sites to position groups * Rename PositionManasger.CreateDefaultGroup -> GetOrCreateDefaultGroup Better describes its behavior * Add Position Groups readme.md * Add Option Strategy BuyingPowerModel - Adding CompositePrositionGroupResolver and OptionStrategyPositionGroupResolver - Adding OptionStrategyPositionGroupBuyingPowerModel handling option strategies based on IBs margin table. Adding regression algorithms - Few changes so that option strategies executed by multiple orders are detected - Adjust OptionStrategyDefinitionMatch to include equity legs in the matching result - Minor tweaks fixing previous rebase - Minor fixes for existing option strategies definitions, adding new missing strategies. - Fixing minor bugs in option strategy matcher. Adding more unit tests * Address self reviews - Fixing bug in 'PositionGroupCollection' - Few minor simplificaitons - Adding BasicTemplateOptionEquityStrategyAlgorithm * Address reviews - Improve regression algorithms margin remaining and used assert logic to be exact. Taking into account spread and fees Co-authored-by: Michael Handschuh <mhandschuh@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.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