When it comes to creating trading bots and algorithmic trading in general, there are countless approaches that one can. Implementing multiple of them is not only an interesting exercise to gain experience in the field, but is also very valuable due to the diversification that comes as a consequence of having multiple uncorrelated algorithms trading simultaneously.
In this article, I’ll try to provide a brief but thorough explanation of the different categories of trading bots that are nowadays being used both by large institutions and retail traders.
If you’re not interested in reading the entire article, these are the different examples of trading bots that can be used:
Common trading bots (trading algorithms used) normally fall within the categories of Mean-Reversion, Momentum, Machine Learning modeling, Sentiment-Based trading, Market Making Algorithms, and arbitrage trading (either pure or statistical arbitrage).
Mean Reversion Strategies
Mean reverting strategies try to profit from sudden and big price changes and their tendency to revert to their “original” price, or at least in that direction.
These strategies are used not only for buying but also for selling an asset, since most mean-reverting algorithms are instructed to go short after substantial price increases, and long after declines.
A simple mean reversion strategy can be created by means of two moving averages:
- Fast Moving Average: average of the most recent closing prices. A popular value for the fast-moving average is 30 (bars).
- Slow Moving Average: a simple average that goes back in time further than the fast-moving one. A typical value is 90 (bars). Thus, it reacts slower to current changes in price.
A mean-reverting algorithm will go long whenever the fast-moving average crosses the slow moving average from above, as a result of a strong price decline.
Conversely, it will sell the asset when the fast moving average crosses the slow moving average from below due to a strong positive price change.
As expected, such a simple algorithm with an arbitrary set of parameters (30 bars, 90 bars) will most probably perform rather poorly if implemented.
Having said that, the academic literature suggests that there is significant evidence of mean-reversion patterns in the prices of stocks. You can refer to this paper in order to review a robust analysis of mean-reversion.
Mean-reversion can be mathematically modeled with an Ornstein-Uhlenbeck process. In a nutshell, this process has three main components: the rate of mean-reversion, the long-term mean, and the average volatility of the process (Brownian motion). The details of the Ornstein-Uhlenbeck are definitely out of the scope of this article, but you can further read about it in this excellent article.
Statistically, it is possible to test for either mean-reversion or momentum of a series of prices using the Durbin-Watson test. I provide a brief explanation of this test in the following section.
Momentum strategies consist of a set of rules that aim to exploit the tendency of asset prices to continue changing in a given direction. There are copious amounts of academic papers rigorously analyzing how price increases tend to be followed, on average, by further subsequent price increases, and vice-versa.
At first, the hypothesis behind momentum trading seems to contradict the thesis of mean-reversion algorithms. Different assets tend to have different behaviors, and market regime changes also determine the overall preponderance of one over another, it is not contradictory to use both techniques simultaneously.
It is not uncommon to have two algorithms trading at the same time, one exploiting mean-reverting patterns and the other one momentum ones. Previous research might show that a given asset features short-term mean-reversion, but momentum over a longer time horizon. Thus, using an intraday mean-reverting strategy together with a momentum interday strategy is plausible.
Momentum is defined as “positive autocorrelation” in statistics. Informally, this means that an estimation error with a given sign (positive or negative) tends to be followed by an estimation error of the same sign.
There are multiple ways to quantify the autocorrelation of a series of historical prices (or any other time series), the most common being the Durbin-Watson test. This formula of this test results in a single value that ranges from 0 to 4, which indicate the following:
- Values close to 0 indicate strong positive autocorrelation (momentum)
- Values close to 2 indicate the absence of autocorrelation
- Values close to 4 indicate strong negative autocorrelation (mean-reversion)
Practitioners that use technical indicators tend to use the following ones to exploit momentum patterns:
- Relative Strength Index (RSI): this indicator aims at providing traders with a simple way to assess both upward (bullish) and downward (bearish) momentum, indicating whether it is overbought or oversold.
- Moving Average Crossover Divergence: a trend-following indicator that shows the momentum of an asset by means of two moving averages. These moving averages are usually simple moving averages (SMA’s) or exponential moving averages (EMA’s).
- Average Directional Index (ADX): an indicator whose purpose is to estimate the strength of momentum, resulting in a value between 0 (no trend) and 100 (very strong trend).
Machine Learning Strategies
Machine Learning (ML) techniques have become increasingly popular for trading in recent years. As a result, a handful of very profitable proprietary firms have emerged and entire research divisions within established hedge funds have been created.
The most obvious application of machine learning for trading purposes estimating (forecasting) the future price of an asset. By using hundreds of different input variables, such as prices, indicators, alternative datasets, and custom-made signals, machine learning models can oftentimes be significantly better than random at predicting price movements.
There are also more subtle approaches in which machine learning models are used for trading purposes. Instead of just trying to predict the price on t+1, ML models are commonly used for creating signals and variables used as inputs for trading. A model that can accurately predict the weather for the coming season will in turn be useful for predicting the season’s output of a given commodity, like soy, corn, or sunflower.
Being able to predict the output of a harvest with relative precision has very useful applications for trading, which is why hedge funds pay costly fees to have access to those kinds of forecasts.
It is worth mentioning that, because of how easy it is to quickly create and deploy a trading algorithm based on machine learning techniques, cautionary examples of important fails have also been reported.
Additionally, machine learning models are only as good as the inputs provided, and poor datasets will lead to poor (and costly) results. Computer scientists condense this valuable consideration with the popular phrase “Garbage in, garbage out”.
What follows is a non-exhaustive list of machine learning models and applications in the trading industry:
- Optimal Order Execution: when placing large orders, institutions have to break them down into smaller trades in order to not have an adverse impact on market prices. Neural Networks, Decision Trees, SARIMAX, and LSTM models are commonly used. These models have to at least outperform other deterministic models popularly employed for these purposes, such as Volume-Weighted Average Price (VWAP) and Time-Weighted Average Price (TWAP) algorithms.
- Price prediction: in order to predict the future price of an asset, Neural Networks, LSTM, XGBoost, Deep Forest, and countless other models are used. Assuming that they are uncorrelated to each other, they are oftentimes used together in order to create stronger and more reliable signals. This practice is referred to as “Ensemble Modelling”.
- Sentiment Analysis: natural language processing techniques are used to estimate how bullish or bearish the current sentiment towards a specific asset or the market in general is. Common sources of information are media outlets, Reddit and Twitter. Due to hardware requirements, practitioners usually use pre-trained models to analyze their data. Popular choices are BERT, RoBERTa, and GPT-3.
- Image Vision: this category of models is used extensively in the financial industry. Satellite images of parking lots are used to estimate the profitability of retail stores, and tracking truck movements in a factory is a good predictor of the production rate.
Unlike many other fields in which machine learning methods are applied, asset prices have an extremely high noise-to-signal ratio. Thus, inexperienced professionals have a tendency to overfit their data and create models that are excellent at explaining the noise in the data used for training, but very poor at finding meaningful patterns that will continue to show up in the future.
You can find lots of interesting linear spurious correlations between clearly unrelated variables in the following link. My favorite spurious correlation is between the number of films featuring Nicholas Cage and the number of pool drownings in any given year.
Arbitrage strategies try to exploit price discrepancies and inefficiencies in the market by both buying an undervalued asset and selling the overvalued one.
Arbitrage strategies come in two flavors: pure arbitrage strategies and statistical arbitrage strategies.
Pure Arbitrage Strategies (High-Frequency Trading)
In the most strict definition, pure arbitrage strategies are risk-free and perform the buying and selling operations simultaneously. These types of arbitrages are in the realm of what is known as high-frequency trading. For example, Deutsche Bank is traded both on the Frankfurt Stock Exchange and on the New York Stock Exchange. Due to the fact that each exchange has its own order book, the same asset can be quoted at different prices during extremely small periods.
This type of trading strategy requires custom-made hardware to be co-located next to both exchanges in order to be feasible, leading to very high operational costs (read my article on server colocation here). Because of this, pure arbitrage strategies are out of reach for most institutional traders, and definitely impossible for all retail traders.
Statistical Arbitrage Strategies
Statistical arbitrage strategies try to exploit short-term price differences between two different assets that commonly tend to have high correlations due to being in the same industry.
By means of different statistical techniques, these algorithms buy the asset that is perceived to be undervalued and sell the one that is thought to be overvalued relative to each other.
Unlike pure arbitrage strategies, statistical-based ones are by no means risk-free. They are based on the assumption that previous correlation is a predictor of future correlation, and that price discrepancies between two assets will be offset because of this.
Strategies Based on News and Sentiment Analysis
These types of strategies follow specific sources of information in order to by means of Natural Language Processing (NLP) models, infer the probability of a price going either up or down.
NLP algorithms are a special category of machine learning, where models are trained with huge amounts of natural language data in order to extract specific information. In the case of stocks and cryptocurrencies, the most obvious application of these models is to infer the future price change of an asset. In other words, it estimates whether the most recent sample of analyzed text excerpts is bullish or bearish.
Take Elon Musk’s Twitter account as an example: for a period of several weeks during 2021, it was possible to place profitable trades just by scraping the most recent tweet, inferring which cryptocurrency was mentioned and whether the overall tone was negative or positive. Just by reacting fast enough to his tweets.
As you might have guessed, the tweet from above not only has a positive sentiment toward Bitcoin but was also very profitable for the owners of the algorithms that quickly reacted to it.
These are the most common sources that both retail and institutional investors use to forecast price changes:
- Amazon Reviews
- Google Trends
- News Websites
Broadly speaking, market-making algorithms are strategies that profit from providing liquidity to the market, whilst avoiding accumulating unnecessary inventory. They commonly quote both buy and sell orders simultaneously with the objective of profiting from the difference between both pricing, which is referred to as the spread.
Before the popularization of algorithmic trading, most stock exchanges often appointed an institution to act as official market makers in order to provide liquidity. Such was the case of the New York Stock Exchange (NYSE), but nowadays there are multiple proprietary firms and hedge funds that colocate their hardware next to the stock exchange servers in order to also act as market makers.
Market-making algorithms benefit from volatile prices with little to no trend. In other words, these algorithms are especially profitable in markets that feature short-term-mean reversion properties.
In order to not accumulate an unwanted net position on a given asset, these algorithms also try to forecast the short-term price changes in the order book. These oftentimes are called order book imbalance algorithms, as they check the difference in the size of the best bid and ask and divide it by its sum.
A negative imbalance means that the order book has a more “robust” ask (sellers) than bid (buyers), meaning that it is more probable for the price to decrease.
By most definitions, these types of algorithms fall under the realm of high-frequency trading, making it almost impossible for retail traders to deploy these types of strategies. These algorithms are not only sophisticated but due to the first-in-first-out rule of most exchanges, having low latency is also important.
In the most basic implementation, rebalancing algorithms periodically adjust the current holdings of a portfolio of assets to their desired or objective proportion, referred to as ‘weights’. These adjustments can be triggered on a periodical basis, like for example on the first Monday of each month. Additionally, rebalancing can be caused by an undesired deviation of an asset from its objective weight.
The differences with respect to the objective weights arise due to the changes in the prices of the assets under management. Assets subject to price increases will have a weight above their target, whereas those that underperformed will be under-represented. In order to readjust the weights of the portfolio, the algorithm will sell those assets that overperformed, and vice-versa.
Take a look at the following example, where we start with 4 equally weighted assets on a given January. Due to the price increase of Asset 1 and Asset 2, we sell a fraction of those assets at the beginning of February. Conversely, we buy some amount of Asset 3 since its price declined during January.
It’s worth mentioning that, although Asset 4 did remain constant, we also have to buy 125 units of it. This is due to the fact that the overall value of our portfolio increased during January, leading to the same value of Asset 4 to represent a smaller fraction of the portfolio.
As you can also see in the above example, we bought and sold fractional units of the Assets. This is not always possible, since some brokers do not allow for fractional trading. This leads to having to hold assets in proportions that are suboptimal when compared to our theoretical model.
rebalancing should not be performed too frequently, due to the transactional costs of doing so (broker fees and slippage).
As you’ve seen, there are countless different approaches when it comes to implementing trading both, and by no means are they mutually exclusive. Obviously, there is no single category that is more profitable than the others, since this would not only go against common sense but also the “No Free Lunch Theorem”.
Whether a trading bot is profitable or not depends mostly on the quality of the data used, the sophistication of the implementation, and the overall insights and market knowledge of the trader behind the computer.