def ad(df_stock: pd.DataFrame, use_open: bool) -> pd.DataFrame: """Calculate AD technical indicator Parameters ---------- df_stock : pd.DataFrame Dataframe of prices use_open : bool Whether to use open prices Returns ------- pd.DataFrame Dataframe with technical indicator """ if use_open: df_ta = ta.ad( high=df_stock["High"], low=df_stock["Low"], close=df_stock["Close"], volume=df_stock["Volume"], open_=df_stock["Open"], ).dropna() # Do not use open stock values else: df_ta = ta.ad( high=df_stock["High"], low=df_stock["Low"], close=df_stock["Close"], volume=df_stock["Volume"], ).dropna() return pd.DataFrame(df_ta)
def add_plots(df: pd.DataFrame, additional_charts: Dict[str, bool]): """Add additional plots to the candle chart. Parameters ---------- df : pd.DataFrame The source data additional_charts : Dict[str, bool] A dictionary of flags to include additional charts Returns ------- Tuple Tuple of lists containing the plots, legends and subplot legends """ panel_number = 2 plots_to_add = [] legends = [] subplot_legends = [] if additional_charts["ad"]: ad = ta.ad(df["High"], df["Low"], df["Close"], df["Volume"]) ad_plot = mpf.make_addplot(ad, panel=panel_number) plots_to_add.append(ad_plot) subplot_legends.extend([panel_number * 2, ["AD"]]) panel_number += 1 if additional_charts["bbands"]: bbands = ta.bbands(df["Close"]) bbands = bbands.drop("BBB_5_2.0", axis=1) bbands_plot = mpf.make_addplot(bbands, panel=0) plots_to_add.append(bbands_plot) legends.extend(["Lower BBand", "Middle BBand", "Upper BBand"]) if additional_charts["cci"]: cci = ta.cci(df["High"], df["Low"], df["Close"]) cci_plot = mpf.make_addplot(cci, panel=panel_number) plots_to_add.append(cci_plot) subplot_legends.extend([panel_number * 2, ["CCI"]]) panel_number += 1 if additional_charts["ema"]: ema = ta.ema(df["Close"]) ema_plot = mpf.make_addplot(ema, panel=0) plots_to_add.append(ema_plot) legends.append("10 EMA") if additional_charts["rsi"]: rsi = ta.rsi(df["Close"]) rsi_plot = mpf.make_addplot(rsi, panel=panel_number) plots_to_add.append(rsi_plot) subplot_legends.extend([panel_number * 2, ["RSI"]]) panel_number += 1 if additional_charts["obv"]: obv = ta.obv(df["Close"], df["Volume"]) obv_plot = mpf.make_addplot(obv, panel=panel_number) plots_to_add.append(obv_plot) subplot_legends.extend([panel_number * 2, ["OBV"]]) panel_number += 1 if additional_charts["sma"]: sma_length = [20, 50] for length in sma_length: sma = ta.sma(df["Close"], length=length) sma_plot = mpf.make_addplot(sma, panel=0) plots_to_add.append(sma_plot) legends.append(f"{length} SMA") if additional_charts["vwap"]: vwap = ta.vwap(df["High"], df["Low"], df["Close"], df["Volume"]) vwap_plot = mpf.make_addplot(vwap, panel=0) plots_to_add.append(vwap_plot) legends.append("vwap") return plots_to_add, legends, subplot_legends
def ad(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( prog='ad', description= """ The Accumulation/Distribution Line is similar to the On Balance Volume (OBV), which sums the volume times +1/-1 based on whether the close is higher than the previous close. The Accumulation/Distribution indicator, however multiplies the volume by the close location value (CLV). The CLV is based on the movement of the issue within a single bar and can be +1, -1 or zero. \n \n The Accumulation/Distribution Line is interpreted by looking for a divergence in the direction of the indicator relative to price. If the Accumulation/Distribution Line is trending upward it indicates that the price may follow. Also, if the Accumulation/Distribution Line becomes flat while the price is still rising (or falling) then it signals an impending flattening of the price.""" ) parser.add_argument('-o', "--offset", action="store", dest="n_offset", type=check_positive, default=0, help='offset') parser.add_argument('--open', action="store_true", default=False, dest="b_use_open", help='uses open value of stock') (ns_parser, l_unknown_args) = parser.parse_known_args(l_args) if l_unknown_args: print( f"The following args couldn't be interpreted: {l_unknown_args}\n") return # Daily if s_interval == "1440min": # Use open stock values if ns_parser.b_use_open: df_ta = ta.ad(high=df_stock['2. high'], low=df_stock['3. low'], close=df_stock['5. adjusted close'], volume=df_stock['6. volume'], offset=ns_parser.n_offset, open_=df_stock['1. open']).dropna() # Do not use open stock values else: df_ta = ta.ad(high=df_stock['2. high'], low=df_stock['3. low'], close=df_stock['5. adjusted close'], volume=df_stock['6. volume'], offset=ns_parser.n_offset).dropna() axPrice = plt.subplot(211) plt.plot(df_stock.index, df_stock['5. adjusted close'].values, 'k', lw=2) plt.title(f"Accumulation/Distribution Line (AD) on {s_ticker}") plt.xlim(df_stock.index[0], df_stock.index[-1]) plt.ylabel(f'Share Price ($)') plt.grid(b=True, which='major', color='#666666', linestyle='-') plt.minorticks_on() plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2) axVolume = axPrice.twinx() plt.bar(df_stock.index, df_stock['6. volume'].values, color='k', alpha=0.8, width=.3) plt.subplot(212) plt.plot(df_ta.index, df_ta.values, 'b', lw=1) plt.xlim(df_stock.index[0], df_stock.index[-1]) plt.axhline(0, linewidth=2, color='k', ls='--') plt.legend([f'Chaikin Oscillator']) plt.xlabel('Time') plt.grid(b=True, which='major', color='#666666', linestyle='-') plt.minorticks_on() plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2) plt.show() # Intraday else: # Use open stock values if ns_parser.b_use_open: df_ta = ta.ad(high=df_stock['2. high'], low=df_stock['3. low'], close=df_stock['4. close'], volume=df_stock['5. volume'], offset=ns_parser.n_offset, open_=df_stock['1. open']).dropna() # Do not use open stock values else: df_ta = ta.ad(high=df_stock['2. high'], low=df_stock['3. low'], close=df_stock['4. close'], volume=df_stock['5. volume'], offset=ns_parser.n_offset).dropna() axPrice = plt.subplot(211) plt.plot(df_stock.index, df_stock['4. close'].values, 'k', lw=2) plt.title(f"Accumulation/Distribution Line (AD) on {s_ticker}") plt.xlim(df_stock.index[0], df_stock.index[-1]) plt.ylabel(f'Share Price ($)') plt.grid(b=True, which='major', color='#666666', linestyle='-') plt.minorticks_on() plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2) axVolume = axPrice.twinx() plt.bar(df_stock.index, df_stock['5. volume'].values, color='k', alpha=0.8, width=.3) plt.subplot(212) plt.plot(df_ta.index, df_ta.values, 'b', lw=1) plt.xlim(df_stock.index[0], df_stock.index[-1]) plt.axhline(0, linewidth=2, color='k', ls='--') plt.legend([f'Chaikin Oscillator']) plt.xlabel('Time') plt.grid(b=True, which='major', color='#666666', linestyle='-') plt.minorticks_on() plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2) plt.show() print("")
def ad(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( prog="ad", description=""" The Accumulation/Distribution Line is similar to the On Balance Volume (OBV), which sums the volume times +1/-1 based on whether the close is higher than the previous close. The Accumulation/Distribution indicator, however multiplies the volume by the close location value (CLV). The CLV is based on the movement of the issue within a single bar and can be +1, -1 or zero. \n \n The Accumulation/Distribution Line is interpreted by looking for a divergence in the direction of the indicator relative to price. If the Accumulation/Distribution Line is trending upward it indicates that the price may follow. Also, if the Accumulation/Distribution Line becomes flat while the price is still rising (or falling) then it signals an impending flattening of the price. """, ) parser.add_argument( "-o", "--offset", action="store", dest="n_offset", type=check_positive, default=0, help="offset", ) parser.add_argument( "--open", action="store_true", default=False, dest="b_use_open", help="uses open value of stock", ) try: ns_parser = parse_known_args_and_warn(parser, l_args) # Daily if s_interval == "1440min": # Use open stock values if ns_parser.b_use_open: df_ta = ta.ad( high=df_stock["2. high"], low=df_stock["3. low"], close=df_stock["5. adjusted close"], volume=df_stock["6. volume"], offset=ns_parser.n_offset, open_=df_stock["1. open"], ).dropna() # Do not use open stock values else: df_ta = ta.ad( high=df_stock["2. high"], low=df_stock["3. low"], close=df_stock["5. adjusted close"], volume=df_stock["6. volume"], offset=ns_parser.n_offset, ).dropna() # Intraday else: # Use open stock values if ns_parser.b_use_open: df_ta = ta.ad( high=df_stock["2. high"], low=df_stock["3. low"], close=df_stock["4. close"], volume=df_stock["5. volume"], offset=ns_parser.n_offset, open_=df_stock["1. open"], ).dropna() # Do not use open stock values else: df_ta = ta.ad( high=df_stock["2. high"], low=df_stock["3. low"], close=df_stock["4. close"], volume=df_stock["5. volume"], offset=ns_parser.n_offset, ).dropna() plt.figure() axPrice = plt.subplot(211) if s_interval == "1440min": plt.plot(df_stock.index, df_stock["5. adjusted close"].values, "k", lw=2) else: plt.plot(df_stock.index, df_stock["4. close"].values, "k", lw=2) plt.title(f"Accumulation/Distribution Line (AD) on {s_ticker}") plt.xlim(df_stock.index[0], df_stock.index[-1]) plt.ylabel("Share Price ($)") plt.grid(b=True, which="major", color="#666666", linestyle="-") plt.minorticks_on() plt.grid(b=True, which="minor", color="#999999", linestyle="-", alpha=0.2) _ = axPrice.twinx() if s_interval == "1440min": plt.bar( df_stock.index, df_stock["6. volume"].values, color="k", alpha=0.8, width=0.3, ) else: plt.bar( df_stock.index, df_stock["5. volume"].values, color="k", alpha=0.8, width=0.3, ) plt.subplot(212) plt.plot(df_ta.index, df_ta.values, "b", lw=1) plt.xlim(df_stock.index[0], df_stock.index[-1]) plt.axhline(0, linewidth=2, color="k", ls="--") plt.legend(["Chaikin Oscillator"]) plt.xlabel("Time") plt.grid(b=True, which="major", color="#666666", linestyle="-") plt.minorticks_on() plt.grid(b=True, which="minor", color="#999999", linestyle="-", alpha=0.2) plt.ion() plt.show() print("") except Exception as e: print(e) print("") return
def add_plots(df, ns_parser): panel_number = 2 plots_to_add = [] legends = [] subplot_legends = [] if ns_parser.ad: ad = ta.ad(df["High"], df["Low"], df["Close"], df["Volume"]) ad_plot = mpf.make_addplot(ad, panel=panel_number) plots_to_add.append(ad_plot) subplot_legends.extend([panel_number * 2, ["AD"]]) panel_number += 1 if ns_parser.bbands: bbands = ta.bbands(df["Close"]) bbands = bbands.drop("BBB_5_2.0", axis=1) bbands_plot = mpf.make_addplot(bbands, panel=0) plots_to_add.append(bbands_plot) legends.extend(["Lower BBand", "Middle BBand", "Upper BBand"]) if ns_parser.cci: cci = ta.cci(df["High"], df["Low"], df["Close"]) cci_plot = mpf.make_addplot(cci, panel=panel_number) plots_to_add.append(cci_plot) subplot_legends.extend([panel_number * 2, ["CCI"]]) panel_number += 1 if ns_parser.ema: ema = ta.ema(df["Close"]) ema_plot = mpf.make_addplot(ema, panel=0) plots_to_add.append(ema_plot) legends.append("10 EMA") if ns_parser.rsi: rsi = ta.rsi(df["Close"]) rsi_plot = mpf.make_addplot(rsi, panel=panel_number) plots_to_add.append(rsi_plot) subplot_legends.extend([panel_number * 2, ["RSI"]]) panel_number += 1 if ns_parser.obv: obv = ta.obv(df["Close"], df["Volume"]) obv_plot = mpf.make_addplot(obv, panel=panel_number) plots_to_add.append(obv_plot) subplot_legends.extend([panel_number * 2, ["OBV"]]) panel_number += 1 if ns_parser.sma: sma_length = [20, 50] for length in sma_length: sma = ta.sma(df["Close"], length=length) sma_plot = mpf.make_addplot(sma, panel=0) plots_to_add.append(sma_plot) legends.append(f"{length} SMA") if ns_parser.vwap: vwap = ta.vwap(df["High"], df["Low"], df["Close"], df["Volume"]) vwap_plot = mpf.make_addplot(vwap, panel=0) plots_to_add.append(vwap_plot) legends.append("vwap") return plots_to_add, legends, subplot_legends