def read(self): """ Read data from the files (and write them at self.price, dividend, split) """ with open(self.__path(), 'r') as f: f.readline() self.price = dict() for line in f: d = workingday.strptime(line[:10], '%Y-%m-%d') c = line[13:-1].split() row = [] for i in xrange(5): row.append(float(c[i])) row.append(int(c[5])) self.price[d] = row self.dividend = dict() self.split = dict() try: with open(self.__path_dividends(), 'r') as f: f.readline() for line in f: d = workingday.strptime(line[:10], '%Y-%m-%d') c = line[10:-1].split() if c[0] == 'Dividend': self.dividend[d] = float(c[1]) elif c[0] == 'Split': self.split[d] = tuple(map(int, c[1].split(':'))) except: pass
def risk_free_return(): file_name = 'FED/yields_10Y.csv' with open(file_name, 'r') as f: for i in xrange(7): f.readline() h = DateSerie() rate = 0 for line in f: c = line[:-1].split(',') try: h[workingday.strptime(c[0], '%Y-%m-%d')] = float(c[1])*0.01 except: pass return h.TimeSerie()
def __download(self, since=workingday(1900, 1, 1)): """ Downloads data from yahoo finance from date begin until today (other sources than yahoo finance may be considered in future) """ until = workingday.today() input_tuple = (self.symbol, str(since.month - 1), str(since.day), str(since.year), str(until.month - 1), str(until.day), str(until.year)) self.price = dict() self.dividend = dict() self.split = dict() try: url = 'http://ichart.yahoo.com/table.csv?s=%s&g=d&a=%s&b=%s&c=%s&d=%s&e=%s&f=%s&ignore=.csv' % input_tuple raw_data = urlopen(url) raw_data.readline() for line in raw_data: l = line.split(',') d = workingday.strptime(l[0], '%Y-%m-%d') row = [ float(l[1]), # Open float(l[2]), # High float(l[3]), # Low float(l[4]), # Close float(l[-1][:-1]), # Adj int(l[5]) ] # Volume self.price[d] = row # get dividend and split data url = 'http://ichart.finance.yahoo.com/x?s=%s&g=v&a=%s&b=%s&c=%s&d=%s&e=%s&f=%s&ignore=.csv' % input_tuple raw_data = urlopen(url) raw_data.readline() for line in raw_data: l = line.split(',') if l[0] == 'DIVIDEND': d = workingday(int(l[1][1:5]), int(l[1][5:7]), int(l[1][7:9])) self.dividend[d] = float(l[2][:-1]) elif l[0] == 'SPLIT': d = workingday(int(l[1][1:5]), int(l[1][5:7]), int(l[1][7:9])) self.split[d] = tuple(map(int, l[2][:-1].split(':'))) except: print 'Error downloading ' + self.symbol
def risk_free_return(): file_name = 'FED/yields_10Y.csv' with open(file_name, 'r') as f: for i in xrange(7): f.readline() h = DateSerie() rate = 0 for line in f: c = line[:-1].split(',') try: h[workingday.strptime(c[0], '%Y-%m-%d')] = float(c[1]) * 0.01 except: pass return h.TimeSerie()
def __download(self, since = workingday(1900,1,1)): """ Downloads data from yahoo finance from date begin until today (other sources than yahoo finance may be considered in future) """ until = workingday.today() input_tuple = (self.symbol, str(since.month - 1), str(since.day), str(since.year), str(until.month - 1), str(until.day), str(until.year)) self.price = dict() self.dividend = dict() self.split = dict() try: url = 'http://ichart.yahoo.com/table.csv?s=%s&g=d&a=%s&b=%s&c=%s&d=%s&e=%s&f=%s&ignore=.csv' % input_tuple raw_data = urlopen(url) raw_data.readline() for line in raw_data: l = line.split(',') d = workingday.strptime(l[0],'%Y-%m-%d') row = [ float(l[1]), # Open float(l[2]), # High float(l[3]), # Low float(l[4]), # Close float(l[-1][:-1]), # Adj int(l[5])] # Volume self.price[d] = row # get dividend and split data url = 'http://ichart.finance.yahoo.com/x?s=%s&g=v&a=%s&b=%s&c=%s&d=%s&e=%s&f=%s&ignore=.csv' % input_tuple raw_data = urlopen(url) raw_data.readline() for line in raw_data: l = line.split(',') if l[0] == 'DIVIDEND': d = workingday(int(l[1][1:5]), int(l[1][5:7]), int(l[1][7:9])) self.dividend[d] = float(l[2][:-1]) elif l[0] == 'SPLIT': d = workingday(int(l[1][1:5]), int(l[1][5:7]), int(l[1][7:9])) self.split[d] = tuple(map(int, l[2][:-1].split(':'))) except: print 'Error downloading ' + self.symbol