stock-tracker-discord/yfi.py
2021-02-18 02:37:15 +00:00

28 lines
751 B
Python

import yfinance as yf
def stock_exists(ticker):
try:
yf.Ticker(ticker).info
return True
except KeyError:
return False
def get_current_price(ticker):
yf_obj = yf.Ticker(ticker)
todays_data = yf_obj.history(period='1d')
return round(todays_data['Close'][0], 2)
def get_delta(ticker, return_old_price=False):
yf_obj = yf.Ticker(ticker)
old_price = yf_obj.history(interval='1d')['Close'][-2]
current_price = get_current_price(ticker)
delta = 100 * ( (current_price / old_price) - 1 )
if return_old_price:
return delta, old_price
else:
return delta
def get_delta_old_price(ticker):
yf_obj = yf.Ticker(ticker)
return yf_obj.history(interval='1d')['Close'][-2]