From 57ced848437a81e15c96c0ae0bf76df01a2a21ef Mon Sep 17 00:00:00 2001 From: didier Date: Sun, 16 May 2021 13:55:55 +0100 Subject: [PATCH] add command to get raw gov trading --- .../government/gov_controller.py | 29 +++- .../government/quiverquant_view.py | 128 +++++++++++++----- 2 files changed, 120 insertions(+), 37 deletions(-) diff --git a/gamestonk_terminal/government/gov_controller.py b/gamestonk_terminal/government/gov_controller.py index 9e0e8c0bf35..5702327a43f 100644 --- a/gamestonk_terminal/government/gov_controller.py +++ b/gamestonk_terminal/government/gov_controller.py @@ -4,10 +4,10 @@ __docformat__ = "numpy" import argparse from typing import List from matplotlib import pyplot as plt +from prompt_toolkit.completion import NestedCompleter from gamestonk_terminal import feature_flags as gtff from gamestonk_terminal.helper_funcs import get_flair from gamestonk_terminal.menu import session -from prompt_toolkit.completion import NestedCompleter from gamestonk_terminal.government import quiverquant_view @@ -23,14 +23,17 @@ class GovController: "buy_congress", "sell_congress", "congress", + "raw_congress", "last_senate", "buy_senate", "sell_senate", "senate", + "raw_senate", "last_house", "buy_house", "sell_house", "house", + "raw_house", ] def __init__( @@ -45,7 +48,6 @@ class GovController: choices=self.CHOICES, ) - @staticmethod def print_help(self): """Print help""" @@ -67,8 +69,11 @@ class GovController: print("") if self.ticker: print(f"Ticker: {self.ticker}") + print(" raw_congress raw congress trades on the ticker") print(" congress congress trades on the ticker") + print(" raw_senate raw senate trades on the ticker") print(" senate senate trades on the ticker") + print(" raw_house raw house trades on the ticker") print(" house house trades on the ticker") print("") return @@ -91,7 +96,7 @@ class GovController: def call_help(self, _): """Process Help command""" - self.print_help(self) + self.print_help() def call_q(self, _): """Process Q command - quit the menu""" @@ -113,9 +118,13 @@ class GovController: """Process sell_congress command""" quiverquant_view.sell_government(other_args, "congress") + def call_raw_congress(self, other_args: List[str]): + """Process raw_congress command""" + quiverquant_view.raw_government(other_args, self.ticker, "congress") + def call_congress(self, other_args: List[str]): """Process congress command""" - quiverquant_view.government(other_args, self.ticker, "congress") + quiverquant_view.government_trading(other_args, self.ticker, "congress") def call_last_senate(self, other_args: List[str]): """Process last_senate command""" @@ -129,9 +138,13 @@ class GovController: """Process sell_senate command""" quiverquant_view.sell_government(other_args, "senate") + def call_raw_senate(self, other_args: List[str]): + """Process raw_senate command""" + quiverquant_view.raw_government(other_args, self.ticker, "senate") + def call_senate(self, other_args: List[str]): """Process senate command""" - quiverquant_view.government(other_args, self.ticker, "senate") + quiverquant_view.government_trading(other_args, self.ticker, "senate") def call_last_house(self, other_args: List[str]): """Process last_house command""" @@ -145,9 +158,13 @@ class GovController: """Process sell_house command""" quiverquant_view.sell_government(other_args, "house") + def call_raw_house(self, other_args: List[str]): + """Process raw_house command""" + quiverquant_view.raw_government(other_args, self.ticker, "house") + def call_house(self, other_args: List[str]): """Process house command""" - quiverquant_view.government(other_args, self.ticker, "house") + quiverquant_view.government_trading(other_args, self.ticker, "house") def menu(ticker: str): diff --git a/gamestonk_terminal/government/quiverquant_view.py b/gamestonk_terminal/government/quiverquant_view.py index 54c2a7f1aef..7ffccbbd2c8 100644 --- a/gamestonk_terminal/government/quiverquant_view.py +++ b/gamestonk_terminal/government/quiverquant_view.py @@ -1,10 +1,10 @@ import argparse from typing import List -import pandas as pd from datetime import datetime, timedelta -from gamestonk_terminal.government import quiverquant_model +import pandas as pd from matplotlib import pyplot as plt import matplotlib.dates as mdates +from gamestonk_terminal.government import quiverquant_model from gamestonk_terminal.helper_funcs import ( parse_known_args_and_warn, check_positive, @@ -14,20 +14,20 @@ from gamestonk_terminal.config_plot import PLOT_DPI from gamestonk_terminal import feature_flags as gtff -def last_government(other_args: List[str], type: str): +def last_government(other_args: List[str], gov_type: str): """Last government trading Parameters ---------- other_args : List[str] Command line arguments to be processed with argparse - type: str + gov_type: str Type of government data between: congress, senate and house """ parser = argparse.ArgumentParser( add_help=False, - prog="last_" + type, - description=f"Last {type} trading. [Source: www.quiverquant.com]", + prog="last_" + gov_type, + description=f"Last {gov_type} trading. [Source: www.quiverquant.com]", ) parser.add_argument( "-p", @@ -57,10 +57,10 @@ def last_government(other_args: List[str], type: str): if not ns_parser: return - df_gov = quiverquant_model.get_government_trading(type) + df_gov = quiverquant_model.get_government_trading(gov_type) if df_gov.empty: - print(f"No {type} trading data found\n") + print(f"No {gov_type} trading data found\n") return df_gov = df_gov.sort_values("TransactionDate", ascending=False) @@ -71,7 +71,7 @@ def last_government(other_args: List[str], type: str): ) ] - if type == "congress": + if gov_type == "congress": df_gov = df_gov[ [ "TransactionDate", @@ -120,20 +120,20 @@ def last_government(other_args: List[str], type: str): print(e, "\n") -def buy_government(other_args: List[str], type: str): +def buy_government(other_args: List[str], gov_type: str): """Top buy government trading Parameters ---------- other_args : List[str] Command line arguments to be processed with argparse - type: str + gov_type: str Type of government data between: congress, senate and house """ parser = argparse.ArgumentParser( add_help=False, - prog="buy_" + type, - description=f"Top buy {type} trading. [Source: www.quiverquant.com]", + prog="buy_" + gov_type, + description=f"Top buy {gov_type} trading. [Source: www.quiverquant.com]", ) parser.add_argument( "-p", @@ -159,10 +159,10 @@ def buy_government(other_args: List[str], type: str): if not ns_parser: return - df_gov = quiverquant_model.get_government_trading(type) + df_gov = quiverquant_model.get_government_trading(gov_type) if df_gov.empty: - print(f"No {type} trading data found\n") + print(f"No {gov_type} trading data found\n") return df_gov = df_gov.sort_values("TransactionDate", ascending=False) @@ -220,20 +220,20 @@ def buy_government(other_args: List[str], type: str): print(e, "\n") -def sell_government(other_args: List[str], type: str): +def sell_government(other_args: List[str], gov_type: str): """Top sell government trading Parameters ---------- other_args : List[str] Command line arguments to be processed with argparse - type: str + gov_type: str Type of government data between: congress, senate and house """ parser = argparse.ArgumentParser( add_help=False, - prog="sell_" + type, - description=f"Top sell {type} trading. [Source: www.quiverquant.com]", + prog="sell_" + gov_type, + description=f"Top sell {gov_type} trading. [Source: www.quiverquant.com]", ) parser.add_argument( "-p", @@ -259,10 +259,10 @@ def sell_government(other_args: List[str], type: str): if not ns_parser: return - df_gov = quiverquant_model.get_government_trading(type) + df_gov = quiverquant_model.get_government_trading(gov_type) if df_gov.empty: - print(f"No {type} trading data found\n") + print(f"No {gov_type} trading data found\n") return df_gov = df_gov.sort_values("TransactionDate", ascending=False) @@ -321,7 +321,7 @@ def sell_government(other_args: List[str], type: str): print(e, "\n") -def plot_government(government: pd.DataFrame, ticker: str, type: str): +def plot_government(government: pd.DataFrame, ticker: str, gov_type: str): """Plot government trading Parameters @@ -330,7 +330,7 @@ def plot_government(government: pd.DataFrame, ticker: str, type: str): Command line arguments to be processed with argparse ticker: str Ticker to plot government trading - type: str + gov_type: str Type of government data between: congress, senate and house """ plt.figure(figsize=plot_autoscale(), dpi=PLOT_DPI) @@ -348,7 +348,7 @@ def plot_government(government: pd.DataFrame, ticker: str, type: str): ] ) plt.grid() - plt.title(f"{type.capitalize()} trading on {ticker}") + plt.title(f"{gov_type.capitalize()} trading on {ticker}") plt.xlabel("Date") plt.ylabel("Amount [1k $]") plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%Y/%m/%d")) @@ -360,7 +360,7 @@ def plot_government(government: pd.DataFrame, ticker: str, type: str): plt.show() -def government(other_args: List[str], ticker: str, type: str): +def government_trading(other_args: List[str], ticker: str, gov_type: str): """Government trading Parameters @@ -369,13 +369,13 @@ def government(other_args: List[str], ticker: str, type: str): Command line arguments to be processed with argparse ticker: str Ticker to get congress trading data from - type: str + gov_type: str Type of government data between: congress, senate and house """ parser = argparse.ArgumentParser( add_help=False, - prog=type, - description=f"{type} trading. [Source: www.quiverquant.com]", + prog=gov_type, + description=f"{gov_type} trading. [Source: www.quiverquant.com]", ) parser.add_argument( "-p", @@ -397,10 +397,10 @@ def government(other_args: List[str], ticker: str, type: str): if not ns_parser: return - df_gov = quiverquant_model.get_government_trading(type, ticker) + df_gov = quiverquant_model.get_government_trading(gov_type, ticker) if df_gov.empty: - print(f"No {type} trading data found\n") + print(f"No {gov_type} trading data found\n") return df_gov = df_gov.sort_values("TransactionDate", ascending=False) @@ -437,9 +437,75 @@ def government(other_args: List[str], ticker: str, type: str): df_gov = df_gov.sort_values("TransactionDate", ascending=True) - plot_government(df_gov, ticker, type) + plot_government(df_gov, ticker, gov_type) print("") except Exception as e: print(e, "\n") + + +def raw_government(other_args: List[str], ticker: str, gov_type: str): + """Raw government trading + + Parameters + ---------- + other_args : List[str] + Command line arguments to be processed with argparse + ticker: str + Ticker to get congress trading data from + gov_type: str + Type of government data between: congress, senate and house + """ + parser = argparse.ArgumentParser( + add_help=False, + prog=gov_type, + description=f"{gov_type} trading. [Source: www.quiverquant.com]", + ) + parser.add_argument( + "-p", + "--past_transactions_days", + action="store", + dest="past_transactions_days", + type=check_positive, + default=10, + help="Past transaction days", + ) + + try: + + if other_args: + if "-" not in other_args[0]: + other_args.insert(0, "-p") + + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + df_gov = quiverquant_model.get_government_trading(gov_type, ticker) + + if df_gov.empty: + print(f"No {gov_type} trading data found\n") + return + + df_gov = df_gov.sort_values("TransactionDate", ascending=False) + if gov_type == "congress": + df_gov = df_gov[ + ["TransactionDate", "Representative", "House", "Transaction", "Range"] + ] + else: + df_gov = df_gov[ + ["TransactionDate", "Representative", "Transaction", "Range"] + ] + + df_gov = df_gov[ + df_gov["TransactionDate"].isin( + df_gov["TransactionDate"].unique()[: ns_parser.past_transactions_days] + ) + ] + + print(df_gov.to_string(index=False)) + print("") + + except Exception as e: + print(e, "\n")