Skip to content

Instantly share code, notes, and snippets.

@marketcalls
Created August 26, 2020 21:38
Show Gist options
  • Save marketcalls/21a25416f3d3b5fe8956104e6f16a0d5 to your computer and use it in GitHub Desktop.
Save marketcalls/21a25416f3d3b5fe8956104e6f16a0d5 to your computer and use it in GitHub Desktop.
Cointegration Python to Amibroker
#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