Exemple #1
0
	  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]
Exemple #2
0
import quantcode.sp500 as sp500

cnx = mysql.connector.connect(user='******',
                              password='',
                              host='localhost',
                              database='quantcode')
cursor = cnx.cursor()

qs.ensure_path('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%"]),
Exemple #3
0
    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]
Exemple #4
0
                              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) '
                'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
                'ON DUPLICATE KEY UPDATE 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'
Exemple #5
0
cnx = mysql.connector.connect(user='******', password='',
                              host='localhost',
                              database='quantcode')
cursor = cnx.cursor()

qs.ensure_path('ETF/')

for category in etf.etf_dict.keys():
	ts = {}
	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.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)
				# edit database here
				add_database = ('INSERT INTO insample '
								'(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:
Exemple #6
0
import quantcode.quantstats as qs
import quantcode.sp500 as sp500

cnx = mysql.connector.connect(user='******', password='',
                              host='localhost',
                              database='quantcode')
cursor = cnx.cursor()

qs.ensure_path('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()