Files
OpenBB/bots/stocks/government/qtrcontracts.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

102 lines
3.3 KiB
Python

import numpy as np
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 qtrcontracts_command(num: int = 20, analysis=""):
"""Displays a look at government contracts [quiverquant.com]"""
# Debug user input
if cfg.DEBUG:
logger.debug("gov-qtrcontracts %s %s", num, analysis)
possible_args = ["total", "upmom", "downmom"]
if analysis == "":
analysis = "total"
elif analysis not in possible_args:
raise Exception(
"Enter a valid analysis argument, options are: total, upmom and downmom"
)
# Retrieve Data
df_contracts = quiverquant_model.get_government_trading("quarter-contracts")
if df_contracts.empty:
raise Exception("No quarterly government contracts found")
tickers = quiverquant_model.analyze_qtr_contracts(analysis, num)
# Output Data
if analysis in {"upmom", "downmom"}:
description = tickers.to_string()
fig, ax = plt.subplots(figsize=plot_autoscale(), dpi=PLOT_DPI)
max_amount = 0
quarter_ticks = []
for symbol in tickers:
amounts = (
df_contracts[df_contracts["Ticker"] == symbol]
.sort_values(by=["Year", "Qtr"])["Amount"]
.values
)
qtr = (
df_contracts[df_contracts["Ticker"] == symbol]
.sort_values(by=["Year", "Qtr"])["Qtr"]
.values
)
year = (
df_contracts[df_contracts["Ticker"] == symbol]
.sort_values(by=["Year", "Qtr"])["Year"]
.values
)
ax.plot(np.arange(0, len(amounts)), amounts / 1_000_000, "-*", lw=2, ms=15)
if len(amounts) > max_amount:
max_amount = len(amounts)
quarter_ticks = [
f"{quarter[0]} - Q{quarter[1]} " for quarter in zip(year, qtr)
]
ax.set_xlim([-0.5, max_amount - 0.5])
ax.set_xticks(np.arange(0, max_amount))
ax.set_xticklabels(quarter_ticks)
ax.grid()
ax.legend(tickers)
titles = {
"upmom": "Highest increasing quarterly Government Contracts",
"downmom": "Highest decreasing quarterly Government Contracts",
}
ax.set_title(titles[analysis])
ax.set_xlabel("Date")
ax.set_ylabel("Amount ($1M)")
fig.tight_layout()
plt.savefig("gov_qtrcontracts.png")
imagefile = "gov_qtrcontracts.png"
imagefile = image_border(imagefile)
output = {
"title": "Stocks: [quiverquant.com] Government Contracts",
"imagefile": imagefile,
"description": description,
}
elif analysis == "total":
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]"]
output = {
"title": "Stocks: [quiverquant.com] Government Contracts",
"description": tickers.to_string(),
}
return output