Files
OpenBB/bots/stocks/government/topbuys.py
Colin Delahunty fae93c67ad Refactor Bot (#1326)
* First commit

* Renamed to bot

* change from discordbot to bots imports

* refractor cmds

+ autocomplete helpers
+ updated dps plots/candle
+ quote cmd

* isort fix

* Update helpers.py

* Added first refactor

* Added commands and refactor

* Added description handler

* Through grains

* ta + image border helper

* Added indices

* merged

* Finished econ

* options refactor

* dark_pool_shorts, candle, quote done

* pagination/dropdown logic to helpers + fixes

* dd/disc/insider done + add image logic to helpers

* linting

* removed plt style

* Almost done with Gov

* fixing conflicts

* Finished gov (excpect ones mentioned)

* screener done

* Test bug refactor

* Store changes to switch

* Finished refactor

* Fixed error

* Delete ::40

* Fixed issue

* Fixed black

* finished

* fixes

Co-authored-by: teh_coderer <me@tehcoderer.com>
Co-authored-by: didierlopes.eth <dro.lopes@campus.fct.unl.pt>
2022-02-20 10:36:59 -05:00

112 lines
3.5 KiB
Python

from datetime import datetime, timedelta
import pandas as pd
from matplotlib import pyplot as plt
import bots.config_discordbot as cfg
from bots.config_discordbot import logger
from bots.helpers import image_border
from gamestonk_terminal.config_plot import PLOT_DPI
from gamestonk_terminal.helper_funcs import plot_autoscale
from gamestonk_terminal.stocks.government import quiverquant_model
def topbuys_command(
gov_type="",
past_transactions_months: int = 5,
num: int = 20,
raw: bool = False,
):
"""Displays most purchased stocks by the congress/senate/house [quiverquant.com]"""
# Debug user input
if cfg.DEBUG:
logger.debug(
"gov-topbuys %s %s %s %s",
gov_type,
past_transactions_months,
num,
raw,
)
possible_args = ["congress", "senate", "house"]
if gov_type == "":
gov_type = "congress"
elif gov_type not in possible_args:
raise Exception(
"Enter a valid government argument, options are: congress, senate and house"
)
# Retrieve Data
df_gov = quiverquant_model.get_government_trading(gov_type)
if df_gov.empty:
logger.debug("No %s trading data found", gov_type)
raise Exception("No trading data found")
df_gov = df_gov.sort_values("TransactionDate", ascending=False)
start_date = datetime.now() - timedelta(days=past_transactions_months * 30)
df_gov["TransactionDate"] = pd.to_datetime(df_gov["TransactionDate"])
df_gov = df_gov[df_gov["TransactionDate"] > start_date].dropna()
# Catch bug where error shown for purchase of >5,000,000
df_gov["Range"] = df_gov["Range"].apply(
lambda x: "$5,000,001-$5,000,001" if x == ">$5,000,000" else x
)
df_gov["min"] = df_gov["Range"].apply(
lambda x: x.split("-")[0].strip("$").replace(",", "").strip()
)
df_gov["max"] = df_gov["Range"].apply(
lambda x: x.split("-")[1].replace(",", "").strip().strip("$")
if "-" in x
else x.strip("$").replace(",", "")
)
df_gov["lower"] = df_gov[["min", "max", "Transaction"]].apply(
lambda x: float(x["min"])
if x["Transaction"] == "Purchase"
else -float(x["max"]),
axis=1,
)
df_gov["upper"] = df_gov[["min", "max", "Transaction"]].apply(
lambda x: float(x["max"])
if x["Transaction"] == "Purchase"
else -float(x["min"]),
axis=1,
)
description = None
df_gov = df_gov.sort_values("TransactionDate", ascending=True)
if raw:
df = pd.DataFrame(
df_gov.groupby("Ticker")["upper"]
.sum()
.div(1000)
.sort_values(ascending=False)
.head(n=num)
)
description = "```" + df.to_string() + "```"
fig, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
df_gov.groupby("Ticker")["upper"].sum().div(1000).sort_values(ascending=False).head(
n=num
).plot(kind="bar", rot=0, ax=ax)
ax.set_ylabel("Amount [1k $]")
ax.set_title(
f"Top {num} purchased stocks over last {past_transactions_months} "
f"months (upper bound) for {gov_type.upper()}"
)
plt.gcf().axes[0].yaxis.get_major_formatter().set_scientific(False)
fig.tight_layout()
plt.savefig("gov_topbuys.png")
imagefile = "gov_topbuys.png"
imagefile = image_border(imagefile)
return {
"title": f"Stocks: [quiverquant.com] Top purchases for {gov_type.upper()}",
"imagefile": imagefile,
"description": description,
}