Algorithmic and Mechanical Forex Strategies | OneStepRemoved

  • Articles
  • Sophisticated Web Sites
  • Automated Trading
  • Testimonials
  • Contact

Cointegration in Forex Pairs Trading

April 23, 2014 by Eddie Flower 21 Comments

Cointegration in forex pairs trading is a valuable tool. For me, cointegration is the foundation for an excellent market-neutral mechanical trading strategy that allows me to profit in any economic environment. Whether a market is in an uptrend, downtrend or simply moving sideways, forex pairs trading allows me to harvest gains year-round.

A forex pairs trading strategy that utilizes cointegration is classified as a form of convergence trading based on statistical arbitrage and reversion to mean. This type of strategy was first popularized by a quantitative trading team at Morgan Stanley in the 1980s using stock pairs, although I and other traders have found it also works very well for forex pairs trading, too.

Forex pairs trading based on cointegration

Forex pairs trading based on cointegration is essentially a reversion-to-mean strategy. Stated simply, when two or more forex pairs are cointegrated, it means the price spread between the separate forex pairs tends to revert to its mean value consistently over time.

It’s important to understand that cointegration is not correlation. Correlation is a short-term relationship regarding co-movements of prices. Correlation means that individual prices move together. Although correlation is relied upon by some traders, by itself it’s an untrustworthy tool.

On the other hand, cointegration is a longer-term relationship with co-movements of prices, in which the prices move together yet within certain ranges or spreads, as if tethered together. I’ve found cointegration to be a very useful tool in forex pairs trading.

During my forex pairs trading, when the spread widens to a threshold value calculated by my mechanical trading algorithms, I “short” the spread between the pairs’ prices. In other words, I’m betting the spread will revert back toward zero due to their cointegration.

Basic forex pairs trading strategies are very simple, especially when using mechanical trading systems: I choose two different currency pairs which tend to move similarly. I buy the under-performing currency pair and sell the out-performing pair. When the spread between the two pairs converges, I close my position for a profit.

Forex pairs trading based on cointegration is a fairly market-neutral strategy. As an example, if a currency pair plummets, then the trade will probably result in a loss on the long side and an offsetting gain on the short side. So, unless all currencies and underlying instruments suddenly lose value, the net trade should be near zero in a worst-case scenario.

By the same token, pairs trading in many markets is a self-funding trading strategy, since the proceeds from short sales can sometimes be used to open the long position. Even without this benefit, cointegration-fueled forex pairs trading still works very well.

Understanding cointegration for forex pairs trading

Cointegration is helpful for me in forex pairs trading because it lets me program my mechanical trading system based on both short-term deviations from equilibrium prices as well as long-term price expectations, by which I mean corrections and returning to equilibrium.

To understand how cointegration-driven forex pairs trading works, it’s important to first define cointegration then describe how it functions in mechanical trading systems.

As I’ve said above, cointegration refers to the equilibrium relationship between sets of time series, such as prices of separate forex pairs that by themselves aren’t in equilibrium. Stated in mathematical jargon, cointegration is a technique for measuring the relationship between non-stationary variables in a time series.

If any two or more time series each have a root value equal to 1, but their linear combination is stationary, then they are said to be cointegrated.

As a simple example, consider the prices of a stock-market index and its related futures contract: Although the prices of each of these two instruments may wander randomly over brief periods of time, ultimately they will return to equilibrium, and their deviations will be stationary.

Here’s another illustration, stated in terms of the classic “random walk” example: Let’s say there are two individual drunks walking homeward after a night of carousing. Let’s further assume these two drunks don’t know each other, so there’s no predictable relationship between their individual pathways. Therefore, there is no cointegration between their movements.

In contrast, consider the idea that an individual drunk is wandering homeward while accompanied by his dog on a leash. In this case, there is a definite connection between the pathways of these two poor creatures.

Although each of the two is still on an individual pathway over a short period of time, and even though either one of the pair may randomly lead or lag the other at any given point in time, still, they will always be found close together. The distance between them is fairly predictable, thus the pair are said to be cointegrated.

Returning now to technical terms, if there are two non-stationary time series, such as a hypothetical set of currency pairs AB and XY, that become stationary when the difference between them is calculated, these pairs are called an integrated first-order series – also call an I(1) series.

Even though neither of these series stays at a constant value, if there is a linear combination of AB and XY that is stationary (described as I(0)), then AB and XY are cointegrated.

The above simple example consists of only two time series of hypothetical forex pairs. Yet, the concept of cointegration also applies to multiple time series, using higher integration orders… Think in terms of a wandering drunk accompanied by several dogs, each on a different-length leash.

In real-world economics, it’s easy to find examples showing cointegration of pairs: Income and spending, or harshness of criminal laws and size of prison population. In forex pairs trading, my focus is on capitalizing on the quantitative and predictable relationship between cointegrated pairs of currencies.

For example, let’s assume that I’m watching those two cointegrated hypothetical currency pairs, AB and XY, and the cointegrated relationship between them is AB – XY = Z, where Z equals a stationary series with a mean of zero, that is I(0).

This would seem to suggest a simple trading strategy: When AB – XY > V, and V is my threshold trigger price, then the forex pairs trading system would sell AB and buy XY, since the expectation would be for AB to decrease in price and XY to increase. Or, when AB – XY < -V, I would expect to buy AB and sell XY.

Avoid spurious regression in forex pairs trading

Yet, it’s not as simple as the above example would suggest. In practice, a mechanical trading system for forex pairs trading needs to calculate cointegration instead of just relying on the R-squared value between AB and XY.

That’s because ordinary regression analysis falls short when dealing with non-stationary variables. It causes so-called spurious regression, which suggests relationships between variables even when there aren’t any.

Suppose, for example, that I regress 2 separate “random walk” time series against each other. When I test to see if there’s a linear relationship, very often I will find high values for R-squared as well as low p-values. Still, there’s no relationship between these 2 random walks.

Formulas and testing for cointegration in forex pairs trading

The simplest test for cointegration is the Engle-Granger test, which works like this:

  • Verify that ABt  and XYt are both I(1)
  • Calculate the cointegration relationship [XYt = aABt + et] by using the least-squares method
  • Verify that the cointegration residuals et are stationary by using a unit-root test like the Augmented Dickey-Fuller (ADF) test

A detailed Granger equation:

ΔABt = α1(XYt-1 − βABt-1) +ut and ΔXYt = α2(XYt-1 − βABt-1) + vt

When XYt-1 − βABt-1 ~ I(0) describes the cointegration relationship.

XYt-1 − βABt-1 describes the extent of the disequilibrium away from the long-run, while αi is both the speed and direction at which the currency pair’s time series corrects itself from the disequilibrium.

When using the Engle-Granger method in forex pairs trading, the beta values of the regression are used to calculate the trade sizes for the pairs.

When using the Engle-Granger method in forex pairs trading, the beta values of the regression are used to calculate the trade sizes for the pairs.

Error correction for cointegration in forex pairs trading:

When using cointegration for forex pairs trading, it’s also helpful to account for how cointegrated variables adjust and return to the long-run equilibrium. So, for example, here are the two sample forex pairs’ time series shown autoregressively:

ABt  = aABt-1 + bXYt-1 + ut  and XYt  = cABt-1 + dXYt-1 + vt

Forex pairs trading based on cointegration

When I use my mechanical trading system for forex pairs trading, the setup and execution are fairly simple. First, I find two currency pairs which seem like they may be cointegrated, such as EUR/USD and GBP/USD.

Then, I calculate the estimated spreads between the two pairs. Next, I check for stationarity using a unit-root test or another common method.

I make sure that my inbound data feed is working appropriately, and I let my mechanical trading algorithms create the trading signals. Assuming I’ve run adequate back-tests to confirm the parameters, I’m finally ready to use cointegration in my forex pairs trading.

I’ve found a MetaTrader indicator which offers an excellent starting point to build a cointegration forex pairs trading system. It looks like a Bollinger Band indicator, yet in fact the oscillator shows the price differential between the two different currency pairs.

When this oscillator moves toward either the high or low extreme, it indicates that the pairs are decoupling, which signals the trades.

Still, to be sure of success I rely on my well-built mechanical trading system to filter the signals with the Augmented Dickey-Fuller test before executing the appropriate trades.

Of course, anyone who wants to use cointegration for his or her forex pairs trading, yet lacks the requisite algo programming skills, can rely on an experienced programmer to create a winning expert advisor.

Through the magic of algorithmic trading, I program my mechanical trading system to define the price spreads based on data analysis. My algorithm monitors for price deviations, then automatically buys and sells currency pairs in order to harvest market inefficiencies.

Risks to be aware of when using cointegration with forex pairs trading

Forex pairs trading is not entirely risk-free. Above all, I keep in mind that forex pairs trading using cointegration is a mean-reversion strategy, which is based on the assumption that the mean values will be the same in the future as they were in the past.

Although the Augmented Dickey-Fuller test mentioned previously is helpful in validating the cointegrated relationships for forex pairs trading, it doesn’t mean that the spreads will continue to be cointegrated in the future.

I rely on strong risk management rules, which means that my mechanical trading system exits from unprofitable trades if or when the calculated reversion-to-mean is invalidated.

When the mean values change, it’s called drift. I try to detect drift as soon as possible. In other words, if the prices of previously-cointegrated forex pairs begin to move in a trend instead of reverting to the previously-calculated mean, it’s time for the algorithms of my mechanical trading system to recalculate the values.

When I use my mechanical trading system for forex pairs trading, I use the autoregressive formula mentioned earlier in this article in order to calculate a moving average to forecast the spread. Then, I exit the trade at my calculated error bounds.

Cointegration is a valuable tool for my forex pairs trading

Using cointegration in forex pairs trading is a market-neutral mechanical trading strategy that lets me trade in any market environment. It’s a smart strategy that’s based on reversion to mean, yet it helps me avoid the pitfalls of some of the other reversion-to-mean forex trading strategies.

Because of its potential use in profitable mechanical trading systems, cointegration for forex pairs trading has attracted interest from both professional traders as well as academic researchers.

There are plenty of recently-published articles, such as this quant-focused blog article, or this scholarly discussion of the subject, as well as plenty of discussion among traders.

Cointegration is a valuable tool in my forex pairs trading, and I highly recommend that you look into it for yourself.

 

Filed Under: How does the forex market work?, Uncategorized Tagged With: cointegration, cointegration calculations, forex pairs trading, mechanical trading

Analyzing Pairs with Correlation and Cointegration

May 10, 2013 by Rupert Hadlow Leave a Comment

Pairs Trading or Market Neutrality have long been seen as complex hedge fund style strategies with limited application for the retail trader. As part of our series on Correlation and Cointegration, we thought it would be beneficial to look at how both regression patterns can be used effectively to identify pair trading opportunities and scenarios, and how to reduce possible pitfalls.

The mystery that surrounds Cointegration and its complexity from a formulation point of view has somewhat put off many traders. Funnily enough, even as I write this post the spell check does not identify Cointegration as a word, which gives you an indication of how often the term is referenced.  

Identifying Good Pairs Trades

Although there are a number of formulas or tests that can be used, one of the most widely adopted is that of the Augmented Dickey Fuller (ADF) Test. Formulating a p-value, the test allows the trader to identify how cointegrated two series are over a specified period in an efficient and simplified way.

To put this into context, if the prices for currency A and currency B are inputted into the model and the p-value comes out at 0.02, then this identifies that 2% of the time the two variables are not stationary or 98% of the time they are cointegrated.  Make sure that you use a good number of values (eg 3 years on a daily chart), otherwise the calculation may not give you the most accurate indication.

There are a number of sites where you can download an excel version of the ADF test including www.quantcode.com

The next part in the calculation process is to work out the correlation. It is recommended that multiple time frames are used to also paint a picture of the cycle of the linear regression. To highlight how much of a discrepancy there can be, we ran scans on the EURUSD/GBPUSD and Gold/Silver Pairings. Results were very interesting.

  • Example 1:          EURUSD and GBPUSD                              

30 Day Correlation:         73.46%
2 Year Correlation:         64.89%
13 Year Correlation:        89.20%
2 Year Cointegration:     13%

The long term correlation indicates that the pairs track a very similar path. However on a short term basis, they will gradually move apart. On a cointegration basis, only 13% of the time over the last two years have the pairs reverted back to the same mean.

  • Example 2:          Gold and Silver

    30 Day Correlation: 94.98%
    2 Year Correlation: 26.99%
    13 Year Correlation: 95.3%
    2 Year Cointegration: 85%

The two year interval highlights how the prices are statistically out of their long term and short term ranges. According to the cointegration figure, the prices of both Gold and Silver reverted back to the same mean 85% of the time.

Conclusion: Gold and Silver is a better pairs trade than EURUSD and GBPUSD.

Cointegration and Correlation Technicals

Now that we determined that gold and silver show the highest correlation and cointegration, we need to analyse the technicals for specific entry and exit points. In the below example, there are three specific graphical representations (top chart is the gold price, middle chart is the regression correlation, and the bottom chart is the Silver price). The linear regression period is 360 days with no look back.

Gold Silver Correlation

A chart showing gold, silver and their correlation

The diagram had two possible entry and exit points over the one year period. In August, the 360 regression channel indicated that the linear value would revert to its mean after a period above the 3rd standard deviation.

According to the chart, a Short Gold and Long Silver pair signal would have been triggered. The second trade came in December, with the linear line breaking back through the bottom standard deviation channel.  

Pitfall

With reference to the above example, we can see a number of issues that could greatly impact performance. Curve fitting as it is commonly known is best described as a specific series fitting a time variable without giving a true and clear picture of performance.

Strategies that require definitions from future pricing or large historical data are usually curve fitted. On paper it may look great, but in real trade scenario, the results may be completely different.

The 360 regression channel chart did not pass the out of sample (curve fitting) test. As can be seen below, when the look back period is amended to 162 Days, the signal would be completely different.    

A moving window for the regression channel provides fewer opportunities for curve fitting

A moving window for the regression channel provides fewer opportunities for curve fitting

Solution

One of the solutions for curve fitting in pairs trading is to reduce the linear regression period to a shorter window or time frame. Although this can result in sensitivity to volatile movements, this reduces the potential risk to forward looking scenarios. Traders should also be aware of changes in the correlation and cointegration values of the pair, as these can shift quickly due to market mispricing or global economic and political events.  

Filed Under: How does the forex market work?, Test your concepts historically, Trading strategy ideas Tagged With: augmented dickey fuller test, cointegration calculations, correlation pairs, curve fitting

FREE trading strategies by email

Trending

Sorry. No data so far.

Archives

  • Dominari
  • How does the forex market work?
  • Indicators
  • MetaTrader Tips
  • MQL (for nerds)
  • NinjaTrader Tips
  • Pilum
  • QB Pro
  • Stop losing money
  • Test your concepts historically
  • Trading strategy ideas
  • Uncategorized
  • What's happening in the current markets?

Translation


Free Trading Strategies

Privacy PolicyRisk Disclosure

Copyright © 2023 OneStepRemoved.com, Inc. All Rights Reserved.