SkillAgentSearch skills...

Finmarketpy

Python library for backtesting trading strategies & analyzing financial markets (formerly pythalesians)

Install / Use

/learn @cuemacro/Finmarketpy
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<img src="finmarketpy_logo.png?raw=true" width="300"/>

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

  • Prebuilt templates for backtesting trading strategies
  • Display historical returns for trading strategies
  • Investigate seasonality of trading strategies
  • Conduct market event studies around data events
  • In built calculator for risk weighting using volatility targeting
  • Written in object oriented way to make code more reusable

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

  • Similar functionality to the trading part of pythalesians
  • Rewritten the API to make it much cleaner and easier to use, as well as having many new features.
  • finmarketpy requires the libraries, which I've written chartpy (for charts) and findatapy (for loading market data) to function
  • By splitting up into smaller more specialised libraries, it should make it easier for contributors
  • Using findatapy, you can download market data easily from Bloomberg, Quandl, Yahoo etc
  • Using chartpy, you can choose to have results displayed in matplotlib, plotly or bokeh by changing single keyword!

Points to note:

  • Please bear in mind at present finmarketpy is under continual development. The API is heavily documented, but we are looking to add more general documentation.
  • Uses Apache 2.0 licence

Gallery

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

<img src="finmarketpy_examples/gallery/fx-trend-cumulative.png?raw=true" width="750"/>

Plot the leverage of the strategy over time

<img src="finmarketpy_examples/gallery/fx-trend-leverage.png?raw=true" width="750"/>

Plot the individual trade returns

<img src="finmarketpy_examples/gallery/fx-trend-trade-returns.png?raw=true" width="750"/>

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

<img src="finmarketpy_examples/gallery/gold-seasonality.png?raw=true" width="750"/> <img src="finmarketpy_examples/gallery/fx-vol-seasonality.png?raw=true" width="750"/>

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

<img src="finmarketpy_examples/gallery/usdjpy-nfp.png?raw=true" width="750"/>

Requirements

Major requirements

  • Required: Python 3.10
  • Required: pandas, numpy etc.
  • Required: findatapy for downloading market data (https://github.com/cuemacro/findatapy)
  • Required: chartpy for funky interactive plots (https://github.com/cuemacro/chartpy)

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) etc.

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

After installation:

  • Make sure you edit the marketconstants.py file (or you can create a marketcred.py file to overwrite the settings)
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 version from that on GitHub)

pip install chartpy
pip install findatapy

FinancePy is an optional dependency for finmarketpy for option pricing. It is recommended to install it separately from PyPI after installing finmarketpy, and without dependencies otherwise it can cause clashes with other libraries (because of its strict version dependencies on libraries like llvmlite, which in practice can be relaxed). The API changes a lot so it recommended to install the specific version listed below.

pip install numba numpy scipy llvmlite ipython pandas prettytable
pip install financepy==0.370 --no-deps

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:

  • a 2 day Python for finance workshop, which can be taught at your firm, to teach you how to use Cuemacro's libraries
  • extensive commercial technical support for our libraries

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 py310class environment, if you've installed Anaconda in C:\Anaconda3, you might find the financepy folder at the below location

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

To upgrade only finmarketpy and other Cuemacro packages (and no dependencies)

pip install -U --no-deps finmarketpy findatapy chartpy

finmarketpy examples

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

Release Notes

  • 0.11.19 - finmarketpy (09 Mar 2025)
  • 0.11.16 - finmarketpy (08 Mar 2025)
  • 0.11.15 - finmarketpy (08 Mar 2025)
  • 0.11.14 - finmarketpy (19 May 2024)
  • 0.11.13 - finmarketpy (01 Jan 2024)
  • 0.11.12 - finmarketpy (26 Apr 2023)
  • 0.11.11 - finmarketpy (07 Oct 2021)
  • 0.11.10 - finmarketpy (06 Oct 2021)
  • 0.11.9 - finmarketpy (01 Jun 2021)
  • 0.11.8 - finmarketpy (25 Jan 2021)
  • 0.11.7 - finmarketpy (20 Oct 2020)
  • 0.11.6 - finmarketpy (02 Oct 2020)
  • 0.11.5 - finmarketpy (24 Aug 2020)
  • 0.11.4 - finmarketpy (06 May 2020)
  • 0.11.3 - finmarketpy (04 Dec 2019)
  • 0.11.1 - finmarketpy (23 Oct 2019)
  • 0.11 - finmarketpy
  • First prerelease version

Coding log

finmarketpy log

  • 09 Mar 2025
    • Removed FinancePy from pyproject.toml
  • 08 Mar 2025
    • Make FinancePy an optional dependency
    • Fixed various deprecation messages related Pandas
    • Made Plotly charts look nicer (autoscale with latest ChartPy)
  • 07 Mar 2025
    • Merge changes for pyproject.toml etc.
    • Formatting towards PEP8
  • 09 Nov 2024
    • Changed trend following example to use FRED
  • 19 May 2024
    • Fixed run in parallel bug on
  • 01 Apr 2024
    • Added Jupyter notebook for new ArcticDB support from findatapy
  • 01 Jan 2024
    • Helper code to reduce boiler plate code for TradingModel
    • Upgraded to FinancePy 0.310 and refactored FXVolSurface
  • 26 Apr 2023
    • Changed sklearn to scikit-learn dependency
  • 05 Apr 2022

Related Skills

View on GitHub
GitHub Stars3.7k
CategoryDevelopment
Updated10h ago
Forks524

Languages

Python

Security Score

100/100

Audited on Mar 26, 2026

No findings