Blogs

Cointegration Test of Gold

Start with importing libraries:

import pandas_datareader.data as pdr
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import coint
from statsmodels.api import OLS

Get data for GLD (Gold Spot ETF) and GDX (Gold Miners ETF). 

gld = pdr.DataReader('GLD', 'yahoo', start='2018-07-12', end='2022-06-30')
gdx = pdr.DataReader('GDX', 'yahoo', start='2018-07-12', end='2022-06-30')

Apply cointegration test using statsmodels:

coint_t, pvalue, crit_value = coint(gld['Adj Close'], gdx['Adj Close'])

Results looks like:

T-stat:  -2.37
P val:  0.34
Critical Vals at 90%, 95%, 99% : [-3.91 -3.34 -3.05]

The test result is not significant. For further analysis, build a model and visualize the spread. 

model = OLS(gld['Adj Close'], gdx['Adj Close'])
result = model.fit()
hedge_ratio = result.params[0]
spread = gld["Adj Close"] - (hedge_ratio * gdx['Adj Close'])

Based on the OLS model the Hedge Ratio is about 5.1

After applying the hedge ratio, the spread looks like:

Halflife 

Calculate the number of days you should expect to hold spread before it becomes profitable

prevSpread = spread.shift()
spreadChange = spread - prevSpread
spreadChange.dropna(inplace=True)
prevSpread.dropna(inplace=True)

model_spread = OLS(spreadChange, prevSpread - np.mean(prevSpread))
result_spread_model = model_spread.fit()
theta = result_spread_model.params[0]
halflife = -np.log(2)/theta

The result is approximately 37 days.