#!/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])
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%"]), float(adf["cvalues"]["5%"]), float(adf["cvalues"]["10%"]), float(adf["icbest"]), float(hurst), float(halflife), float(vratio["vratio"]), float(vratio["zscore"]), float(vratio["pval"]), float(adf["adf"]), float(adf["pval"]), adf["usedlag"], adf["nobs"], float(adf["cvalues"]["1%"]),
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%"]), float(adf["cvalues"]["5%"]), float(adf["cvalues"]["10%"]), float(adf["icbest"]), float(hurst), float(halflife), float(vratio["vratio"]), float(vratio["zscore"]), float(vratio["pval"]), float(adf["adf"]), float(adf["pval"]), adf["usedlag"], adf["nobs"], float(adf["cvalues"]["1%"]), float(adf["cvalues"]["5%"]), float(adf["cvalues"]["10%"]), float(adf["icbest"]), float(hurst), float(halflife), float(vratio["vratio"]), float(vratio["zscore"]), float(vratio["pval"])) cursor.execute(add_database, data) cnx.commit() cursor.close() cnx.close()