def getPrice(self, ticker, startDate, endDate, dateAscending=True): # Get the price series for single ticker # ----Input----- # ticker: ticker name for the stock # startDate: the start date of price series, the format is 'YYYY-MM-DD' # endDate: the end date of price series, the format is 'YYYY-MM-DD' # dateAscending: whether rank the price series by date ascending, the default value is true # ----output---- # price series for multiple stocks in pandas DataFrame format and use date as index startDate = dt.datetime.strptime(startDate, '%Y-%m-%d').date() endDate = dt.datetime.strptime(endDate, '%Y-%m-%d').date() if (startDate > endDate): raise Exception('startDate is later than endDate') #table = YahooFinancials(ticker) #yahooPrices = table.get_historical_price_data(startDate.isoformat(), endDate.isoformat(), 'daily')[ticker]['prices'] if (self.approach == 'Yahoo'): try: table = YahooFinancials(ticker) yahooPrices = table.get_historical_price_data( startDate.isoformat(), endDate.isoformat(), 'daily')[ticker]['prices'] table = pd.DataFrame(yahooPrices) table = table[['adjclose', 'formatted_date']] table.columns = [ticker, 'date'] table = table.sort_values(by='date', axis=0, ascending=dateAscending) table = table.set_index('date') table.index = pd.to_datetime(table.index) except: table = pd.DataFrame(columns=[ticker, 'date']) table = table.set_index('date') return table
def getDetailPriceInfo(self, ticker, startDate, endDate, columns=['close', 'date'], dateAscending=True, frequency='D'): # Get the aggregated detailed price series for single ticker, including open, high, low, close, adj_close, volume # ----Input----- # ticker: ticker name for single stocks # startDate: the start date of price series, the format is 'YYYY-MM-DD' # endDate: the end date of price series, the format is 'YYYY-MM-DD' # columns: the columns in the output DataFrame, the default columns are 'close' and 'date' # dateAscending: whether rank the price series by date ascending, the default value is true # frequency: aggregate frequency, default value is 'D', also accept 'W' for week and 'M' for month # ----output---- # aggregated price series for single stock if (frequency not in ['D', 'W', 'M']): raise Exception( '''invalid frequency, available range ['D','W','M']''', frequency) available_Columns = [ 'date', 'open', 'high', 'close', 'adj_close', 'low', 'volume' ] for column in columns: if (column not in available_Columns): raise Exception( '''invalid column names, available range ['date','open','high','close','adj_close', 'low', 'volume']''', column) if ('date' not in columns): columns.append('date') if (self.approach == 'Yahoo'): table = YahooFinancials(ticker) table = pd.DataFrame( table.get_historical_price_data(startDate, endDate, 'daily')[ticker]['prices']) newColumns = FinanceData.columnNameMapping(columns) table = table[newColumns] table.columns = columns table = table.sort_values(by='date', axis=0, ascending=dateAscending) table = table.set_index('date') table.index = pd.to_datetime(table.index) if (frequency == 'D'): return table else: result = [] for column in columns: if (column in ['open']): result.append(table.resample(frequency).first()[column]) elif (column in ['close', 'adj_close']): result.append(table.resample(frequency).last()[column]) elif (column in ['high']): result.append(table.resample(frequency).max()[column]) elif (column in ['low']): result.append(table.resample(frequency).min()[column]) elif (column in ['volume']): result.append(table.resample(frequency).sum()[column]) return (pd.concat(result, axis=1))