From fc777d2b02130cef92ec21061cd4a29177b9cb2d Mon Sep 17 00:00:00 2001 From: DidierRLopes Date: Thu, 3 Jun 2021 22:24:24 +0100 Subject: [PATCH] Add corporate lobbying data to government menu (#486) * Add corporate lobbying data to government menu * Update README.md * Update quiverquant_view.py --- gamestonk_terminal/government/README.md | 26 ++++ .../government/gov_controller.py | 16 ++- .../government/quiverquant_model.py | 8 +- .../government/quiverquant_view.py | 134 +++++++++++++++++- 4 files changed, 177 insertions(+), 7 deletions(-) diff --git a/gamestonk_terminal/government/README.md b/gamestonk_terminal/government/README.md index 129a9e22cee..fd0f83fc9d3 100644 --- a/gamestonk_terminal/government/README.md +++ b/gamestonk_terminal/government/README.md @@ -27,6 +27,8 @@ This data has been provided by [quiverquant](https://www.quiverquant.com). * plot sum of last government contracts * [qtr_contracts](#qtr_contracts) * quarterly government contracts best regression +* [top_lobbying](#top_lobbying) + * top corporate lobbying tickers #### WITH TICKER PROVIDED @@ -48,6 +50,8 @@ This data has been provided by [quiverquant](https://www.quiverquant.com). * plot sum of contracts on the ticker * [qtr_contracts_hist](#qtr_contracts_hist) * quarterly government contracts historical +* [lobbying](#lobbying) + * corporate lobbying details ## last_congress ```text @@ -187,6 +191,17 @@ Quarterly government contracts best regression. [Source: www.quiverquant.com] ![top_promising_stocks](https://user-images.githubusercontent.com/25267873/120394857-f963a800-c32b-11eb-9d3f-2295f7216b67.png) +## top_lobbying +```text +usage: top_lobbying [-t TOP] +``` +Top lobbying spent. [Source: www.quiverquant.com] + +* -t : Top corporate lobbying tickers with biggest amounts. Default 10. + +![top_lobbying](https://user-images.githubusercontent.com/25267873/120707733-9e0ff200-c4b2-11eb-9430-552d92f3fc74.png) + + #### WITH TICKER PROVIDED ## raw_congress @@ -285,3 +300,14 @@ Quarterly government contracts historical. [Source: www.quiverquant.com] ![qtr_contracts_hist](https://user-images.githubusercontent.com/25267873/120394929-113b2c00-c32c-11eb-8241-78d5d1328386.png) + +## lobbying +```text +usage: lobbying [-l LAST] +``` +Lobbying details. [Source: www.quiverquant.com] + +* -l : Last corporate lobbying details. Default: 10. + +Captura de ecrã 2021-06-03, às 21 25 02 + diff --git a/gamestonk_terminal/government/gov_controller.py b/gamestonk_terminal/government/gov_controller.py index c90613a5f28..08071c80a1c 100644 --- a/gamestonk_terminal/government/gov_controller.py +++ b/gamestonk_terminal/government/gov_controller.py @@ -40,6 +40,8 @@ class GovController: "contracts", "qtr_contracts", "qtr_contracts_hist", + "top_lobbying", + "lobbying", ] def __init__( @@ -75,6 +77,7 @@ class GovController: print(" last_contracts last government contracts") print(" sum_contracts plot sum of last government contracts") print(" qtr_contracts quarterly government contracts best regression") + print(" top_lobbying top corporate lobbying tickers") print("") if self.ticker: print(f"Ticker: {self.ticker}") @@ -87,6 +90,7 @@ class GovController: print(" raw_contracts raw contracts on the ticker") print(" contracts plot sum of contracts on the ticker") print(" qtr_contracts_hist quarterly government contracts historical") + print(" lobbying corporate lobbying details") print("") return @@ -196,11 +200,19 @@ class GovController: def call_qtr_contracts(self, other_args: List[str]): """Process qtr_contracts command""" - quiverquant_view.call_qtr_contracts(other_args) + quiverquant_view.qtr_contracts(other_args) def call_qtr_contracts_hist(self, other_args: List[str]): """Process qtr_contracts_hist command""" - quiverquant_view.call_qtr_contracts_hist(other_args, self.ticker) + quiverquant_view.qtr_contracts_hist(other_args, self.ticker) + + def call_top_lobbying(self, other_args: List[str]): + """Process top_lobbying command""" + quiverquant_view.top_lobbying(other_args) + + def call_lobbying(self, other_args: List[str]): + """Process lobbying command""" + quiverquant_view.lobbying(other_args, self.ticker) def menu(ticker: str): diff --git a/gamestonk_terminal/government/quiverquant_model.py b/gamestonk_terminal/government/quiverquant_model.py index 9fedb5cb801..60cc1703fb1 100644 --- a/gamestonk_terminal/government/quiverquant_model.py +++ b/gamestonk_terminal/government/quiverquant_model.py @@ -13,7 +13,7 @@ def get_government_trading(gov_type: str, ticker: str = "") -> pd.DataFrame: Parameters ---------- gov_type: str - Type of government data between: Congress, Senate, House, Contracts and Quarter-Contracts + Type of government data between: Congress, Senate, House, Contracts, Quarter-Contracts and Corporate-Lobbying ticker : str Ticker to get congress trading data from @@ -57,6 +57,12 @@ def get_government_trading(gov_type: str, ticker: str = "") -> pd.DataFrame: else: url = "https://api.quiverquant.com/beta/live/govcontracts" + elif gov_type == "corporate-lobbying": + if ticker: + url = f"https://api.quiverquant.com/beta/historical/lobbying/{ticker}" + else: + url = "https://api.quiverquant.com/beta/live/lobbying" + else: return pd.DataFrame() diff --git a/gamestonk_terminal/government/quiverquant_view.py b/gamestonk_terminal/government/quiverquant_view.py index 03ceade274c..501be42529b 100644 --- a/gamestonk_terminal/government/quiverquant_view.py +++ b/gamestonk_terminal/government/quiverquant_view.py @@ -15,6 +15,8 @@ from gamestonk_terminal.helper_funcs import ( from gamestonk_terminal.config_plot import PLOT_DPI from gamestonk_terminal import feature_flags as gtff +# pylint: disable=C0302 + def last_government(other_args: List[str], gov_type: str): """Last government trading @@ -765,7 +767,7 @@ def contracts(other_args: List[str], ticker: str): print(e, "\n") -def call_qtr_contracts(other_args: List[str]): +def qtr_contracts(other_args: List[str]): """Quarter contracts Parameters @@ -859,7 +861,6 @@ def call_qtr_contracts(other_args: List[str]): plt.title("Quarterly Government Contracts - Top promising stocks") plt.xlabel("Date") plt.ylabel("Amount [1k $]") - plt.show() if gtff.USE_ION: plt.ion() @@ -871,7 +872,7 @@ def call_qtr_contracts(other_args: List[str]): print(e, "\n") -def call_qtr_contracts_hist(other_args: List[str], ticker: str): +def qtr_contracts_hist(other_args: List[str], ticker: str): """Quarter contracts Parameters @@ -918,7 +919,6 @@ def call_qtr_contracts_hist(other_args: List[str], ticker: str): plt.title(f"Quarterly Government Contracts Historical on {ticker.upper()}") plt.xlabel("Date") plt.ylabel("Amount [1k $]") - plt.show() if gtff.USE_ION: plt.ion() @@ -928,3 +928,129 @@ def call_qtr_contracts_hist(other_args: List[str], ticker: str): except Exception as e: print(e, "\n") + + +def top_lobbying(other_args: List[str]): + """Top lobbying based on tickers that have biggest amounts for the past couple months + + Parameters + ---------- + other_args : List[str] + Command line arguments to be processed with argparse + """ + parser = argparse.ArgumentParser( + add_help=False, + prog="top_lobbying", + description="Top lobbying. [Source: www.quiverquant.com]", + ) + parser.add_argument( + "-t", + "--top", + action="store", + dest="top", + type=check_positive, + default=10, + help="Top corporate lobbying tickers with biggest amounts", + ) + + try: + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + df_lobbying = quiverquant_model.get_government_trading("corporate-lobbying") + + if df_lobbying.empty: + print("No corporate lobbying found\n") + return + + d_lobbying = {} + for symbol in df_lobbying["Ticker"].unique(): + d_lobbying[symbol] = sum( + float(amount) + for amount in df_lobbying[df_lobbying["Ticker"] == symbol] + .replace(np.nan, 0)["Amount"] + .values + ) + + df_amount = pd.DataFrame.from_dict( + d_lobbying, orient="index", columns=["Amount"] + ).sort_values(by=["Amount"], ascending=False) + + plt.figure(figsize=plot_autoscale(), dpi=PLOT_DPI) + + plt.bar( + df_amount.head(ns_parser.top).index, + df_amount.head(ns_parser.top).values.flatten() / 1000, + ) + plt.xlabel("Ticker") + plt.ylabel("Sum Amount [1k $]") + plt.title( + f"Total amount spent on corporate lobbying since {df_lobbying['Date'].min()}" + ) + + if gtff.USE_ION: + plt.ion() + + plt.show() + print("") + + except Exception as e: + print(e, "\n") + + +def lobbying(other_args: List[str], ticker: str): + """Corporate lobbying details + + Parameters + ---------- + other_args : List[str] + Command line arguments to be processed with argparse + ticker: str + Ticker to get corporate lobbying data from + """ + parser = argparse.ArgumentParser( + add_help=False, + prog="lobbying", + description="Lobbying details [Source: www.quiverquant.com]", + ) + parser.add_argument( + "-l", + "--last", + action="store", + dest="last", + type=check_positive, + default=10, + help="Last corporate lobbying details", + ) + try: + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + df_lobbying = quiverquant_model.get_government_trading( + "corporate-lobbying", ticker=ticker + ) + + if df_lobbying.empty: + print("No corporate lobbying found\n") + return + + for _, row in ( + df_lobbying.sort_values(by=["Date"], ascending=False) + .head(ns_parser.last) + .iterrows() + ): + amount = ( + "$" + str(int(float(row["Amount"]))) + if row["Amount"] is not None + else "N/A" + ) + print(f"{row['Date']}: {row['Client']} {amount}") + if row["Amount"] is not None: + print("\t" + row["Specific_Issue"].replace("\n", " ").replace("\r", "")) + print("") + print("") + + except Exception as e: + print(e, "\n")