def indicator_adder(df): ''' this adds the most common indicators to a dataframe with lower case close open high low volume takes: dataframe returns : dataframe ''' df['rsi'] = pta.rsi(df.close) df['riz'] = pta.rsi(df.close,length=2) df['vwap'] = pta.vwap(df.high,df.low,df.close,df.volume) df[['stoch_k','stoch_d']] = pta.stoch(df.high,df.low,df.close) if len(df)>20: df['ema'] = pta.ema(df.close,length=20) #if len(df)>6: #df = super_trend(df) return df
def vwap(day_df: pd.DataFrame, offset: int) -> pd.DataFrame: """Gets volume weighted average price (VWAP) Parameters ---------- day_df: pd.DataFrame Dataframe of dates and prices for the last trading day offset: int Length of offset Returns ---------- df_vwap: pd.DataFrame Dataframe with VWAP data """ df_vwap = ta.vwap( high=day_df["High"], low=day_df["Low"], close=day_df["Close"], volume=day_df["Volume"], offset=offset, ) return pd.DataFrame(df_vwap)
def vwap(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( add_help=False, prog="vwap", description=""" The Volume Weighted Average Price that measures the average typical price by volume. It is typically used with intraday charts to identify general direction. """, ) parser.add_argument( "-o", "--offset", action="store", dest="n_offset", type=check_positive, default=0, help="offset", ) try: ns_parser = parse_known_args_and_warn(parser, l_args) if not ns_parser: return # Daily if s_interval == "1440min": df_ta = ta.vwap( 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, ) # Intraday else: df_ta = ta.vwap( 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, ) _, axPrice = plt.subplots() if s_interval == "1440min": plt.plot(df_stock.index, df_stock["5. adjusted close"].values, color="k") else: plt.plot(df_stock.index, df_stock["4. close"].values, color="k") plt.plot(df_ta.index, df_ta.values) plt.title(f"VWAP on {s_ticker}") plt.xlim(df_stock.index[0], df_stock.index[-1]) plt.xlabel("Time") plt.ylabel("Share Price ($)") plt.legend([s_ticker, "VWAP"]) _ = 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.ylabel("Volume") 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("")
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 integrate_with_indicators_forecasting(input_path, output_path, start_date, test_set, end_date_for_clustering): folder_creator(PATH_INTEGRATED_FOLDER, 1) for crypto in os.listdir(input_path): for date_to_predict in test_set: #end_date=pd.to_datetime(date_to_predict) - timedelta(days=1) end_date = date_to_predict df = cut_dataset_by_range(input_path, crypto.replace(".csv", ""), start_date, end_date, features_to_use=None) df["Date"] = pd.to_datetime(df["Date"]) day_to_predict = df.loc[len(df.Date) - 1] df = df[:-1] #remove the day to predict #df = df.sort_values('Date', ascending=True) data_series_of_target_feature = df[TARGET_FEATURE] for lookback_value in LOOKBACK_TEST: df['VWAP'] = panda.vwap(df['High'], df['Low'], df['Close'], df['Volume'], lookback_value=lookback_value) for lookback_value in LOOKBACK_TEST: df[str('SMA_' + str(lookback_value))] = get_SMA( data_series_of_target_feature, lookback_value) for lookback_value in LOOKBACK_TEST: df[str('EMA_' + str(lookback_value))] = get_EMA( data_series_of_target_feature, lookback_value) for lookback_value in LOOKBACK_TEST: df[str('RSI_' + str(lookback_value))] = get_RSI( data_series_of_target_feature, lookback_value) df_macd = get_MACD(data_series_of_target_feature) df['MACD_12_26_9'] = df_macd['MACD_12_26_9'] df['MACDH_12_26_9'] = df_macd['MACDH_12_26_9'] df['MACDS_12_26_9'] = df_macd['MACDS_12_26_9'] df_bbs = panda.bbands(data_series_of_target_feature) df['BBL_20'] = df_bbs['BBL_20'] df['BBM_20'] = df_bbs['BBM_20'] df['BBU_20'] = df_bbs['BBU_20'] df['MOM'] = panda.mom(data_series_of_target_feature) df_stoch = panda.stoch(df['High'], df['Low'], df['Close']) df['STOCHF_14'] = df_stoch['STOCHF_14'] df['STOCHF_3'] = df_stoch['STOCHF_3'] df['STOCH_5'] = df_stoch['STOCH_5'] df['STOCH_3'] = df_stoch['STOCH_3'] df['CMO'] = panda.cmo(data_series_of_target_feature) df['DPO'] = panda.dpo(data_series_of_target_feature) df['UO'] = panda.uo(df['High'], df['Low'], df['Close']) df['lag_1'] = df['Close'].shift(1) """df['lag_7'] = df['Close'].shift(7) df = df.iloc[7:]""" df = df.append(day_to_predict, ignore_index=True) df.fillna(value=0, inplace=True) df.to_csv(os.path.join( output_path, crypto.replace(".csv", "") + str("_" + date_to_predict) + ".csv"), sep=",", index=False)
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
sns.set() initial = pd.read_csv( 'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&interval=1min&symbol=msft&apikey=OUMVBY0VK0HS8I9E&datatype=csv&outputsize=full' ) initial = initial[::-1].reset_index(drop=True) volume = initial['volume'] initial.drop(['volume', 'timestamp'], axis=1, inplace=True) bbands = ta.bbands(initial['close'], length=50, std=2) #calculating indicators rsi = ta.rsi(initial['close'], length=20) ema_50 = ta.ema(initial['close'], length=5) ema_200 = ta.ema(initial['close'], length=20) ema_500 = ta.ema(initial['close'], length=50) #macd = ta.macd(initial['close'], 5, 35, 5) vwap = ta.vwap(initial['high'], initial['low'], initial['close'], volume) initial = pd.concat([initial, bbands, ema_50, ema_200, ema_500, vwap], axis=1) initial.columns = [ 'open', 'high', 'low', 'close', 'bband1', 'useless', 'bband2', 'ema1', 'ema2', 'ema3', 'vwap' ] initialInvestment = 1000 b = backtester.Backtester(initialInvestment) initial = initial.dropna() X = initial[:-1] y = initial['close'].shift(-1) X = X.dropna() y = y.dropna() X_train, X_test, y_train, y_test = train_test_split(X,
def vwap(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( prog='vwap', description= """ The Volume Weighted Average Price that measures the average typical price by volume. It is typically used with intraday charts to identify general direction. """ ) 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) if l_unknown_args: print( f"The following args couldn't be interpreted: {l_unknown_args}\n" ) return # Daily if s_interval == "1440min": df_ta = ta.vwap(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) pfig, axPrice = plt.subplots() plt.plot(df_stock.index, df_stock['5. adjusted close'].values, color='k') plt.plot(df_ta.index, df_ta.values) plt.title(f"VWAP on {s_ticker}") plt.xlim(df_stock.index[0], df_stock.index[-1]) plt.xlabel('Time') plt.ylabel('Share Price ($)') plt.legend([s_ticker, "VWAP"]) axVolume = axPrice.twinx() plt.bar(df_stock.index, df_stock['6. volume'].values, color='k', alpha=0.8, width=.3) plt.ylabel('Volume') 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("") # Intraday else: df_ta = ta.vwap(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) pfig, axPrice = plt.subplots() plt.plot(df_stock.index, df_stock['4. close'].values, color='k') plt.plot(df_ta.index, df_ta.values) plt.title(f"VWAP on {s_ticker}") plt.xlim(df_stock.index[0], df_stock.index[-1]) plt.xlabel('Time') plt.ylabel('Share Price ($)') plt.legend([s_ticker, "VWAP"]) axVolume = axPrice.twinx() plt.bar(df_stock.index, df_stock['5. volume'].values, color='k', alpha=0.8, width=.3) plt.ylabel('Volume') 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("") except: print("")