By Vibhu Singh

Almost every trading strategy in the market tries to exploit two prevailing phenomena: momentum and mean reversion. In this article, we will talk about the mean reversion and mean reversion strategies for trading.

This article covers the following topics:

- What is Mean Reversion?
- Mean Reversion Strategies
- Principle of Pairs Trading
- Correlation vs Cointegration
- Selection of Pairs
- Pairs Trading in Python
- Further Improvements

## What is Mean Reversion?

In finance, mean reversion theory states that security prices and economic indicators such as interest rates will tend to revert to the historical mean prices.

For example, many investors had bought Infosys stock when the price crashed after the resignation of CEO, Mr. Vishal Sikka because they expect Infosys stock to trade higher in the future when the management issues are solved.

The mean reversion principles can be applied to fundamental factors such as buying a stock with low PE and expecting the PE to rise to historical average PE or industry PE.

A value investor commonly follows this approach to buy the stock for long-term investments. Similarly, these principles can be applied using technical indicators to create a short-term mean reversion trading strategies.

## Mean Reversion Strategies

Statistical arbitrage strategies use mean reversion principle to take advantage of the price inefficiencies between a group of securities. It is one of the popular types of trading strategies in quantitative trading. There are various types of statistical arbitrage trading strategies:

### Directional Trading

In directional trading, each instrument’s signal is independent of the other’s instruments.

For example, over the past one month, the price of crude oil has increased by $2. One day, without any significant news or factor, crude oil price changes by $20. By the mean reversion principle, you can expect the price of crude oil to fall in the coming days such that the average change in the price of crude oil remains the same and you can exploit this opportunity.

### Cointegrated Portfolio Trading

In this type of trading strategies trading signals depend on the two or more cointegrated instruments. Pairs trading is one of the most famous examples of such a type of trading style.

Since we are using two cointegrated instruments to make a trade, therefore the name pairs trading. But it’s not always a pair, it could also be triplets or could be more than that. If you found five cointegrated stocks, you can make a portfolio of that and do the trading.

This article will focus on the concepts of pairs trading and a trading strategy based on that.

## Principle of Pairs Trading

Let’s say you have a pair of instruments with similar fundamentals, belonging to the same sectors and similar economic links. For example, stocks like Google and Microsoft or Facebook and Twitter.

Since they have similar fundamentals, you expect both the stocks to behave similarly. You also expect the ratio or spread of such stocks to remain constant with time. However, due to a temporary change in the demand and supply and other factors, there might be a divergence in the spread between the pairs.

In such scenarios, one security outperforms the other. According to the mean reversion principle, you expect this divergence to revert to normal with time. In such scenarios, when there is a temporal divergence, you can perform the pairs trade. That’s buying the underperforming security and selling the outperforming security.

## Correlation vs Cointegration

Most of the people are confused between correlation and cointegration, and they often think the same. But that’s not the case.

When two price series move in the same or opposite direction, then there is some correlation between the price series. If one price series moves in either up or down direction and other price series also move in the same direction, there is a positive correlation between them.

While one price series moves in the upward or downward direction, the other moves opposite to that, then both series are negatively correlated.

Cointegration is a statistical property of two or more price series that indicates if a linear combination of the series is stationary, then both series are cointegrated with each other.

For example, if the linear combination of two stocks is stationary, both stocks are cointegrated with each other. A price series is said to be stationary if it’s mean and variance is constant over time.

**Statistical test for cointegration: **Augmented dickey fuller or ADF test is one of the statistical tests for cointegration. In Python, this can be easily done through the statsmodels library.

As explained in the principle of pairs trading, the ratio or spread between stocks must converge to the mean over time for pairs trading to work. That is, both stocks must be cointegrated with each other.

Just looking at the correlation between the stocks might give you spurious results because the prices of the two stocks may keep on increasing without ever mean-reverting. There is a myth that goes around that the two correlated instruments must be cointegrated and vice versa.

Let’s define two price series such that they are correlated but not cointegrated.

*By looking at both price series, they seem correlated with each other but what about cointegration?*

For two price series to be cointegrated, the spread/ratio between two price series should be stationary. Let’s take the difference between the above series.

The spread is trending. It is clear from the results that both price series are correlated but not cointegrated.** So we can conclude that the prices can be correlated but not cointegrated.**

Now consider another scenario where prices are cointegrated but not correlated.

It seems evident that both price series are not correlated, as there seems no relation between them.

*But what about cointegration?*

The spread hovers between -10 to +10. So, we can say that the two price series can be cointegrated without being correlated.

Before implementing pairs trading, we need to check whether the price series are cointegrated or not so that their spread/ratio reverts to the mean.

## Selection of Pairs

### How to select stock pairs?

Suppose you have a large universe of stocks. The first step is to segregate stocks on the basis of market capitalization, sector, daily traded volume etc. After segregating, you can check for a correlation between the securities in each group.

The correlation helps to filter the number of pairs to a more manageable set. Once you get the securities in a small set of groups, you can check for cointegrated pairs within the group and select the cointegrated pairs.

### How to select forex pairs?

The basic idea behind selecting pairs in forex is similar to that of stocks. We need to find countries that have similar economic fundamentals.

Some of the suitable candidates are

- EUR/USD and CHF/USD,
- AUD/USD and CAD/USD or
- USD/KRW (US dollar/South Korean won) and USD/HKD (US dollar/Hong Kong dollar).

These pairs belong to the same economic zone as Euro and Swiss Franc belong to the Eurozone. Doing pairs trading in the currency market has its advantage, such as liquidity in currencies is higher, thus lowering transaction costs.

### How to select pairs in the futures market?

In the futures market, there are not many good pairs, even with similar economic exposure. This may be due to differences in demand and supply. Therefore, in futures, you can’t merely rely on economic exposure for choosing pairs.

## Pairs Trading in Python

Steps to implement pairs trading in Python

- Select forex pairs
- Calculate ratio and check for cointegration
- Create feature to generate trading signals
- Define entry and exit point
- Calculate pnl

### Step 1 - Select forex pairs

As discussed above, we select pairs having similar economic fundamentals. Euro and Swiss Franc both belong to a similar economic zone, so we start with this pair, but we can’t just only rely on the fundamental factors.

### Step 2 - Calculate ratio and check for cointegration

We calculate the ratio between the currency pairs. If the ratio is stationary, then we can say that the currency pairs are cointegrated. We are using an ADF test to check whether the ratio is stationary or not. One thing to remember while using the ADF test is that the test result changes by changing the order of ratio.

The p-value from the ADF test for the EURUSD/CHFUSD ratio is less than the 0.05 significance level. Therefore we can say that the ratio is stationary. Similarly, the p-value for the ratio of CHFUSD/EURUSD is also less than 0.05 significance level. Again, this ratio is stationary.

We will use the EURUSD/CHFUSD ratio for pairs trading. Let’s take a look at the cointegrated ratio to make sure this makes sense.

### Step 3 - Create a feature to generate trading signals

The absolute ratio isn’t very useful in statistical terms. It can be observed through the above ratio graph that it does not look like it moves around a stable mean. We need to normalise the ratio. This is done using z-score.

Z score is defined as:

**Z Score = (Value — Mean) / Standard**

Now it’s easier to observe that the ratio moves around the mean, but sometimes it divergences from the mean, which we can take advantage of.

### Step 4 - Define entry and exit points

If the z score crosses below the lower threshold, then we buy and exit the position when it reaches the mean. If the z score crosses above the upper threshold, then we sell and exit the position when it reaches the mean.

### Step 5 - Calculate PnL

## Further Improvements

The strategy can be further optimised using different values of the lookback period of moving average and standard deviation.

### Features to generate trading signals

The feature that we used to define the entry and exit position is z-score. You can use a different variant of z-score such as:

z score: (15-day moving average — 50-day moving average) / 50-day standard deviation

Another approach is to use the Bollinger Band for signal generation.

### Stop loss

You can set the stop loss above and below your threshold level.

For example, in the above strategy, the set threshold was plus/minus 2 standard deviations. You can set the stop loss at plus/minus 3 standard deviations. When the ratio/spread crosses that threshold, you can exit the position. Another approach is to exit the position when a prefixed loss is hit.

### Holding Period

You can keep the position for a day, week or month and exit after that. How long you can keep the position can be found using a concept known as the half-life. It tells how long it would take the time series to revert to the mean. It gives an idea of the expected holding period for a particular trade.

When you exit the position based on time, you will wait for the price to revert to mean to initiate new positions.

Check out this webinar to learn about mean reversion strategies.

### Conclusion

Mean reversion strategy will only work on a range-bound or stationary stock price series. The strategy will not work on a stock price series that trends higher or trends lower for a long time.

To check if a price series is stationary or not a statistical technique called an augmented Dickey-Fuller (ADF) test is the most commonly used.

Unfortunately, most stock price series are not stationary. But fortunately, we can create a portfolio of stocks such that the market value of that portfolio is stationary and implement a profitable mean reversion trading strategy on that portfolio. This forms the basis on which pairs trading, **triplet trading**.

Learn Mean Reversion Strategies in detail in the Quantra course.

*Disclaimer: The views, opinions, and information provided within this guest post are those of the author alone and do not represent those of QuantInsti ^{®}. The accuracy, completeness, and validity of any statements made or the links shared within this article are not guaranteed. We accept no liability for any errors, omissions or representations. Any liability with regards to infringement of intellectual property rights remains with them.*