Skip to content

Commit

Permalink
Gov discord (OpenBB-finance#1093)
Browse files Browse the repository at this point in the history
* add documentation to gov_menu

* improve lasttrades

* improve last contracts

* improve qtrcontracts cmd
  • Loading branch information
DidierRLopes authored Dec 23, 2021
1 parent 17c9b01 commit f88d592
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 78 deletions.
61 changes: 50 additions & 11 deletions discordbot/stocks/government/gov_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ class GovernmentCommands(discord.ext.commands.Cog):

def __init__(self, bot: discord.ext.commands.Bot):
self.bot = bot
self.bot.help_command.cog = self

@discord.ext.commands.command(name="stocks.gov.lasttrades")
def cog_unload(self):
self.bot.help_command = None

@discord.ext.commands.command(
name="stocks.gov.lasttrades",
usage="[gov_type] [past_transactions_days] [representative]",
)
async def lasttrades(
self,
ctx: discord.ext.commands.Context,
Expand All @@ -43,7 +50,10 @@ async def lasttrades(
"""
await lasttrades_command(ctx, gov_type, past_transactions_days, representative)

@discord.ext.commands.command(name="stocks.gov.topbuys")
@discord.ext.commands.command(
name="stocks.gov.topbuys",
usage="[gov_type] [past_transactions_months] [num] [raw]",
)
async def topbuys(
self,
ctx: discord.ext.commands.Context,
Expand All @@ -67,7 +77,10 @@ async def topbuys(
"""
await topbuys_command(ctx, gov_type, past_transactions_months, num, raw)

@discord.ext.commands.command(name="stocks.gov.topsells")
@discord.ext.commands.command(
name="stocks.gov.topsells",
usage="[gov_type] [past_transactions_months] [num] [raw]",
)
async def topsells(
self,
ctx: discord.ext.commands.Context,
Expand All @@ -91,7 +104,10 @@ async def topsells(
"""
await topsells_command(ctx, gov_type, past_transactions_months, num, raw)

@discord.ext.commands.command(name="stocks.gov.lastcontracts")
@discord.ext.commands.command(
name="stocks.gov.lastcontracts",
usage="[past_transactions_days] [num]",
)
async def lastcontracts(
self, ctx: discord.ext.commands.Context, past_transactions_days="", num=""
):
Expand All @@ -106,7 +122,10 @@ async def lastcontracts(
"""
await lastcontracts_command(ctx, past_transactions_days, num)

@discord.ext.commands.command(name="stocks.gov.qtrcontracts")
@discord.ext.commands.command(
name="stocks.gov.qtrcontracts",
usage="[analysis] [num]",
)
async def qtrcontracts(
self, ctx: discord.ext.commands.Context, num="", analysis=""
):
Expand All @@ -121,7 +140,10 @@ async def qtrcontracts(
"""
await qtrcontracts_command(ctx, num, analysis)

@discord.ext.commands.command(name="stocks.gov.toplobbying")
@discord.ext.commands.command(
name="stocks.gov.toplobbying",
usage="[num] [raw]",
)
async def toplobbying(self, ctx: discord.ext.commands.Context, num="", raw=""):
"""Displays top lobbying firms [quiverquant.com]
Expand All @@ -134,7 +156,10 @@ async def toplobbying(self, ctx: discord.ext.commands.Context, num="", raw=""):
"""
await toplobbying_command(ctx, num, raw)

@discord.ext.commands.command(name="stocks.gov.gtrades")
@discord.ext.commands.command(
name="stocks.gov.gtrades",
usage="[ticker] [gov_type] [past_transactions_months] [raw]",
)
async def gtrades(
self,
ctx: discord.ext.commands.Context,
Expand All @@ -158,7 +183,10 @@ async def gtrades(
"""
await gtrades_command(ctx, ticker, gov_type, past_transactions_months, raw)

@discord.ext.commands.command(name="stocks.gov.contracts")
@discord.ext.commands.command(
name="stocks.gov.contracts",
usage="[ticker] [past_transaction_days] [raw]",
)
async def contracts(
self,
ctx: discord.ext.commands.Context,
Expand All @@ -179,7 +207,10 @@ async def contracts(
"""
await contracts_command(ctx, ticker, past_transaction_days, raw)

@discord.ext.commands.command(name="stocks.gov.histcont")
@discord.ext.commands.command(
name="stocks.gov.histcont",
usage="[ticker]",
)
async def histcont(self, ctx: discord.ext.commands.Context, ticker=""):
"""Displays historical quarterly-contracts [quiverquant.com]
Expand All @@ -190,7 +221,10 @@ async def histcont(self, ctx: discord.ext.commands.Context, ticker=""):
"""
await histcont_command(ctx, ticker)

@discord.ext.commands.command(name="stocks.gov.lobbying")
@discord.ext.commands.command(
name="stocks.gov.lobbying",
usage="[ticker] [num]",
)
async def lobbying(self, ctx: discord.ext.commands.Context, ticker="", num=""):
"""Displays lobbying details [quiverquant.com]
Expand All @@ -203,11 +237,16 @@ async def lobbying(self, ctx: discord.ext.commands.Context, ticker="", num=""):
"""
await lobbying_command(ctx, ticker, num)

@discord.ext.commands.command(
name="stocks.gov",
usage="[ticker]",
)
# pylint: disable=too-many-branches
@discord.ext.commands.command(name="stocks.gov")
async def government_menu(self, ctx: discord.ext.commands.Context, ticker=""):
"""Stocks Context - Shows Government Menu
Run `!help GovernmentCommands` to see the list of available commands.
Returns
-------
Sends a message to the discord user with the commands from the gov context.
Expand Down
57 changes: 28 additions & 29 deletions discordbot/stocks/government/lastcontracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async def lastcontracts_command(ctx, past_transactions_days="", num=""):
df_contracts.sort_values("Date", ascending=False)

df_contracts["Date"] = pd.to_datetime(df_contracts["Date"])
df_contracts["Date"] = df_contracts["Date"].dt.date

df_contracts.drop_duplicates(inplace=True)
df_contracts = df_contracts[
Expand All @@ -44,42 +45,40 @@ async def lastcontracts_command(ctx, past_transactions_days="", num=""):
)
]

df_contracts = df_contracts[
["Date", "Ticker", "Amount", "Description", "Agency"]
][:num]
df_contracts_str = df_contracts.to_string()
if len(df_contracts_str) <= 4000:
embed = discord.Embed(
df_contracts = df_contracts[["Date", "Ticker", "Amount", "Agency"]][:num]

initial_str = "Page 0: Overview"
i = 1
for col_name in df_contracts["Ticker"].values:
initial_str += f"\nPage {i}: {col_name}"
i += 1

columns = []
df_contracts = df_contracts.T
columns.append(
discord.Embed(
title="Stocks: [quiverquant.com] Top buy government trading",
description="```" + df_contracts_str + "```",
description=initial_str,
colour=cfg.COLOR,
)
embed.set_author(
).set_author(
name=cfg.AUTHOR_NAME,
icon_url=cfg.AUTHOR_ICON_URL,
)
await ctx.send(embed=embed)
else:
i = 0
str_start = 0
str_end = 4000
columns = []
while i <= len(df_contracts_str) / 4000:
columns.append(
discord.Embed(
title="Stocks: [quiverquant.com] Top buy government trading",
description="```" + df_contracts_str[str_start:str_end] + "```",
colour=cfg.COLOR,
).set_author(
name=cfg.AUTHOR_NAME,
icon_url=cfg.AUTHOR_ICON_URL,
)
)
for column in df_contracts.columns.values:
columns.append(
discord.Embed(
description="```"
+ df_contracts[column].fillna("").to_string()
+ "```",
colour=cfg.COLOR,
).set_author(
name=cfg.AUTHOR_NAME,
icon_url=cfg.AUTHOR_ICON_URL,
)
str_end = str_start
str_start += 4000
i += 1
)

await pagination(columns, ctx)
await pagination(columns, ctx)

except Exception as e:
embed = discord.Embed(
Expand Down
94 changes: 58 additions & 36 deletions discordbot/stocks/government/lasttrades.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,51 +85,73 @@ async def lasttrades_command(
f"{', '.join(df_gov['Representative'].str.split().str[0].unique())}"
)

embed = discord.Embed(
title=f"Stocks: [quiverquant.com] Trades by {representative}",
description="```" + df_gov_rep.to_string(index=False) + "```",
colour=cfg.COLOR,
)
embed.set_author(
name=cfg.AUTHOR_NAME,
icon_url=cfg.AUTHOR_ICON_URL,
initial_str = "Page 0: Overview"
i = 1
for col_name in df_gov_rep["Ticker"].values:
initial_str += f"\nPage {i}: {col_name}"
i += 1

columns = []
df_gov_rep = df_gov_rep.T
columns.append(
discord.Embed(
title=f"Stocks: [quiverquant.com] Trades by {representative}",
description=initial_str,
colour=cfg.COLOR,
).set_author(
name=cfg.AUTHOR_NAME,
icon_url=cfg.AUTHOR_ICON_URL,
)
)
for column in df_gov_rep.columns.values:
columns.append(
discord.Embed(
description="```"
+ df_gov_rep[column].fillna("").to_string()
+ "```",
colour=cfg.COLOR,
).set_author(
name=cfg.AUTHOR_NAME,
icon_url=cfg.AUTHOR_ICON_URL,
)
)

await pagination(columns, ctx)

await ctx.send(embed=embed)
else:
df_gov_str = df_gov.to_string(index=False)
if len(df_gov_str) <= 4000:
embed = discord.Embed(
title=f"Stocks: [quiverquant.com] Last transactions for {gov_type.upper()}",
description="```" + df_gov_str + "```",
initial_str = "Page 0: Overview"
i = 1
for col_name in df_gov["Ticker"].values:
initial_str += f"\nPage {i}: {col_name}"
i += 1

columns = []
df_gov = df_gov.T
columns.append(
discord.Embed(
title=f"Stocks: [quiverquant.com] Trades for {gov_type.upper()}",
description=initial_str,
colour=cfg.COLOR,
)
embed.set_author(
).set_author(
name=cfg.AUTHOR_NAME,
icon_url=cfg.AUTHOR_ICON_URL,
)
await ctx.send(embed=embed)
else:
i = 0
str_start = 0
str_end = 4000
columns = []
while i <= len(gov_type) / 4000:
columns.append(
discord.Embed(
title=f"Stocks: [quiverquant.com] Last transactions for {gov_type.upper()}",
description="```" + gov_type[str_start:str_end] + "```",
colour=cfg.COLOR,
).set_author(
name=cfg.AUTHOR_NAME,
icon_url=cfg.AUTHOR_ICON_URL,
)
)

for column in df_gov.columns.values:
columns.append(
discord.Embed(
description="```"
+ df_gov[column].fillna("").to_string()
+ "```",
colour=cfg.COLOR,
).set_author(
name=cfg.AUTHOR_NAME,
icon_url=cfg.AUTHOR_ICON_URL,
)
str_end = str_start
str_start += 4000
i += 1
)

await pagination(columns, ctx)
await pagination(columns, ctx)

except Exception as e:
embed = discord.Embed(
Expand Down
17 changes: 15 additions & 2 deletions discordbot/stocks/government/qtrcontracts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import discord
from matplotlib import pyplot as plt
from tabulate import tabulate
import numpy as np

from gamestonk_terminal.config_plot import PLOT_DPI
Expand Down Expand Up @@ -110,10 +111,22 @@ async def qtrcontracts_command(ctx, num="", analysis=""):
await ctx.send(embed=embed)

elif analysis == "total":
description = tickers.to_string()

tickers.index = [ind + " " * (7 - len(ind)) for ind in tickers.index]
tickers[:] = [str(round(val[0] / 1e9, 2)) for val in tickers.values]
tickers.columns = ["Amount [M]"]

tickers_str = tabulate(
tickers,
headers=tickers.columns,
showindex=True,
numalign="right",
stralign="center",
)

embed = discord.Embed(
title="Stocks: [quiverquant.com] Government contracts",
description=description,
description=tickers_str,
colour=cfg.COLOR,
)
embed.set_author(
Expand Down

0 comments on commit f88d592

Please sign in to comment.