Ejemplo n.º 1
0
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)}
Ejemplo n.º 2
0
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)}
Ejemplo n.º 3
0
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)}