In [3]:
import numpy as np
import pandas as pd

import statsmodels
from statsmodels.tsa.stattools import coint
import statsmodels.api as stat
import statsmodels.tsa.stattools as ts

import matplotlib.pyplot as plt
from nsepy import get_history
from datetime import date
In [5]:
S1 = get_history(symbol = 'HDFC', 
                        start = date(2020,1,1), 
                        end = date(2020,8,27))

S2 = get_history(symbol = 'HDFCBANK', 
                        start = date(2020,1,1), 
                        end = date(2020,8,27))
In [7]:
#lookback 40 days
result = stat.OLS(S1[['Close']].tail(40), S2[['Close']].tail(40)).fit()
In [8]:
#adf test
a = ts.adfuller(result.resid)
In [9]:
#adf test results
a
Out[9]:
(-2.514163877097726,
 0.11206602491867673,
 4,
 35,
 {'1%': -3.6327426647230316,
  '5%': -2.9485102040816327,
  '10%': -2.6130173469387756},
 279.3622794263615)
In [11]:
#adf - The test statistic.
a[0]
Out[11]:
-2.514163877097726
In [12]:
#pvalue - MacKinnon‚ÄĚs approximate p-value based on MacKinnon (1994, 2010).
a[1]
Out[12]:
0.11206602491867673
In [13]:
#used lag - The number of lags used.
a[2]
Out[13]:
4
In [14]:
#nobs - The number of observations used for the ADF regression and calculation of the critical values
a[3]
Out[14]:
35
In [16]:
#critical values - Critical values for the test statistic at the 1 %, 5 %, and 10 % levels. Based on MacKinnon (2010).
a[4]["1%"], a[4]["5%"], a[4]["10%"]
Out[16]:
(-3.6327426647230316, -2.9485102040816327, -2.6130173469387756)
In [17]:
#icbest - The maximized information criterion if autolag is not None.
a[5]
Out[17]:
279.3622794263615
In [18]:
#end of file
In [ ]: