#from __future__ import print_function from datetime import datetime from pandas_datareader import data, wb import statsmodels.tsa.stattools as ts import numpy as np from hurst import hurst amzn = data.DataReader("AMZN", "yahoo", datetime(2000, 1, 1), datetime(2015, 1, 1)) ans = ts.adfuller(amzn['Adj Close'], 1) #create Geometric Brownian Motion, mean-reverting, and trending series gbm = np.log(np.cumsum(np.random.randn(100000)) + 1000) mr = np.log(np.random.randn(100000) + 1000) tr = np.log(np.cumsum(np.random.randn(100000) + 1) + 1000) #print HE he_gbm = hurst(gbm) he_mr = hurst(mr) he_tr = hurst(tr) he_amzn = hurst(amzn['Adj Close'])
import statsmodels.tsa.stattools as st import matplotlib.pylab as plt import numpy as np import pandas as pd df_caus = pd.read_csv('USDCAD.csv') plt.hold(False) df_caus.y.plot() #plt.show() print st.adfuller(df_caus.y,maxlag=1) import hurst as h print 'H doviz kuru', h.hurst(df_caus.y) from arch.unitroot import VarianceRatio vr = VarianceRatio(np.log(df_caus.y)) print(vr.summary().as_text()) df_caus['ylag'] = df_caus['y'].shift(1) df_caus['deltaY'] = df_caus['y'] - df_caus['ylag'] import statsmodels.formula.api as smf results = smf.ols('deltaY ~ ylag', data=df_caus).fit() lam = results.params['ylag'] print lam halflife=-np.log(2)/lam print halflife, 'days'
hedgeRatio = results.params["ewa"] print hedgeRatio df["coint"] = df["ewc"] - hedgeRatio * df["ewa"] plt.hold(False) df["coint"].plot() # plt.show() import pyconometrics print pyconometrics.cadf(np.matrix(df["ewa"]).H, np.matrix(df["ewc"]).H, 0, 1) import statsmodels.tsa.stattools as st import hurst print "hurst", hurst.hurst(df["coint"]) print st.adfuller(df["coint"], maxlag=1) from johansen import coint_johansen, print_johan_stats res = coint_johansen(df[["ewa", "ewc"]], 0, 1) print_johan_stats(res) cols = ["ewc", "ewa", "ige"] res3 = coint_johansen(df[cols], 0, 1) print_johan_stats(res3) df["yport"] = np.dot(df[cols], res3.evec[:, 0]) plt.hold(False) df["yport"].plot() # plt.show()
import statsmodels.formula.api as smf results = smf.ols('ewc ~ ewa', data=df).fit() hedgeRatio = results.params['ewa'] print hedgeRatio df['coint'] = df['ewc'] - hedgeRatio * df['ewa'] plt.hold(False) df['coint'].plot() #plt.show() import pyconometrics print pyconometrics.cadf(np.matrix(df['ewa']).H, np.matrix(df['ewc']).H, 0, 1) import statsmodels.tsa.stattools as st import hurst print 'hurst', hurst.hurst(df['coint']) print st.adfuller(df['coint'], maxlag=1) from johansen import coint_johansen, print_johan_stats res = coint_johansen(df[['ewa', 'ewc']], 0, 1) print_johan_stats(res) cols = ['ewc', 'ewa', 'ige'] res3 = coint_johansen(df[cols], 0, 1) print_johan_stats(res3) df['yport'] = np.dot(df[cols], res3.evec[:, 0]) plt.hold(False) df['yport'].plot() #plt.show()
'300495', '002038', '300668', '300654', '300640', '603021', '300708', '300720', '603960', '603579',\ '300579', '603085', '002842', '002881', '002895', '603683', '002922', '603898', '603126', '300431',\ '002513', '603318', '300395', '300424', '002738', '600406', '600348', '603655', '002923', '300626',\ '603696', '600228', '603912', '300552', '002841', '603737', '002896', '600389', '002921', '300396',\ '601069', '300625', '603871', '300631', '002908', '002883', '002461', '002878', '002893', '603040',\ '300621', '603108', '300437', '300423', '300422', '603690', '002919', '300620', '002886', '603900',\ '002851', '300740', '300595', '603533', '603080', '603679', '300434', '603335', '002264', '300390',\ '002715', '603678', '300623', '603283', '300558', '603517', '002863', '600234', '002917', '600354',\ '300438', '300388', '300439', '002725', '603660', '300607', '002916', '603728', '603066', '603516',\ '002492', '603138', '300406', '002732', '603688', '002915', '002901', '300638', '002336', '600959',\ '603505', '300589', '300012', '002859', '002905', '300628', '603882', '002079', '300629', '002910',\ '600390', '300577', '603289', '603260', '603937', '300603', '002735', '603499', '300398', '603466',\ '603315', '603329', '300400', '603881', '603895', '300602', '002873', '300711', '300659', '300665',\ '300103', '300671', '603825', '603616', '002753', '002747', '600323', '300498', '603429', '600691',\ '002752', '300664', '300102', '603818', '603011', '603788', '300706', '300712', '002803', '300128',\ '603601', '300470', '600123', '603416', '300698', '600080', '603599', '603228', '300298', '002423',\ '603566', '300717', '000793', '300461', '601211', '300448', '300662', '300716', '000553', '603567',\ '002378', '603229', '000584', '002391', '603203', '300700', '603968', '603997', '603808', '002756',\ '000633', '002024', '300462', '603363', '300339', '300477', '603160', '002838', '002390', '000587'] for code_id in code_list: if code_id in ['601990', '603587', '603666', '601066']: continue tmp_df = df[code_id] tmp_df = tmp_df.dropna(axis = 0, how = 'all') res = hurst(tmp_df) if res > 0.5: name = get_name(code_id) print("code_id:%s, name:%s hurst:%s" % (code_id, name, res)) #if cadf[0] < cadf[4]['5%'] and cadf[0] < cadf[4]['1%'] and cadf[0] < cadf[4]['10%']: # name = get_name(code_id) # print("code_id:%s, name:%s C0:%s C1:%s B1:%s B5:%s B10%s" % (code_id, name, cadf[0], cadf[1], cadf[4]['1%'], cadf[4]['5%'], cadf[4]['10%']))
import statsmodels.tsa.stattools as st import matplotlib.pylab as plt import numpy as np import pandas as pd df_caus = pd.read_csv('USDCAD.csv') plt.hold(False) df_caus.y.plot() #plt.show() print st.adfuller(df_caus.y, maxlag=1) import hurst as h print 'H doviz kuru', h.hurst(df_caus.y) from arch.unitroot import VarianceRatio vr = VarianceRatio(np.log(df_caus.y)) print(vr.summary().as_text()) df_caus['ylag'] = df_caus['y'].shift(1) df_caus['deltaY'] = df_caus['y'] - df_caus['ylag'] import statsmodels.formula.api as smf results = smf.ols('deltaY ~ ylag', data=df_caus).fit() lam = results.params['ylag'] print lam halflife = -np.log(2) / lam print halflife, 'days'
results = smf.ols('ewc ~ ewa', data=df).fit() hedgeRatio = results.params['ewa'] print hedgeRatio df['coint'] = df['ewc']-hedgeRatio*df['ewa'] plt.hold(False) df['coint'].plot() #plt.show() import pyconometrics print pyconometrics.cadf(np.matrix(df['ewa']).H, np.matrix(df['ewc']).H,0,1) import statsmodels.tsa.stattools as st import hurst print 'hurst', hurst.hurst(df['coint']) print st.adfuller(df['coint'],maxlag=1) from johansen import coint_johansen, print_johan_stats res = coint_johansen(df[['ewa','ewc']], 0, 1) print_johan_stats(res) cols = ['ewc','ewa','ige'] res3 = coint_johansen(df[cols], 0, 1) print_johan_stats(res3) df['yport'] = np.dot(df[cols], res3.evec[:,0]) plt.hold(False) df['yport'].plot() #plt.show()