当前位置: 首页>>代码示例>>Python>>正文


Python Response.invalid_date方法代码示例

本文整理汇总了Python中response.Response.invalid_date方法的典型用法代码示例。如果您正苦于以下问题:Python Response.invalid_date方法的具体用法?Python Response.invalid_date怎么用?Python Response.invalid_date使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在response.Response的用法示例。


在下文中一共展示了Response.invalid_date方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: range_volatility

# 需要导入模块: from response import Response [as 别名]
# 或者: from response.Response import invalid_date [as 别名]
def range_volatility(ticker, components): 
	# Parse and check: find components matching dates, ensure start and end are present, ensure dates are valid
	today, dates = current_date(), []
	for each in components: 
		if PATTERNS['valid_date'].match(each):
			dates.append(each)
	if len(dates)!=2:
		return {"message": Response.vol_required_dates(ticker)}
	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)}

	# Volatility Calculation
	dates = sorted(dates)
	start, end = dates[0], dates[1]
	try:
		quotes = data.DataReader(ticker, 'google')['Close'].loc[start:end]
		if len(quotes) < 10: 
			return {"message": Response.vol_range_size(ticker)}
	except Exception:
		return {"message": Response.data_notfound(ticker)}
	logreturns = np.log(quotes / quotes.shift(1))
	vol = round(np.sqrt(252*logreturns.var()), 5)
	return {"message" : Response.range_vol(ticker, start, end, vol)}
开发者ID:davelindo,项目名称:finbot,代码行数:30,代码来源:api.py

示例2: get_fred

# 需要导入模块: from response import Response [as 别名]
# 或者: from response.Response import invalid_date [as 别名]
def get_fred(symbol, components): 
	symbol = symbol.upper()
	try: 
		df = data.get_data_fred(symbol)
	except Exception: 
		return {"message":Response.fred_notfound(symbol)}

	today, dates = current_date(), []
	for each in components: 
		if PATTERNS['valid_date'].match(each):
			dates.append(each)
	# No dates: get most recent value
	if not dates: 
		df.dropna()
		last_value = df.tail(1)[symbol][0]
		last_value = ('%.3f' % last_value)
		return {"message": Response.basic_fred(symbol, last_value)}

	# Clean dates
	if len(dates)>2: 
		return {"message": Response.too_many_dates(symbol)}
	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)}


	# Return price data for one day
	if len(dates)==1: 
		date = dates[0]
		ts = pd.DatetimeIndex.asof(df.index, date)
		if pd.isnull(ts):
			return {"message": Response.fred_date_notfound(symbol, date)}
		value = df.loc[ts][symbol]
		if pd.isnull(value): 
			return {"message": Response.fred_date_notfound(symbol, date)}
		return {"message": Response.date_fred(symbol, date, value)}

	# If 2 dates are entered, returned the range during the given period
	else: 
		dates = sorted(dates)
		start, end = dates[0], dates[1]
		df = df.loc[start:end]
		high = ('%.3f' % df[symbol].max())
		low = ('%.3f' % df[symbol].min())
		return {"message": Response.fred_data_range(symbol, start, end, high, low)}
开发者ID:davelindo,项目名称:finbot,代码行数:51,代码来源:api.py

示例3: historical_data

# 需要导入模块: from response import Response [as 别名]
# 或者: from response.Response import invalid_date [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)}
开发者ID:davelindo,项目名称:finbot,代码行数:45,代码来源:api.py


注:本文中的response.Response.invalid_date方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。