本文整理汇总了Python中response.Response.historical_price方法的典型用法代码示例。如果您正苦于以下问题:Python Response.historical_price方法的具体用法?Python Response.historical_price怎么用?Python Response.historical_price使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类response.Response
的用法示例。
在下文中一共展示了Response.historical_price方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: historical_data
# 需要导入模块: from response import Response [as 别名]
# 或者: from response.Response import historical_price [as 别名]
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)}