diff --git a/gamestonk_terminal/discovery/disc_controller.py b/gamestonk_terminal/discovery/disc_controller.py index de17f94334a..6125a377773 100644 --- a/gamestonk_terminal/discovery/disc_controller.py +++ b/gamestonk_terminal/discovery/disc_controller.py @@ -89,7 +89,7 @@ class DiscoveryController: print(" valuation valuation of sectors, industry, country [Finviz]") print(" performance performance of sectors, industry, country [Finviz]") print(" spectrum spectrum of sectors, industry, country [Finviz]") - print(" ratings latest ratings [MarketBeat]") + print(" ratings top ratings updates [MarketBeat]") print("") def switch(self, an_input: str): diff --git a/gamestonk_terminal/discovery/marketbeat_view.py b/gamestonk_terminal/discovery/marketbeat_view.py index aad9b03ec5c..2a55f7c3479 100644 --- a/gamestonk_terminal/discovery/marketbeat_view.py +++ b/gamestonk_terminal/discovery/marketbeat_view.py @@ -2,6 +2,8 @@ __docformat__ = "numpy" import argparse +import numpy as np +import pandas as pd from typing import List from gamestonk_terminal.helper_funcs import ( check_positive, @@ -17,38 +19,98 @@ def ratings_view(other_args: List[str]): Parameters ---------- other_args : List[str] - argparse other args - ["-n", "5"] + argparse other args - ["-t", "100"] """ - parser = argparse.ArgumentParser( add_help=False, prog="ratings", description="""Latest ratings. [Source: MarketBeat]""", ) - parser.add_argument( - "-n", - "--num", + "-t", + "--threshold", action="store", - dest="n_num", + dest="n_threshold", type=check_positive, - default=10, - help="number of ratings to print", + default=100, + help="threshold to show latest ratings that show a drastic increase", ) + if other_args: + if "-" not in other_args[0]: + other_args.insert(0, "-t") + ns_parser = parse_known_args_and_warn(parser, other_args) if not ns_parser: return - ratings = marketbeat_model.get_ratings() - for idx, rating in enumerate(ratings): - print( - rating["ticker"], - rating["action"].replace(" by", ""), - rating["impact"], - rating["current_price"], - "->", - rating["target_price"], + try: + ratings = marketbeat_model.get_ratings() + + df_ratings = pd.DataFrame(ratings) + df_ratings = df_ratings[df_ratings["target_price"] != ""] + df_ratings["old_target"] = df_ratings["target_price"].apply( + lambda x: x.split(" ➝ ")[0] if "➝" in x else "" ) - if idx >= ns_parser.n_num - 1: - break + df_ratings["new_target"] = df_ratings["target_price"].apply( + lambda x: x.split(" ➝ ")[1] if "➝" in x else x + ) + + df_ratings["clean_current_price"] = df_ratings["current_price"].apply( + lambda x: "".join([c if c.isdigit() or c == "." else "" for c in str(x)]) + ) + df_ratings["clean_current_price"] = df_ratings["clean_current_price"].astype( + float + ) + df_ratings["clean_target_price"] = df_ratings["new_target"].apply( + lambda x: "".join([c if c.isdigit() or c == "." else "" for c in str(x)]) + ) + df_ratings["clean_target_price"] = df_ratings["clean_target_price"].astype( + float + ) + + df_ratings["pct_increase"] = ( + 100 + * (df_ratings["clean_target_price"] - df_ratings["clean_current_price"]) + / df_ratings["clean_current_price"] + ) + df_ratings["pct_increase"] = df_ratings["pct_increase"].round(2) + + df_ratings["pct_abs"] = np.abs(df_ratings["pct_increase"]) + df_ratings["pct_abs"] = df_ratings["pct_abs"].round(2) + + df_ratings = df_ratings.sort_values("pct_abs", ascending=False) + + df_ratings_top = df_ratings[df_ratings["pct_abs"] > ns_parser.n_threshold][ + [ + "ticker", + "brokerage", + "rating", + "current_price", + "old_target", + "new_target", + "pct_increase", + ] + ] + + df_ratings_top["pct_increase"] = df_ratings_top["pct_increase"].apply( + lambda x: str(x) + " %" + ) + + print( + df_ratings_top.rename( + columns={ + "ticker": "Ticker", + "brokerage": "Brokerage", + "rating": "Rating", + "current_price": "Current Price", + "old_target": "Old Target", + "new_target": "New Target", + "pct_increase": "Percentage Increase", + } + ).to_string(index=False) + ) + + print("") + except Exception as e: + print(e, "\n")