How to Backtest a Trading Strategy By Example

This is part of Kollider’s “Long Story Short” series, where we share short and sweet thoughts and tips with our community. It is…

How to Backtest a Trading Strategy By Example

This is part of Kollider’s “Long Story Short” series, where we share short and sweet thoughts and tips with our community. It is educational and is not financial advice.

So you’ve created your own automated trading system and have even implemented your own strategy idea to boot. But now what? Should you immediately set your bot loose into the market?

Today, we’ll teach you about backtesting, a tool that experienced traders big and small use to validate their trade ideas. It’s an extra step that could dramatically improve the performance and longevity of your trading.

When it comes to backtesting, there’s no shortage of articles on the topic. And this is a good thing. There’s so much depth to this subject that you could spend weeks reading about it and still find yourself wondering how to use it.

In the spirit of our “Long Story Short”, we’ll teach you the basics of backtesting by showing you how it works through a previous strategy we’ve discussed, “the spread trade”. If you haven’t yet, make sure to do a quick read through it here.

Remember that in our version of the spread trade, we came up with a pricing model:

Spread_Price = First_Product_Price — Second_Product_Price

In the same simplified article, we talked about buying the spread on October 24 and selling it on October 26. Conveniently, that’s one of the largest moves in the spread price in that time frame. This was a chance move. We would have needed to be lucky to magically know the bottom and top of that range.

Spread price between Okex and Bitfinex BTCUSD perpetual swaps using 1-day prices

In reality, we generally want to be more methodical in our approach. Instead of guessing each day if we should buy or sell, we look back at past prices and find our buying and selling points. Let’s revisit those spread prices and this time test different price threshold settings.

The same prices, with each coloured line representing a different threshold setting.

In this example, we’re trying four different price settings. The black line suggests that we would buy the spread anytime it hits -$1,000 and sell anytime it hits +$750. The green line has -$500 and $0 as its thresholds. And so on.

Conceptually, what we’re doing to backtest this is pretending to run the strategy with these settings through our past data (one “run” per strategy setting), simulating matches whenever the prices hit our threshold, and then measuring our realised and unrealised profit for each setting. We then pick the setting that has the best P&L performance and decide to take that one live.

For example, in the black line setting, our strategy would buy the spread at five points in time (Oct 3, Oct 13, Oct 19, and Nov 7), and it would sell three times (Oct 19, Oct 26, and Nov 2). If we assume that we add one-BTC worth of long or short each time we hit a buy or sell threshold, we could easily calculate our strategy’s profit for those five buys and three sells. For the unrealised P&L at the end of the black-line backtest, we pretend that we are selling the two-BTC remaining long position at the last spread price we see, +$275. We repeat this same process for the orange, purple, and green line settings and pick the highest P&L.

You can see that we can do this simple example by hand. Doing it programmatically would let us test a lot more threshold settings and pick a potentially better-configured spread trade. And that’s it.

This is very much a simplified look at backtesting, but it’s a good start to build your understanding. In practice, there are more things to consider increasing the accuracy and robustness of your strategy’s performance in the wild. We might also take the stability of our strategy’s profit into account. We may want to use a longer set of dates instead of the 5 weeks, as in our example here. Similarly, the future doesn’t always follow the past, so we should probably balance the fit and figure out how to manage how minutely, or broadly we look into the market (e.g. in this article, we took a one-day look at spread prices, but perhaps we may want to look at how hourly prices would fare for us).

If we zoom in and use much more granular historical market data, we should also probably start investigating the assumptions our strategy makes and start embedding that into our backtesting system. We may begin then to consider how realistic its bought and sold prices actually are. We then get into slippage, order execution and market data latencies, maker and taker fees, etc.

You can backtest any strategy idea, and not just this version of the spread trade. How do you know just which details to change or actually pay attention to for your own strategy’s backtesting? Well, that’s a story for another day.

Know anyone else interested in learning about bitcoin, Lightning, and trading? Follow us on Twitter and help us share the word.