The Joys of Algorithmic Trading and Backtesting

Kollider
5 min readNov 18, 2021

--

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.

For a lot of us, there’s nothing more enjoyable than watching market prices and coming up with a plethora of trading signals and ideas that you want to try out. This is why we love backtesting at Kollider. Why? You can very quickly craft and hone many of your trading ideas without risking capital.

In our previous Long Story Short, we started to teach you about backtesting by going through a spread trade example. Although that laid a foundation, you may wonder how that could be applied to other types of trading strategies.

Today, we’ll take a look at a few of the simple steps we think about when we discuss backtesting. And in order to best illustrate this framework, we’ll go over two more strategy examples. Despite the fact these approaches can be performed manually, these approaches are best performed programmatically (but don’t be deterred, humans!)

There are many nuances to backtesting, which makes it difficult to generalise. In order to simplify, we could summarise the steps as follows.

  1. Pick a Strategy. By strategy, we mean something that makes buying or selling decisions. This could be a market-making one, some directional one, a spread trade as we’ve discussed before, or a mixture of all.
  2. Parameterize It. Figure out the moving parts that can be changed in order to change performance. Then, make them configurable with a variable. Some common ones present in most strategies are the size of the orders we send out, adjustments to our order prices (e.g. the lean we talked about in market making), and thresholds for our signals (or what triggers us to send orders out) like we showed in the last article.
  3. Run Strategy on Different Settings. We then create different settings for each parameter and “play” each configured version of the strategy through our historical data.
  4. Score Their Performance. Measure performance of each run. Though this could solely be P&L, we may consider other factors like stability of the profits or volume traded by the strategy with that setting.
  5. Select the Best Performing Configuration. Based on our performance scoring, we’ll be able to pick the best settings.

In the same way that the shapes of different strategies will vary a lot, the steps listed above can also vary a lot depending on the strategy idea you are backtesting. Remember, our focus here is backtesting, so the details of the trading strategies will be kept light, and these strategies may not work in practice (but backtest them just to be sure!).

Momentum Trading

The momentum strategy is currently having (*ahem*) a moment in the world of directional strategies, especially with the growing influence of retail trading. In simple terms, momentum strategies use signals to help a trader guess whether the market will continue moving in the same direction.

A popular signal within the momentum world uses two moving averages (“MA”): a “slow” one that uses the average of prices for a larger period of time, say the most recent 8 weeks; and a “faster” one that averages a smaller period, say the most recent week. Each MA computes a single price. As soon as the faster one crosses over the slower one, we are prompted to go in that direction. As an example, if bitcoin’s price suddenly doubles after a week, our one-week MA will adjust quickly and cross the 8-week MA (if the fast one was previously below), and continue to move beyond it if bitcoin prices continue to change rapidly. In this world, we view this as a buy signal.

Now that we understand what a momentum strategy entails, we can go through each of the backtesting steps. Let’s skip #4 and #5 since we can just score and select by P&L for now.

  1. Pick a Strategy. Let’s have a strategy that uses a crossover MA signal that we check at the beginning of our trading day. If a fast one crosses a slower one, we take that direction (buy if the fast one is moving up, sell if the fast one is moving down).
  2. Parameterize It. In the example above, we pegged the fast MA to 1 week and the slow to 8 weeks. We also said that as long as the fast crosses the slow, we’ll treat that as a trading signal. We’ll keep it simple and parameterize just these. One variable to configure is the number of weeks for the fast MA. The second is for the number of weeks for the slow MA. A third is the percentage that the fast has to cross the slow before we take it as a signal. For example, should the fast MA price be 0% (i.e. just having crossed), 0.5%, or 1% above the slow one before our strategy tries to buy into the market and take a long position?
  3. Run Strategy on Different Settings. We have three parameters here. Three example settings we may run are {1 week, 8 weeks, 0%}, {1 week, 4 weeks, 0.5%}, {2 weeks, 26 weeks, 3%}. Note that for the 26 week one, we’ll need much more historical data. Also, we would need something that calculates the daily past MAs for the given weekly settings above.

Market Making

As a strategy, we featured market making before. Let’s see how it might work if we do the backtesting above.

  1. Pick a Strategy. Here, we’ll go with the market making strategy we discussed before. We’ll again use a moving average, this time for the reference price. For the offsets, we’ll make it symmetrical so that the buy offset is the same as the sell. Finally, let’s skip the lean for this exercise.
  2. Parameterize It. We’ll do two here. Let’s do the window size of the reference price MA in terms of minutes. And, let’s have the offset in terms of a percentage off either side of the reference price.
  3. Run Strategy on Different Settings. Again, this can be any multitude of settings, but here are three examples: {15-minute price window, 0.25% off the reference price}, {30 minutes, 0.5%}, and {120 minutes, 2%}

You can repeat these steps for any variation of any strategy framework. Perhaps you have a momentum strategy that enters into positions by a crossover MA signal, but exits whenever the profit or loss on that position has been at least some threshold (hint: that threshold can be a parameter). We didn’t discuss order quantities and max positions you could allow the strategy to take, but those parameters are a little more straightforward to understand, so we’ll let the reader experiment with those.

As with our previous article, this is a simplified version of backtesting that is intended as a starting point. In improving its process, you might look into best practices relating to optimal fit. Additionally, you may want to improve the accuracy of the simulated matches by including exchange fees, delays when sending orders to the exchange, and latencies when receiving market data. Otherwise, the results of the backtest could be optimistic compared to reality. However, optimism goes a long way in the backtesting world.

That’s all there is to it! Play in the sandbox, dream up new ideas, and test them. Enjoy the beautiful chaos. Until next time, happy trading!

Typically, algorithmic traders are found in sandboxes.

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

--

--

Kollider

Building new ways to access cryptocurrency markets.