diff --git a/stock_market_cli.py b/stock_market_cli.py index 94ae1025d4b..1d20d37d3db 100644 --- a/stock_market_cli.py +++ b/stock_market_cli.py @@ -70,9 +70,11 @@ def print_help(s_ticker, s_start, s_interval, b_is_market_open): print(" adx average directional movement index") print(" cci commodity channel index") print(" aroon aroon indicator") + print(" bbands bollinger bands") if s_interval != "1440min": print(" vwap volume weighted average price") + ''' print("\nPrediction:") print(" ma") print(" ema") @@ -82,7 +84,7 @@ def print_help(s_ticker, s_start, s_interval, b_is_market_open): print(" rnn") print(" lstm") print(" prophet") - + ''' print(f"\nMarket {('CLOSED', 'OPEN')[b_is_market_open]}.") print("Stonks and things") @@ -116,7 +118,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', 'rsi', 'adx', - 'cci', 'aroon', 'ratings']) + 'cci', 'aroon', 'bbands', 'ratings']) # Print first welcome message and help print("\nWelcome to Didier's Stock Market Bot\n") @@ -229,6 +231,11 @@ def main(): smta.aroon(l_args, s_ticker, s_interval, df_stock) continue + # --------------------------------------------------- BBANDS --------------------------------------------------- + elif ns_known_args.cmd == 'bbands': + smta.bbands(l_args, s_ticker, s_interval, df_stock) + continue + # -------------------------------------------------------------------------------------------------------------- # ------------------------------------------------ PREDICTION -------------------------------------------------- # -------------------------------------------------------------------------------------------------------------- diff --git a/stock_market_helper_funcs.py b/stock_market_helper_funcs.py index ba096e0b9b2..4318f53f734 100644 --- a/stock_market_helper_funcs.py +++ b/stock_market_helper_funcs.py @@ -42,13 +42,20 @@ def plot_view_stock(df, symbol): # ----------------------------------------------------------------------------------------------------------------------- def plot_stock_ta(df_stock, s_ticker, df_ta, s_ta): - plt.plot(df_stock.index, df_stock.values) + plt.plot(df_stock.index, df_stock.values, color='k') plt.plot(df_ta.index, df_ta.values) plt.title(f"{s_ta} on {s_ticker}") plt.xlim(df_stock.index[0], df_stock.index[-1]) plt.xlabel('Time') plt.ylabel('Share Price ($)') - plt.legend([s_ticker, s_ta]) + # Pandas series + if len(df_ta.shape) == 1: + l_legend = [s_ticker, s_ta] + # Pandas dataframe + else: + l_legend = df_ta.columns.tolist() + l_legend.insert(0, s_ticker) + plt.legend(l_legend) 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 819bbc26023..1e60283ee28 100644 --- a/stock_market_technical_analysis.py +++ b/stock_market_technical_analysis.py @@ -28,7 +28,7 @@ def sma(l_args, s_ticker, s_interval, df_stock): if l_unknown_args: print(f"The following args couldn't be interpreted: {l_unknown_args}") - try: + try: # Daily if s_interval == "1440min": df_ta = ta.sma(df_stock['5. adjusted close'], length=ns_parser.n_length, offset=ns_parser.n_offset).dropna() @@ -36,7 +36,8 @@ def sma(l_args, s_ticker, s_interval, df_stock): # Intraday else: df_ta = ta.sma(df_stock['4. close'], length=ns_parser.n_length, offset=ns_parser.n_offset).dropna() - plot_stock_ta(df_stock['4. close'], s_ticker, df_ta, "SMA") + plot_stock_ta(df_stock['4. close'], s_ticker, df_ta, "SMA") + except: print("") return @@ -339,4 +340,49 @@ def aroon(l_args, s_ticker, s_interval, df_stock): plot_ta(s_ticker, df_ta, "AROON") except: print("") - return \ No newline at end of file + return + + +# ----------------------------------------------------- BBANDS ----------------------------------------------------- +def bbands(l_args, s_ticker, s_interval, df_stock): + parser = argparse.ArgumentParser(prog='bbands', + description=""" Bollinger Bands consist of three lines. The middle band is a simple + moving average (generally 20 periods) of the typical price (TP). The upper and lower + bands are F standard deviations (generally 2) above and below the middle band. + The bands widen and narrow when the volatility of the price is higher or lower, respectively. + \n \nBollinger Bands do not, in themselves, generate buy or sell signals; they are an + indicator of overbought or oversold conditions. When the price is near the upper or lower + band it indicates that a reversal may be imminent. The middle band becomes a support or + resistance level. The upper and lower bands can also be interpreted as price targets. + When the price bounces off of the lower band and crosses the middle band, then the + upper band becomes the price target. """) + + parser.add_argument('-l', "--length", action="store", dest="n_length", type=check_positive, default=5, help='length') + parser.add_argument('-s', "--std", action="store", dest="n_std", type=check_positive, default=2, help='std') + parser.add_argument('-m', "--mamode", action="store", dest="s_mamode", default="sma", help='mamode') + parser.add_argument('-o', "--offset", action="store", dest="n_offset", type=check_positive, default=0, help='offset') + + 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: + # Daily + if s_interval == "1440min": + df_ta = ta.bbands(close=df_stock['5. adjusted close'], length=ns_parser.n_length, std=ns_parser.n_std, + mamode=ns_parser.s_mamode, offset=ns_parser.n_offset).dropna() + plot_stock_ta(df_stock['5. adjusted close'], s_ticker, df_ta, "BBANDS") + # Intraday + else: + df_ta = ta.bbands(close=df_stock['4. close'], length=ns_parser.n_length, std=ns_parser.n_std, + mamode=ns_parser.s_mamode, offset=ns_parser.n_offset).dropna() + plot_stock_ta(df_stock['4. close'], s_ticker, df_ta, "BBANDS") + + except: + print("") + return