Cointegration
Published in
1 min readJul 6, 2017
As i already investigate the futures inter-market correlation , lets investigate inter-market cointegration (since 01/01/2017) as per above
Great article about cointegration: https://www.quantopian.com/lectures/integration-cointegration-and-stationarity#notebook
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import Quandl
import pandas_datareader.data as web
import talib as ta
import seaborn; seaborn.set()
from statsmodels.tsa.stattools import coint
def find_cointegrated_pairs(data):
n = data.shape[1]
score_matrix = np.zeros((n, n))
pvalue_matrix = np.ones((n, n))
keys = data.keys()
pairs = []
for i in range(n):
for j in range( n):
S1 = data[keys[i]]
S2 = data[keys[j]]
result = coint(S1, S2)
score = result[0]
pvalue = result[1]
score_matrix[i, j] = score
pvalue_matrix[i, j] = pvalue
if pvalue < 0.05:
pairs.append((keys[i], keys[j]))
return score_matrix, pvalue_matrix, pairs
data = pd.read_pickle('data/data_Backtest.pkl')
data = data['20170101':]
scores, pvalues, pairs = find_cointegrated_pairs(data.ix[:,:25])
seaborn.heatmap(pvalues, xticklabels=data.ix[:,:25].columns.values, yticklabels=data.ix[:,:25].columns.values, cmap='RdYlGn_r'
#, mask = (pvalues >= 0.05)
, annot=True
)
plt.show()