def historical_data(ticker, components): # Prices adjusted for splits today, dates = current_date(), [] for each in components: if PATTERNS['valid_date'].match(each): dates.append(each) if not dates: return {"message": Response.missing_dates(ticker)} # Validate dates for each in dates: if each > today: return {"message": Response.invalid_date(each)} try: date = datetime.datetime.strptime(each, '%Y-%m-%d') except ValueError: return {"message": Response.invalid_date(each)} # Validate ticker and fetch data try: quotes = data.get_data_google(ticker) except Exception: return {"message": Response.data_notfound(ticker)} # Return price data for one day if len(dates)==1: date = dates[0] try: quote = quotes.loc[date] except KeyError: return {"message": Response.no_data_for_date(date)} return {"message": Response.historical_price( ticker, date, quote['Open'], quote['High'], quote['Low'], quote['Close'], int(quote['Volume']))} # If 2 dates are entered, returned the range during the given period elif len(dates)==2: dates = sorted(dates) start, end = dates[0], dates[1] quotes = quotes.loc[start:end] high = round(quotes['High'].max(),2) low = round(quotes['Low'].min(),2) return {"message": Response.historical_range(ticker, start, end, high, low)} else: return {"message": Response.too_many_dates(ticker)}