Steversteves

Arbitrage, Co-Integration & Pairs

Education
NASDAQ:NVDA   NVIDIA
If you are interested in quantitative trading (“quant” trading) or even if you’re only interested in investing, chances are you may have heard of arbitrage, co-integration and pairs trading. But chances are equally likely you have no idea what these truly mean and how to assess/measure and exploit these different concepts in your trading life and investment life. And it makes sense, these are really complex concepts and generally utilized by quantitative traders in institutional trading. But, why let them have all the fun. So I’m going to try to simplify these concepts and give you a working idea of:

  • a) What these things mean;
  • b) Why they are imported for traders and investors; and
  • c) How to start applying these concepts to your trading and/or investment portfolio.


First, let’s get started with the pesky definition details.

Arbitrage: Isn’t that a big bird?!

In finance, arbitrage generally refers to any short to mid term trading strategies that revolve around mean reversion models and the exploitation of pricing inefficiencies. Despite the fact that the “Efficient market Hypothesis” postulates that markets are efficient and thus unpredictable, I am going to show you that they are in fact very inefficient and all it takes is a little math to reveal these inefficiencies.

And Co-Integration?

Co-integration is kind of like co-habitation, perhaps more copulation. If two tickers were going to have a child together, it would be a co-integration. Essentially, co-integration refers to what a ticker would look like while integrated into another ticker. The official explanation of cointegration is essentially when two or more nonstationary time series move together in such a way that their linear combination results in a stationary time series. Now this is the involves modelling one ticker after another, so that you can essentially predict one ticker’s price or behaviour, from another.

It may help to show you some examples of co-integration via charts. So let’s take a look at some examples below, using the SPTS indicator to perform linear regression assessments:


In the chart above, I have placed a co-integration model of GOOG and MSFT through linear regression. Essentially, we use GOOG’s close price to calculate the respective fair value of MSFT. From this, we can see when MSFT is over-valued from GOOG and vice versa, and we can see when the two are fully integrated (a regression to the mean).

Now cointegration isn’t a natural order. Not everything can co-integrate. Sometimes, there just isn’t a significant relationship. For this, we must apply some statistical tests. There are many that can be used, such as the Engle Granger Test, but the most popular is the Dickey Fuller test or Augmented Dickey Fuller test. We will talk more about this later.

Pairs Trading

Pairs trading refers to trading both arbitrage and co-integration. Remember our GOOG/MSFT example above? Well, pairs trading involves assessing, modelling and trading this co-integrated relationship. Let me explain through charts:


In the chart above we can see both MSFT and GOOG are in an uptrend. However, MSFT is lagging below GOOG and GOOG is holding slightly above MSFT. Now, because they are both in an uptrend, you don’t want to short, but if you wanted to maximize your profits, you are going to long the stock that is under-performing its benchmark or “cointegrated” pair, i.e. GOOG.

We can see this translated into about a 6.18% gain on MSFT vs only a 4.75% gain had you longed GOOG.

Now, traditional pairs trading generally involves finding two stocks that are highly correlated but have extremely diverged, shorting the one that is overvalued and longing the one that is undervalued. But I can tell you, that is almost IMPOSSIBLE to find in the real world! So I settle for this as a pseudo-pairs trading fill in!

Here is an intra-day example between SPY and QQQ from Tuesday October 31st:


Do you notice anything in this chart?

During the morning sell down, SPY lagged QQQ and QQQ sold more starkly. SPY held higher above QQQ, which we can see from our co-integration model. So what would have made more sense, to long SPY or QQQ for the bounce?

If you said QQQ, you are correct:


From its lowest to highest point, SPY bounced 0.57%.
From its lowest to highest point, QQQ bounced 0.85%.

And we were able to see that QQQ had more upside room than SPY by looking at the co-integration model overlaid on the chart.

Now do you remember when I talked about arbitrage and how I said the market isn’t so efficient when you pull in these pesky math equations? This is why. There is arbitrage everywhere, everyday, on every stock. These small inconsistencies in pricing that can be exploited for profit. Now, as retail traders, we likely don’t have the computing and modelling power/setup necessary to scan millions of tickers, creating multiple co-integration models to look for these pricing anomalies and capitalize on them, but we can absolutely have our own co-integration model built on stocks that we trade frequently, or that we like to invest in.

But before we get into the how, let’s look at some different examples of co-integration and mean reversion. First, let’s look at an example of a “perfect integration” i.e. the same stock with SPY and SPX:


You can see that the blue lines align perfectly with the candles. But watch what happens when we overlay SPY and ES1! (the futures version):


Do you notice something.. strange?

Despite ES1! And SPY being pretty identical, we can notice a few “drifts” or inconsistencies in the pricing. This is likely attributable to the extended trading time of ES1! Vs SPY which is limited in its trading times, but it does highlight how these inconsistencies can “pop up” on tickers/futures that are trading almost identical underlying assets (in this case, the S&P 500).

What about investments? How is this useful for investments?

This is actually a really great question and.. well friends… I’m going to show you how your investments can thrive with a little bit of quantitative love!

Let’s use a very straight forward example, SPY and QQQ. They are pretty similar, tend to follow each other. Over the past 75 days, SPY and QQQ have a Pearson Correlation of 0.94. That is a pretty substantial relationship!

If we look at what SPY looks like overlaid with QQQ, here we are:


In the chart above, you can see there are areas where SPY outperforms QQQ and QQQ outperforms SPY. If we were to export SPY and QQQ trading data since July 2020 into Excel and create a investment model based on the modelled relationship between SPY and QQQ, it would look something like this:


Column B has our SPY close Price, C our QQQ close price, E through G are how we calculate the Dickey Fuller statistic, H and I are our SPY and QQQ Returns respectively, and J is our investment of $100.

Now, if we create a SPY trading algorithm off this model and tell it to long and hold SPY as long as it is undervalued in comparison to QQQ, sell when its over-valued and re-buy when its undervalued, assuming an initial investment of 100$, between July 22nd, 2020 and October 31st, 2023, you would have made exactly $36.40 or 36%. Had you just held SPY for that time and not sold when SPY became over-valued against QQQ, you would have made $27.95, or around 28%. The difference seems marginal maybe, but it is quite a stark percent difference.

And if you change that 100 to, say, 10,000, the difference starts to add up.

So … How can do you do it?

So now for the technical stuff. I am going to try to keep this as easy and straight forward as possible.

In a nutshell, the steps involved in developing a co-integration model include the following:

  • a) Finding a rational basis for the integration: It is insufficient to think that one stock should relate to the other. You need a rationale basis as to why these stocks are similar, which can later be confirmed by some tests.
  • b) Reference a correlation table or matrix to compare the stocks of interest and identify the highest correlated stock pairs.
  • c) Once you have identified the highest correlated stock pairs, you can use an indicator such as SPTS to create a regression model, or you can use Excel to create a regression model (for simplicity, I am going to show you how to do it on the SPTS indicator).
  • d) Create your co-integration model in Excel by exporting your two stocks of interest and plotting in your linear regression formula.
  • e) Perform the Dickey Fuller test to ensure stationarity.
  • f) You’re done!

Now, let’s walk through the steps, using NVDA and SMH as our two basis.

Determine rationality:

NVDA is a subsector of chips, which is tracked by SMH.

Check Correlation:


We can see on this correlation matrix, NVDA has a 0.82 correlation to SMH. It is the strongest, even stronger than QQQ which has a 0.74 correlation. In general, you want a correlation >= 0.8. So SMH has passed the first and second step!

Create a Regression Model

For simplicity, let’s use SPTS.


Launching SPTS, it is going to ask us to set a start and end time. I am going to start from the beginning of 2018 till current (approx. 5 years). Then, in the settings, I am going to select “Linear Regression”


SPTS is going to output a linear regression model (green arrow):


We are going to take a look at the Significance or P value, which is 0.9, even better than our correlation matrix result! And our R squared which is 0.8. This is an excellent R2 reading! We generally will accept R2 of 0.5 or greater, so 0.8 is perfect! We can also see our Error is 138.59, which means the variance present between NVDA and SMH is around $138.59 in both directions.

The meat of our model is the equation y = 2.7994x + -114.15. We need this formula for the next step.

Export the data into Excel

We are going to click the dropdown menu in the top right hand corner by our chart title and click “Export chart data”:


We are going to do this for both our tickers.

Once we have the data, we are going to just leave the Time and the Close price for NVDA and copy over the close price for SMH so that our Excel file looks like this:


Now, we are going to create a column in D called “Co-Integration”. In this column, we are going to use our model equation that was generated by SPTS like so:


Remember, we are converting SMH to NVDA, so we take the SMH close price and substitute for X in the equation.

Once that is done, we can drag and drop the data to calculate the expected close for NVDA:


If we want to use Excel to calculate the equation, we can select an empty cell and use the formula =SLOPE(known ys (NVDA), Known_xs (SMH)):


Then, in another column, we use =INTERCEPT(known_ys (NVDA), known_xs (SMH)):


Perform Dickey Fuller Test

So now we have our data, we have to ensure there is stationarity. Stationarity refers to the principle that the statistical processes of a stock will remain the same. Stationarity is required for any time series analysis, without stationarity, time series would not work in the way we need it to for this.

Now don’t get confused by the term. Stationarity doesn’t mean that the stock price can’t change, just that its statistical attributes will remain the same. For example, say NVDA and SMH both gain 4% in one day, then following day they lose 2%. Now, say in two months, both gain 4.2% and lose 2.2% the next day. This is a simplistic example of stationarity. The statistical processes that are driving these two tickers remain constant, despite the price increase or decrease.

So to perform a stationarity analysis, we need to first do what is called “Differencing”. All this means, is we need to subtract NVDA from our Co-integration formula like so:


Then we drag down. (Notice we assigned this column X. This is for simplicity).


One thing you will notice is the value of X is negative. This implies that NVDA is actually OVERVALUED in comparison to SMH. We are going to be seeing a lot of negatives ;).

Now that we have created our X column, we can go ahead and calculate what is called delta x or x change. This is the difference between the second and first x value, calculated as such:


We simply subtracted the proceeding value from the previous value. Once we put in the formula once we can just drag and drop it:


Now the last thing we need to do is created a lagged value of X. This is because the Dickey Fuller test requires lagged values to assess stationarity. So we simply carry down the previous X like so:


Then… what do you think?..... we drag and drop :p.


So now we are ready to calculate the Dickey Fuller result. And this is actually really easy! All we do is use the formula =LINEST(known_ys, known_xs). Our known Y’s are going to be the delta x and our known x’s are going to be the lagged x.

But before we use this function, we are going to highlight 2 rows like so:


The top called coefficient and the bottom error. This will give us the regression coefficient (the same thing we multiplied our SMH value by), as well as the standard error. Once we have our two boxes highlighted, we will put in our command like so:


NOW BEFORE YOU PRESS ENTER!

You need to force the LINEST function to only print the two values we want, so to do this, after we put in our known Ys and Known Xs, we are going to use the comma “,” and put 0 then comma “,” and put “1”. This is going to tell Excel we want a negative coefficient (for the DF test) and to print our standard Error:


Then, we are going to press ctrl + shift + enter. Or command + shift + enter on a mac. This is going to force Excel to only print the two variables of interesting:


And there are our results! Now, to calculate the DF test, all we do is divide the coefficient by the error like so:


And here is the result:


So what does it mean? Well, there are 3 critical values on Dickey Fuller

T Critical at 10% confidence = -1.75
T Critical at 5% = - 1.616
T Critical at 1% = -2.567

To be significant, the t-statistic needs to be below a critical value.

As a vague rule of thumb, in general, the more negative a value is, the more confident we are to reject the null hypothesis, that the data is NOT stationary. Here, we fail to have a very negative value and we fail to come in lower than any of the critical values. As such, we have to accept the null hypothesis and the fact that this data is non-stationary.

If we take, by comparison, the t-statistic of SPY and QQQ since 2020, it is a value of -2.048 within a normal distribution. Thus, we can say that the data is stationary up until a confidence level of 5%. However it is not significant within a 1% confidence level.

A side note on distribution:
While the distribution type does not technically affect the DF results, we should pay attention to whether we are operating with a normal distribution or an abnormal distribution.

So we need to check the distribution, which we can do with SPTS:


So should we accept NVDA and SMH as stationary?

No. Unfortunately, it is not a stationary dataset and we cannot use SMH to determine the fair market value of NVDA. I wanted to use this example to show you that stationarity is not a rule and it can be a challenge ascertain it in your models. But if you are big into the indices, generally you will find stationarity if you are looking between 3 to 4 years back max.

To recap, stationarity depends on


  • a) The distribution type (a normal distribution should be ABOVE the critical values and a non-normal distribution should be below the critical values).
  • b) In both cases, the more negative a value is, the stronger we can reject the null hypothesis. For example, if we returned a value of -4 on a normal distribution, this would indicate strong evidence of stationarity and would create an extremely reliable model.
  • c) If the data distribution is non-normal, we need the value to fall below the critical values. So, for -1.75, we would need the value to be less that -1.75 for 10% confidence, less than -1.61 for 5%, etc.

That said, I went ahead and applied the algo despite nonstationary data, to see how it would have faired using SMH as its anchor point. The initial investment was $200 at the start of 2018 and it would only buy NVDA when the value of NVDA fell below the FMV based on SMH. The result?

Our $200 would be $734.85 as of today, assuming we bought when NVDA was below FMV and sold when it crossover the FMV according to SMH, for a total return of around 267%.

What about if we bought NVDA and just held it from 2018 till now?

Our investment would be up to $1636.52 for a total of around 718%.

This is why stationarity matters, because it will affect how our investment does! You see it worked well on SPY and QQQ because there was stationarity to ensure consistency, but NVDA does not have it, too erratic.

So what about if you have stationarity?

If you have stationarity, then good! The principle is, you want to long the stock when your X value is positive (indicating it is undervalued by comparison to your co-integrated pair) and get out and/or short the opposite stock when your x value is negative.

As an investment strategy, I generally recommend not shorting, but just getting out when the x value turns negative (i.e. is over-valued), or simply setting a stop-loss to maintain the bulk of your gains and letting it do what it do.

You can also apply this on the shorter timeframes, like the 1 hour or 2 hour or 4 hour, like the examples I showed above.

Concluding remarks

So that concludes my very lengthy post. I really hope you learned something from this, took something away. These are really complex topics and there are not a lot of good resources out there on how to do them properly. Even just finding resources on the Dickey Fuller test, which is predominately only used in economics, was difficult. So I wanted to provide some information on these more complex strategies and principles that I think most traders and investors should have some idea about.

Hope you enjoyed and safe trades to all!

Thanks for reading!





Premium indicators and content have launched! Get access at: www.patreon.com/steversteves
Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.