Now before we dive into testing a strategy we must first define what makes a good pair to test in the first place. This is a question that has not one answer but several depending on the approach you want to take. We will discuss some of the possibilities and the weapons of choice if you want to code a good pairs trading strategy.
Testing for Cointegration
The idea: While it may be difficult to predict the price direction of an asset , it may be easier to predict relative behavior of two assets. Here is an illustrative example, imagine a a man is wandering the streets with a dog. Both paths of man and dog are non-stationary and are difficult to predict, but the distance between them is mean reverting and stationary.
So what is cointegration?
- Cointegration doesn’t exist when two quantities do not stay together.
- Cointegration is high when two quantities move together or remain in close distance to each other.
Association Rule Mining
Rule discovery is a method of determining stock or market relationships by examining individual correlations. The goal of association rule mining is to provide traders with more insight, not to solve the pairs trading problem. For example, one method of generating stock rules is to mine frequent two item sets (two stocks) from stock data. “If the price of AAPL rises, the price of MSFT is likely to rise as well,” for example, or vice versa. On the basis of historical data, association rules can be used to forecast future stock price movements. This will assist in determining asset price convergence.
In both the data mining and machine learning fields, clustering is the most important unsupervised learning technique. Essentially, you’re looking for connections. Because this method does not rely on prior knowledge of a cluster, it is the best strategy for finding pairs. Chaotic Map Clustering is a method for identifying similar temporal behavior of traded stock prices.
The chaotic map clustering algorithm is used, by assigning each company to a map and the financial time series’ correlation coefficients are linked to the coupling strengths between maps. Because companies belonging to the same industrial branch are frequently grouped together, the simulation of chaotic map dynamics leads to a natural partition of the data. Portfolio optimization strategies can take advantage of the identification of clusters of companies in a given stock market index.
Python Example – Clustering with K-Means
To perform a clustering analysis in python with K-Means clustering algorithm following packages are imported:
from pandas_datareader import data
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import plotly.graph_objects as go
The dictionary ‘companies dict’ is defined, with the ‘key’ being the firm’s name and the ‘value’ being the stock code of the company. There are 28 companies that are being considered.
Unsupervised learning models such as K-means clustering are a sort of unsupervised learning model. Unsupervised models are used to learn from unlabeled or unclassified data sets. It searches for commonalities in the data set and responds to the presence or absence of such commonalities in each data point.
A K-means clustering model basically starts with K centroids and categorizes data points that are close (similar) to the centroids as clusters.
Above we have shown how you can implement such an algorithm and use the results for further research on tradeable pairs.
Key Issues in Pairs Trading
Pairs mining is an intriguing research topic that has revealed challenges and research issues that database and data mining researchers must address. A few of these issues are addressed in this section.
High dimensionality: One of the most difficult aspects of mining pairs is that their relationship is embedded in a large amount of data (i.e., large number of stocks and long historical period). To mine and extract pairs from such data, an efficient algorithm is required.
Market liquidity: A liquid market is defined by the presence of investors who are ready and willing to buy and sell stocks at any time. This shows which stocks are readily available. Liquidity is critical in the pairs trading universe, so traders should think about it before looking for asset pairs.
Historical data: When mining pairs, it’s critical to come to a decision about how much data should be included in the study or how long the historical price period should be. The prediction process or the judgment on two stock pairs may be influenced by different periods. Furthermore, historical data may contain noise or random values, which may affect the analysis by resulting in inaccurate information or buy/sell alerts.
Coding the Strategy
So far, we’ve talked about the statistical challenges of picking the right pair to trade. We can say that the spread between two assets is mean reverting using the cointegration test.
The next step is to define the extreme points, which will trigger trading orders for our pairs trading strategy. The z-score is a statistical construct that is used to identify these points.
The z-score is calculated from our distribution’s raw data points, resulting in a normal distribution with a mean of 0 and a standard deviation of 1. We can create threshold levels such as 2 sigma, 3 sigma, and so on using this distribution.
###Formula for the z-score###
z= (x-mean)/standard deviation
###Strategy entry points###
Spread=s = log(a)-nlog(b)
Calculate z-score of s, using rolling mean and standard deviation for a time period of t intervals (moving average). The calculated value is our z. The threshold parameters are adjusted according to the backtest results.
###When Z-score crosses upper threshold###
Short asset A // Long asset B
###When Z-score crosses lower threshold###
Long asset A // Short asset B
Strategy exit points
If the spread continues to blow up we use either a stop loss or a sigma value triggering the exit condition. The condition and exact setup is defined after we run our optimization tests.
- Pair Tested: USD/CAD / NYMEX Crude Oil Futures
- Backtest Period: 01.2018 – 05.2021
- Walk Forward Analysis: 01.2014 – 12.2017
- Pyramiding: No (trading one contract per trade signal)
- Long Entry: Cross Lower Threshold USD/CAD Z Score 2
- Short Entry: Cross Upper Threshold USD/CAD Z Score 1.9
- Bars used for calculation: 8 (last 8 trading days are used to calculate the z scores)
###Strategy Exit Rules###
- Stop Loss: $1800/contract (additional safety net)
- Long Exit: If trade crosses above Z Score 0
- Short Exit: If trade crosses below Z Score 0
To arrive at the optimized parameters we have run over 10,000 backtests. Above parameters are optimal in terms of consistency considering following variables: payout, profit factor, profit percentage, no deviation from backtested results when slightly changing the parameter’s (no jumps).
###Long and Short Trades Combined Overall Trade Performance###
###Long Only Trades###
###Short Only Trades###
Monte Carlo Simulation
Running 1,000 simulations of random trades where each simulation starts with $50,000 and trades for four years gives us the following risk profile.
Our sweet spot is around 10%. Assuming we risk 10% per trade, we can lose a maximum of $5,000 with a starting capital of $50,000. The average drawdown is 18.58%. Let’s move on to the walk-forward analysis using the same parameters as before.
Walk Forward Analysis
There is no better way to avoid curve fitting than to use WFA. It works by dividing the data into several training and validation sets, then optimizing for the best values on the training sets. The validation set is given the best values. We have one training set ranging from 2018 to 2021 and one validation set from 2014 to 2017 which are used in our backtest.
The validation set yields a return of 174.7 percent and a winning percentage of 55.8. The following is a detailed analysis of the strategy’s performance:
Monte Carlo Simulation WFA
The monte carlo simulation shows an average drawdown of 30% if we risk 10% per trade. The probability of blowing up our account is now much higher and clocks in at 19%. This is way too high. In order to reduce these oscillations we would need to adjust the stop loss and the z scores in regular intervals. Instead of using a fixed $ stop loss it is wiser to use a percentage based stop loss.
What next – Pairs Trading 2.0
The z scores need to be adjusted depending on current market volatility. In a next step clustering the historical data for USD/CAD and Crude Oil based on volatility is crucial if we want to keep our % failures (MC simulation) low. Large changes in prices tend to cluster together, resulting in persistence of the amplitudes of price changes.
With such an approach we can fine tune our z scores for long/short entries when necessary avoiding larger losses in the long run and a more stable strategy performance.
For both in-sample and out-of-sample backtests, the strategy is profitable. Depending on asset volatility, it’s critical to adjust the z score for long and short entries on a regular basis. Another option is to halt trading when volatility reaches a predetermined level.
By grasping the concepts of correlation and cointegration, we can see that trading a properly formed pair of assets yields profits that are resistant to volatility jumps. Market events and uncertainty are factored into the strategy by adjusting our z scores. By adding another stop loss component to our trading strategy, we were able to add an additional safety net and reduce our losses when volatility starts to rise.