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, sell below!
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
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)
Major requirements
- Required: Python 3.4, 3.5
- Required: pandas 0.18, numpy etc.
- Required: findatapy for downloading market data (https://github.com/cuemacro/findatapy)
- Required: chartpy for funky interactive plots (https://github.com/cuemacro/chartpy)
For detailed installation instructions for finmarketpy and its associated Python libraries go to https://github.com/cuemacro/finmarketpy/INSTALL.md (which includes details on how to setup your entire Python environment).
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 file
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 file (to add Plotly API key) and for findatapy, you will also need to change the DataConstants file to add the Quandl API (and possibly change other configuration settings there).
pip install git+https://github.com/cuemacro/chartpy.git
pip install git+https://github.com/cuemacro/findatapy.git
Contributors are always welcome for finmarketpy, findatapy and chartpy. If you'd like to contribute, have a look at [Planned Features](PLANNED_FEATURES.md] for areas we're looking for help on. Or if you have any ideas for improvements to the libriares please let us know too!
In finmarketpy/examples you will find several examples, including some simple trading models
- 0.11 - finmarketpy
- First prerelease version
- 03 May 2017 - Added more planned features.
- 13 Apr 2017 - Changed finish date on FX trend following model
- 12 Mar 2017 - Added FX vol surface animation example
- 25 Feb 2017 - Added signal delay parameter
- 24 Feb 2017 - Refactored backtesting classes so have consistent naming
- 21 Feb 2017 - Refactored BacktestEngine to use SwimPool
- 20 Feb 2017 - Extra install instructions
- 14 Feb 2017 - Added Planned Features page
- 08 Feb 2017 - Added SHOW_CHARTS parameter for TradingModel and made SMA work with old pandas
- 05 Feb 2017 - Added more installation notes and fixed Excel output in TradeAnalysis if notional not specified
- 02 Feb 2017 - Further changes to constraints on max long/shorts (with refactoring)
- 01 Feb 2017 - Added constraints for max longs/shorts and plots in BacktestEngine
- 25 Jan 2017 - Additional work on stops/take profit with multiple assets & plotting bug fixes for TradeAnalysis
- 24 Jan 2017 - Fixing issues around stops/take profits and adding fields in TechParams
- 19 Jan 2017 - Change location of examples in project
- 16 Jan 2017 - Added method in BacktestEngine for debugging of P&L (dumps table with signals/assets/returns)
- 12 Jan 2017 - Added detailed installation notes
- 11 Jan 2017 - Rewrote large number of comments, added ATR calculation and basic stop loss/take profit functionality
- 07 Jan 2017 - Now outputs position sizes scaled by notional & by user defined contract sizes
- 06 Jan 2017 - Added user defined weightings for strategies & general bug fixes
- 04 Jan 2017 - Added a period shift parameter for calculating leverage (in RiskEngine)
- 30 Nov 2016 - Added seasonality example for NFP
- 24 Nov 2016 - Added seasonality example for gasoline
- 17 Nov 2016 - Changed source to ChartConstants default for TradingModel
- 14 Oct 2016 - Fixed arctic references in MarketConstants
- 13 Oct 2016 - Fixed IR plotting for BacktestEngine, added YoY metric plots
- 11 Oct 2016 - Added to TradeAnalysis another way to plot return statistics for a portfolio
- 10 Oct 2016 - Added returns_example to show how to use PyFolio via finmarketpy, added dataframe input for TradeAnalysis, fixed typo in readme
- 07 Oct 2016 - Add .idea to .gitignore
- 06 Oct 2016 - Split out plotting of no of trades and position proportion
- 22 Sep 2016 - Fixed sorting of columns when signal plotting
- 21 Sep 2016 - Allow plotting of multiple signal days
- 15 Sep 2016 - Merged finmarketpy and pythalesians fully, released version 0.11
- 12 Sep 2016 - Fixed issue with TradeAnalysis (method names)
- 02 Sep 2016 - Fixed issue with external dataframe eco events, added event study example
- 01 Sep 2016 - Added seasonality example for FX vol
- 22 Aug 2016 - Fixed boot issue and added credentials
- 17 Aug 2016 - Uploaded first code