Python use cases for trading desks

If there’s one thing I get asked most by traders, it is usually a question about how to learn Python! I recently taught a Python for finance course at Queen Mary University of London. Judging by the take up of the course, Python is  also a topic of interest for students wanting to break into the finance industry, not just for traders already in the industry. Subtle plug alert here.. I’ve started teaching my Python for finance workshop in-house too, so if you’re at a financial firm, and you’d like me to come and teach it at your firm drop me a message! I’m currently taking bookings for early 2020!


Another question I often get asked is what are the use cases for Python within a trading desk? Sometimes folks want to learn Python, but say Excel appears to cater for most of their needs. In practice, it’s usually the case, that traders have had to adapt to the limitations of what Excel does. Python basically opens up many possibilities for trading desk tools, which are limited by Excel. Here we’ll list a few examples of the types of tasks you could do in Python on a trading desk to help traders.


Use Excel as a front end and Python as a backend with xlwings
The choice isn’t to use Excel or Python. Instead, we can use Excel as a front end (which traders like!), and then use Python to do all the heavy duty number crunching, by using the xlwings library. I’ve done some client projects for traders using xlwings, and found it quite intuitive to use (it helps to know a bit of VBA as well as Python and I’m also teaching a VBA/Excel workshop too, if that’s of interest!).


Working with large datasets and distribute that computation
Let’s say you’re a volatility trader, and you want to do a relatively simple realised volatility calculation on a rolling basis. It’s pretty easy to do this on Excel for daily data. But let’s say you want to use 1 minute (or 5 minute) data, and you want to compute this for many assets. Excel is probably going to have difficulties with this. With Python it’s a breeze, plus you can take advantage of many builtin libraries like Pandas which can efficiently compute rolling realised vol with 1 line of code. You can also do TCA (transaction cost analysis) style calculations in Python more easily, combining benchmark high frequency tick data with your own trade data. Cuemacro’s tcapy TCA library is written in Python, and uses a number of tips and tricks, to speed up the computation and also distribute it.


Working with alternative data in the investment process
Just imagine you could crunch machine readable news, to make observations about specific company, rather than manually searching through countless articles. Natural language processing lets you do this, and there are many libraries in Python to help here. Often alternative datasets which you might want to work with in finance, can be in text (or image) form. Hence, Python ends up being a gateway to using alternative data in your investment process. 


Reusing code across the organisation, saving time
What often happens in banks is that the same spreadsheets end up getting created many times over by different folks across a trading floor: ie. repeatedly reworking the wheel! Instead of that, factor out the common functionality into a Python library that everyone can use and have it written properly once. Admittedly, Python isn’t unique in this aspect, but it’s a lot quicker to create a Python library than C++. In terms of use cases, this could range from creating a option pricing library (and indeed many banks have this), backtesting libraries (similar to Cuemacro’s finmarketpy library).

Use machine learning libraries with Python
Once you have the ability to look at vast quantities of data, you might want to use more advanced ways to crunch the data. This could be for example to find anomalous trades (whether for compliance reasons, fat fingers etc.). There are lots of machine learning libraries such as scikit-learn and TensorFlow, which can be accessed in Python. 


Stop those manual Excel updates!
I have to admit I have spent far too much during my career manually updating Excel spreadsheets. It is a painful process, which ultimately is also prone to error. Every trading desk will have scores of Excel spreadsheets, which junior folks (and senior folks too!) have likely spent hundreds of hours updating. It’s time to stop that and use Python to automate the process. This could be market monitor style updates, which are sent internally to the trading desk (or to clients) or sending out price runs to clients. Python has extensive tools for getting market data from many sources (including databases) and relatively quick and easy to use ways of automating e-mails. Your juniors will thank you for this.


Python is open source, so you’re not forced to use a specific vendor
So many solutions sound great, but ultimately force you to be dependent on a specific vendor (good for a vendor, but maybe not so good for the client). Python is developed on many open source tools. You don’t need to pay expensive licences to deploy Python on every trader’s system (whether for them to run Python tools created elsewhere, or for them to write their own code) – it’s all free. Management and shareholders are going to be happy about this.


Learning Python helps upskill you and your team!
In this age where there is constant talk about automation, learning Python helps to upskill you and your team. It’ll all make you and your team more efficient. Longer team learning new skills can only be a good thing, when it comes to your career. Employees will be happier!


There are of course countless use cases for Python on a trading desk, and they are likely to vary between sell side and buy side. If you’ve found any particularly good use cases on a trading desk for Python let me know!