How to write a successful trading algorithm
If you want to succeed as a trader in future, you're going to need to understand how algorithms work. As Google chairman Eric Schmidt told the audience at last week's SALT Conference, either algorithms are going to be doing all the trading themselves, or humans are going to be asking algorithms whether particular trades make sense. Either way, they're going to be a big part of the job.
Helpfully, Richard B. Olsen, a quantitative finance veteran and the Swiss-based founder of Olsen Limited, a quant hedge fund, and OANDA, an FX trading site, has just released his very detailed guide to creating an automated trading algorithm, or "Alpha Engine."
You can see Olsen's full package, which addresses FX traders, either here, or here on Github. If you want the dummy's version, with text and charts instead of equations and code, we've parsed Olsen's approach below.
You won't be ready to write your own algorithm if you read it, but you will at least have an idea how a successful alpha-generating trading algorithm can be constructed.
1. Keep it simple
If you want to create a good algorithmic (i.e. mathematical) model of reality, Olsen says you need to keep it simple. Complexity at a macro level is almost always the result of simple rules of interaction at the micro level. You're trying to model these rules. "The system can be naturally reduced to a set of agents and a set of functions describing the interactions between the agents," says Olsen. The network is simply the ideal formal representation of your system. The nodes in the network are the agents and the links between the agents describe how they interact.
Olsen chooses the FX market because he says it's one of the easiest to replicate in the form of a model. Prices ('quotes') are always given as one currency in reference to another currency: they're symmetric. When you're going long one currency (buying it because you think the price will rise), you'll usually be going short the other (click here for a definition of short selling).
2. Define your approach to time
Olsen uses a so-called "endogenous time scale" in which time isn't time as we usually conceive it, but time as defined by specific actions and events. Only if these actions and events take place, does the 'system's clock tick.' For this reason, Olsen calls it "intrinsic time" - it's a definition of time intrinsic to his model. This has the advantage of filtering out all the irrelevant information that occurs between events. The "signal to noise ratio" is improved in algo-language.
3. Define the events you want to look at
What are the events that cause time to progress? Olsen defines them as: a directional change in the price of a currency (δ), and an overshoot in the price after that directional change (ω). His 'event-based' price curve is made up of δ and ω and Olsen it "the coastline."
For the model to function, however, it's necessary to define the 'event thresholds' at which it becomes active. In the "price up mode", the highest price is updated and continuously increased. When the price starts to fall again, the difference between the highest price and the current price is evaluated and if the difference exceeds a predefined threshold a directional change event is registered. If the price then continues to move in the same direction as the directional change, for the size of the threshold, an overshoot event is registered too.
You can see a graphical example below. The chart on the right shows Olsen's coastline representation of a EUR USD price curve. The blue triangles represent directional-change. The green bullets represent the overshoot events.
4. Add in some 'scaling laws'
It also helps to anticipate the size of the overshoot. In the FX market, Olsen says there are 'scaling law relations' which frequently relate to directional price changes and price overshoots: "A directional change δ is followed by an overshoot ω of the same magnitude hωi ≈ δ." This can then be built into the model.
5. Define what happens when your events take place
Olsen's "Alpha Engine" is a 'counter-trending trading model.' In it, trading positions which go against the trend are either maintained or increased by the algorithm.
The ultimate aim is to trade the whole length of the FX price curve (what Olsen calls "the coastline"), in so-called "coastline trading". You can see how this works in theory in the chart below.
6. Add in a probability indicator
So far, so good. Except Olsen notes that when markets are experiencing very strong trends, they don't always operate according to the model above. Instead, agents' behaviour changes as they amount very large inventories. Because of this, the algorithm also needs a probability indicator ( L) which helps keep the model accurate during times of severe market stress. When the price overshoots and trading agents already have a lot of inventory, they won't buy and sell as much as usual. By adding 'L' into his algorithm, Olsen's therefore able to moderate the extent of inventory changes in treacherous markets.
7. Add in asymmetric thresholds that reflect the market's direction
Lastly, Olsen says it helps to define your event thresholds in terms of the market's direction. You might want different thresholds to trigger your events depending upon whether the market is moving up, or down. Analytically, this is expressed as: δ → ( δup for increasing prices; δdown for decreasing prices). Therefore, ω = ω(δup, δdown) denotes the length of the overshoot corresponding to the new upward and downward directional change thresholds.
The effect of asymmetric thresholds is shown in the charts below. In the left hand panel, the price overshoots and there are two identical trading events represented by the down arrows (where short existing positions are increased). In the right hand chart, an asymmetric threshold is used to divide the overshoot into four segments so that short positions are increased four times instead of two and the event is "smeared out."
Once you've got your trading algorithm, you need to backtest it. This means seeing how it performs compared to real market data from the past. Olsen backtested his model from the beginning of 2006 to the beginning of 2014. He says it yields an un-levered return of 21.3401%, with an annual Sharp ratio of 3.06.
Taken from The Alpha Engine: Designing an Automated Trading Algorithm by Anton Golub, James Glattfelder and Richard Olsen.
Photo credit: Algorithms by Kevin Dooley is licensed under CC BY 2.0.