I've been working on making an asset allocation model that requires I price a lot of financial instruments (i.e. bonds, options) and optimize based on a certain constraint. I was originally doing this in Matlab, but am now looking to switch it to Python. Is there a package that would be equivalent to the Matlab Financial Toolbox in Python? I've read about many of them (i.e. vollib, QuantLib/PyQL), but I'm not sure which one is the most trusted / popular / commonly used.
I took a quick look at Matlab's Financial Toolbox and attempted to map the features to corresponding Python packages –
For asset allocation, portfolio optimization, and risk analytics:
- Standard packages such as
scipyprovide a large number of optimizers that should suit your needs. There are also pre-canned packages that do portfolio optimizations more directly, but I don't have much experience with them.
- Specialized packages such as
alphalensprovide a ton of functions that make performance and risk analytics super easy.
- For some risk applications, you may also find
For pricing instruments -
- As you've already mentioned,
Quantlibis almost certainly the best open-source solution out there and there are a few ways to make Quantlib accessible from Python (e.g., QuantLib-Python).
- If you have other C++ code that you already use for pricing, take a look at Boost Python.
For time series analysis:
- A combination of
statsmodelsis the gold standard and should be more than sufficient for most purposes. The former provides a large collection of utilities for working with time series (
Panel, etc.), while the latter provides a comprehensive library for running anything for linear regressions to sophisticated Dynamic Factor Models.
pandas-datareaderhas many pre-built functions for retrieving financial and economic data from public sources.
- I've also found
archquite nifty for running GARCH-type models.
For Technical Analysis:
ta-libhas an easy-to-use Python wrapper.
For Financial Charts:
matplotlibis the core library.
pandashas built-in plotting functions that use matplotlib to make many chart types very easy to work with.
- For prettier charts, also check out
- I have yet to run into problems can't solved by
And for general numeric computing, you need:
numpyfor numerical computing;
scikit-learnfor machine learning.
sympyfor symbolic mathematics.
To get started, check out Anaconda from Continuum Analytics. They package all the various scientific and statistical Python libraries under one convenient installer. Some of these packages can be challenging to install if you try to install them piecemeal. If you've got Anaconda you've more or less got the full financial toolbox for Python and then you can start exploring the individual packages. Anaconda provides a list of installed packages with links to the documentation for each package: https://docs.continuum.io/anaconda/pkg-docs.html
If you want to start with a specific library provided by Anaconda, I would suggest pandas. It farms out to many other libraries under the hood for calculations, but it's a pretty ubiquitous top-level library (i.e. library the user interacts with) when it comes to quantitative finance in Python.