From ede36834c5540e8b2da8fd201f1b2226648e7fb3 Mon Sep 17 00:00:00 2001 From: Colin Delahunty <72827203+colin99d@users.noreply.github.com> Date: Mon, 18 Oct 2021 19:07:02 -0400 Subject: [PATCH] Added raw stock data #817 (#820) --- .../common/quantitative_analysis/qa_view.py | 45 ++++++++++++ .../stocks/stocks_controller.py | 72 ++++++++++++++++++- website/content/stocks/candle/_index.md | 8 ++- .../quantitative_analysis/data/_index.md | 14 ++++ website/data/menu/main.yml | 2 + 5 files changed, 136 insertions(+), 5 deletions(-) create mode 100755 website/content/stocks/quantitative_analysis/data/_index.md diff --git a/gamestonk_terminal/common/quantitative_analysis/qa_view.py b/gamestonk_terminal/common/quantitative_analysis/qa_view.py index fce314c3ed6..f52deada9a6 100644 --- a/gamestonk_terminal/common/quantitative_analysis/qa_view.py +++ b/gamestonk_terminal/common/quantitative_analysis/qa_view.py @@ -497,3 +497,48 @@ def display_unitroot( "unitroot", data, ) + + +def display_raw( + df: pd.DataFrame, export: str, sort: str, des: bool, num: int = 20 +) -> None: + """Return raw stock data + + Parameters + ---------- + df : DataFrame + DataFrame with historical information + export : str + Export data as CSV, JSON, XLSX + sort : str + The column to sort by + des : bool + Whether to sort descending + num : int + Number of rows to show + """ + + export_data( + export, + os.path.dirname(os.path.abspath(__file__)), + "history", + df, + ) + + if sort: + df = df.sort_values(by=sort, ascending=des) + + if gtff.USE_TABULATE_DF: + print( + tabulate( + df.head(num), + headers=[x.title() if x != "" else "Date" for x in df.columns], + tablefmt="fancy_grid", + showindex=True, + floatfmt=".2f", + ) + ) + else: + print(df.to_string(index=False)) + + print("") diff --git a/gamestonk_terminal/stocks/stocks_controller.py b/gamestonk_terminal/stocks/stocks_controller.py index cbd201633bb..a5c8dd8cd38 100644 --- a/gamestonk_terminal/stocks/stocks_controller.py +++ b/gamestonk_terminal/stocks/stocks_controller.py @@ -34,6 +34,7 @@ from gamestonk_terminal.stocks.screener import screener_controller from gamestonk_terminal.stocks.stocks_helper import candle, load, quote from gamestonk_terminal.stocks.technical_analysis import ta_controller from gamestonk_terminal.helper_funcs import valid_date +from gamestonk_terminal.common.quantitative_analysis import qa_view # pylint: disable=R1710 @@ -209,10 +210,75 @@ Market {('CLOSED', 'OPEN')[b_is_stock_market_open()]} def call_candle(self, other_args: List[str]): """Process candle command""" - candle( - self.ticker + "." + self.suffix if self.suffix else self.ticker, - other_args, + parser = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog="data", + description="Shows historic data for a stock", ) + parser.add_argument( + "--export", + choices=["csv", "json", "xlsx"], + default="", + type=str, + dest="export", + help="Export dataframe data to csv,json,xlsx file", + ) + parser.add_argument( + "-s", + "--sort", + choices=[ + "AdjClose", + "Open", + "Close", + "High", + "Low", + "Volume", + "Returns", + "LogRet", + ], + default="", + type=str, + dest="sort", + help="Choose a column to sort by", + ) + parser.add_argument( + "-d", + "--descending", + action="store_false", + dest="descending", + default=True, + help="Sort selected column descending", + ) + parser.add_argument( + "--raw", + action="store_true", + dest="raw", + default=False, + help="Shows raw data instead of chart", + ) + + try: + ns_parser = parse_known_args_and_warn(parser, other_args) + if not ns_parser: + return + + if ns_parser.raw: + qa_view.display_raw( + self.stock, + ns_parser.export, + ns_parser.sort, + ns_parser.descending, + ) + + else: + candle( + self.ticker + "." + self.suffix if self.suffix else self.ticker, + other_args, + ) + + except Exception as e: + print(e, "\n") def call_news(self, other_args: List[str]): """Process news command""" diff --git a/website/content/stocks/candle/_index.md b/website/content/stocks/candle/_index.md index 13d6472c01f..cf117821c08 100755 --- a/website/content/stocks/candle/_index.md +++ b/website/content/stocks/candle/_index.md @@ -1,14 +1,18 @@ ``` -usage: candle [-s S_START] [--plotly] [-h] +usage: candle [-s S_START] [--plotly] [-h] [--raw] [-e {csv, json, xlsx}] -s SORT] [-d] ``` -Displays candle chart of loaded ticker +Displays candle chart of loaded ticker, or shows raw data with --raw ``` optional arguments: -s S_START, --start_date S_START Start date for candle data (default: 2021-03-19) --plotly Flag to show interactive plot using plotly. (default: False) + --raw shows the raw data instead of a chart + -e, --export The format to export the information into + -s, --sort Which column to sort the information by + -d, --descending Show the information in descending order -h, --help show this help message (default: False) ``` diff --git a/website/content/stocks/quantitative_analysis/data/_index.md b/website/content/stocks/quantitative_analysis/data/_index.md new file mode 100755 index 00000000000..4c0e2c7df22 --- /dev/null +++ b/website/content/stocks/quantitative_analysis/data/_index.md @@ -0,0 +1,14 @@ +``` +usage: data [-e {csv, json, xlsx}] [-s SORT] [-d] [-n NUMBER] [-h] +``` + +Shows a table of historical information for a stock with the option to export the data [Source: Yahoo Finance] + +``` +optional arguments: + -e, --export The format to export the information into + -s, --sort Which column to sort the information by + -d, --descending Show the information in descending order + -n, --num Number of results to show + -h, Help +``` diff --git a/website/data/menu/main.yml b/website/data/menu/main.yml index 1374354cc54..890a870d9dd 100755 --- a/website/data/menu/main.yml +++ b/website/data/menu/main.yml @@ -487,6 +487,8 @@ main: ref: "/stocks/quantitative_analysis/cusum" - name: capm ref: "/stocks/quantitative_analysis/capm" + - name: data + ref: "/stocks/quantitative_analysis/data" - name: prediction techniques ref: "/stocks/prediction_techniques" sub: