From 41a7221bd57c2c3b3fc634becf51ee4fe0ba5b5b Mon Sep 17 00:00:00 2001 From: didier Date: Sat, 26 Dec 2020 02:10:34 +0000 Subject: [PATCH] Add rsi to technical analysis --- stock_market_cli.py | 10 ++++++++-- stock_market_helper_funcs.py | 5 +++-- stock_market_technical_analysis.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/stock_market_cli.py b/stock_market_cli.py index e39a4cf03ae..17989bc02b7 100644 --- a/stock_market_cli.py +++ b/stock_market_cli.py @@ -55,8 +55,9 @@ def print_help(s_ticker, s_start, b_is_market_open): print(" sma simple moving average [daily]") print(" ema exponential moving average [daily]") print(" macd moving average convergence/divergence [daily]") - print(" vwap volume weighted average price [intradaily]") + print(" vwap volume weighted average price [intraday]") print(" stoch stochastic oscillator [daily]") + print(" rsi relative strength index [daily]") print("\nPrediction:") print(" ma") @@ -98,7 +99,7 @@ def main(): # Add list of arguments that the main parser accepts main_parser.add_argument('cmd', choices=['quit', 'help', 'gainers' ,'view', 'load', 'clear', - 'sma', 'ema', 'macd', 'vwap', 'stoch', + 'sma', 'ema', 'macd', 'vwap', 'stoch', 'rsi', 'ratings']) # Print first welcome message and help @@ -192,6 +193,11 @@ def main(): smta.stoch(l_args, s_ticker, df_stock) continue + # ---------------------------------------------------- RSI ---------------------------------------------------- + elif ns_known_args.cmd == 'rsi': + smta.rsi(l_args, s_ticker, df_stock) + continue + # -------------------------------------------------------------------------------------------------------------- # ------------------------------------------------ PREDICTION -------------------------------------------------- # -------------------------------------------------------------------------------------------------------------- diff --git a/stock_market_helper_funcs.py b/stock_market_helper_funcs.py index 8e29e4ff6ef..ba096e0b9b2 100644 --- a/stock_market_helper_funcs.py +++ b/stock_market_helper_funcs.py @@ -62,8 +62,9 @@ def plot_ta(s_ticker, df_ta, s_ta): plt.title(f"{s_ta} on {s_ticker}") plt.xlim(df_ta.index[0], df_ta.index[-1]) plt.xlabel('Time') - plt.ylabel('Share Price ($)') - plt.legend(df_ta.columns) + #plt.ylabel('Share Price ($)') + #if isinstance(df_ta, pd.DataFrame): + # plt.legend(df_ta.columns) plt.grid(b=True, which='major', color='#666666', linestyle='-') plt.minorticks_on() plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2) diff --git a/stock_market_technical_analysis.py b/stock_market_technical_analysis.py index 8de6aa1983f..522e8ca5894 100644 --- a/stock_market_technical_analysis.py +++ b/stock_market_technical_analysis.py @@ -190,3 +190,33 @@ def stoch(l_args, s_ticker, df_stock): except: print("") return + + +# ----------------------------------------------------- RSI ----------------------------------------------------- +def rsi(l_args, s_ticker, df_stock): + parser = argparse.ArgumentParser(prog='rsi', + description=""" The Relative Strength Index (RSI) calculates a ratio of the + recent upward price movements to the absolute price movement. The RSI ranges + from 0 to 100. The RSI is interpreted as an overbought/oversold indicator when + the value is over 70/below 30. You can also look for divergence with price. If + the price is making new highs/lows, and the RSI is not, it indicates a reversal. """) + + parser.add_argument('-p', "--timeperiod", action="store", dest="n_timeperiod", type=check_positive, default=60, + help='The short period.') + + 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: + df_ta = ta.rsi(df_stock['5. adjusted close'], time_period=ns_parser.n_timeperiod).dropna() + + plot_ta(s_ticker, df_ta, f"{ns_parser.n_timeperiod} RSI") + except: + print("") + return \ No newline at end of file