def compute_correlations(start_date, end_date, downsample_days, name, symbol_file): log.info("computing %d-day correlations between %s and %s" % (downsample_days, start_date, end_date)) ndays = (end_date - start_date).days ts0 = None for symbol in utils.itersymbols(symbol_file): ts = load_time_series(symbol, start_date, end_date, downsample_days=downsample_days) if ts is not None: if ts0 is None: ts0 = ts else: ts0 = ts0.join(ts) else: log.warning("skipping %s because data is not available for %s" % (symbol, start_date)) #print "*" * 40 #print ts #print ts0 #print "*" * 40 if ts0: corr = ts0.corr() filename = "corr_%s_%s_%s-day_%s.csv" % (str( start_date.date()), str(end_date.date()), downsample_days, name) corr.to_csv(filename) log.info("wrote %s" % filename) #convert_to_excel(filename) else: log.warning("no csv file written")
def compute_correlations(start_date, end_date, downsample_days, name, symbol_file): log.info("computing %d-day correlations between %s and %s" % (downsample_days, start_date, end_date)) ndays = (end_date - start_date).days ts0 = None for symbol in utils.itersymbols(symbol_file): ts = load_time_series(symbol, start_date, end_date, downsample_days=downsample_days) if ts is not None: if ts0 is None: ts0 = ts else: ts0 = ts0.join(ts) else: log.warning("skipping %s because data is not available for %s" % (symbol, start_date)) #print "*" * 40 #print ts #print ts0 #print "*" * 40 if ts0: corr = ts0.corr() filename = "corr_%s_%s_%s-day_%s.csv" % (str(start_date.date()), str(end_date.date()), downsample_days, name) corr.to_csv(filename) log.info("wrote %s" % filename) #convert_to_excel(filename) else: log.warning("no csv file written")
import sys import argparse #from http import HTTPClient import requests import utils import datetime import logging logging.basicConfig() log = logging.getLogger() log.setLevel(logging.INFO) def fetch_data(symbol): url = "http://ichart.yahoo.com/table.csv?s=%s&a=00&b=1&c=1980&g=d&ignore=.csv" % symbol log.info("downloading data for %s" % symbol) data = requests.get(url).text filename = "%s.csv" % symbol with open(filename, "w") as fp: fp.write(data) log.info("Wrote file '%s'" % filename) if __name__ == "__main__": parser = argparse.ArgumentParser(description='Yahoo! price fetcher') parser.add_argument('--symbol-file', type=str, default="symbols.txt", help='symbol file name') args = parser.parse_args() if os.path.exists(args.symbol_file): for symbol in utils.itersymbols(args.symbol_file): fetch_data(symbol) else: log.error("can't open %s" % args.symbol_file)