To work as a trader today you need a wide range of skills. You still have to be numerate, cool under pressure, and have an excellent memory. But this is no longer enough. Increasingly, traders have to be able to program.
I’ve written before about why Python is a good all round language for traders. It’s widely used to backtest trading strategies, and for risk management. With modern hardware it is powerful enough to price bespoke derivatives. While it’s not fast enough for high frequency trading, it can be used to run systematic trading strategies which don’t require low latency, or for market making in less liquid markets. It can also be used to analyse and tweak the behaviour of high frequency algorithms.
But most traders won’t come from a computer science background, and aren’t interested in spending years becoming expert programmers. They just want to know the absolute minimum to do their jobs. There are many books and free online courses for Python, but working your way through an entire course is probably unnecessary. Instead you need to be selective.
Firstly, download your Python distribution (assuming it isn’t already on your computer). Make sure you have an up to date version of Python (ideally 3.8, but definitely at least 3.0 as older versions are quite different).
Next you need to get yourself an ‘Integrated Development Environment (IDE)’; essentially a text editor specifically for writing code. You can write code in any editor, including Notepad or TextEdit, but a decent IDE will allow you to quickly test and debug code without leaving the editing environment, and will even spot errors in your code as you type. Popular IDEs include Eclipse, PyCharm, and Atom.
Now to start learning. You should understand the basic types of objects that Python offers: strings and numbers (make sure you can distinguish between integers and floats), and the types of ‘collection’ that contain other objects: dictionaries, lists and tuples.
To do anything interesting you will need to use ‘control structures’: if-then-else, for and while loops. For example you might have a trading strategy: if there is a larger size on the bid then cross the spread else leave the resting order on the bid. For is used to loop over different values, such as different days in an historic backtest; while code runs whilst a given condition is true, e.g. while the market is open, get the current order book. Python has a special kind of for loop called a ‘list comprehension’. It is definitely worth knowing as it makes your code more readable and efficient.
Boolean operations such as and, or, not allow you to make logical comparisons, e.g. if the stock is past it’s ex-dividend date and the dividend is not yet paid, then add the dividend to the total return series. You can also use all and any. Can all the companies in the basket can be shorted? Do any of the returns exceed the Value At Risk limit?
The greatest invention in the history of programming is probably the ‘function’. Functions allow you to write reusable code, saving time and energy. For example, rather than calculating the non parametric ‘greeks’ of an option separately, you could write a function that calculates the change in option price given a change to any of it’s inputs. This could then be modified for the delta (change to price), vega (change to volatility), and so on. It’s better to split large functions into small pieces, which makes it harder for bugs to hide and promotes reuse.
As a trader you’re probably going to need to do a fair bit of maths; and that means using the import statement to access in built ‘modules’ like math, decimal and statistics. Other useful modules are datetime and calendar, so you can answer questions like how many days until the next coupon is paid? Many python tutorials include long sections on creating modules and packages; you can skip this.
In fact you can entirely avoid the more obscure technical parts of the language. However, it’s important to get your head around the tricky concepts of ‘mutability’ and ‘referencing’, as these are the source of numerous weird bugs. The copy module can get you out of a lot of trouble here, as can good programming practice such as not reusing variable names.
But if you have more time, then I would recommend learning about ‘object orientated’ programming. You can get Python to do anything you want without it (in the jargon, Python is ‘Turing complete’ without objects), but with it your code will be easier to test, read, and debug. If your code has to interact with other peoples software, like as an internal quant pricing module, then learning about objects will make your life easier.
Once you have the basics in hand, you should consider learning some of external third party Python packages. The library you need will depend on the type of trader you are. Fortunately, I’ve already written another article that will help you out here.
Robert Carver is a former head of fixed income at quantitative hedge fund AHL, and the author of ‘Leveraged Trading’, 'Systematic Trading' and 'Smart Portfolios'. He now uses an automated system written in Python (using PyCharm) to trade his own capital.
Have a confidential story, tip, or comment you’d like to share? Contact: email@example.com in the first instance. Whatsapp/Signal/Telegram also available. Bear with us if you leave a comment at the bottom of this article: all our comments are moderated by human beings. Sometimes these humans might be asleep, or away from their desks, so it may take a while for your comment to appear. Eventually it will – unless it’s offensive or libelous (in which case it won’t.)
Photo by Vlad Tchompalov on Unsplash