Jupyter Notebook Python Open Source Financial Library to backtest trading strategies, plot charts, seamlessly download market data, analyse market patterns and much more!
PyThalesians
PyThalesians is a Python financial library developed by the Thalesians (http://www.thalesians.com). I have used the library to develop my own trading strategies and I've included simple samples which show some of the functionality including an FX trend following model and other bits of financial analysis.
There are many open source Python libraries for making trading strategies around! However, I've developed this one to be as flexible as possible in terms of what types of strategies you can develop with it. In addition, a lot of the library can be used to analyse and plot financial data for broader based analysis, of the type that I've had to face being in markets over the years. Hence, it can be used by a wider array of users.
At present the PyThalesians offers:
Backtesting of systematic trading strategies for cash markets (including cross sectional style trading strategies)
Sensitivity analysis for systematic trading strategies parameters
Seamless historic data downloading from Bloomberg (requires licence), Yahoo, Quandl, Dukascopy and other market data sources
Produces beautiful line plots with PyThalesians wrapper (via Matplotlib), Plotly (via cufflinks) and a simple wrapper for Bokeh
Analyse seasonality analysis of markets
Calculates some technical indicators and gives trading signals based on these
Helper functions built on top of Pandas
Automatic tweeting of charts
And much more!
Please bear in mind at present PyThalesians is currently a highly experimental alpha project and isn't yet fully
documented
Uses Apache 2.0 licence
Gallery
Below we give some examples of analysis we've done with PyThalesians. Some of these can be run by scripts in the examples folder.
Using PyThalesians to create a simple FX trend following strategy (you can run this
backtest using cashbacktest_examples.py)
Using PyThalesians to plot & calculate USD/JPY intraday moves around non-farm payrolls over past 10 years
Using PyThalesians to calculate intraday vol in major FX crosses by time of day
Using PyThalesians to create the Thalesians CTA index (trend following), which replicates Newedge CTA index benchmark
Using PyThalesians with Cufflinks (Plotly wrapper) to plot interactive Plotly chart (using plotly_examples.py) - click the below to get to the interactive chart
Using PyThalesians to plot via Bokeh EUR/USD in the 3 hours following FOMC statements
Using PyThalesians to plot combination of bar/line/scatter for recent equity returns
(you can run this analysis using bokeh_examples.py)
Using PyThalesians and PyFolio to plot return statistics of FX CTA strategy
(you can run this analysis using strategyfxcta_example.py)
Using PyThalesians to plot with Plotly map of USA unemployment rate by state (using FRED data)
(you can run this analysis using histecondata_examples.py)
Using PyThalesians to plot G10 CPI YoY rates (using FRED data)
(you can run this analysis using histecondata_examples.py)
Using PyThalesians to plot rolling correlatons in FX (using Bloomberg data)
(you can run this analysis using correlation_examples.py)
Using PyThalesians to plot seconds data around last NFP (using Bloomberg data)
(you can run this analysis using tick_examples.py)
Using PyThalesians to plot AUD/USD total returns from spot & deposit data (comparing with spot and Bloomberg
generated total return index) (you can run this analysis using indicesfx_examples.py)
Requirements
PyThalesians has been tested on Windows 8 & 10, running Bloomberg Terminal software.
I currently run PyThalesians using Anaconda 2.5 (Python 3.5 64bit) on Windows 10. Potentially, it could also work on the
Bloomberg Server API (but I have not explicitly tested this). I have also tried running it on Ubuntu and Mac OS X (excluding Bloomberg API)
Recommended: Cufflinks a nice Plotly
wrapper when using Pandas dataframes (Jorge Santos project now supports Python 3
https://github.com/jorgesantos/cufflinks - so I recommend using that rather than my fork)
Once installed please make sure you edit pythalesians.util.constants file for the following variables:
Change the root path variable - this will ensure that the logging (and a number of other features works correctly).
Failure to do so will result in the project not starting
Change the default Bloomberg settings (Which API to use? What server address to use?)
Write in API keys for Quandl, Twitter, Plotly etc.
Latest version can be installed using setup.py or pip (see below)
After installation, the easiest way to get started is by looking at the example scripts. I am hoping to add some Jupyter notebooks, illustrating how to use the library too. The example scripts show how to:
Download market data from many different sources, Bloomberg, Yahoo, Quandl, Dukascopy etc
Plot line charts, with different styles
About the Thalesians
The Thalesians are a think tank of dedicated professionals with an interest in quantitative finance, economics,
mathematics, physics and computer science, not necessarily in that order. We run quant finance events in London, New York,
Budapest, Prague and Frankfurt (join our Meetup.com group at http://events.thalesians.com). We also publish research
on systematic trading and also consult in the area. One of our clients is RavenPack, a major news analytics vendor.
Major contributors to PyThalesians
Saeed Amen - Saeed is managing director and co-founder of the Thalesians. He has a decade of experience creating and successfully running systematic trading models at Lehman Brothers and Nomura. Independently, he runs a systematic trading model with proprietary capital. He is the author of Trading Thalesians – What the ancient world can teach us about trading today (Palgrave Macmillan). He graduated with a first class honours master’s degree from Imperial College in Mathematics and Computer Science.
Supporting PyThalesians project
If you find PyThalesians useful (and in particular if you are commercial company) please consider supporting the project
through sponsorship or by using our consultancy/research services in systematic trading. If you would like to contribute to the project, also let me know: it's a big task to try to build up this library on my own!
21 Apr 2016 - Got rid of deprecated Pandas methods in EventStudy
18 Apr 2016 - Fixed some incompatibility issues with Pandas 0.18
06 Apr 2016 - Added more trade statistics output
01 Apr 2016 - Speeded up joining operations, noticeable when fetching high freq time series
21 Mar 2016 - Added IPython notebook to demonstrate how to backtest simple FX trend following trading strategy
19 Mar 2016 - Tested with Python 3.5 64 bit (Anaconda 2.5 on Windows 10)
17 Mar 2016 - Refactored some of graph/time series functions and StrategyTemplate
11 Mar 2016 - Fixed warnings in matplotlib 1.5
09 Mar 2016 - Added more TradeAnalysis features (for sensitivity analysis of trading strategies)
01 Mar 2016 - Added IPython notebook to demonstrate how to download market data and plot
27 Feb 2016 - Fixed total returns FX example
20 Feb 2016 - Added more parameters for StrategyTemplate
13 Feb 2016 - Edited time series filter methods
11 Feb 2016 - Added example to plot BoJ interventions against USDJPY spot
10 Feb 2016 - Updated project description
01 Feb 2016 - Added LightEventsFactory to make it easier to deal with econ data events (stored as HDF5 files)
20 Jan 2016 - Added kurtosis measure for trading strategy results, fixed Quandl issue
19 Jan 2016 - Changed examples folder name
15 Jan 2016 - Added risk on/off FX correlation example
05 Jan 2016 - Added total return (spot) indices construction for FX and example
26 Dec 2015 - Fixed problem with econ data downloaders
24 Dec 2015 - Added datafactory templates for creating custom indicators
19 Dec 2015 - Refactored Dukascopy downloader
10 Dec 2015 - Various bug fixes
22 Nov 2015 - Increased vol targeting features for doing backtesting
07 Nov 2015 - Added feature to download tick data from Bloomberg (with example)
05 Nov 2015 - Added intraday event study class (and example)
02 Nov 2015 - Added easy wrapper for doing rolling correlations (and example)
28 Oct 2015 - Added more sensitivity analysis for trading strategies
26 Oct 2015 - Various bug fixes for Bloomberg Open API downloader
14 Oct 2015 - Added capability to do parallel downloading of market data (thread/multiprocessing library), with an
example for benchmarking and bug fixes for Bloomberg downloader
25 Sep 2015 - Refactored examples into different folders / more seasonality examples
19 Sep 2015 - Added support for Plotly choropleth map plots & easy downloading of economic data via FRED/Bloomberg/Quandl
12 Sep 2015 - Added basic support for PyFolio for statistical analysis of strategies
21 Aug 2015 - Added stacked charts (with matplotlib & bokeh) & several bug fixes
15 Aug 2015 - Added bar charts (with matplotlib & bokeh) & added more time series filter functions
09 Aug 2015 - Improved Bokeh support
07 Aug 2015 - Added Plotly support (via Jorge Santos Cufflinks wrapper)
04 Aug 2015 - Added ability to download from FRED and example for downloading from FRED.
29 Jul 2015 - Added backtesting functions (including simple FX trend following strategy) and various bug fixes/comments.
24 Jul 2015 - Added functions for doing simple seasonality studies and added examples.
17 Jul 2015 - Created example to show how to use technical indicators.
13 Jul 2015 - Changed location of conf, renamed examples folder to pythalesians_examples. Can now be installed using setup.py.
10 Jul 2015 - Added ability to download Dukascopy FX tick data (data is free for personal use - check Dukascopy terms & conditions). Note that past month of data is generally not made available by Dukascopy