#!/usr/bin/env python import itertools import os import shutil import johansen import quantstats as qs import sp500 if not os.path.exists('TS'): os.makedirs('TS') ts = {} for stock in sp500.sp500: filename = 'TS/' + stock + '.csv' qs.download(stock, filename) if os.path.isfile(filename): ts.update({stock: qs.load(filename)}) print 'Creation date: %s UTC' % str(time.strftime('%Y-%m-%d')) print 'Pair Member 1,Pair Member 2,p-value' for key1 in ts.keys(): for key2 in ts.keys(): print '%s,%s,%f' % (key1, key2, qs.cadf_helper(ts[key1], ts[key2])['results']['pval']) # for pair in itertools.combinations(ts.keys(), r=2): # print '%s,%s,%f' % (pair[0], pair[1], qs.cadf(ts[pair[0]], ts[pair[1]])['results']['pval'])
os.makedirs('ETF') for category in etf.etf_dict.keys(): ts = {} if not os.path.exists('ETF/' + category): os.makedirs('ETF/' + category) for ticker in etf.etf_dict[category]: filename = 'ETF/' + category + '/' + ticker + '.csv' qs.download(ticker, filename) ts.update({ticker: qs.load(filename)}) for key1 in ts.keys(): for key2 in ts.keys(): if key1 != key2: # print key1 + "*" + key2 cadf = qs.cadf_helper(ts[key1], ts[key2]) volume1 = qs.getVolume(key1, category) volume2 = qs.getVolume(key2, category) add_database = ('INSERT INTO etfcadf ' '(ticker1, ticker2, volume1, volume2, category, cadf, hedgeRatio, pval, usedlag, nobs, cvalue1, cvalue5, cvalue10, icbest, hurst, halflife, vratio, vratiozscore, vratiopval) ' 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)' 'ON DUPLICATE KEY UPDATE volume1 = %s, volume2 = %s, category = %s, cadf = %s, hedgeRatio = %s, pval = %s, usedlag = %s, nobs = %s, cvalue1 = %s, cvalue5 = %s, cvalue10 = %s, icbest = %s, hurst = %s, halflife = %s, vratio = %s, vratiozscore = %s, vratiopval = %s') data = (key1, key2, volume1, volume2, etf.categories[category], float(cadf['results']['adf']), float(cadf['hedgeRatio']), float(cadf['results']['pval']), cadf['results']['usedlag'], cadf['results']['nobs'], float(cadf['results']['cvalues']['1%']), float(cadf['results']['cvalues']['5%']), float(cadf['results']['cvalues']['10%']), float(cadf['results']['icbest']), float(cadf['hurst']), float(cadf['halflife']), float(cadf['vratio']['vratio']), float(cadf['vratio']['zscore']), float(cadf['vratio']['pval']), volume1, volume2, etf.categories[category], float(cadf['results']['adf']), float(cadf['hedgeRatio']), float(cadf['results']['pval']), cadf['results']['usedlag'], cadf['results']['nobs'], float(cadf['results']['cvalues']['1%']), float(cadf['results']['cvalues']['5%']), float(cadf['results']['cvalues']['10%']), float(cadf['results']['icbest']), float(cadf['hurst']), float(cadf['halflife']), float(cadf['vratio']['vratio']), float(cadf['vratio']['zscore']), float(cadf['vratio']['pval'])) try: cursor.execute(add_database, data) except mysql.connector.errors.ProgrammingError, e: print "%s*%s*%s" % (e, key1, key2) cnx.commit() cursor.close()
import itertools import os import shutil import johansen import quantstats as qs import sp500 if not os.path.exists('TS'): os.makedirs('TS') ts = {} for stock in sp500.sp500: filename = 'TS/' + stock + '.csv' qs.download(stock, filename) if os.path.isfile(filename): ts.update({stock: qs.load(filename)}) print 'Creation date: %s UTC' % str(time.strftime('%Y-%m-%d')) print 'Pair Member 1,Pair Member 2,p-value' for key1 in ts.keys(): for key2 in ts.keys(): print '%s,%s,%f' % (key1, key2, qs.cadf_helper( ts[key1], ts[key2])['results']['pval']) # for pair in itertools.combinations(ts.keys(), r=2): # print '%s,%s,%f' % (pair[0], pair[1], qs.cadf(ts[pair[0]], ts[pair[1]])['results']['pval'])