cuemacro / finmarketpy

Python library for backtesting trading strategies & analyzing financial markets (formerly pythalesians)
http://www.cuemacro.com
Apache License 2.0
3.41k stars 490 forks source link
backtesting-trading-strategies python trading-strategies

finmarketpy (formerly pythalesians)

Downloads

finmarketpy is a Python based library that enables you to analyze market data and also to backtest trading strategies using a simple to use API, which has prebuilt templates for you to define backtest. Included in the library

Contributors for the project are very much welcome, see below!

Merging with pythalesians

I had previously written the open source PyThalesians financial library (which has been merged with this - so can focus on maintaining one set of libraries). This new finmarketpy library has

Points to note:

Gallery

Calculate the cumulative returns of a trading strategy historically (see finmarketpy_examples/tradingmodelfxtrend_example.py)

Plot the leverage of the strategy over time

Plot the individual trade returns

Calculate seasonality of any asset: here we show gold and FX volatility seasonality (see examples/seasonality_examples.py)

Calculate event study around events for asset (see examples/events_examples.py)

Requirements

Major requirements

Installation

For detailed installation instructions for finmarketpy and its associated Python libraries go to https://github.com/cuemacro/finmarketpy/blob/master/INSTALL.md (which includes details on how to setup your entire Python environment).

Also take a look at https://github.com/cuemacro/teaching/blob/master/pythoncourse/installation/installing_anaconda_and_pycharm.ipynb from my Python for finance workshop course, where I keep notes specifically about setting up your Anaconda environment for data science (including for findatapy/chartpy/finmarketpy), including YAML files etc.

You can install the library using the below (better to get the newest version from repo, as opposed to releases).

After installation:

pip install git+https://github.com/cuemacro/finmarketpy.git

But beforehand please make sure you have already installed both chartpy, findatapy and any other dependencies. In chartpy you will need to change the chartconstants.py file (to add Plotly API key) and for findatapy, you will also need to change the dataconstants.py file to add the Quandl API (and possibly change other configuration settings there or add a datacred.py file in the util folder, alternatively you will be prompted on your first run to input the API key which will be installed). If you do pip with git you'll get the very latest commit.

pip install git+https://github.com/cuemacro/chartpy.git
pip install git+https://github.com/cuemacro/findatapy.git

However you can also pip install to get from PyPI (might be a slighter older verison from that on GitHub)

pip install chartpy
pip install findatapy

Note that if you use the option pricing/total returns you might need to get the latest FinancePy version from GitHub https://github.com/domokane/FinancePy/ as opposed to PyPI

pip install git+https://github.com/domokane/FinancePy/FinancePy.git

Binder and Jupyter - Run finmarketpy in your browser

You can run some of the Jupyter notebooks in Binder interactively in your browser to play around with finmarketpy. It might take a few minutes for the Binder instance to start. We are currently working on having more notebooks in Binder, so stay tuned!

Note that you will need to get a Quandl API key to download market data to use some of these, and you can sign up for a free account at https://www.quandl.com.

Synchronizing your fork of finmarketpy with master

I found this article useful for explaining how to update your fork to match the master changes.

Contributors

Contributors are always welcome for finmarketpy, findatapy and chartpy. If you'd like to contribute, have a look at Planned Features for areas we're looking for help on. Or if you have any ideas for improvements to the libriares please let us know too!

Sponsorship, workshops and support for Cuemacro libraries

We have spent many years writing finmarketpy and other open source libraries at Cuemacro, and we are keen to do so for many years into the future.

If you using our libraries and are interested in sponsoring Cuemacro's open source libraries, you can do so through the GitHub sponsorship page at https://github.com/sponsors/cuemacro

We also offer commercial services for our Cuemacro libraries, which include:

If you are interested in our commercial services please contact saeed@cuemacro.com

All these sources of funding, whether it is sponsorship or our commercial services, help us to maintain Cuemacro's libraries, so we can improve our open source libraries for the community.

Problems with Numba and doing options pricing in finmarketpy/financepy

Underneath finmarketpy uses financepy to do option pricing. It uses Numba to speed up the computation.

You may sometimes experience Numba errors like such as Failed in nopython mode pipeline (step: nopython frontend)

One possible way to fix this is to delete the __pycache__ folders underneath wherever financepy is installed:

Eg. if you are using the py38class environment, if you've installed Anaconda in C:\Anaconda3, you might find the financepy folder at the below location

C:\Anaconda3\envs\py38class\Lib\site-packages\financepy

finmarketpy examples

In finmarketpy/examples you will find several examples, including some simple trading models

Release Notes

Coding log

finmarketpy log

pythalesians log

End of note