Created
August 26, 2020 21:38
-
-
Save marketcalls/21a25416f3d3b5fe8956104e6f16a0d5 to your computer and use it in GitHub Desktop.
Cointegration Python to Amibroker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Python Code for cointegration, adftest, critical values - Values will be returned to Amibroker | |
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 | |
from datetime import date | |
def adf(array1,array2,lookback,element): | |
df1=pd.DataFrame.from_records({'Close': array1}) | |
df2=pd.DataFrame.from_records({'Close': array2}) | |
result = stat.OLS(df1[['Close']].tail(int(lookback)), df2[['Close']].tail(int(lookback))).fit() | |
a = ts.adfuller(result.resid) | |
return a[int(element)] | |
def criticalvalues(array1,array2,lookback,element,str): | |
df1=pd.DataFrame.from_records({'Close': array1}) | |
df2=pd.DataFrame.from_records({'Close': array2}) | |
result = stat.OLS(df1[['Close']].tail(int(lookback)), df2[['Close']].tail(int(lookback))).fit() | |
a = ts.adfuller(result.resid) | |
return a[int(element)][str] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment