New Python open source financial libraries

20160918-swan

I’ve been using Python for several years and have built up a large amount of code for analysing financial markets. A year ago, I decided to open source PyThalesians, which became the no 2 Python trading library in GitHub with over 1000 stars and over 100 forks on GitHub. This week I merged PyThalesians into a new rewritten project finmarketpy, splitting off the market data downloading functionality into findatapy, and data visualisation elements into chartpy. The best news is that all of these libraries are free to download from GitHub!

 

The idea behind breaking down functionality into more specialised smaller Python libraries is that it is more accessible and should (hopefully!) encourage more contributors. It also makes it easier for users to pick and choose functionality, without having to delve into a massive library to discover it. After spending a few years using the code myself to develop trading strategies, I’ve better understood where I can improve the API. Thus, I’ve also spent a lot of time simplifying the API and making the libraries more Pythonic, whilst adding more new exciting features and rewriting them all from the ground up. I’ve also wanted to make some components (eg. chartpy) accessible to anyone dealing with data, not purely those working in finance.

 

So what do these new Python financial libraries do?

 

finmarketpy is a trading backtesting library for Python. It allows you to simply specify your trading algorithm using predefined templates, and then calculate the historical returns of your trading strategy. It also has other types of market analysis such as event studies. finmarketpy has as its main dependencies findatapy and chartpy. Below, we show the example of a trading strategy we have backtested with finmarketpy.

 

findatapy is a market data library and also performs basic market data analysis. With a few lines of code you can download historical data from many different sources including Bloomberg, Quandl, FRED, Google Finance, Yahoo Finance etc. (I also want to add Thomson Reuters, but haven’t yet got access to an Eikon terminal to test). Change a single word to change the data source! It also has some nice shortcuts for downloading FX data – one line of code to get the whole FX vol surface, automatically calculates cross rates and much more! findatapy also has the ability to make aliases for custom tickers, so you don’t need to remember the tickers for every vendor! If you already have a backtester (other than finmarketpy) like zipline you can simply plug in findatapy to get your data too. In time, I also want to add features for live data subscription to it. Below, we show you can download free FX tick data from DukasCopy using findatapy, into pandas dataframes.

md_request = MarketDataRequest(start_date='14 Jun 2016', finish_date='15 Jun 2016',
                                   category='fx', fields=['bid', 'ask'], freq='tick', 
                                   data_source='dukascopy', tickers=['EURUSD'])

df = market.fetch_market(md_request)

 

chartpy makes it easy to do data visualisation in Python. There are many great Python chart libraries like bokeh, matplotlib and plotly. However, all the APIs are different and it takes time to learn them all. With chartpy, I’ve written easy to use wrappers for each of these. Just change a single keyword to switch from matplotlib to bokeh to plotly. I’ve also made the API a lot a simpler than before, to reduce the number of lines of codes you need to access the API. I’m also planning to add more support for other libraries. It supports many basic plot types, including line, bar, surface etc. In the future, I’m planning to add more charting types. Here we give a few examples of the types of plot you can do with chartpy.

 

 

Below, we show you can plot pandas dataframes with chartpy, with matplotlib, bokeh and plotly.

chart = Chart(df=df, chart_type='line', style=style)

# we now plot using multiple plotting libraries, with the same dataframe
chart.plot(engine='matplotlib')
chart.plot(engine='bokeh')
chart.plot(engine='plotly')

If you have the time, please check out finmarketpy, findatapy and chartpy on GitHub. If you find the libraries useful, have a chat with me, if you want to learn more about them! Contributors are always welcome too!