COMMENT: How to write a systematic trading algorithm

eFC logo
How to write a trading algorithm

Systematic trading is a large, and growing, part of the market. Architects of these trading strategies come from a variety of backgrounds: coders who have never traded, traders who don’t know how to code, and data scientists with impeccable ivory tower credentials who can’t code or trade. As a result fundamental mistakes are common. Here are some rules to keep you out of trouble:

1. Be aware of market structure

Financial theory is beautifully elegant but the market isn’t as tidy in reality. Market movements don’t follow nice theoretical statistical distributions. Bid ask spreads can widen rapidly outside of expected norms, and liquidity can vanish in a heartbeat.

Borrowing costs can be squeezed to extremes, and some stocks might not be borrowable at all due to shortages or regulatory issues. Many algo designers have come to grief over prosaic details like day count conventions or non decimal pricing in the US treasury market. Do your research before designing your system.

2. Think carefully about the costs of market impact

In a clean simulation environment price movements aren’t affected by your trading. In reality once your algo goes live it’s trading will make prices move, and increase your costs of trading. This is particularly problematic if you’re trading quickly, in size, or in illiquid markets.

Careful tactics can reduce your market impact, but it will never be zero. Realistic estimates are vital when deciding whether your alpha will survive after paying it’s cut of trading costs.

3. Time is of the essence

Einstein had a few things to say about time. Astronauts who travel at high speed through space will perceive time passing more slowly. Time can also behave unusually in financial markets. Ticks can arrive at your trading server in the wrong order, or with incorrect timestamps. It’s difficult to get sychronised prices across multiple markets if you’re implementing a relative value algo. Dealing with the myriad different changes in daylight savings time is an operational headache. Your code and processes need to be able to cope with these problems.

Latency is also a consideration: the time delays involved in receiving data, processing, sending orders, and getting fill data back. Very high frequency trading strategies will die if their latency is too high. You’ll need to pay big bucks to compete in the latency arms race: co-locating your trading client near the exchange, and using high performance optimised hardware. But even slower algos need a realistic estimate of their likely latency to ensure they don’t have unrealistic expectations of performance.

4. Watch out for bad data

Systematic algos are designed to run automatically. That means they should be able to handle any and every kind of ‘corner case’: unexpected or bad input data. It’s not unusual for a raw data feed to produce prices that are out by a factor of 100, or for a zero to arrive instead of a valid price.

More difficult to spot are large price changes that could be real – another flash crash – or could be bogus. If you’re trading slowly enough then potentially bad data can be flagged for manual checking. Faster algos need to decide whether to accept poor data; or ignore it and wait in hope for the market to normalize.

5. Keep a close eye on your strategy

Just because algos run automatically doesn’t mean you can’t monitor them – and you should. Partly this will involve working out what’s already gone wrong, but you’ll also need tools to identify potential issues before they happen. Your system should generate regular reports to update you: calculations, trades, profits, risk and system diagnostics. Simple log files are fine for but large or complex systems should regularly dump diagnostic data into databases for interrogation or live monitoring.

The system should fire off alerts when things go really wrong. Being woken at 2am with an SMS from your trading algo isn’t much fun, but it’s preferable to losing your shirt when your algo blows up.

Robert Carver is a former head of fixed income at quantitative hedge fund AHL, and the author of 'Systematic Trading' and 'Smart Portfolios'. He now designs trading strategies for trading his own money.

Have a confidential story, tip, or comment you’d like to share? Contact: sbutcher@efinancialcareers.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.)

Related articles

Close