From d24d60cea2f9f382fea9724f5824c41b80214c38 Mon Sep 17 00:00:00 2001 From: didier Date: Sun, 27 Dec 2020 23:53:09 +0000 Subject: [PATCH] Add discounted cash flow to fundamental analysis --- stock_market_cli.py | 10 ++++++-- stock_market_fundamental_analysis.py | 37 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/stock_market_cli.py b/stock_market_cli.py index 287d5fed589..fa8214a8450 100644 --- a/stock_market_cli.py +++ b/stock_market_cli.py @@ -63,6 +63,7 @@ def print_help(s_ticker, s_start, s_interval, b_is_market_open): #print("- details - ") print(" profile profile of the company") print(" rating rating of the company from strong sell to strong buy") + print(" dcf discounted cash flow of the company") # print("- financial statement -") print(" income income statements of the company (default: AV)") print(" balance balance sheet of the company (default: AV)") @@ -128,7 +129,7 @@ def main(): 'sma', 'ema', 'macd', 'vwap', 'stoch', 'rsi', 'adx', 'quote', 'cci', 'aroon', 'bbands', 'ad', 'obv', 'rating', 'profile', 'income', 'balance', 'cash', 'metrics', 'ratios', 'growth', - 'enterprise']) + 'enterprise', 'dcf']) # Print first welcome message and help print("\nWelcome to Didier's Stock Market Bot\n") @@ -205,11 +206,16 @@ def main(): smfa.enterprise(l_args, s_ticker) continue - # --------------------------------------------------- RATING -------------------------------------------------- + # --------------------------------------------------- RATING ---------------------------------------------------- elif ns_known_args.cmd == 'rating': smfa.rating(l_args, s_ticker) continue + # --------------------------------------------- DISCOUNTED CASH FLOW ----------------------------------------------- + elif ns_known_args.cmd == 'dcf': + smfa.discounted_cash_flow(l_args, s_ticker) + continue + # ----------------------------------------------- INCOME_STATEMENT ----------------------------------------------- elif ns_known_args.cmd == 'income': smfa.income_statement(l_args, s_ticker) diff --git a/stock_market_fundamental_analysis.py b/stock_market_fundamental_analysis.py index c1347004938..142c01f24af 100644 --- a/stock_market_fundamental_analysis.py +++ b/stock_market_fundamental_analysis.py @@ -123,6 +123,43 @@ def rating(l_args, s_ticker): print("") +# ------------------------------------------ DISCOUNTED CASH FLOW ------------------------------------------------------ +def discounted_cash_flow(l_args, s_ticker): + parser = argparse.ArgumentParser(prog='dcf', + description=""" """) + parser.add_argument('-n', "--num", action="store", dest="n_num", type=check_positive, default=1, help='Number of latest info') + 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}") + + if ns_parser.n_num == 1: + pd.set_option('display.max_colwidth', -1) + else: + pd.options.display.max_colwidth = 40 + + try: + if ns_parser.b_quarter: + df_fa = fa.discounted_cash_flow(s_ticker, cfg.API_KEY_FINANCIALMODELINGPREP, period='quarter') + else: + df_fa = fa.discounted_cash_flow(s_ticker, cfg.API_KEY_FINANCIALMODELINGPREP) + + df_fa = df_fa.iloc[:, 0:ns_parser.n_num] + df_fa = df_fa.applymap(lambda x: long_number_format(x)) + df_fa.index = [''.join(' ' + char if char.isupper() else char.strip() for char in idx).strip() for idx in df_fa.index.tolist()] + df_fa.index = [s_val.capitalize() for s_val in df_fa.index] + print(df_fa) + print("") + except: + print("") + + # ---------------------------------------------------- INCOME_STATEMENT ---------------------------------------------------- def income_statement(l_args, s_ticker): parser = argparse.ArgumentParser(prog='income',