- Made image the default command for watchlist and portfolio

access the text versions with watchlist_text, wlt, portfolio_text, pft
- python black formatting
This commit is contained in:
Kate 2021-03-06 20:07:20 +00:00
parent 8df8712e61
commit d49c2955e4

96
main.py
View file

@ -19,13 +19,16 @@ async def ping(ctx):
await ctx.send("pong!") await ctx.send("pong!")
@bot.command(aliases=['pf']) @bot.command(aliases=["pft"])
async def portfolio(ctx): async def portfolio_text(ctx):
user = str(ctx.message.author) user = str(ctx.message.author)
return await ctx.send(str( "```" + table.generate_table(database.get_stocks(user)) + "```" )) return await ctx.send(
str("```" + table.generate_table(database.get_stocks(user)) + "```")
)
@bot.command()
async def pf2(ctx): @bot.command(aliases=["pf"])
async def portfolio(ctx):
user = str(ctx.message.author) user = str(ctx.message.author)
portfolio = database.get_stocks(user) portfolio = database.get_stocks(user)
@ -36,33 +39,52 @@ async def pf2(ctx):
for stock in portfolio.keys(): for stock in portfolio.keys():
delta, old_price = yfi.get_delta(stock, return_old_price=True) delta, old_price = yfi.get_delta(stock, return_old_price=True)
current_price = yfi.get_current_price(stock) current_price = yfi.get_current_price(stock)
data.append([ data.append(
[
stock, stock,
Decimal(portfolio[stock]), Decimal(portfolio[stock]),
round(float(Decimal(portfolio[stock]) * Decimal(current_price)), 2), round(float(Decimal(portfolio[stock]) * Decimal(current_price)), 2),
str( round(delta, 2) ) + "%" str(round(delta, 2)) + "%",
]) ]
)
yesterday_portfolio_value[stock] = Decimal(portfolio[stock]) * Decimal(old_price) yesterday_portfolio_value[stock] = Decimal(portfolio[stock]) * Decimal(
current_portfolio_value[stock] = Decimal(portfolio[stock]) * Decimal(current_price) old_price
)
current_portfolio_value[stock] = Decimal(portfolio[stock]) * Decimal(
current_price
)
yesterday_portfolio_total_value = sum([yesterday_portfolio_value[stock] for stock in yesterday_portfolio_value]) yesterday_portfolio_total_value = sum(
current_portfolio_total_value = sum([current_portfolio_value[stock] for stock in current_portfolio_value]) [yesterday_portfolio_value[stock] for stock in yesterday_portfolio_value]
)
current_portfolio_total_value = sum(
[current_portfolio_value[stock] for stock in current_portfolio_value]
)
total_delta = 100 * ( (current_portfolio_total_value / yesterday_portfolio_total_value) - 1 ) total_delta = 100 * (
(current_portfolio_total_value / yesterday_portfolio_total_value) - 1
)
data.append(["Total", "", round( sum([x[2] for x in data]), 2) , str( round(total_delta, 2) ) + "%"]) data.append(
[
"Total",
"",
round(sum([x[2] for x in data]), 2),
str(round(total_delta, 2)) + "%",
]
)
image = t2.generate_table( image = t2.generate_table(
headers=["Ticker", "Shares", "Value ($)", "24h"], headers=["Ticker", "Shares", "Value ($)", "24h"],
data=data, data=data,
title = str(user + "'s Portfolio") title=str(user + "'s Portfolio"),
) )
await ctx.send(file=discord.File(image, "table.png"))
await ctx.send(file=discord.File(image, 'table.png'))
image.close() image.close()
@bot.command() @bot.command()
async def add(ctx, stock, amount): async def add(ctx, stock, amount):
user = str(ctx.message.author) user = str(ctx.message.author)
@ -74,25 +96,31 @@ async def add(ctx, stock, amount):
return return
if database.add_stock(user, stock, amount): if database.add_stock(user, stock, amount):
return await ctx.send(str( "```" + table.generate_table(database.get_stocks(user)) + "```" )) return await ctx.send(
str("```" + table.generate_table(database.get_stocks(user)) + "```")
)
else: else:
return await ctx.send("Stock **{0}** does not exist!".format(stock)) return await ctx.send("Stock **{0}** does not exist!".format(stock))
@bot.command(aliases=['del'])
@bot.command(aliases=["del"])
async def delete(ctx, stock): async def delete(ctx, stock):
stock = stock.upper() stock = stock.upper()
user = str(ctx.message.author) user = str(ctx.message.author)
return await ctx.send(str( "```" + table.generate_table(database.delete_stock(user, stock)) + "```" )) return await ctx.send(
str("```" + table.generate_table(database.delete_stock(user, stock)) + "```")
)
@bot.command(aliases=['wl']) @bot.command(aliases=["wlt"])
async def watchlist(ctx): async def watchlist_text(ctx):
user = str(ctx.message.author) user = str(ctx.message.author)
wlist = database.get_watchlist(user) wlist = database.get_watchlist(user)
return await ctx.send(str("```" + table.watchlist_table(wlist) + "```")) return await ctx.send(str("```" + table.watchlist_table(wlist) + "```"))
@bot.command(aliases=['w'])
@bot.command(aliases=["w"])
async def watch(ctx, stock, est_price: typing.Optional[int]): async def watch(ctx, stock, est_price: typing.Optional[int]):
if not est_price: if not est_price:
@ -113,7 +141,7 @@ async def watch(ctx, stock, est_price: typing.Optional[int]):
return await ctx.send("Stock **{0}** does not exist!".format(stock)) return await ctx.send("Stock **{0}** does not exist!".format(stock))
@bot.command(aliases=['uw']) @bot.command(aliases=["uw"])
async def unwatch(ctx, *stocks): async def unwatch(ctx, *stocks):
user = str(ctx.message.author) user = str(ctx.message.author)
stocks = [stock.upper() for stock in stocks] stocks = [stock.upper() for stock in stocks]
@ -122,8 +150,8 @@ async def unwatch(ctx, *stocks):
return await ctx.send("Updated watchlist") return await ctx.send("Updated watchlist")
@bot.command() @bot.command(aliases=["wl"])
async def wl2(ctx, user: typing.Optional[discord.Member]): async def watchlist(ctx, user: typing.Optional[discord.Member]):
if not user: if not user:
user = str(ctx.message.author) user = str(ctx.message.author)
@ -134,21 +162,25 @@ async def wl2(ctx, user: typing.Optional[discord.Member]):
watchlist = database.get_watchlist(user) watchlist = database.get_watchlist(user)
data = [] data = []
for stock in watchlist.keys(): for stock in watchlist.keys():
data.append([ data.append(
[
stock, stock,
yfi.get_current_price(stock), yfi.get_current_price(stock),
Decimal(watchlist[stock]), Decimal(watchlist[stock]),
str( round(yfi.get_delta(stock), 2) ) + "%" str(round(yfi.get_delta(stock), 2)) + "%",
]) ]
)
image = t2.generate_table( image = t2.generate_table(
headers=["Ticker", "Value ($)", "Est. Price ($)", "Delta"], headers=["Ticker", "Value ($)", "Est. Price ($)", "Delta"],
data=data, data=data,
title=str(user + "'s Watchlist"), title=str(user + "'s Watchlist"),
bg = 'lightgreen', border='limegreen' bg="lightgreen",
border="limegreen",
) )
await ctx.send(file=discord.File(image, 'table.png')) await ctx.send(file=discord.File(image, "table.png"))
image.close() image.close()
bot.run(config['apikey'])
bot.run(config["apikey"])