From 9eb40e70452a7f6a8669794e2a8613e7d37d6a09 Mon Sep 17 00:00:00 2001 From: didier Date: Wed, 10 Mar 2021 21:00:34 +0000 Subject: [PATCH] Add comparison analysis README --- .../comparison_analysis/README.md | 58 ++++++++++++++++ .../comparison_analysis/yahoo_finance_api.py | 66 +++++++++++++++++-- 2 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 gamestonk_terminal/comparison_analysis/README.md diff --git a/gamestonk_terminal/comparison_analysis/README.md b/gamestonk_terminal/comparison_analysis/README.md new file mode 100644 index 00000000000..9809f5dd9c2 --- /dev/null +++ b/gamestonk_terminal/comparison_analysis/README.md @@ -0,0 +1,58 @@ +# COMPARISON ANALYSIS + +This menu aims to compare a pre-loaded stock with similar companies, and the usage of the following commands along with an example will be exploited below. + +* [get](#get) + * get similar companies [Polygon] +* [select](#select) + * select similar companies +* [historical](#historical) + * historical data comparison [Yahoo Finance] +* [correlation](#correlation) + * correlation between similar companies [Yahoo Finance] + + +## get + +```text +get +``` + +Get similar companies to compare with. [Source: Polygon] + + +## select + +```text +usage: select [-s L_SIMILAR] +``` + +Select similar companies, e.g. NIO,XPEV,LI + +* -s : similar companies to compare with + + +## historical + +```text +usage: historical [-s L_SIMILAR] [-a L_ALSO] [-t TYPE_CANDLE] +``` + +Historical price comparison between similar companies [Source: Yahoo Finance] + +* -s : similar companies to compare with. Default pre-loaded ones. +* -a : apart from loaded similar companies also compare with. +* -t : type of candles: o-open, h-high, l-low, c-close, a-adjusted close. Default 'a'. + + +## corr + +```text +usage: corr [-s L_SIMILAR] [-a L_ALSO] [-t TYPE_CANDLE] +``` + +Historical price comparison between similar companies [Source: Yahoo Finance] + +* -s : similar companies to compare with. Default pre-loaded ones. +* -a : apart from loaded similar companies also compare with. +* -t : type of candles: o-open, h-high, l-low, c-close, a-adjusted close. Default 'a'. diff --git a/gamestonk_terminal/comparison_analysis/yahoo_finance_api.py b/gamestonk_terminal/comparison_analysis/yahoo_finance_api.py index 45510f11916..95dca00a14f 100644 --- a/gamestonk_terminal/comparison_analysis/yahoo_finance_api.py +++ b/gamestonk_terminal/comparison_analysis/yahoo_finance_api.py @@ -11,6 +11,27 @@ from gamestonk_terminal.helper_funcs import parse_known_args_and_warn register_matplotlib_converters() +d_candle_types = { + "o": "Open", + "h": "High", + "l": "Low", + "c": "Close", + "a": "Adj Close", +} + + +def check_one_of_ohlca(type_candles: str) -> str: + if ( + type_candles == "o" + or type_candles == "h" + or type_candles == "l" + or type_candles == "c" + or type_candles == "a" + ): + return type_candles + raise argparse.ArgumentTypeError("The type of candles specified is not recognized") + + def historical(l_args, df_stock, s_ticker, s_start, s_interval, similar): parser = argparse.ArgumentParser( prog="historical", @@ -31,7 +52,16 @@ def historical(l_args, df_stock, s_ticker, s_start, s_interval, similar): dest="l_also", type=lambda s: [str(item) for item in s.split(",")], default=[], - help="apart from similar companies (from Polygon API) also compare with.", + help="apart from loaded similar companies also compare with.", + ) + parser.add_argument( + "-t", + "--type", + action="store", + dest="type_candle", + type=check_one_of_ohlca, + default="a", # in case it's adjusted close + help=("type of candles: o-open, h-high, l-low, c-close, a-adjusted close."), ) try: @@ -48,14 +78,19 @@ def historical(l_args, df_stock, s_ticker, s_start, s_interval, similar): plt.figure() plt.title(f"Similar companies to {s_ticker}") - plt.plot(df_stock.index, df_stock["5. adjusted close"].values, lw=2) + df_stock = yf.download(s_ticker, start=s_start, progress=False) + plt.plot( + df_stock.index, df_stock[d_candle_types[ns_parser.type_candle]].values + ) + # plt.plot(df_stock.index, df_stock["5. adjusted close"].values, lw=2) l_min = [df_stock.index[0]] l_leg = [s_ticker] for symbol in similar: df_similar_stock = yf.download(symbol, start=s_start, progress=False) if not df_similar_stock.empty: plt.plot( - df_similar_stock.index, df_similar_stock["Adj Close"].values + df_similar_stock.index, + df_similar_stock[d_candle_types[ns_parser.type_candle]].values, ) l_min.append(df_similar_stock.index[0]) l_leg.append(symbol) @@ -78,7 +113,8 @@ def historical(l_args, df_stock, s_ticker, s_start, s_interval, similar): def correlation(l_args, df_stock, s_ticker, s_start, s_interval, similar): parser = argparse.ArgumentParser( prog="corr", - description=""" Correlation heatmap based on historical price comparison between similar companies. [Source: Yahoo Finance] + description=""" Correlation heatmap based on historical price comparison between similar + companies. [Source: Yahoo Finance] """, ) parser.add_argument( @@ -95,7 +131,16 @@ def correlation(l_args, df_stock, s_ticker, s_start, s_interval, similar): dest="l_also", type=lambda s: [str(item) for item in s.split(",")], default=[], - help="apart from similar companies (from Polygon API) also compare with.", + help="apart from loaded similar companies also compare with.", + ) + parser.add_argument( + "-t", + "--type", + action="store", + dest="type_candle", + type=check_one_of_ohlca, + default="a", # in case it's adjusted close + help=("type of data: o-open, h-high, l-low, c-close, a-adjusted close"), ) try: @@ -124,12 +169,19 @@ def correlation(l_args, df_stock, s_ticker, s_start, s_interval, similar): min_start_date = max(l_min) - df_stock = d_stock[s_ticker]["Adj Close"].rename(s_ticker) + df_stock = d_stock[s_ticker][ + d_candle_types[ns_parser.type_candle] + ].rename(s_ticker) for symbol in d_stock: if symbol != s_ticker: if not d_stock[symbol].empty: df_stock = pd.concat( - [df_stock, d_stock[symbol]["Adj Close"].rename(symbol)], + [ + df_stock, + d_stock[symbol][ + d_candle_types[ns_parser.type_candle] + ].rename(symbol), + ], axis=1, )