os.makedirs('ETF') ts = [] categories = [] stocks = [] if len(sys.argv) > 1: for i in xrange(1, len(sys.argv)): categories.append(sys.argv[i]) else: for category in etf.etf_dict.keys(): categories.append(category) for category in categories: qs.ensure_path('ETF/' + category) for ticker in etf.etf_dict[category]: filename = 'ETF/' + category + '/' + ticker + '.csv' qs.download(ticker, filename) if os.path.isfile(filename): ts.append(qs.load(filename)) stocks.append(ticker) # shutil.rmtree("ETF", True) ts = qs.johansen_trim(ts) jresults = johansen.coint_johansen(ts, 0, 1) for item in jresults.keys(): print '%s: ' % item, jresults[item]
cnx = mysql.connector.connect(user='******', password='', host='localhost', database='quantcode') cursor = cnx.cursor() if not os.path.exists('ETF'): os.makedirs('ETF') ts = {} for category in etf.etf_dict.keys(): qs.ensure_path('ETF/' + category) for ticker in etf.etf_dict[category]: filename = 'ETF/' + category + '/' + ticker + '.csv' qs.download(ticker, filename, endmonth=12, endday=31, endyear=2007) # download(ticker, filename, endmonth=1, endday=1, endyear=1) is ok if os.path.isfile(filename): ts.update({ticker: qs.load(filename)}) for key1 in ts.keys(): for key2 in ts.keys(): if key1 != key2: # print key1 + "*" + key2 try: cadf = qs.cadf_helper(ts[key1], ts[key2]) except numpy.RankWarning, e: print e, key1, key2 # edit database here add_database = ( 'INSERT INTO alletf '
#!/usr/bin/env python import csv import os import shutil import sys import quantcode.johansen as johansen import quantcode.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) for i in enumerate(ts): # augmented Dickey-Fuller test from statsmodels results = qs.adf(ts[i], 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[i]) print 'Hurst exponent: ', qs.hurst(ts[i]) # print qs.hurst2(closes) print 'CADF results: ', qs.cadf(ts[0], ts[1])
warnings.filterwarnings('error') cnx = mysql.connector.connect(user='******', password='', host='localhost', database='quantcode') cursor = cnx.cursor() if not os.path.exists('ETF'): os.makedirs('ETF') ts = {} for category in etf.etf_dict.keys(): qs.ensure_path('ETF/' + category) for ticker in etf.etf_dict[category]: filename = 'ETF/' + category + '/' + ticker + '.csv' qs.download(ticker, filename, endmonth=12, endday=31, endyear=2007) # download(ticker, filename, endmonth=1, endday=1, endyear=1) is ok if os.path.isfile(filename): ts.update({ticker: qs.load(filename)}) for key1 in ts.keys(): for key2 in ts.keys(): if key1 != key2: # print key1 + "*" + key2 try: cadf = qs.cadf_helper(ts[key1], ts[key2]) except numpy.RankWarning, e: print e, key1, key2 # edit database here add_database = ('INSERT INTO alletf ' '(ticker1, ticker2, cadf, hedgeRatio, pval, usedlag, nobs, cvalue1, cvalue5, cvalue10, icbest, hurst, halflife, vratio, vratiozscore, vratiopval) '