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) ts.update({stock: qs.load(filename)}) # shutil.rmtree('TS', True) results_file = open('results.csv', 'w') results_file.write('Creation date: %s UTC\n' % str(datetime.datetime.utcnow())) results_file.write('Pair Member 1,Pair Member 2,p-value') results = [] counter = 1 for pair in itertools.combinations(ts.keys(), r=2): results_file.write('\n%s,%s,%f' % (pair[0], pair[1], qs.cadf(ts[pair[0]], ts[pair[1]])['results']['pval'])) print counter counter += 1 results_file.close()
cnx = mysql.connector.connect(user='******', password='', host='localhost', database='quantcode') cursor = cnx.cursor() if not os.path.exists('ETF'): 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)
import sys 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.append(qs.load(filename)) # shutil.rmtree("TS", True) minimum = sys.maxint for item in ts: if len(item) < minimum: minimum = len(item) for index in enumerate(ts): ts[index[0]] = ts[index[0]][len(ts[index[0]]) - minimum:] jresults = johansen.coint_johansen(ts, 0, 1) for item in jresults.keys(): print '%s: ' % item, jresults[item] # print 'Creation date: %s UTC' % str(time.strftime('%Y-%m-%d')) # print 'Pair Member 1,Pair Member 2,p-value'
cnx = mysql.connector.connect(user='******', password='', host='localhost', database='quantcode') cursor = cnx.cursor() 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)}) for key in ts.keys(): adf = qs.adf(ts[key]) hurst = qs.hurst(ts[key]) halflife = qs.halflife(ts[key]) vratio = qs.vratio(ts[key], adf['usedlag']) cnx.commit() add_database = ( "INSERT INTO adfresults " "(ticker, adf, 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)" "ON DUPLICATE KEY UPDATE adf = %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 = (key, float(adf["adf"]), float(adf["pval"]), adf["usedlag"], adf["nobs"], float(adf["cvalues"]["1%"]),
#!/usr/bin/env python import csv import os import shutil import sys import johansen import quantstats as qs ts = [] for i in xrange(1, 3): filename = sys.argv[i] + '.csv' qs.download(sys.argv[i], filename) ts.append(qs.load(filename)) if len(sys.argv) >= 4 and sys.argv[3] == '-r': os.remove(filename) # augmented Dickey-Fuller test from statsmodels results = qs.adf(ts[0], maxlag=None, regression='c', autolag='AIC') print 'ADF results: ', results # https://www.quantopian.com/posts/some-code-from-ernie-chans-new-book-implemented-in-python print 'half-life: ', qs.halflife(ts[0]) print 'Hurst exponent: ', qs.hurst(ts[0]) # print qs.hurst2(closes) print 'CADF results: ', qs.cadf(ts[0], ts[1])