From 34128fc857ea24020fcccc53b27284044c2fadf9 Mon Sep 17 00:00:00 2001 From: didier Date: Sun, 27 Dec 2020 23:00:07 +0000 Subject: [PATCH] Add financial ratios to fundamental analysis --- stock_market_cli.py | 8 ++++- stock_market_fundamental_analysis.py | 44 +++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/stock_market_cli.py b/stock_market_cli.py index dd94d6817f6..54c65d74a0f 100644 --- a/stock_market_cli.py +++ b/stock_market_cli.py @@ -67,6 +67,7 @@ def print_help(s_ticker, s_start, s_interval, b_is_market_open): print(" cash cash flow of the company (default: AV)") #print("- ratios -") print(" metrics key metrics of the company") + print(" ratios financial ratios of the company") print("\nTechnical Analysis:") print(" sma simple moving average") @@ -123,7 +124,7 @@ def main(): main_parser.add_argument('cmd', choices=['quit', 'help', 'gainers', 'sectors', 'view', 'load', 'clear', 'sma', 'ema', 'macd', 'vwap', 'stoch', 'rsi', 'adx', 'cci', 'aroon', 'bbands', 'ad', 'obv', 'ratings', - 'income', 'balance', 'cash', 'metrics']) + 'income', 'balance', 'cash', 'metrics', 'ratios']) # Print first welcome message and help print("\nWelcome to Didier's Stock Market Bot\n") @@ -210,6 +211,11 @@ def main(): smfa.key_metrics(l_args, s_ticker) continue + # ----------------------------------------------- FINANCIAL_RATIOS ----------------------------------------------- + elif ns_known_args.cmd == 'ratios': + smfa.financial_ratios(l_args, s_ticker) + continue + # -------------------------------------------------------------------------------------------------------------- # -------------------------------------------- TECHNICAL ANALYSIS ---------------------------------------------- # -------------------------------------------------------------------------------------------------------------- diff --git a/stock_market_fundamental_analysis.py b/stock_market_fundamental_analysis.py index 04cd40284cf..de96a0c3980 100644 --- a/stock_market_fundamental_analysis.py +++ b/stock_market_fundamental_analysis.py @@ -233,7 +233,6 @@ def key_metrics(l_args, s_ticker): else: pd.options.display.max_colwidth = 40 - fd = FundamentalData(key=cfg.API_KEY_ALPHAVANTAGE, output_format='pandas') if ns_parser.b_quarter: df_fd = fa.key_metrics(s_ticker, cfg.API_KEY_FINANCIALMODELINGPREP, period='quarter') else: @@ -247,6 +246,49 @@ def key_metrics(l_args, s_ticker): df_fd.index = [s_val.capitalize() for s_val in df_fd.index] df_fd.columns.name = "Fiscal Date Ending" + print(df_fd) + print("") + except: + print("") + return + + +# ---------------------------------------------------- FINANCIAL_RATIOS ---------------------------------------------------- +def financial_ratios(l_args, s_ticker): + parser = argparse.ArgumentParser(prog='ratios', + description=""" """) + + parser.add_argument('-n', "--num", action="store", dest="n_num", type=check_positive, default=1, help='Number of informations') + parser.add_argument('-q', action="store_true", default=False, dest="b_quarter", help='Quarter fundamental data') + + try: + (ns_parser, l_unknown_args) = parser.parse_known_args(l_args) + except SystemExit: + print("") + return + + if l_unknown_args: + print(f"The following args couldn't be interpreted: {l_unknown_args}") + + try: + if ns_parser.n_num == 1: + pd.set_option('display.max_colwidth', -1) + else: + pd.options.display.max_colwidth = 40 + + if ns_parser.b_quarter: + df_fd = fa.financial_ratios(s_ticker, cfg.API_KEY_FINANCIALMODELINGPREP, period='quarter') + else: + df_fd = fa.financial_ratios(s_ticker, cfg.API_KEY_FINANCIALMODELINGPREP) + + df_fd = df_fd.iloc[:, 0:ns_parser.n_num] + df_fd = df_fd.mask(df_fd.astype(object).eq(ns_parser.n_num*['None'])).dropna() + df_fd = df_fd.mask(df_fd.astype(object).eq(ns_parser.n_num*['0'])).dropna() + df_fd = df_fd.applymap(lambda x: long_number_format(x)) + df_fd.index = [''.join(' ' + char if char.isupper() else char.strip() for char in idx).strip() for idx in df_fd.index.tolist()] + df_fd.index = [s_val.capitalize() for s_val in df_fd.index] + df_fd.columns.name = "Fiscal Date Ending" + print(df_fd) print("") except: