def __buildExl(self, stock, workbook): ''' get one stock historical data and store it ''' try: ws = workbook.add_sheet(stock) #get data yahooFinance = YahooFinance() allData = yahooFinance.getHistoricalPrices(stock, self.__startDate, self.__endDate) for col, field in enumerate( ['date', 'open', 'high', 'low', 'close', 'volume', 'adjClose']): ws.write(0, col, field) for row, data in enumerate(allData): for col, field in enumerate([ 'date', 'open', 'high', 'low', 'close', 'volume', 'adjClose' ]): ws.write(row + 1, col, getattr(data, field)) except UfException as excp: raise excp except Exception: raise UfException( Errors.UNKNOWN_ERROR, "historicalStorage.__buildExl got unknown error %s" % traceback.print_exc())
def linearRegression(self): if self.__regressioned: return if not self.__benchmarkValues: self.__benchmarkValues = YahooFinance().getHistoricalPrices(self.__benchmark, self.__dateValues[0].date, self.__dateValues[-1].date) tradeSuspended = False if 0 in map(lambda x: float(x.adjClose), self.__dateValues): tradeSuspended = True #filter out date tha't not in both stock and benchmark dateSet = set([dateValue.date for dateValue in self.__dateValues]) & set([dateValue.date for dateValue in self.__benchmarkValues]) self.__dateValues = filter(lambda dateValue: dateValue.date in dateSet, self.__dateValues) self.__benchmarkValues = filter(lambda dateValue: dateValue.date in dateSet, self.__benchmarkValues) if len(self.__dateValues) <= 1 or tradeSuspended: msg = "Not enought dateValues" if len(self.__dateValues) <= 1 else "trade suspended" LOG.debug(msg) self.__beta = 0 self.__alpha = 0 self.__regressioned = True return try: x = [float(self.__benchmarkValues[index + 1].adjClose)/float(self.__benchmarkValues[index].adjClose) for index in range(len(self.__benchmarkValues) - 1)] y = [float(self.__dateValues[index + 1].adjClose)/float(self.__dateValues[index].adjClose) for index in range(len(self.__dateValues) - 1)] (self.__beta, self.__alpha) = numpy.polyfit(x, y, 1) self.__regressioned = True except BaseException as excep: raise UfException(Errors.UNKNOWN_ERROR, "stockMeasurement.linearRegression got unknown error %s" % excep)
def buildBenchmarkValues(): print "Building BenchmarkValues" for key in benchmarks.values(): benchmarkValues[key] = YahooFinance().getHistoricalPrices( key, '19010101', '20130101') time.sleep(1) print "BenchmarkValues %s built" % benchmarkValues.keys()
def __buildExl(self, stock, workbook): ''' get one stock historical data and store it ''' try: ws = workbook.add_sheet(stock) #get data yahooFinance = YahooFinance() allData = yahooFinance.getHistoricalPrices(stock, self.__startDate, self.__endDate) for col, field in enumerate(['date', 'open', 'high', 'low', 'close', 'volume', 'adjClose']): ws.write(0, col, field) for row, data in enumerate(allData): for col, field in enumerate(['date', 'open', 'high', 'low', 'close', 'volume', 'adjClose']): ws.write(row+1, col, getattr(data, field) ) except UfException as excp: raise excp except Exception: raise UfException(Errors.UNKNOWN_ERROR, "historicalStorage.__buildExl got unknown error %s" % traceback.print_exc())
class HistoricalDataFeeder(BaseModule): ''' feeder that get stock data from Yahoo Finance ''' def __init__(self): ''' Constructor ''' super(HistoricalDataFeeder, self).__init__() self.__yahooFinance = YahooFinance() def execute(self, input): ''' preparing data''' super(HistoricalDataFeeder, self).execute(input) return {'history stock': self.__yahooFinance.get_historical_prices(input, '1990-01-01', date.today())}
class HistoricalDataFeeder(BaseModule): ''' feeder that get stock data from Yahoo Finance ''' def __init__(self): ''' Constructor ''' super(HistoricalDataFeeder, self).__init__() self.__yahooFinance = YahooFinance() def execute(self, input): ''' preparing data''' super(HistoricalDataFeeder, self).execute(input) return { 'history stock': self.__yahooFinance.getHistoricalPrices(input, '1990-01-01', date.today()) }
def __init__(self): ''' Constructor ''' super(HistoricalDataFeeder, self).__init__() self.__yahooFinance = YahooFinance()
def testGetHistoricalPrices(self): yahooFinance = YahooFinance() data = yahooFinance.get_historical_prices('^STI', '20110101', '20110110') assert len(data)