From b82e4225b6155e8948c26a5082b972790c538e34 Mon Sep 17 00:00:00 2001 From: Danglewood <85772166+deeleeramone@users.noreply.github.com> Date: Wed, 15 Dec 2021 07:17:43 -0800 Subject: [PATCH] Dividend History Chart (#1053) * Dividend History Chart * changed to default number 10 * changed divs flag default to false * updating the expected result for test_fa_controller. * else statement --- .../fundamental_analysis/fa_controller.py | 22 ++++--- .../yahoo_finance_view.py | 64 +++++++++++++------ .../test_menu_quick_exit.txt | 1 - .../test_menu_system_exit.txt | 1 - .../test_fa_controller/test_print_help.txt | 1 - .../test_fa_controller/test_switch_help.txt | 1 - 6 files changed, 57 insertions(+), 33 deletions(-) diff --git a/gamestonk_terminal/stocks/fundamental_analysis/fa_controller.py b/gamestonk_terminal/stocks/fundamental_analysis/fa_controller.py index 513552e5d97..036b65c4ac8 100644 --- a/gamestonk_terminal/stocks/fundamental_analysis/fa_controller.py +++ b/gamestonk_terminal/stocks/fundamental_analysis/fa_controller.py @@ -77,7 +77,6 @@ class FundamentalAnalysisController: def __init__(self, ticker: str, start: str, interval: str, suffix: str = ""): """Constructor - Parameters ---------- ticker : str @@ -109,7 +108,6 @@ Fundamental Analysis: q quit this menu, and shows back to main menu quit quit to abandon program load load a new ticker - Ticker: {self.ticker} {f"Note that only Yahoo Finance currently supports foreign exchanges{Style.DIM}{newline}" if self.suffix else ""} data fundamental and technical data of company [FinViz] @@ -147,7 +145,6 @@ Other Sources: def switch(self, an_input: str): """Process and dispatch input - Returns ------- True, False or None @@ -460,23 +457,34 @@ Other Sources: add_help=False, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="divs", - description="Get historical dividends for company", + description="Historical dividends for a company", ) parser.add_argument( "-n", "--num", dest="num", type=check_positive, - default=12, + default=16, help="Number of previous dividends to show", ) + parser.add_argument( + "-p", + "--plot", + dest="plot", + default=False, + action="store_true", + help="Plots changes in dividend over time", + ) ns_parser = parse_known_args_and_warn( parser, other_args, export_allowed=EXPORT_ONLY_RAW_DATA_ALLOWED ) if not ns_parser: return yahoo_finance_view.display_dividends( - ticker=self.ticker, num=ns_parser.num, export=ns_parser.export + ticker=self.ticker, + num=ns_parser.num, + plot=ns_parser.plot, + export=ns_parser.export, ) @try_except @@ -837,7 +845,6 @@ Other Sources: @try_except def key_metrics_explained(other_args: List[str]): """Key metrics explained - Parameters ---------- other_args : List[str] @@ -867,7 +874,6 @@ def key_metrics_explained(other_args: List[str]): def menu(ticker: str, start: str, interval: str, suffix: str = ""): """Fundamental Analysis menu - Parameters ---------- ticker : str diff --git a/gamestonk_terminal/stocks/fundamental_analysis/yahoo_finance_view.py b/gamestonk_terminal/stocks/fundamental_analysis/yahoo_finance_view.py index d064d34045e..f0a19d66ce6 100644 --- a/gamestonk_terminal/stocks/fundamental_analysis/yahoo_finance_view.py +++ b/gamestonk_terminal/stocks/fundamental_analysis/yahoo_finance_view.py @@ -3,17 +3,18 @@ __docformat__ = "numpy" import os import webbrowser +import matplotlib.pyplot as plt import pandas as pd from tabulate import tabulate from gamestonk_terminal.stocks.fundamental_analysis import yahoo_finance_model from gamestonk_terminal import feature_flags as gtff -from gamestonk_terminal.helper_funcs import export_data +from gamestonk_terminal.helper_funcs import export_data, plot_autoscale +from gamestonk_terminal.config_plot import PLOT_DPI def open_headquarters_map(ticker: str): """Headquarters location of the company - Parameters ---------- ticker : str @@ -25,7 +26,6 @@ def open_headquarters_map(ticker: str): def open_web(ticker: str): """Website of the company - Parameters ---------- ticker : str @@ -37,7 +37,6 @@ def open_web(ticker: str): def display_info(ticker: str): """Yahoo Finance ticker info - Parameters ---------- ticker : str @@ -64,7 +63,6 @@ def display_info(ticker: str): def display_shareholders(ticker: str): """Yahoo Finance ticker shareholders - Parameters ---------- ticker : str @@ -92,7 +90,6 @@ def display_shareholders(ticker: str): def display_sustainability(ticker: str): """Yahoo Finance ticker sustainability - Parameters ---------- other_args : List[str] @@ -123,7 +120,6 @@ def display_sustainability(ticker: str): def display_calendar_earnings(ticker: str): """Yahoo Finance ticker calendar earnings - Parameters ---------- ticker : str @@ -147,15 +143,16 @@ def display_calendar_earnings(ticker: str): print("") -def display_dividends(ticker: str, num: int = 12, export: str = ""): +def display_dividends(ticker: str, num: int = 12, plot: bool = False, export: str = ""): """Display historical dividends - Parameters ---------- ticker: str Stock ticker num: int Number to show + plot: bool + Plots hitsorical data export: str Format to export data """ @@ -165,19 +162,44 @@ def display_dividends(ticker: str, num: int = 12, export: str = ""): return div_history["Dif"] = div_history.diff() div_history = div_history[::-1] - div_history.index = pd.to_datetime(div_history.index, format="%Y%m%d").strftime( - "%Y-%m-%d" - ) - if gtff.USE_TABULATE_DF: - print( - tabulate( - div_history.head(num), - tablefmt="fancy_grid", - headers=["Amount Paid ($)", "Change"], - floatfmt=".2f", - ) + if plot: + fig, ax = plt.subplots( + figsize=plot_autoscale(), constrained_layout=False, dpi=PLOT_DPI ) + ax.plot( + div_history.index, + div_history["Dividends"], + ls="-", + linewidth=0.75, + marker=".", + markersize=4, + mfc="k", + mec="k", + c="k", + alpha=1, + ) + ax.set_xlabel("Date") + ax.set_ylabel("Amount ($)") + ax.set_title(f"Dividend History for {ticker}") + ax.set_xlim(div_history.index[-1], div_history.index[0]) + if gtff.USE_ION: + plt.ion() + fig.tight_layout() + plt.show() else: - print(div_history.to_string()) + div_history.index = pd.to_datetime(div_history.index, format="%Y%m%d").strftime( + "%Y-%m-%d" + ) + if gtff.USE_TABULATE_DF: + print( + tabulate( + div_history.head(num), + tablefmt="fancy_grid", + headers=["Amount Paid ($)", "Change"], + floatfmt=".2f", + ) + ) + else: + print(div_history.to_string()) print("") export_data(export, os.path.dirname(os.path.abspath(__file__)), "divs", div_history) diff --git a/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_menu_quick_exit.txt b/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_menu_quick_exit.txt index 8b17b2d034d..fc420d387ca 100644 --- a/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_menu_quick_exit.txt +++ b/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_menu_quick_exit.txt @@ -5,7 +5,6 @@ Fundamental Analysis: q quit this menu, and shows back to main menu quit quit to abandon program load load a new ticker - Ticker: TSLA data fundamental and technical data of company [FinViz] diff --git a/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_menu_system_exit.txt b/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_menu_system_exit.txt index 855d5ae0481..81368221426 100644 --- a/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_menu_system_exit.txt +++ b/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_menu_system_exit.txt @@ -5,7 +5,6 @@ Fundamental Analysis: q quit this menu, and shows back to main menu quit quit to abandon program load load a new ticker - Ticker: TSLA data fundamental and technical data of company [FinViz] diff --git a/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_print_help.txt b/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_print_help.txt index e41d7a226f0..cfa5c37190d 100644 --- a/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_print_help.txt +++ b/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_print_help.txt @@ -5,7 +5,6 @@ Fundamental Analysis: q quit this menu, and shows back to main menu quit quit to abandon program load load a new ticker - Ticker: data fundamental and technical data of company [FinViz] diff --git a/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_switch_help.txt b/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_switch_help.txt index e41d7a226f0..cfa5c37190d 100644 --- a/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_switch_help.txt +++ b/tests/gamestonk_terminal/stocks/fundamental_analysis/txt/test_fa_controller/test_switch_help.txt @@ -5,7 +5,6 @@ Fundamental Analysis: q quit this menu, and shows back to main menu quit quit to abandon program load load a new ticker - Ticker: data fundamental and technical data of company [FinViz]