Added ticker command

This commit is contained in:
Kate 2021-03-17 01:45:24 +00:00
parent cd0e4111bc
commit 46838c888c
2 changed files with 25 additions and 2 deletions

17
main.py
View file

@ -25,6 +25,23 @@ async def ping(ctx):
await ctx.send("pong!")
@bot.command()
async def ticker(ctx, stock):
user = str(ctx.message.author)
stock = stock.upper()
data = yfi.stock_info(stock)
return await ctx.send(
"""**{ticker}**
Bid: {bid}
Ask: {ask}
""".format(
ticker=stock, bid=data["bid"], ask=data["ask"]
)
)
@bot.command(aliases=["pft"])
async def portfolio_text(ctx):
user = str(ctx.message.author)

10
yfi.py
View file

@ -3,12 +3,18 @@ import yfinance as yf
def stock_exists(ticker):
try:
yf.Ticker(ticker).info
return True
return yf.Ticker(ticker).info
except KeyError:
return False
def stock_info(ticker):
if not (stock := stock_exists(ticker)):
return False
return stock
def get_current_price(ticker):
yf_obj = yf.Ticker(ticker)
todays_data = yf_obj.history(period="1d")