diff --git a/stock_market_cli.py b/stock_market_cli.py index 69bd090f36e..dd94d6817f6 100644 --- a/stock_market_cli.py +++ b/stock_market_cli.py @@ -59,13 +59,14 @@ def print_help(s_ticker, s_start, s_interval, b_is_market_open): if s_ticker: print("\nFundamental Analysis:") - print("- details - ") + #print("- details - ") print(" ratings company ratings from strong sell to strong buy") - print("- financial statement -") + # print("- financial statement -") print(" income income statements of the company (default: AV)") print(" balance balance sheet of the company (default: AV)") print(" cash cash flow of the company (default: AV)") - print("- ratios -") + #print("- ratios -") + print(" metrics key metrics of the company") print("\nTechnical Analysis:") print(" sma simple moving average") @@ -122,7 +123,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']) + 'income', 'balance', 'cash', 'metrics']) # Print first welcome message and help print("\nWelcome to Didier's Stock Market Bot\n") @@ -204,6 +205,11 @@ def main(): smfa.cash_flow(l_args, s_ticker) continue + # ------------------------------------------------- KEY_METRICS -------------------------------------------------- + elif ns_known_args.cmd == 'metrics': + smfa.key_metrics(l_args, s_ticker) + continue + # -------------------------------------------------------------------------------------------------------------- # -------------------------------------------- TECHNICAL ANALYSIS ---------------------------------------------- # -------------------------------------------------------------------------------------------------------------- diff --git a/stock_market_fundamental_analysis.py b/stock_market_fundamental_analysis.py index 831f9362753..04cd40284cf 100644 --- a/stock_market_fundamental_analysis.py +++ b/stock_market_fundamental_analysis.py @@ -203,6 +203,50 @@ def cash_flow(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 + + +# ---------------------------------------------------- KEY_METRICS ---------------------------------------------------- +def key_metrics(l_args, s_ticker): + parser = argparse.ArgumentParser(prog='metrics', + 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 + + 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: + df_fd = fa.key_metrics(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: diff --git a/stock_market_helper_funcs.py b/stock_market_helper_funcs.py index 4ffe3f24401..40692f3d2cc 100644 --- a/stock_market_helper_funcs.py +++ b/stock_market_helper_funcs.py @@ -128,18 +128,27 @@ def b_is_stock_market_open(): # ----------------------------------------------------------------------------------------------------------------------- -def long_number_format(s_num): - if not isinstance(s_num, str): - s_num = str(s_num) - if s_num.lstrip("-").isdigit(): - n_num = int(s_num) - n_num /= 1.0 +def long_number_format(num): + if isinstance(num, float): magnitude = 0 - while abs(n_num) >= 1000: + while abs(num) >= 1000: magnitude += 1 - n_num /= 1000.0 - if n_num.is_integer(): - return '%d%s' % (n_num, ['', ' K', ' M', ' B', ' T', ' P'][magnitude]) + num /= 1000.0 + if num.is_integer(): + return '%d%s' % (num, ['', ' K', ' M', ' B', ' T', ' P'][magnitude]) else: - return '%.3f%s' % (n_num, ['', ' K', ' M', ' B', ' T', ' P'][magnitude]) - return s_num \ No newline at end of file + return '%.3f%s' % (num, ['', ' K', ' M', ' B', ' T', ' P'][magnitude]) + if isinstance(num, int): + num = str(num) + if num.lstrip("-").isdigit(): + num = int(num) + num /= 1.0 + magnitude = 0 + while abs(num) >= 1000: + magnitude += 1 + num /= 1000.0 + if num.is_integer(): + return '%d%s' % (num, ['', ' K', ' M', ' B', ' T', ' P'][magnitude]) + else: + return '%.3f%s' % (num, ['', ' K', ' M', ' B', ' T', ' P'][magnitude]) + return num \ No newline at end of file