Trade Bot Logic

A common strategy in the financial markets is to trade 2 correlated stocks against one another, shorting one and longing the other as their correlation deviates away from the norm. This is called pair trading, and though it doesn’t always work, it is easy to execute and analyze. At the core of pair trading is the concept of mean reversion, or simply put, making the assumption that the 2 stocks will revert back to their norm, allowing traders to close the trade and book a profit.

There are many ways to model correlation, such as running a Kolmogorov–Smirnov test, and although some of these models can be executed quite successfully, they can end up being quite complex. This is where this logic has an advantage in the cryptocurrency markets.

Cryptocurrencies trade like stocks. Each ticker backs a project, like a stock, and over the long term provide a good representation of the project’s fundamentals, while experiencing wild fluctuations in the interim. But despite trading like stocks, they have properties of currencies – trading in multiple markets and never closing. This makes pair trading correlation far more simple to analyze, since it lets traders trade directly from currency to currency. In other words, we can trade from BTC directly to ETH, without having to go back to USD or another base fiat. This would be equivalent to buying AAPL with AMZN stock directly, which obviously cannot be done.

This matters because we can analyze and apply calculations to the price of BTC and ETH directly, and not just the correlation. Typically, the movements between cryptocurrency are a lot less volatile – similar to movements between stocks within the same industry. When ETH is down 10%, chances are EOS, NEO, and others will be down a similar amount, meaning the price change between ETH-EOS directly will be little to unchanged.

In the long run, the coin prices will digress away from one another, and despite being more closely correlated is still a speculation playground for traders. Short term, however, is where the opportunity is. Although there can be some sharp price swings, for the most part the returns in crypto to crypto trading are relatively muted.

The advantages of bot-ing

A bot serves one real purpose; to do things humans can’t. This rule should be the basis of every bot design.

To most people, this means using a bot to either a) trade lightning fast, or b) catch fast moves that break out. a) is possible if you have some kind of an advantage but for the average person not so much. b) sounds great in theory, but actual results using automation are fairly lackluster.

The Algorithm

Let’s take a different approach. This algorithm looks to find the most boring market out there. Boring would be defined by a standard deviation lower than the rolling average – indicating a flat, range-bound market – a human traders nightmare.

Locking on to these markets, the algorithm strangles the coins, providing support and acting as resistance. At any sign of volatility, bids are immediately pulled and the algo searches elsewhere for another boring market to control.

The above is the actual performance using this algorithm over the first half of this month (Oct 1, 2018 – Oct 15, 2018). The X value represents different coins that were traded. It returned 10.69% over this time frame, which is fairly consistent with it’s past performance.

A few things stand out about this strategy.

  1. It is consistent – rather than searching for stocks that break out for massive returns, we look for steady, slow moving markets. This means that the return should be fairly consistent with little surprises on the downside, or up.
  2. It can be easily analyzed. Looking at the charts above, you can see which coins the algorithm favored trading over the past 2 weeks, and also which ones it had trouble trading – suggesting they weren’t well correlated. Underperformers can be easily removed from the trade list.
  3. It is easy to backtest. Backtesting the strategy against past data is perhaps one of the most important parts of creating algorithms, to ensure your system actually performs as it should. This should be as simple as looping the strategy through 100 different stocks and being able to accurately predict what the win rate would have been. The chart below shows the algorithm being tested against the past 12 days of data on a few pairs. The yellow line represents where the algorithm is calculating support (a price to bid at), and the black stars represent points at which the algorithm would have been buying.

Notice that when the price dropped quickly that the algorithm did not buy. That is because when the markets get moving quickly, it shuts off and searches elsewhere. It is only interested in trading boring markets it can control.

Great, so the system has a win-rate of 80%+.

That certainly sounds good but it’s only half the work. Now it’s time to figure out how to handle the 20% of losses. There’s no point winning 80% of the time when the losses are unpredictable and can wipe you out. This will take time to figure out, but the first step is in finding the weak point in the system.

1. Slow grinds. This algo is immune to fast drops in prices because it will quickly retreat when the standard deviation rises, but what about slow grinds lower?

When a stock is experiencing a slow grind lower, one thing is certain; it’s current price is always going to be in the low end of the trading range. That means if you subtract the current price by the lowest point in the timeframe, then divide that by the highest point minus the lowest point ((Current price-low)/(high-low)), you’re left with the point at which the current price is in relation to the high and low on a scale of 1-10. Take that number and roll it back against any given timeframe and you should see a series of numbers that are scattered. That is, unless the price is slowly grinding lower. In that scenario you would be constantly hitting a number between 0-2, since the stock is constantly hitting new lows and not bouncing back. In that case, let’s disable the bot then too.

Take a look at that. It ignored the slow grind lower despite it being right in its buying range and producing a low standard deviation. The buy only triggered after it saw a spike, indicating that there are indeed some buyers and it is safe to assume it will spike again.

2. What happens if the algorithm takes a position, then the price after a position is taken?

This happens more often than you would imagine, and is in fact also the algorithm’s weak point. Speaking of which, it happened quite recently:

But before we get to a solution, there are a few things that need to be considered:

  1. Assuming that the two coins are properly correlated, a bounce should be imminent and we should not be too hasty to sell
  2. But what if the price drops another 10% just as quickly, so perhaps it would be better to sell out fast and cut losses?
  3. But then, what if we set that stop loss too tightly and we get stopped out of what would have been winners?

Those are all ‘if’ questions that you cannot reasonably expect to know. However, what you can do is position for the highest probability outcome. Here’s what we know:

  • Volatility precipitates a large drop, which would trigger a high standard deviation
  • If it is not unusually volatile, it would suggest that neither side is in a rush to buy or sell and should eventually balance out – a bounce happening at some point is a high probability bet.
  • Patience is not an issue for algorithms.