def bollinger_plot(company_name, new_df, time=0): fig = plt.figure(figsize=(20, 20)) ax0 = fig.add_subplot(211) ax1 = fig.add_subplot(212) x_axis = new_df.index[time:] MACD = ta.macd(new_df.close) buy_signal, sell_signal, buy_index, sell_index = get_MACD(new_df) ax0.fill_between(x_axis, new_df["Upper"][time:], new_df["Lower"][time:], color="grey") ax0.plot(x_axis, new_df["close"][time:], color="Gold", lw=2, label="Close price") ax0.plot(x_axis, new_df["SMA"][time:], color="Blue", lw=2, label="SMA") ax1.bar(MACD["MACDh_12_26_9"].index, MACD["MACDh_12_26_9"], label="bar") ax1.plot(MACD['MACD_12_26_9'], label="MACD") ax1.plot(MACD["MACDs_12_26_9"], label="MACDS") ax1.scatter(buy_index, buy_signal, color="red", label="sell signal", marker="v", alpha=1) ax1.scatter(sell_index, sell_signal, color="green", label="buy signal", marker="^", alpha=1) ax0.legend() ax1.legend() ax0.title.set_text(company_name) ax1.title.set_text(company_name) # fig.tight_layout() fig.tight_layout() return fig
def get_df(symbol='BTCUSDT', tf='1h') -> pd.DataFrame: interval_dict = { '1m': Client.KLINE_INTERVAL_1MINUTE, '5m': Client.KLINE_INTERVAL_5MINUTE, '15m': Client.KLINE_INTERVAL_15MINUTE, '1h': Client.KLINE_INTERVAL_1HOUR, '4h': Client.KLINE_INTERVAL_4HOUR, '1d': Client.KLINE_INTERVAL_1DAY, '1w': Client.KLINE_INTERVAL_1WEEK, '1M': Client.KLINE_INTERVAL_1MONTH, } klines = client.futures_klines(symbol=symbol, interval=interval_dict[tf]) rows_list = [] for kline in klines: row_dict = { 'date': datetime.fromtimestamp(int(str(kline[0])[0:10])), 'open': float(kline[1]), 'high': float(kline[2]), 'low': float(kline[3]), 'close': float(kline[4]), 'volume': float(kline[5]), } rows_list.append(row_dict) df = pd.DataFrame(rows_list) df['rsi'] = ta.rsi(df.close, 14) df = pd.concat((df, ta.macd(df.close, 12, 26, 9)), axis=1) df['ema_20'], df['ema_50'] = ta.ema(df.close, 20), ta.ema(df.close, 50) if len(df) >= 288: df['ema_200'] = ta.ema(df.close, 200) else: df['ema_200'] = ta.ema(df.close, len(df.close)-3) df.set_index('date', inplace=True) df.rename_axis('date', inplace=True) df = df.tail(88) return df
def macd( values: pd.DataFrame, n_fast: int = 12, n_slow: int = 26, n_signal: int = 9, ) -> pd.DataFrame: """Moving average convergence divergence Parameters ---------- values: pd.Series Values for calculation n_fast : int Fast period n_slow : int Slow period n_signal : int Signal period Returns ---------- pd.DataFrame Dataframe of technical indicator """ return pd.DataFrame( ta.macd(values, fast=n_fast, slow=n_slow, signal=n_signal).dropna() )
def bollinger_plot(company_name, new_df, time=0): fig, ax = plt.subplots(4, figsize=(15, 15)) x_axis = new_df.index[time:] ax[0].fill_between(x_axis, new_df["Upper"][time:], new_df["Lower"][time:], color="grey") ax[0].plot(x_axis, new_df["close"][time:], color="Gold", lw=2, label="Close price") ax[0].plot(x_axis, new_df["SMA"][time:], color="Blue", lw=2, label="SMA") ax[1].plot((ta.coppock(new_df.close))) (buy_signal, sell_signal, buy_index, sell_index) = get_coppock(new_df) ax[1].scatter(buy_index, buy_signal, color="green", label="Buy signal", marker="^", alpha=1) ax[1].scatter(sell_index, sell_signal, color="red", label="Sell signal", marker="v", alpha=1) MACD = ta.macd(new_df.close, fast = 5, slow = 35, signal = 5) buy_signal, sell_signal, buy_index, sell_index = get_MACD(new_df, fast = 5, slow = 35, signal = 5) ax[2].bar(MACD.iloc[:,1].index, MACD.iloc[:,1], label="bar") ax[2].plot(MACD.iloc[:,0] , label="MACD") ax[2].plot(MACD.iloc[:,2], label="MACDS") ax[2].scatter(buy_index, buy_signal, color="red", label="sell signal", marker="v", alpha=1) ax[2].scatter(sell_index, sell_signal, color="green", label="buy signal", marker="^", alpha=1) ax[2].legend() ax[1].legend() ax[0].scatter(x_axis, new_df["Bollinger Buy"][time:], color="green", label="Buy signal", marker="^", alpha=1) ax[0].scatter(x_axis, new_df["Bollinger Sell"][time:], color="red", label="Sell signal", marker="v", alpha=1) ax[0].set_title(company_name) ax[3].plot(ta.rsi(new_df.close)) ax[3].axhline(30, linestyle="--", color="red") ax[3].axhline(70, linestyle="--", color="green") plt.legend() plt.savefig("{}".format(str(datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")) + ".png"))
def MACD_backtest(df, funds = 10, difference_coef = 2, current_coef = 1e01, intercept = funds/2, win = 0, loss = 0, gradient = True, gradient_threshold = 0.1, difference_bool = True, sell_params = True, difference_threshold = 10, fast = 12, slow = 26, signal = 9, gain_list = [], loss_list = [], kelly = 0.5, RSI_bool = True, RSI_buy_parameter = 40, RSI_sell_parameter = 60): original_funds = funds RSI = ta.rsi(df.close) funds = funds (buy_signal, sell_signal, sell_index, buy_index) = CryptoFunctions.get_MACD(df, fast = fast, slow = slow, signal = signal) MACD = ta.macd(df.close) if sell_index[0] < buy_index[0]: del sell_index[0] if buy_index[-1] > sell_index[-1]: del buy_index[-1] total_profit = 0 total_exposure = 0 length = len(buy_index) for index in range(len(buy_index)): current_rsi = RSI[buy_index[index]] if buy_index[index] <= 50: mean_rsi = np.nanmean(RSI[buy_index[index] - 20:buy_index[index]]) else: mean_rsi = np.nanmean(RSI[buy_index[index]-50:buy_index[index]]) RSI_difference = current_rsi - mean_rsi if gradient and difference_bool and RSI_bool: #print("COP") macd_buy_gradient = MACD.iloc[:,0].diff()[buy_index[index]] macd_sell_gradient = MACD.iloc[:,0].diff()[sell_index[index]] #if macd_buy_gradient <= gradient_threshold and RSI_difference >= -difference_threshold and if current_rsi > RSI_buy_parameter: if index + 1 < length: index += 1 if sell_params: #if macd_sell_gradient >= -gradient_threshold and RSI_difference <= difference_threshold and \ if current_rsi < RSI_sell_parameter: if index + 1 < length: index += 1 #print(kelly) amount_to_invest = kelly*funds total_exposure += amount_to_invest funds -= amount_to_invest buy_price = df.iloc[buy_index[index],4] held = amount_to_invest/buy_price sell_price = df.iloc[sell_index[index],4] funds += sell_price*held difference = sell_price - buy_price if difference >= 0: win += 1 gain_list.append(difference*held) else: loss += 1 loss_list.append(difference*held) total_profit += held*difference fees = 0.0025*amount_to_invest + 0.0025*sell_price*held total_profit -= fees gain_percentage = (total_profit/original_funds)*100 return (total_profit, total_exposure, total_exposure/gain_percentage, gain_percentage, win, loss, gain_list, loss_list)
def get_macd(source): ''' ### MACD help(ta.macd) ''' # MACD # Recommended parameters: 12_26_9, 5, 35, 5 fast_macd = [12, 5] slow_macd = [26, 35] signal_macd = [9, 5] close = source['Close'] macd_features = pd.DataFrame(index=source.index) # def ctitle(indicator_name, ticker='SPY', length=100): # return f"{ticker}: {indicator_name} from {recent_startdate} to {recent_startdate} ({length})" # recent_startdate = source_cut.tail(recent).index[0] # recent_enddate = source_cut.tail(recent).index[-1] # price_size = (16, 8) # ind_size = (16, 2) # ticker = 'SPY' # recent = 126 # half_of_recent = int(0.5 * recent) # def plot_MACD(macddf): # macddf[[macddf.columns[0], macddf.columns[2]]].tail(recent).plot(figsize=(16, 2), color=cscheme('BkBu'), linewidth=1.3) # macddf[macddf.columns[1]].tail(recent).plot.area(figsize=ind_size, stacked=False, color=['silver'], linewidth=1, title=ctitle(macddf.name, ticker=ticker, length=recent), grid=True).axhline(y=0, color="black", lw=1.1) for fmacd, smacd, sigmacd in zip(fast_macd, slow_macd, signal_macd): print("Generate fast mcd={}, slow macd={}, signal macd={}".format( fmacd, smacd, sigmacd)) macddf = ta.macd(close, fast=fmacd, slow=smacd, signal=sigmacd) # display(macddf.iloc[:,0].head(50)) # plot_MACD(macddf) macd_features = macd_features.join( pd.Series(macddf[macddf.columns[0]], name='MACD_' + str(fmacd) + "_" + str(smacd) + "_" + str(sigmacd))) macd_features = macd_features.join( pd.Series(macddf[macddf.columns[2]], name='MACDS_' + str(fmacd) + "_" + str(smacd) + "_" + str(sigmacd))) print("Number of features: {}".format(macd_features.shape)) print(macd_features.iloc[20:40, :]) #macddf = ta.macd(close, fast=8, slow=21, signal=9, min_periods=None, append=True) # features = features.join(macd_features) return macd_features
def calc_macd(self, ema_sht: int, ema_lng: int, sig_period: int, fillna: int = 0): """Function to create a macd dataframe from the data""" # macd_data = calc_macd(self.data, ema_lng, ema_sht, sig_period) macd_data = ta.macd(self.data, ema_sht, ema_lng, sig_period, fillna=fillna) return macd_data
def macd(s_interval: str, df_stock: pd.DataFrame, n_fast: int, n_slow: int, n_signal: int) -> pd.DataFrame: """Moving average convergence divergence Parameters ---------- s_interval: str Stock time interval df_stock: pd.DataFrame Dataframe of prices n_fast : int Fast period n_slow : int Slow period n_signal : int Signal period Returns ---------- df_ta: pd.DataFrame Dataframe of technical indicator """ # Daily if s_interval == "1440min": df_ta = ta.macd(df_stock["Adj Close"], fast=n_fast, slow=n_slow, signal=n_signal).dropna() # Intraday else: df_ta = ta.macd(df_stock["Close"], fast=n_fast, slow=n_slow, signal=n_signal).dropna() return pd.DataFrame(df_ta)
def __call__(self, *args, **kwargs): self.data_center = DataCenter() if len(self.ts_codes) == 0: return result = pandas.DataFrame(columns=('ts_code', 'in_price', 'in_date', 'origin_from', 'in_reason', 'finished', 'manual')) for ts_code in self.ts_codes: field_suffix = "_" + str(self.fast) + "_" + str( self.slow) + "_" + str(self.signal) macd_field_name = 'MACD' + field_suffix histogram_field_name = 'MACDh' + field_suffix signal_field_name = 'MACDs' + field_suffix base_infos = self.data_center.fetch_base_data(ts_code) if base_infos is None or len(base_infos) == 0: continue close = base_infos['close'] macd_ret = ta.macd(close, self.fast, self.slow, signal=self.signal) if len(macd_ret) < 2: continue rst_length = len(macd_ret) start_index = min(rst_length, self.BUY_SIGNAL_PERIOD) start_index = rst_length - start_index low_flag = False for i in range(start_index, rst_length): if macd_ret.at[i, macd_field_name] is not None and macd_ret.at[i, signal_field_name] is not None and \ macd_ret.at[i, macd_field_name] < macd_ret.at[i, signal_field_name]: low_flag = True else: if low_flag: now_time = datetime.datetime.now() now_time_str = now_time.strftime('%Y%m%d') temp_dict = { 'ts_code': ts_code, 'in_price': close[i], 'in_date': base_infos.at[start_index, 'trade_date'], 'origin_from': 'macd', 'in_reason': 'macd金叉', 'finished': 0, 'manual': 0 } result = result.append(temp_dict, ignore_index=True) return result
def on_candle(self, symbol, candles, db): candle = candles.iloc[-1] hours = int(datetime.strftime(candle.date, '%H')) mins = int(datetime.strftime(candle.date, '%M')) macdDf = ta.macd(close=candles['close'], fast=12, slow=21, signal=9) stochDf = ta.stoch(high=candles['high'], low=candles['low'], close=candles['close']) macd = macdDf.iloc[-1]['MACD_12_21_9'] macd_signal = macdDf.iloc[-1]['MACDs_12_21_9'] stoch = stochDf.iloc[-1]['STOCHk_14_3_3'] prev_stoch = stochDf.iloc[-2]['STOCHk_14_3_3'] #print(candle.date, 'macd>signal:', macd > macd_signal, 'stoch>30:', stoch>30, 'stoch>prev_stoch:', stoch > prev_stoch) buy_signal = bool(macd > macd_signal and stoch > 30 and stoch > prev_stoch) #donchian_low = ta.donchian(candles["high"], candles["low"], 20, 20).iloc[-1]["DCL_20_20"] #self.plotter.addPtToLine(symbol, 'Donchian Low', candle.date, donchian_low) #self.check_positions(candle, candles) def getSl(candle, candles): atr60 = ta.atr(candles["high"], candles["low"], candles["close"], length=60, mamode="ema").iloc[-1] return candle['low'] - atr60 if buy_signal and not self.trader.position_open_symbol(symbol): self.sl = getSl(candle, candles) self.plotter.addPtToPointsGroup(symbol, 'SL', candle['date'], self.sl) self.pos_ids[symbol] = self.trader.openPosition( symbol, 'buy', 1, candle['close'], candle['date'], self.sl) elif self.trader.position_open_symbol(symbol): updated_sl = getSl(candle, candles) if updated_sl > self.sl: self.sl = updated_sl self.plotter.addPtToPointsGroup(symbol, 'SL', candle['date'], self.sl) if candle['low'] < self.sl: self.trader.closePosition(self.pos_ids[symbol], self.sl, candle['date'], 'Hit stop loss')
def get_MACD(df): MACD = ta.macd(df.close) crosses = np.argwhere(np.diff(np.sign(MACD["MACD_12_26_9"] - MACD["MACDs_12_26_9"]))).flatten() buy_signal = [] sell_signal = [] buy_index = [] sell_index = [] values = [] values2 = [] for i in (MACD["MACD_12_26_9"][crosses]): values.append(i) for i in (MACD["MACDs_12_26_9"][crosses]): values2.append(i) for i in range(len(values)): if values[i] > values2[i]: buy_index.append(MACD["MACD_12_26_9"][crosses].index[i]) buy_signal.append(values[i]) elif values[i] <= values2[i]: sell_index.append(MACD["MACDs_12_26_9"][crosses].index[i]) sell_signal.append(values2[i]) return (buy_signal, sell_signal, buy_index, sell_index)
def get_MACD(df, fast = 12, slow = 26, signal = 9): MACD = ta.macd(df.close, fast = fast, slow = slow, signal = signal) # print(MACD.iloc[:,2]) crosses = np.argwhere(np.diff(np.sign(MACD.iloc[:,0] - MACD.iloc[:,2]))).flatten() buy_signal = [] sell_signal = [] buy_index = [] sell_index = [] values = [] values2 = [] for i in (MACD.iloc[:,0][crosses]): values.append(i) for i in (MACD.iloc[:,2][crosses]): values2.append(i) # print(values, values2) for i in range(len(values)): if values[i] > values2[i]: buy_index.append(MACD.iloc[:,0][crosses].index[i]) buy_signal.append(values[i]) elif values[i] <= values2[i]: sell_index.append(MACD.iloc[:,2][crosses].index[i]) sell_signal.append(values2[i]) return (buy_signal, sell_signal, buy_index, sell_index)
st.line_chart(data[['Adj Close', 'SMA', 'EMA']]) # Bollinger Bands data[['lower_band', 'middle_band', 'upper_band']] = ta1.bbands(data['Adj Close'], timeperiod=20).iloc[:, 0:3] # Plot st.header(f"Bollinger Bands\n {company_name}") st.line_chart(data[['Adj Close', 'upper_band', 'middle_band', 'lower_band']]) # ## MACD (Moving Average Convergence Divergence) # MACD data[['macd', 'macdhist', 'macdsignal']] = ta1.macd(data['Adj Close'], fastperiod=12, slowperiod=26, signalperiod=9).iloc[:, 0:3] # Plot st.header(f"Moving Average Convergence Divergence\n {company_name}") st.line_chart(data[['macd', 'macdsignal']]) ## CCI (Commodity Channel Index) # CCI cci = ta.trend.cci(data['High'], data['Low'], data['Close'], window=31, constant=0.015)
end=end_date) # create ta indicators df[rsi] = ta.rsi(df['Adj Close'], length=rsi_length) # rsi df[[lower_bband, mean, upper_bband]] = ta.bbands(df['Adj Close'], length=bband_length, std=bband_std) # bollinger bands df[[stochk, stochd]] = ta.stoch(df.High, df.Low, df['Adj Close'], k=stoch_k, d=stoch_d, smooth_k=stoch_smoothk) df[[macd, macdh, macds]] = ta.macd(df['Adj Close'], fast=macd_fast, slow=macd_slow, signal=macd_signal) # push data to screener screener.loc[ticker] = df.drop( ['High', 'Low', 'Open', 'Close', 'Volume'], 1).iloc[-1].round(2) # latest recent indicators values, to 2 decimals screener.loc[ticker, 'LAST'] = df['Adj Close'].iloc[-1].round( 2) # last price, to 2 decimals screener.loc[ticker, 'MACDsignal'] = macd_score( df[macd], df[macds])[0].iloc[-1] # macd signal - need data series hence here screener.loc[ticker, 'MACDscore'] = macd_score( df[macd], df[macds])[1].iloc[-1] # macd score - need data series hence here
def macd_judge(base_data, fast=12, slow=26, signal=9): """ 通过macd指标确定买卖时机点 DIFF线从下向上穿越DEA线时买入 DIFF线从上向下穿越DEA线时卖出 :param signal: :param slow: :param fast: :param base_data: :return: """ field_suffix = "_" + str(fast) + "_" + str(slow) + "_" + str(signal) macd_field_name = 'MACD' + field_suffix histogram_field_name = 'MACDh' + field_suffix signal_field_name = 'MACDs' + field_suffix if not base_data.empty and len(base_data) > 0: ret_data = pandas.DataFrame(columns=('flag', 'percent')) close = base_data['close'] macd_ret = ta.macd(close, fast, slow, signal=signal) temp_dict = { "flag": Simulate.DO_NOTHING, 'percent': 0 } ret_data = ret_data.append(temp_dict, ignore_index=True) for i in range(1, len(macd_ret)): if macd_ret.at[i, macd_field_name] is not None and macd_ret.at[i, signal_field_name] is not None and \ macd_ret.at[i, macd_field_name] >= macd_ret.at[i, signal_field_name]: if macd_ret.at[i - 1, macd_field_name] < macd_ret.at[i - 1, signal_field_name]: # 即是所谓的diff线从下向上穿越了DEA线,考虑买入 temp_dict = { "flag": Simulate.BUY_FLAG, 'percent': 0.1 } ret_data = ret_data.append(temp_dict, ignore_index=True) else: temp_dict = { "flag": Simulate.DO_NOTHING, 'percent': 0 } ret_data = ret_data.append(temp_dict, ignore_index=True) elif macd_ret.at[i, macd_field_name] is not None and macd_ret.at[i, signal_field_name] is not None and \ macd_ret.at[i, macd_field_name] < macd_ret.at[i, signal_field_name]: if macd_ret.at[i - 1, macd_field_name] > macd_ret.at[i - 1, signal_field_name]: # 即是所谓的diff线从上向下穿越了DEA线,考虑卖出 temp_dict = { "flag": Simulate.SOLD_FLAG, 'percent': 0.1 } ret_data = ret_data.append(temp_dict, ignore_index=True) else: temp_dict = { "flag": Simulate.DO_NOTHING, 'percent': 0 } ret_data = ret_data.append(temp_dict, ignore_index=True) else: temp_dict = { 'flag': Simulate.DO_NOTHING, 'percent': 0 } ret_data = ret_data.append(temp_dict, ignore_index=True) return ret_data
#print(values.tail()) values['SPX-RSI'] = ta.rsi( values['SPX'] ) BBANDS = ta.bbands( values['SPX'] ) keys = BBANDS.keys().to_list() Upper = BBANDS[ 'BBU_5' ] Lower = BBANDS[ 'BBL_5' ] Upper_perc = Upper / values['SPX'] Lower_perc = Lower / values['SPX'] values[ 'BBU-Distance' ] = Upper_perc values[ 'BBL-Distance' ] = Lower_perc values['MACD-Histogram'] = ta.macd( values[ 'SPX' ] )[ 'MACDH_12_26_9' ] imp = ['Gold','USD Index', 'Oil', 'SPX','VIX', 'High Yield Fund' , 'Nikkei', 'Dax', '10Yr', '2Yr' , 'EEM' ,'XLE', 'XLF', 'XLI', 'AUDJPY'] # Calculating Short term -Historical Returns change_days = [1,3,5,14,21] data = pd.DataFrame(data=values['Date']) for i in change_days: print(data.shape) x= values[cols].pct_change(periods=i).add_suffix("-T-"+str(i)) data=pd.concat(objs=(data,x),axis=1) x=[] #print(data.shape) # Calculating Long term Historical Returns change_days = [60,90,180,250]
def get_MACD(data_series_of_target_feature): return panda.macd(data_series_of_target_feature)
import seaborn as sns sns.set() initial = pd.read_csv( 'https://www.alphavantage.co/query?function=FX_INTRADAY&from_symbol=eur&to_symbol=USD&interval=1min&apikey=OUMVBY0VK0HS8I9E&outputsize=full&datatype=csv', index_col='timestamp', parse_dates=True) initial = initial[::-1] #initial = initial[initial.index > '2020-07-05 09:30:00'] bbands = ta.bbands(initial['close'], length=200, std=2) #calculating indicators ema_5 = ta.ema(initial['close'], length=50) ema_20 = ta.ema(initial['close'], length=200) ema_50 = ta.ema(initial['close'], length=500) macd = ta.macd(initial['close'], 5, 35, 5) rsi = ta.rsi(initial['close'], 50) initial = pd.concat([initial, bbands, ema_5, ema_20, ema_50, macd, rsi], axis=1) initial.columns = [ 'open', 'high', 'low', 'close', 'bband1', 'useless', 'bband2', 'ema1', 'ema2', 'ema3', 'macd', 'macdh', 'macds', 'rsi' ] initialInvestment = 1000 numTrades = 0 buyx = [] buyy = [] sellx = [] selly = [] b = backtester.Backtester(initialInvestment)
#recent_startdate = source_cut.tail(recent).index[0] #recent_enddate = source_cut.tail(recent).index[-1] #price_size = (16, 8) #ind_size = (16, 2) #ticker = 'SPY' #recent = 126 #half_of_recent = int(0.5 * recent) #def plot_MACD(macddf): # macddf[[macddf.columns[0], macddf.columns[2]]].tail(recent).plot(figsize=(16, 2), color=cscheme('BkBu'), linewidth=1.3) # macddf[macddf.columns[1]].tail(recent).plot.area(figsize=ind_size, stacked=False, color=['silver'], linewidth=1, title=ctitle(macddf.name, ticker=ticker, length=recent), grid=True).axhline(y=0, color="black", lw=1.1) for fmacd, smacd, sigmacd in zip(fast_macd, slow_macd, signal_macd): print("Generate fast mcd={}, slow macd={}, signal macd={}".format(fmacd, smacd, sigmacd)) macddf = ta.macd(close, fast=fmacd, slow=smacd, signal=sigmacd) #display(macddf.iloc[:,0].head(50)) #plot_MACD(macddf) macd_features = macd_features.join(pd.Series(macddf[macddf.columns[0]], name='MACD_' + str(fmacd) + "_" + str(smacd)+ "_" + str(sigmacd))) macd_features = macd_features.join(pd.Series(macddf[macddf.columns[2]], name='MACDS_' + str(fmacd) + "_" + str(smacd)+ "_" + str(sigmacd))) print("Number of features: {}".format(macd_features.shape)) display(macd_features.iloc[20:40,:]) plt.figure(num=None, figsize=(10, 7), dpi=80, facecolor='w', edgecolor='k') plt.subplot(311) plt.plot(source_cut['Date'][0:100],source_cut['Close'][0:100]) plt.title("Close") plt.subplot(312) plt.title("MACD Variant 1")
def macd(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( add_help=False, prog="macd", description=""" The Moving Average Convergence Divergence (MACD) is the difference between two Exponential Moving Averages. The Signal line is an Exponential Moving Average of the MACD. \n \n The MACD signals trend changes and indicates the start of new trend direction. High values indicate overbought conditions, low values indicate oversold conditions. Divergence with the price indicates an end to the current trend, especially if the MACD is at extreme high or low values. When the MACD line crosses above the signal line a buy signal is generated. When the MACD crosses below the signal line a sell signal is generated. To confirm the signal, the MACD should be above zero for a buy, and below zero for a sell. """, ) parser.add_argument( "-f", "--fast", action="store", dest="n_fast", type=check_positive, default=12, help="The short period.", ) parser.add_argument( "-s", "--slow", action="store", dest="n_slow", type=check_positive, default=26, help="The long period.", ) parser.add_argument( "--signal", action="store", dest="n_signal", type=check_positive, default=9, help="The signal period.", ) parser.add_argument( "-o", "--offset", action="store", dest="n_offset", type=check_positive, default=0, help="How many periods to offset the result.", ) try: ns_parser = parse_known_args_and_warn(parser, l_args) if not ns_parser: return # Daily if s_interval == "1440min": df_ta = ta.macd( df_stock["Adj Close"], fast=ns_parser.n_fast, slow=ns_parser.n_slow, signal=ns_parser.n_signal, offset=ns_parser.n_offset, ).dropna() # Intraday else: df_ta = ta.macd( df_stock["Close"], fast=ns_parser.n_fast, slow=ns_parser.n_slow, signal=ns_parser.n_signal, offset=ns_parser.n_offset, ).dropna() plt.figure(figsize=plot_autoscale(), dpi=PLOT_DPI) plt.subplot(211) plt.title(f"Moving Average Convergence Divergence (MACD) on {s_ticker}") if s_interval == "1440min": plt.plot(df_stock.index, df_stock["Adj Close"].values, "k", lw=2) else: plt.plot(df_stock.index, df_stock["Close"].values, "k", lw=2) 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) plt.subplot(212) plt.plot(df_ta.index, df_ta.iloc[:, 0].values, "b", lw=2) plt.plot(df_ta.index, df_ta.iloc[:, 2].values, "r", lw=2) plt.bar(df_ta.index, df_ta.iloc[:, 1].values, color="g") plt.legend( [ f"MACD Line {df_ta.columns[0]}", f"Signal Line {df_ta.columns[2]}", f"Histogram {df_ta.columns[1]}", ] ) plt.xlim(df_stock.index[0], df_stock.index[-1]) 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.xlabel("Time") if gtff.USE_ION: plt.ion() plt.show() print("") except Exception as e: print(e) print("")
def macd(l_args, s_ticker, s_interval, df_stock): parser = argparse.ArgumentParser( prog='macd', description= """ The Moving Average Convergence Divergence (MACD) is the difference between two Exponential Moving Averages. The Signal line is an Exponential Moving Average of the MACD. \n \n The MACD signals trend changes and indicates the start of new trend direction. High values indicate overbought conditions, low values indicate oversold conditions. Divergence with the price indicates an end to the current trend, especially if the MACD is at extreme high or low values. When the MACD line crosses above the signal line a buy signal is generated. When the MACD crosses below the signal line a sell signal is generated. To confirm the signal, the MACD should be above zero for a buy, and below zero for a sell. """ ) parser.add_argument('-f', "--fast", action="store", dest="n_fast", type=check_positive, default=12, help='The short period.') parser.add_argument('-s', "--slow", action="store", dest="n_slow", type=check_positive, default=26, help='The long period.') parser.add_argument("--signal", action="store", dest="n_signal", type=check_positive, default=9, help='The signal period.') parser.add_argument('-o', "--offset", action="store", dest="n_offset", type=check_positive, default=0, help='How many periods to offset the result.') 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.macd(df_stock['5. adjusted close'], fast=ns_parser.n_fast, slow=ns_parser.n_slow, signal=ns_parser.n_signal, offset=ns_parser.n_offset).dropna() plt.subplot(211) plt.title( f"Moving Average Convergence Divergence (MACD) on {s_ticker}") plt.plot(df_stock.index, df_stock['4. close'].values, 'k', lw=2) 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) plt.subplot(212) plt.plot(df_ta.index, df_ta.iloc[:, 0].values, 'b', lw=2) plt.plot(df_ta.index, df_ta.iloc[:, 2].values, 'r', lw=2) plt.bar(df_ta.index, df_ta.iloc[:, 1].values, color='g') plt.legend([ f'MACD Line {df_ta.columns[0]}', f'Signal Line {df_ta.columns[2]}', f'Histogram {df_ta.columns[1]}' ]) plt.xlim(df_stock.index[0], df_stock.index[-1]) 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.xlabel('Time') plt.show() # Intraday else: df_ta = ta.macd(df_stock['4. close'], fast=ns_parser.n_fast, slow=ns_parser.n_slow, signal=ns_parser.n_signal, offset=ns_parser.n_offset).dropna() plt.subplot(211) plt.title( f"Moving Average Convergence Divergence (MACD) on {s_ticker}") plt.plot(df_stock.index, df_stock['4. close'].values, 'k', lw=2) 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) plt.subplot(212) plt.plot(df_ta.index, df_ta.iloc[:, 0].values, 'b', lw=2) plt.plot(df_ta.index, df_ta.iloc[:, 2].values, 'r', lw=2) plt.bar(df_ta.index, df_ta.iloc[:, 1].values, color='g') plt.legend([ f'MACD Line {df_ta.columns[0]}', f'Signal Line {df_ta.columns[2]}', f'Histogram {df_ta.columns[1]}' ]) plt.xlim(df_stock.index[0], df_stock.index[-1]) 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.xlabel('Time') plt.show() print("") except: print("")
minimum_order = current_price * 0.02 number_can_buy = funds / current_price print("Amount in wallet is: {}, Current price is: {}, The total £ available in ETH is: {} and Funds are {}".format( amount_held, current_price, number_can_buy, funds)) (RSI, current_rsi, RSI_gradient, mean_rsi, RSI_difference) = CryptoFunctions.get_RSI(new_data, funds) amount_to_invest = kelly*funds minimum_order = current_price*0.02 if amount_to_invest <= minimum_order: amount_to_invest = minimum_order + 0.1 print("Amount to invest", amount_to_invest) print("Current RSI", current_rsi) print("RSI gradient", RSI_gradient) print("Mean RSI", mean_rsi) print("RSI difference", RSI_difference) (buy_signal, sell_signal, sell_index, buy_index) = CryptoFunctions.get_MACD(new_data, fast = 5, slow = 35, signal = 5) MACD = ta.macd(new_data.close) macd_gradient = MACD.iloc[:,0].diff().iloc[-1] print("MACD gradient", macd_gradient) length = len(MACD) print(buy_index) for i in buy_index: if (length - 2 == i or length - 3 == i) and can_buy and current_rsi <= 36: print("Buy!") buy = True for j in sell_index: if (length - 2 == j or length - 3 == j) and bought: print("Sell!") sell = True if buy and can_buy: CryptoFunctions.bollinger_plot("ETH", CryptoFunctions.get_new(CryptoFunctions.add_cols(new_data)), time=0) buy_price = current_price
df["RSI2"] = ta.rsi(high=df.high, low=df.low, close=df.close, length=14) df['ATR2'] = df.ta.atr() df['Momentum'] = df.ta.mom() df['adx'] = ta.adx(high=df.high, low=df.low, close=df.close, length=14)['ADX_14'] print('---------------------') df['pvt'] = ta.pvt(close=df.close, volume=df.volume) indicator_bb = BollingerBands(close=df.close, window=14, window_dev=2) df['bb_bbm'] = indicator_bb.bollinger_mavg() df['bb_bbh'] = indicator_bb.bollinger_hband() df['bb_bbl'] = indicator_bb.bollinger_lband() df['bb_bbhi'] = indicator_bb.bollinger_hband_indicator() df['bb_bbli'] = indicator_bb.bollinger_lband_indicator() macd = ta.macd(close=df.close, fast=12, slow=26, signal_indicators=True, signal=9) df['macd_osc'] = macd['MACD_12_26_9'] df['macd_h'] = macd['MACDh_12_26_9'] df['macd_s'] = macd['MACDs_12_26_9'] df['macd_xa'] = macd['MACDh_12_26_9_XA_0'] df['macd_xb'] = macd['MACDh_12_26_9_XB_0'] df['macd_a'] = macd['MACD_12_26_9_A_0'] df.to_csv('test.csv') vp = get_volume_profile(df) ref_value = 3020 vp['mean_close'] = round(vp['mean_close'], 2) vp['vp_trend'] = vp.pos_volume > vp.neg_volume vp['total_trend'] = (vp.total_volume - vp.total_volume.shift(1)) >= 0
color='red', alpha=1) ax.set_title(stocksymbols[0] + " Price History with buy and sell signals", fontsize=10, backgroundcolor='blue', color='white') ax.set_xlabel(f'{startdate} - {end_date}', fontsize=18) ax.set_ylabel('Close Price INR (₨)', fontsize=18) legend = ax.legend() ax.grid() plt.tight_layout() plt.show() ########### MACD Implementation ############# macd = ta.macd(data['Close']) data = pd.concat([data, macd], axis=1).reindex(data.index) def MACD_Strategy(df, risk): MACD_Buy = [] MACD_Sell = [] position = False for i in range(0, len(df)): if df['MACD_12_26_9'][i] > df['MACDs_12_26_9'][i]: MACD_Sell.append(np.nan) if position == False: MACD_Buy.append(df['Adj Close'][i]) position = True
def create_feature(df): """ Create some more features """ # Preprocessing data df.drop(columns=['ticker'], inplace=True) # Dealing with missing data: # if drop na here, it will cause some features discontinuities. Need to be improved to some extend df.dropna(inplace=True) # Length of data is not enough to create additional features if len(df) < min_df_length: return None # Add lag data (default 10 lags) df_history = create_historical_data(df) df = pd.concat([df, df_history], axis=1) # Add technical analysis indicators # MACD df[['macd_fast', 'macd_slow', 'macd_signal']] = ta.macd(df['close']) # PVT df['pvt'] = ta.pvt(df['close'], df['volume']) # Rate of Change roc_list = [1, 5, 10] for roc in roc_list: df['roc_' + str(roc)] = ta.roc(df['close'], length=roc) # SMA ma_list = [5, 10, 20, 50] for ma in ma_list: df['MA_' + str(ma)] = ta.sma(df['close'], length=ma) # EMA ema_list = [12, 26] for ma in ema_list: df['EMA_' + str(ma)] = ta.ema(df['close'], length=ma) # Optional features # DIFF df['diff'] = ta.ema(df['close'], length=12) - ta.ema(df['close'], length=26) # Return df['return'] = ta.percent_return(df['close']) df['return_30'] = ta.percent_return(df['close'], length=30) df['log_return'] = ta.log_return(df['close']) rsi_list = [3, 20] for rsi in rsi_list: df['RSI_' + str(rsi)] = ta.rsi(df['close'], length=rsi) df['bias'] = ta.bias(df['close']) # Delta Open Close, High Low df['close_open'] = df['close'] - df['open'] df['high_low'] = df['high'] - df['low'] # df['CMO'] = ta.cmo(df['close'], 14) # cci_list = [14, 24] # for cci in cci_list: # df['CCI_' + str(cci)] = ta.cci(df['high'], df['low'], df['close'], cci) # df['AD'] = ta.ad(df['high'], df['low'], df['close'], df['volume'], df['open'], 14) # df['psy'] = ta.psl(df['close'], length=20) # # cross_pvt_ema # df['cross_pvt_ema'] = ta.cross(ta.pvt(df['close'], df['volume']), # ta.ema(df['close'], length=10)) # # # cross_ma5_ma10 # df['cross_ma5_ma10'] = ta.cross_value(ta.sma(df['close'], length=5), # ta.sma(df['close'], length=10)) # End of Optional features # Dealing with missing data again, by the process of creating features df.dropna(inplace=True) if len(df) == 0: return None return df
now = datetime.now() # dd/mm/YY H:M:S new_dict = {} indic_dict = {} open1, high, low, close = get_close() new_dict['open'] = float(open1) new_dict['high'] = float(high) new_dict['low'] = float(low) new_dict['close'] = float(close) X_test = X_test.append(new_dict, ignore_index=True) if i < 51: print(X_test.tail()) if i >= 51: bbands_close = ta.bbands(X_test['close'], length=50, std=2) #calculating indicators macd_close = ta.macd(X_test['close'], 5, 35, 5) rsi_close = np.array(ta.rsi(X_test['close'], 14))[-1] ema_5_close = np.array(ta.ema(X_test['close'], 5))[-1] ema_20_close = np.array(ta.ema(X_test['close'], 20))[-1] ema_50_close = np.array(ta.ema(X_test['close'], 50))[-1] indic_dict['open'] = float(open1) indic_dict['high'] = float(high) indic_dict['low'] = float(low) indic_dict['close'] = float(close) indic_dict['useless'] = bbands_close['BBM_50'].iloc[-1] indic_dict['bband1'] = bbands_close['BBL_50'].iloc[-1] indic_dict['bband2'] = bbands_close['BBU_50'].iloc[-1] #BUYS BUYS BUYS indic_dict['macd'] = macd_close['MACD_5_35_5'].iloc[-1] indic_dict['macdh'] = macd_close['MACDH_5_35_5'].iloc[-1] indic_dict['macds'] = macd_close['MACDS_5_35_5'].iloc[-1] indic_dict['rsi'] = rsi_close
def preprocess( self ): print('\n PREPROCESSING \n') ticker_details = pd.read_excel('Ticker List.xlsx') ticker = ticker_details['Ticker'].to_list() names = ticker_details['Description'].to_list() #Extracting Data from Yahoo Finance and Adding them to Values table using date as key end_date= "2020-06-19" start_date = "2000-01-01" date_range = pd.bdate_range(start=start_date,end=end_date) values = pd.DataFrame({ 'Date': date_range}) values['Date']= pd.to_datetime(values['Date']) #Extracting Data from Yahoo Finance and Adding them to Values table using date as key for i in ticker: raw_data = YahooFinancials(i) raw_data = raw_data.get_historical_price_data(start_date, end_date, "daily") df = pd.DataFrame(raw_data[i]['prices'])[['formatted_date','adjclose']] df.columns = ['Date1',i] df['Date1']= pd.to_datetime(df['Date1']) values = values.merge(df,how='left',left_on='Date',right_on='Date1') values = values.drop(labels='Date1',axis=1) self.VV = values #Renaming columns to represent instrument names rather than their ticker codes for ease of readability names.insert(0,'Date') values.columns = names values.tail() #Front filling the NaN values in the data set values = values.fillna(method="ffill",axis=0) values = values.fillna(method="bfill",axis=0) values.isna().sum() #Return values['SPX-RSI'] = ta.rsi( values['SPX'] ) BBANDS = ta.bbands( values['SPX'] ) keys = BBANDS.keys().to_list() Upper = BBANDS[ 'BBU_5' ] Lower = BBANDS[ 'BBL_5' ] Upper_perc = Upper / values['SPX'] Lower_perc = Lower / values['SPX'] values[ 'BBU-Distance' ] = Upper_perc values[ 'BBL-Distance' ] = Lower_perc values['MACD-Histogram'] = ta.macd( values[ 'SPX' ] )[ 'MACDH_12_26_9' ] # Co-ercing numeric type to all columns except Date cols=values.columns.drop('Date') values[cols] = values[cols].apply(pd.to_numeric,errors='coerce').round(decimals=4) #print(values.tail()) imp = ['Gold','USD Index', 'Oil', 'SPX','VIX', 'High Yield Fund' , 'Nikkei', 'Dax', '10Yr', '2Yr' , 'EEM' ,'XLE', 'XLF', 'XLI', 'AUDJPY'] # Calculating Short term -Historical Returns change_days = [1,3,5,14,21] data = pd.DataFrame(data=values['Date']) for i in change_days: x= values[cols].pct_change(periods=i).add_suffix("-T-"+str(i)) data=pd.concat(objs=(data,x),axis=1) x=[] # Calculating Long term Historical Returns change_days = [60,90,180,250] for i in change_days: x= values[imp].pct_change(periods=i).add_suffix("-T-"+str(i)) data=pd.concat(objs=(data,x),axis=1) x=[] #Calculating Moving averages for SPX moving_avg = pd.DataFrame(values['Date'],columns=['Date']) moving_avg['Date']=pd.to_datetime(moving_avg['Date'],format='%Y-%b-%d') moving_avg['SPX/15SMA'] = (values['SPX']/(values['SPX'].rolling(window=15).mean()))-1 moving_avg['SPX/30SMA'] = (values['SPX']/(values['SPX'].rolling(window=30).mean()))-1 moving_avg['SPX/60SMA'] = (values['SPX']/(values['SPX'].rolling(window=60).mean()))-1 moving_avg['SPX/90SMA'] = (values['SPX']/(values['SPX'].rolling(window=90).mean()))-1 moving_avg['SPX/180SMA'] = (values['SPX']/(values['SPX'].rolling(window=180).mean()))-1 moving_avg['SPX/90EMA'] = (values['SPX']/(values['SPX'].ewm(span=90,adjust=True,ignore_na=True).mean()))-1 moving_avg['SPX/180EMA'] = (values['SPX']/(values['SPX'].ewm(span=180,adjust=True,ignore_na=True).mean()))-1 moving_avg = moving_avg.dropna(axis=0) #Merging Moving Average values to the feature space data['Date']=pd.to_datetime(data['Date'],format='%Y-%b-%d') self.RAW_data = pd.merge(left=data,right=moving_avg,how='left',on='Date') self.RAW_y = pd.DataFrame(data=values['Date']) self.RAW_values = values