- 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:
parent
8df8712e61
commit
d49c2955e4
1 changed files with 81 additions and 49 deletions
96
main.py
96
main.py
|
@ -19,13 +19,16 @@ async def ping(ctx):
|
|||
await ctx.send("pong!")
|
||||
|
||||
|
||||
@bot.command(aliases=['pf'])
|
||||
async def portfolio(ctx):
|
||||
@bot.command(aliases=["pft"])
|
||||
async def portfolio_text(ctx):
|
||||
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)
|
||||
portfolio = database.get_stocks(user)
|
||||
|
||||
|
@ -36,33 +39,52 @@ async def pf2(ctx):
|
|||
for stock in portfolio.keys():
|
||||
delta, old_price = yfi.get_delta(stock, return_old_price=True)
|
||||
current_price = yfi.get_current_price(stock)
|
||||
data.append([
|
||||
data.append(
|
||||
[
|
||||
stock,
|
||||
Decimal(portfolio[stock]),
|
||||
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)
|
||||
current_portfolio_value[stock] = Decimal(portfolio[stock]) * Decimal(current_price)
|
||||
yesterday_portfolio_value[stock] = Decimal(portfolio[stock]) * Decimal(
|
||||
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])
|
||||
current_portfolio_total_value = sum([current_portfolio_value[stock] for stock in current_portfolio_value])
|
||||
yesterday_portfolio_total_value = sum(
|
||||
[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(
|
||||
headers=["Ticker", "Shares", "Value ($)", "24h"],
|
||||
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()
|
||||
|
||||
|
||||
@bot.command()
|
||||
async def add(ctx, stock, amount):
|
||||
user = str(ctx.message.author)
|
||||
|
@ -74,25 +96,31 @@ async def add(ctx, stock, amount):
|
|||
return
|
||||
|
||||
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:
|
||||
return await ctx.send("Stock **{0}** does not exist!".format(stock))
|
||||
|
||||
@bot.command(aliases=['del'])
|
||||
|
||||
@bot.command(aliases=["del"])
|
||||
async def delete(ctx, stock):
|
||||
stock = stock.upper()
|
||||
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'])
|
||||
async def watchlist(ctx):
|
||||
@bot.command(aliases=["wlt"])
|
||||
async def watchlist_text(ctx):
|
||||
user = str(ctx.message.author)
|
||||
|
||||
wlist = database.get_watchlist(user)
|
||||
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]):
|
||||
|
||||
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))
|
||||
|
||||
|
||||
@bot.command(aliases=['uw'])
|
||||
@bot.command(aliases=["uw"])
|
||||
async def unwatch(ctx, *stocks):
|
||||
user = str(ctx.message.author)
|
||||
stocks = [stock.upper() for stock in stocks]
|
||||
|
@ -122,8 +150,8 @@ async def unwatch(ctx, *stocks):
|
|||
return await ctx.send("Updated watchlist")
|
||||
|
||||
|
||||
@bot.command()
|
||||
async def wl2(ctx, user: typing.Optional[discord.Member]):
|
||||
@bot.command(aliases=["wl"])
|
||||
async def watchlist(ctx, user: typing.Optional[discord.Member]):
|
||||
|
||||
if not user:
|
||||
user = str(ctx.message.author)
|
||||
|
@ -134,21 +162,25 @@ async def wl2(ctx, user: typing.Optional[discord.Member]):
|
|||
watchlist = database.get_watchlist(user)
|
||||
data = []
|
||||
for stock in watchlist.keys():
|
||||
data.append([
|
||||
data.append(
|
||||
[
|
||||
stock,
|
||||
yfi.get_current_price(stock),
|
||||
Decimal(watchlist[stock]),
|
||||
str( round(yfi.get_delta(stock), 2) ) + "%"
|
||||
])
|
||||
str(round(yfi.get_delta(stock), 2)) + "%",
|
||||
]
|
||||
)
|
||||
|
||||
image = t2.generate_table(
|
||||
headers=["Ticker", "Value ($)", "Est. Price ($)", "Delta"],
|
||||
data=data,
|
||||
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()
|
||||
|
||||
bot.run(config['apikey'])
|
||||
|
||||
bot.run(config["apikey"])
|
||||
|
|
Loading…
Add table
Reference in a new issue