Skip links

Implementing a Pairs trading Strategy in Python using EUR USD and USD JPY Pairs

What is Pairs Trading?

A pairs trading strategy is a market-neutral trading approach that involves identifying two highly correlated financial instruments, such as stocks, options, futures, or currencies, and simultaneously buying one and selling the other. The goal of pairs trading is to profit from the mean reversion of the price relationship between the two financial instruments.

Pairs trading works by betting that two or more securities will diverge or converge in price to restore their historical correlation. This strategy is categorized as a statistical arbitrage and convergence trading strategy. Pairs trading can be applied in various market conditions, such as uptrends, downtrends, or sideways movements, and is suitable for different types of financial instruments.

Somе common stratеgiеs usеd in pairs trading includе:

  1. Corrеlation-basеd: This stratеgy involvеs finding pairs of financial instrumеnts with a strong corrеlation,  typically with a corrеlation coеfficiеnt grеatеr than 0. 9.  Tradеrs look for tеmporary dеviations in thе pricе rеlationship and takе positions accordingly,  еxpеcting thе pricеs to rеvеrt to thеir historical corrеlation.
  2. Distancе-basеd: In this approach,  tradеrs calculatе thе distancе bеtwееn thе pricеs of thе two instrumеnts and look for dеviations from thе historical avеragе distancе.  Whеn thе distancе bеtwееn thе instrumеnts widеns or narrows significantly,  tradеrs takе positions еxpеcting thе distancе to rеvеrt to its historical avеragе.
  3. Stochastic-basеd: This stratеgy usеs stochastic indicators to idеntify ovеrbought and ovеrsold conditions in thе two instrumеnts.  Whеn onе instrumеnt is ovеrbought and thе othеr is ovеrsold,  tradеrs takе positions еxpеcting thе pricеs to rеvеrt to thеir historical rеlationship.
  4. Cointеgration-basеd: In this approach,  tradеrs look for pairs of instrumеnts that arе cointеgratеd,  mеaning thеy havе a long-tеrm еquilibrium rеlationship.  Tradеrs usе statistical tеsts,  such as thе Augmеntеd Dickеy-Fullеr tеst,  to confirm thе prеsеncе of cointеgration and takе positions whеn thе sprеad bеtwееn thе instrumеnts dеviatеs from its long-tеrm mеan.
  5. Stochastic diffеrеntial-basеd: This stratеgy involvеs modеling thе pricе rеlationship bеtwееn thе two instrumеnts using stochastic diffеrеntial еquations.  Tradеrs usе thеsе modеls to idеntify dеviations from thе еxpеctеd pricе rеlationship and takе positions accordingly.

Thеsе stratеgiеs can bе appliеd to various financial instrumеnts,  such as stocks,  options,  futurеs,  or currеnciеs,  and can bе usеd in diffеrеnt markеt conditions,  such as uptrеnds,  downtrеnds,  or sidеways movеmеnts.  It is еssеntial to bе awarе of thе corrеlation bеtwееn various instrumеnts and thеir shifting trеnds to managе your portfolio еffеctivеly. 

Before we get started some basic Statistics about the two pairs:

When comparing the exchange rate statistics of EUR/USD and USD/JPY currency pairs, we observe distinct patterns. The mean exchange rate for EUR/USD is approximately 1.2015, while that for USD/JPY is around 106.0099. The median exchange rate for EUR/USD is 1.1753, slightly lower than the median of 108.417 for USD/JPY. Interestingly, the mode, representing the most frequent value, is 1.1216 for EUR/USD and 76.8600 for USD/JPY.

The standard deviation, which measures the spread of data, is 0.1138 for EUR/USD and 16.1312 for USD/JPY, indicating higher volatility in the latter. Skewness, portraying the symmetry of the distribution, is 0.4043 for EUR/USD and 0.0279 for USD/JPY, suggesting a slight positive skew for the former and nearly symmetric distribution for the latter. Kurtosis, reflecting the tails’ behavior, is negative for both pairs (-0.9372 for EUR/USD and -0.1708 for USD/JPY), signifying flatter peaks and thinner tails than a normal distribution.

In terms of the exchange rate range, EUR/USD shows a range of 0.5248, with values ranging from 0.9596 to 1.4844, while USD/JPY has a larger range of values between 75.7400 and 150.1760. These statistics provide valuable insights into the behavior and characteristics of these currency pairs, which can inform trading strategies, particularly those involving pairs trading.

The goal of Pairs trading is to profit from the mean reversion of their price relationship between pairs, based on their correlations

Step by Step Implementation in Python

Detailed explanation of each step in the context of the code for implementing pairs trading using statistical analysis regression:

  1. Identify correlated currency pairs: In the code, we download historical data for EUR/USD and USD/JPY currency pairs using Yahoo Finance. We then align the indices of the two series to ensure that they have the same date range. The correlation between these two currency pairs can be calculated using the `corr()` method in pandas, as shown in the code
  2. Run OLS regression: We perform an Ordinary Least Squares (OLS) regression on the aligned currency pairs using the `sm.OLS()` function from the `statsmodels` library. The slope coefficient obtained from the regression serves as the hedge ratio
  3. Calculate the spread: We calculate the spread between the two currency pairs using the hedge ratio. The spread is the linear combination of the log prices of the two currency pairs
  4. Test for cointegration: We check the stationarity of the spread and the presence of a unit root using the Augmented Dickey-Fuller test from the `statsmodels` library
  5. Identify trading signals: We look for deviations in the price relationship between the two currency pairs. In the code, we use the specified long and short thresholds to identify long and short entry and exit signals
  6. Monitor and adjust: In the code, we perform a simple backtest using the identified trading signals. We update the positions and capital based on the entry and exit signals. This backtesting process simulates the monitoring and adjustment of the strategy

This code implements the pairs trading strategy using statistical analysis regression for the EUR/USD and USD/JPY currency pairs. The code includes data retrieval, OLS regression, spread calculation, cointegration testing, trading signals identification, and backtesting. Note that this is a simple example and may require further optimization and risk management for real-world trading.

Cointegration

It is a statistical property of a collection of time series variables that are individually non-stationary but have a long-term equilibrium relationship. In the context of pairs trading, cointegration allows us to construct a stationary time series (spread) from two non-stationary asset price series, which can be exploited using a mean reversion trading strategy.

For the EUR/USD and USD/JPY currency pairs, the provided ADF test results show an ADF statistic of -0.8967 and a p-value of 0.7891. Since the p-value is greater than the common significance level of 0.05, we fail to reject the null hypothesis, which means there is not enough evidence to conclude that the spread between these two currency pairs is stationary. Consequently, we cannot confirm the presence of cointegration between these two currency pairs based on this test result.

It is important to note that the ADF test is sensitive to the choice of lag length and model specification, so you may want to try different lag lengths or use other tests for cointegration, such as the Johansen test, to further investigate the relationship between the two currency pairs. If cointegration is confirmed, a mean reversion trading strategy can be applied to profit from the mean reversion of the price relationship between the two currency pairs.

In the context of pairs trading, cointegration is an essential property to ensure that the spread between the two currency pairs is mean-reverting. If the spread is not stationary, it implies that the price relationship between the two currency pairs may not revert to its long-term mean, making it difficult to profit from a mean reversion trading strategy. Therefore, based on the given test result, it is not ideal for pairs trading, as there is not enough evidence to confirm the presence of cointegration between the EUR/USD and USD/JPY currency pairs. It is important to note that the ADF test is sensitive to the choice of lag length and model specification, so you may want to try different lag lengths or use other tests for cointegration, such as the Johansen test, to further investigate the relationship between the two currency pairs.

Cointegration indicates a long-term equilibrium relationship between non-stationary time series.

Conclusion

The implementation of a pairs trading strategy using statistical regression involves steps such as downloading historical data for correlated currency pairs, aligning their indices, and calculating their correlation. Through Ordinary Least Squares (OLS) regression, the hedge ratio is determined, which guides the calculation of the spread between the pairs. Augmented Dickey-Fuller (ADF) tests are conducted to assess cointegration and the stationarity of the spread, with cointegration being essential for successful mean reversion trading. Trading signals are identified based on spread deviations, and a backtesting process simulates strategy performance. However, the provided example indicates that the tested currency pairs, EUR/USD and USD/JPY, lack sufficient evidence for cointegration, highlighting the importance of further investigation and alternative tests.