def write_technical_analysis(symbols_list): for symbol in symbols_list: try: file_path = f'data/ohlc/{symbol}.csv' dataframe = pd.read_csv(file_path, parse_dates=True, index_col='Timestamp') sma6 = btalib.sma(dataframe, period=6) sma9 = btalib.sma(dataframe, period=9) sma10 = btalib.sma(dataframe, period=10) sma20 = btalib.sma(dataframe, period=20) rsi = btalib.rsi(dataframe) macd = btalib.macd(dataframe) sub_SMA6_SMA20 = btalib.sub(sma6, sma20) dataframe['SMA-6'] = round(sma6.df, 3) dataframe['SMA-9'] = round(sma9.df, 3) dataframe['SMA-10'] = round(sma10.df, 3) dataframe['SMA-20'] = round(sma20.df, 3) dataframe['RSI'] = round(rsi.df, 2) dataframe['MACD'] = round(macd.df['macd'], 2) dataframe['Signal'] = round(macd.df['signal'], 2) dataframe['Histogram'] = macd.df['histogram'] dataframe['Delta_SMA6_SMA20'] = round(sub_SMA6_SMA20.df, 4) f = open(file_path, 'w+') dataframe.to_csv(file_path, sep=',', index=True) f.close() except: print(f'{symbol} is not writing the technical data.')
def calculate_indicator(self): # RSI 15 Indicator if self.indicator == 'RSI': self.klines['rsi'] = btalib.rsi(self.klines.close, period=15).df # SMA 12-50 Indicator elif self.indicator == 'SMA': self.klines['12sma'] = btalib.sma(self.klines.close, period=12).df self.klines['50sma'] = btalib.sma(self.klines.close, period=50).df # MACD 12-26-9 Indicator elif self.indicator == 'MACD': self.klines['macd'], self.klines['macdSignal'], self.klines[ 'macdHist'] = btalib.macd(self.klines.close, pfast=12, pslow=26, psignal=9) else: return None
import btalib import pandas as pd import pandas_datareader.data as pdr import datetime as dt df = pdr.DataReader('BTC-USD', 'yahoo', start=(dt.datetime(2020, 1, 1)), end=(dt.datetime(2020, 10, 1))) #df = pd.read_csv('data/ohlc/AAPL.csv', parse_dates=True, index_col='Date') sma = btalib.sma(df, period=50) df['sma'] = sma.df rsi = btalib.rsi(df) df['rsi'] = rsi.df print(df)
high_list.append(bar['h']) low_list.append(bar['l']) close_list.append(bar['c']) volume_list.append(bar['v']) symbol_list.append(symbol) # append each list to its own column in df df['symbol'] = symbol_list df['time'] = time_list df['open'] = open_list df['high'] = high_list df['low'] = low_list df['close'] = close_list df['volume'] = volume_list ##################################################-ADDING IN INDICATORS-################################################## sma2 = btalib.sma(df, period=2) df['sma2'] = sma2.df sma5 = btalib.sma(df, period=5) df['sma5'] = sma5.df sma10 = btalib.sma(df, period=10) df['sma10'] = sma10.df sma20 = btalib.sma(df, period=20) df['sma20'] = sma20.df sma200 = btalib.sma(df, period=200) df['sma200'] = sma200.df df['rsi'] = ta.momentum.rsi( df.close, n=6, fillna=False ) # btalib rsi not working, we will use talib for this indicator ##################################################-CALCULATE PIVOT POINT AND RESISTANCE LEVEL-################################################## # df['pivot_point'] = (df['high'].shift(1) + df['low'].shift(1) + df['close'].shift(1))/3
from btalib import indicator, indicators import btalib import pandas as pd import numpy as np import plotly.express as px import plotly.graph_objs as go data = pd.read_csv( '/Users/cosmos/GitHub/haasomeapitools/market_data/BINANCE|ADA|BNB.csv', parse_dates=True) data.fillna(0) print(data) sma = btalib.sma(data, period=4) # # sma = btalib.sma(df, period=15) # print(sma.params.period) # print(sma.p.period) # print(sma.params['period']) # print(list(sma.params)) # print(dict(sma.params)) # stochastic = btalib.stochastic(data,period = 5) # print(stochastic.df) first_close_price = data.Close.iloc[0] ema = btalib.ema(data,_seed =first_close_price) bbands = btalib.bbands(data,_ma=btalib.wma) print(bbands.df) newcols = [] # data2 = pd.merge(data,bbands.df,how='right',on='index') # print(bbands) data2 = data.join(bbands.df,how='outer')
def on_message(ws, message): global closes, smas, df, position, pnl, open_long, open_short, close_long, close_short tmp = deque([], maxlen=31) json_message = json.loads(message) candle = json_message["k"] # pseudo code: Refresh data with wss data # store intraperiod ticks in temporary list 'tmp' # if candle closed, append close price to 'closes' if candle['x'] == False: for close in list(closes): # append prices from closed candles to tmp tmp.append(close) tmp.append([float(candle['c'])]) # at last, add latest price stream to tmp df = pd.DataFrame(data=list(tmp), columns=['close']) # create a dataframe to use btalib library # print(df) # only last df row changes # calculate slow and fast sma fast_sma = btalib.sma(df, period=5) slow_sma = btalib.sma(df, period=30) print('fast sma: {} | slow sma: {}'.format( round(fast_sma.df.to_numpy()[-1][0], 2), round(slow_sma.df.to_numpy()[-1][0], 2) )) print(round(sum(pnl), 2)) # Strategy if position == 1 and float(fast_sma.df.to_numpy()[-1][0]) < slow_sma.df.to_numpy()[-1][0]: # short condition print('fast sma {} < slow sma {} | EXIT LONG & GO SHORT'.format( round(fast_sma.df.to_numpy()[-1][0], 2), round(slow_sma.df.to_numpy()[-1][0], 2) )) position = -1 close_long.append(float(candle['c'])) open_short.append(float(candle['c'])) pnl.append(close_long[-1] - open_long[-1]) elif position == -1 and float(fast_sma.df.to_numpy()[-1][0]) > slow_sma.df.to_numpy()[-1][0]: # long condition print('fast sma {} > slow sma {} | EXIT SHORT & GO LONG'.format( round(fast_sma.df.to_numpy()[-1][0], 2), round(slow_sma.df.to_numpy()[-1][0], 2) )) position = 1 close_short.append(float(candle['c'])) open_long.append(float(candle['c'])) pnl.append(open_short[-1] - close_short[-1]) elif position == 0: # position = 0 # if fast_sma crosses over slow_sma: go long if float(fast_sma.df.to_numpy()[-1][0]) > slow_sma.df.to_numpy()[-1][0]: print('fast sma {} > slow sma {} | GO LONG'.format( round(fast_sma.df.to_numpy()[-1][0], 2), round(slow_sma.df.to_numpy()[-1][0], 2) )) position = 1 open_long.append(float(candle['c'])) # elif fast_sma crosses under slow_sma: go short elif float(fast_sma.df.to_numpy()[-1][0]) < slow_sma.df.to_numpy()[-1][0]: print('fast sma {} < slow sma {} | GO SHORT'.format( round(fast_sma.df.to_numpy()[-1][0]), round(slow_sma.df.to_numpy()[-1][0]) )) position = -1 open_short.append(float(candle['c'])) # none of these else: pass else: # position != 0, -1, 1 pass else: # Edit condition to append last data and pop it, except if candle is closed closes.append([float(candle['c'])]) df = pd.DataFrame(data=closes, columns=['close']) fast_sma = btalib.sma(df, period=5) slow_sma = btalib.sma(df, period=30) # Strategy if position == 1 and float(fast_sma.df.to_numpy()[-1][0]) < slow_sma.df.to_numpy()[-1][0]: # short condition print('fast sma {} < slow sma {} | EXIT LONG & GO SHORT'.format( round(fast_sma.df.to_numpy()[-1][0], 2), round(slow_sma.df.to_numpy()[-1][0], 2) )) position = -1 close_long.append(float(candle['c'])) open_short.append(float(candle['c'])) pnl.append(close_long[-1] - open_long[-1]) elif position == -1 and float(fast_sma.df.to_numpy()[-1][0]) > slow_sma.df.to_numpy()[-1][0]: # long condition print('fast sma {} > slow sma {} | EXIT SHORT & GO LONG'.format( round(fast_sma.df.to_numpy()[-1][0], 2), round(slow_sma.df.to_numpy()[-1][0], 2) )) position = 1 close_short.append(float(candle['c'])) open_long.append(float(candle['c'])) pnl.append(open_short[-1] - close_short[-1]) elif position == 0: # position = 0 # if fast_sma crosses over slow_sma: go long if float(fast_sma.df.to_numpy()[-1][0]) > slow_sma.df.to_numpy()[-1][0]: print('fast sma {} > slow sma {} | GO LONG'.format( round(fast_sma.df.to_numpy()[-1][0], 2), round(slow_sma.df.to_numpy()[-1][0], 2) )) position = 1 open_long.append(float(candle['c'])) # elif fast_sma crosses under slow_sma: go short elif float(fast_sma.df.to_numpy()[-1][0]) < slow_sma.df.to_numpy()[-1][0]: print('fast sma {} < slow sma {} | GO SHORT'.format( round(fast_sma.df.to_numpy()[-1][0], 2), round(slow_sma.df.to_numpy()[-1][0], 2) )) position = -1 open_short.append(float(candle['c'])) # else do nothing else: pass else: # position != 0, -1, 1 pass
# Get historical data historical_data = futures_get_hist( SYMBOL, INTERVAL ) closes = deque([], maxlen=30) # where ticks will be stored smas = deque([], maxlen=30) for candle in historical_data: closes.append([float(candle[4])]) df = pd.DataFrame(data=closes, columns=['close']) sma = btalib.sma(df, period=30) smas.append([sma.df.to_numpy()[-1][0]]) # Get live ticks def on_open(ws): print('Opened connection') def on_close(ws): print('Closed connection') def on_message(ws, message): global closes, smas, df, position, pnl, open_long, open_short, close_long, close_short tmp = deque([], maxlen=31)
import btalib import pandas as pd df = pd.read_csv('data/ohlc/SPSC.txt', parse_dates=True, index_col="Date") sma = btalib.sma(df) rsi = btalib.rsi(df) macd = btalib.macd(df) #SMA15 = btalib.sma(df, period=15) #SMA50 = btalib.sma(df, period=50) #SMA200 = btalib.sma(df, period=200) #if SMA50 > SMA200: df['sma'] = sma.df df['rsi'] = rsi.df #macd = ema(data, 12) - ema(data, 26) #signal = ema(macd, 9) #histogram = macd - signal df['macd'] = macd.df['macd'] df['signal'] = macd.df['signal'] df['histogram'] = macd.df['histogram'] oversold_days = df[df['rsi'] < 30] overbought_days = df[df['rsi'] > 60] print(oversold_days) print(overbought_days)
coin_columns = ["Date", "Open", "High", "Low", "Close"] for row in historic_data: del row[5:] dataframe = pandas.DataFrame(historic_data, columns = coin_columns) print(dataframe) dataframe.describe() print(dataframe.head()) dataframe["5-Day Moving Average"] = dataframe.Close.rolling(5).mean() print(dataframe) !pip install bta-lib import btalib simple_moving_average = btalib.sma(dataframe.Close) print(simple_moving_average.df) dataframe["Simple Moving Average"] = btalib.sma(dataframe.Close, period=10).df print(dataframe)
def on_message(ws, message): client = Client(config.API_KEY, config.API_SECRET) info = client.get_account() exchange_info = client.get_exchange_info() balances = (info['balances']) symbols = exchange_info['symbols'] try: inPosition = inPosition except: inPosition = False cash = 12 json_message = json.loads(message) TRADE_SYMBOL = json_message['s'] TOKEN = TRADE_SYMBOL.replace('USDT', '') candle = json_message['k'] t = datetime.datetime.fromtimestamp(candle['t'] / 1000) day = t.strftime('%Y-%m-%d') candle['t'] = day formatted_message = (candle['t'], candle['o'], candle['h'], candle['l'], candle['c'], candle['v'], 0.00) if candle['x']: #If the candle is closed, we append to the csv f = open(f"data/all_time_daily_{TOKEN.lower()}.csv", 'a') writer = csv.writer(f) writer.writerow(formatted_message) f.close() print("Today's data added to CSV!") df = pd.read_csv(f'data/all_time_daily_{TOKEN.lower()}.csv', parse_dates=True) stochastic = btalib.stochastic(df.High, df.Low, df.Close) todays_stochastic = stochastic.df.iloc[[-1], [-1]] fast_moving_average = btalib.sma( df, period=12, ) slow_moving_average = btalib.sma( df, period=26, ) #Execute buy and sell orders based off of conditions: if not inPosition: if (fast_moving_average.df.iloc[[-1], [-1]] >= slow_moving_average.df.iloc[[-1], [-1]]).bool() and ( todays_stochastic > 50).bool(): amount_to_invest = (0.95 * cash) TRADE_QUANTITY = round((amount_to_invest / df.Close.iloc[-1]), 5) print( f"Buying {TRADE_QUANTITY} of {TRADE_SYMBOL} at {df.Close[0]}") order_succeeded = order(SIDE_BUY, TRADE_QUANTITY, TRADE_SYMBOL) if order_succeeded: print( f"Bought {TRADE_QUANTITY} of {TRADE_SYMBOL} Successfully") inPosition = True if inPosition: if (fast_moving_average.df.iloc[[-1], [-1]] <= slow_moving_average.df.iloc[[-1], [-1]]).bool() and ( todays_stochastic < 50).bool(): TRADE_QUANTITY = sellAmount(TOKEN) print( f"Selling {TRADE_QUANTITY} shares of {TRADE_SYMBOL} at ${df.Close[0]}" ) order_succeeded = order(SIDE_SELL, TRADE_QUANTITY, TRADE_SYMBOL) if order_succeeded: print(f"Sold {TRADE_QUANTITY} of {TRADE_SYMBOL} Successfully") inPosition = False
def on_message(ws, message): global current_mess, previous_mess, df, minutes_processed, candlesticks, sheet previous_mess = current_mess current_mess = json.loads(message) if current_mess['type'] != 'ticker': return time_as_datetime = dateutil.parser.isoparse( current_mess["time"]).strftime("%m/%d/%y %H:%M") times = time_as_datetime if not times in minutes_processed: print("---New Minute---") print("{} @ {}".format(current_mess["price"], times)) minutes_processed[times] = True #print(candlesticks[-1]) #print(minutes_processed) #print(candlesticks) if len(candlesticks) > 0: #print(candlesticks[-1]) candlestick = candlesticks[-1] candlestick["Close"] = previous_mess["price"] #json_stats = open_json("stats.txt") # minute_row = int(json_stats['minute_row']) # in_position = json_stats['in_position'] minute_row = sheet.cell(1, 20).value in_position = sheet.cell(5, 20).value sheet.update_cell(minute_row, 1, candlestick["time"]) sheet.update_cell(minute_row, 2, candlestick["Close"]) sheet.update_cell(minute_row, 3, df['sma_s'][-1]) sheet.update_cell(minute_row, 4, df['sma_l'][-1]) sheet.update_cell(minute_row, 5, df['cross'][-1]) sheet.update_cell(minute_row, 6, in_position) #minute_row = minute_row + 1 #json_stats['minute_row'] = str(minute_row) data = [] keys = list(candlestick.keys())[1:] for key in keys: data.append(candlestick[key]) data.append(0) data.append(0) data.append(0) time = candlestick["time"] #candlestick.pop("time") #print(candlestick) dc = pd.DataFrame([data], columns=list(df.columns), index=[time]) #print(dc) df = pd.concat([df, dc]) #print(df.round(1)) #print("SMA") #time = candlesticks[-1]['time'] try: #print("done") sma_short = btalib.sma(df.Close, period=50) sma_long = btalib.sma(df.Close, period=100) df['sma_s'] = sma_short.df df['sma_l'] = sma_long.df except: print("Error") if float(df['sma_l'].iloc[-2]) > float(df['sma_s'].iloc[-2]): if float(df['sma_l'].iloc[-1]) < float(df['sma_s'].iloc[-1]): df.loc[candlestick["time"], 'cross'] = 1 if in_position == 'FALSE': #json_stats = document(coin_client, 'buy', sheet, json_stats) document(coin_client, 'buy', sheet) elif float(df['sma_l'].iloc[-2]) < float(df['sma_s'].iloc[-2]): if float(df['sma_l'].iloc[-1]) > float(df['sma_s'].iloc[-1]): df.loc[candlestick["time"], 'cross'] = -1 if in_position == 'TRUE': #json_stats = document(coin_client, 'sell', sheet, json_stats) document(coin_client, 'sell', sheet) #to_json(json_stats, "stats.txt") if len(candlesticks) > 10: candlesticks = candlesticks[-10:] key_times = list(minutes_processed.keys()) if len(key_times) > 10: new_minutes = {} for key in key_times[-10:]: new_minutes[key] = minutes_processed[key] minutes_processed = new_minutes candlesticks.append({ "time": times, "Open": current_mess["price"], "High": current_mess["price"], "Low": current_mess["price"], "Close": current_mess["price"] }) if len(df) > 105: df = df[-105:] if len(candlesticks) > 0: if current_mess["price"] > candlesticks[-1]["High"]: candlesticks[-1]["High"] = current_mess["price"] if current_mess["price"] < candlesticks[-1]["Low"]: candlesticks[-1]["Low"] = current_mess["price"]
symbol = 'BTCUSDT' # Import data from csv file df = pd.read_csv('data\{}.csv'.format(symbol)) # Data preparation df.index = pd.to_datetime(df['Datetime'], unit='s') df = df.drop(columns=[ 'Datetime', 'Close time', 'Quote asset volume', 'Taker BUY base asset volume', 'Taker BUY quote asset volume', 'Ignore' ]) # SMA indicators df['sma_5'] = btalib.sma(df.Close, period=5).df df['sma_10'] = btalib.sma(df.Close, period=10).df # arr = df['sma_10'].to_numpy() # print(arr) # print(df['sma_10_numpy'], df['sma_10']) # print('Types :', type(df['sma_10_numpy']), type(df['sma_10'])) df = df.iloc[10:] n = 0 # Strategy: If sma-5 CROSSES OVER sma-10: Buy / vice-versa
"300 hours ago UTC") for line in bars: del line[6:] #print(bars) df = pd.DataFrame(bars, columns=['date', 'open', 'high', 'low', 'close', 'Volume']) df['date'] = pd.to_datetime(df['date'], unit='ms') df['close'] = df['close'].astype('float64') df['Volume'] = df['Volume'].astype('float64') df.set_index('date', inplace=True) #print(btc_df.head()) #df = pd.read_csv('btc_bars3.csv',parse_dates=True,index_col='date') #btc_df[] sma12 = btalib.sma(df.close, period=12) sma26 = btalib.sma(df.close, period=26) df['ema12'] = df['close'].ewm(span=12).mean() df['ema26'] = df['close'].ewm(span=26).mean() df['macd'] = df['ema12'] - df['ema26'] df['signal'] = df['macd'].ewm(span=9, adjust=False).mean() OBV = [] OBV.append(0) for i in range(1, len(df.close)): if df.close.iloc[i] > df.close.iloc[ i - 1]: # If the closing price is above the prior close price OBV.append(OBV[-1] + df.Volume.iloc[i] ) # then: Current OBV = Previous OBV + Current Volume elif df.close.iloc[i] < df.close.iloc[i - 1]: OBV.append(OBV[-1] - df.Volume.iloc[i])
btc_df['20sma'] = btc_df.close.rolling(20).mean() print(btc_df.tail(5)) # calculate just the last value for the 20 moving average mean = btc_df.close.tail(20).mean() # get the highest closing price in 2020 max_val = btc_df.close['2020'].max() print(mean) print(max_val) # technical indicators using bta-lib # sma sma = btalib.sma(btc_df.close) print(sma.df) # create sma and attach as column to original df btc_df['sma'] = btalib.sma(btc_df.close, period=20).df print(btc_df.tail()) # calculate rsi and macd rsi = btalib.rsi(btc_df, period=14) macd = btalib.macd(btc_df, pfast=20, pslow=50, psignal=13) print(rsi.df) print(macd.df) # access last rsi value print(rsi.df.rsi[-1])
# ---- CREATE DATAFRAME ----- # Create a pandas dataframe from the array with open("historicalData.csv", "rb") as fp: historicalData = pickle.load(fp) headers = ["Timestamp", "Open", "High", "Low", "Close"] df = pd.DataFrame(historicalData, columns=headers) # Convert the epoch timestamp to regular date df["Timestamp"] = df["Timestamp"] / 1000 df["Date"] = pd.to_datetime(df["Timestamp"], unit="s") df["Close"] = pd.to_numeric(df["Close"]) # ---- Adding Technical Indicators ----- # SMA Pandas & bta-lib df["Closing_SMA_5"] = btalib.sma(df["Close"], period=5).df df["Closing_SMA_50"] = btalib.sma(df["Close"], period=50).df df["Closing_SMA_200"] = df["Close"].rolling(window=200).mean() # EMA df["Closing EMA_10"] = df["Close"].ewm(span=10, adjust=False).mean() df["Closing EMA_20"] = df["Close"].ewm(span=20, adjust=False).mean() df["Closing EMA_50"] = df["Close"].ewm(span=50, adjust=False).mean() df["Closing EMA_500"] = df["Close"].ewm(span=500, adjust=False).mean() print(df.tail(30)) # TRADING STRATEGY: testing on historicalData tradePlaced = False typeOfTrade = False dollarWallet = 10000 simpelWallet = 10000
def create_indicator(cls, klines: pd.DataFrame, periode: int = 20): sma = btalib.sma(klines.copy(), period=periode) sma.df.columns = [f"sma{periode}"] return sma.df
def buysell(): for symbol in tickers: bars = client.get_historical_klines(symbol, Client.KLINE_INTERVAL_1HOUR, "300 hours ago UTC") for line in bars: del line[6:] # each symbol is df = pd.DataFrame( bars, columns=['date', 'open', 'high', 'low', 'close', 'Volume']) df['date'] = pd.to_datetime(df['date'], unit='ms') df['close'] = df['close'].astype('float64') df['Volume'] = df['Volume'].astype('float64') df.set_index('date', inplace=True) sma12 = btalib.sma(df.close, period=12) sma26 = btalib.sma(df.close, period=26) df['ema12'] = df['close'].ewm(span=12).mean() df['ema26'] = df['close'].ewm(span=26).mean() df['macd'] = df['ema12'] - df['ema26'] df['signal'] = df['macd'].ewm(span=9, adjust=False).mean() #"""Calculate On-Balance Volume (OBV)""" #not sure if the below lib method was calculating OBV correctly. Done it manually below. #df['obv'] = btalib.obv(df.close,df.Volume)[-1] OBV = [] OBV.append(0) for i in range(1, len(df.close)): if df.close.iloc[i] > df.close.iloc[ i - 1]: #If the closing price is above the prior close price OBV.append( OBV[-1] + df.Volume.iloc[i] ) #then: Current OBV = Previous OBV + Current Volume elif df.close.iloc[i] < df.close.iloc[i - 1]: OBV.append(OBV[-1] - df.Volume.iloc[i]) else: OBV.append(OBV[-1]) df['OBV'] = OBV df['OBV_EMA'] = df['OBV'].ewm(com=20).mean() df['OBV_pc'] = df['OBV'].pct_change() * 100 df['OBV_pc'] = np.round(df['OBV_pc'].fillna(0), 2) #true if EMA12 is above the EMA26 df['ema12gtema26'] = df['ema12'] > df['ema26'] # true if the current frame is where EMA12 crosses over above df['ema12gtema26co'] = df['ema12gtema26'].ne( df['ema12gtema26'].shift()) df.loc[df['ema12gtema26'] == False, 'ema12gtema26co'] = False #true if ema12 is below the ema26 df['ema12ltema26'] = df['ema12'] < df['ema26'] #true if the current frame is where ema12 crosses over below df['ema12ltema26co'] = df['ema12ltema26'].ne( df['ema12ltema26'].shift()) df.loc[df['ema12ltema26'] == False, 'ema12ltema26co'] = False #true if MACD is above signal df['macdgtsignal'] = df['macd'] > df['signal'] #true if the current frame is where macd crosses over above df['macdgtsignalco'] = df['macdgtsignal'].ne( df['macdgtsignal'].shift()) df.loc[df['macdgtsignal'] == False, 'macdgtsignalco'] = False #true if the macd is below the signal df['macdltsignal'] = df['macd'] < df['signal'] #true if the current frame is where macd crosses over below df['macdltsignalco'] = df['macdltsignal'].ne( df['macdltsignal'].shift()) df.loc[df['macdltsignal'] == False, 'macdltsignalco'] = False df_last = df.tail(1) ema12gtema26 = bool(df_last['ema12gtema26'].values[0]) ema12gtema26co = bool(df_last['ema12gtema26co'].values[0]) macdgtsignal = bool(df_last['macdgtsignal'].values[0]) macdgtsignalco = bool(df_last['macdgtsignalco'].values[0]) ema12ltema26 = bool(df_last['ema12ltema26'].values[0]) ema12ltema26co = bool(df_last['ema12ltema26co'].values[0]) macdltsignal = bool(df_last['macdltsignal'].values[0]) macdltsignalco = bool(df_last['macdltsignalco'].values[0]) obv = float(df_last['OBV'].values[0]) obv_pc = float(df_last['OBV_pc'].values[0]) #print(symbol) if ((ema12gtema26co == True and macdgtsignal == True and obv_pc > 0.1)): action = 'BUY' # criteria for a sell signal elif (ema12ltema26co == True and macdltsignal == True): action = 'SELL' # anything other than a buy or sell, just wait else: action = 'WAIT' #print(df) if action == 'BUY' or action == 'SELL': print(action) print(symbol)
def tech_ind_features(data): """ Generate technical indicators * :param data(pd.DataFrame): Raw data for processing :return transformed_df(pd.DataFrame): Dataframe of features, sample balanced and normalised """ data['smoothed_close'] = data.close.rolling(9).mean().rolling(21).mean().shift(-15) data['dx'] = np.diff(data['smoothed_close'], prepend=data['smoothed_close'][0]) data['dx_signal'] = pd.Series(data['dx']).rolling(9).mean() data['ddx'] = np.diff(np.diff(data['smoothed_close']), prepend=data['smoothed_close'][0:2]) data['labels'] = np.zeros(len(data)) data['labels'].iloc[[(data.ddx < 0.1) & (data.dx <= 0) & (data.dx_signal > 0)]] = 1 data['labels'].iloc[[(data.ddx > -0.075) & (data.dx >= 0) & (data.dx_signal < 0)]] = 2 #Filter and drop all columns except close price, volume and date (for indexing) relevant_cols = list( compress( data.columns, [False if i in [1, 3, 4, 5, 6, len(data.columns)-1] else True for i in range(len(data.columns))] ) ) data = data.drop(columns=relevant_cols).rename(columns={'open_date_time': 'date'}) data.set_index('date', inplace = True) #Define relevant periods for lookback/feature engineering periods = [ 9, 14, 21, 30, 45, 60, 90, 100, 120 ] #Construct technical features for image synthesis for period in periods: data[f'ema_{period}'] = btalib.ema( data.close, period = period ).df['ema'] data[f'ema_{period}_dx'] = np.append(np.nan, np.diff(btalib.ema( data.close, period = period ).df['ema'])) data[f'rsi_{period}'] = btalib.rsi( data.close, period = period ).df['rsi'] data[f'cci_{period}'] = btalib.cci( data.high, data.low, data.close, period = period ).df['cci'] data[f'macd_{period}'] = btalib.macd( data.close, pfast = period, pslow = period*2, psignal = int(period/3) ).df['macd'] data[f'signal_{period}'] = btalib.macd( data.close, pfast = period, pslow = period*2, psignal = int(period/3) ).df['signal'] data[f'hist_{period}'] = btalib.macd( data.close, pfast = period, pslow = period*2, psignal = int(period/3) ).df['histogram'] data[f'volume_{period}'] = btalib.sma( data.volume, period = period ).df['sma'] data[f'change_{period}'] = data.close.pct_change(periods = period) data = data.drop(data.query('labels == 0').sample(frac=.90).index) data = data.replace([np.inf, -np.inf], np.nan).dropna() data_trimmed = data.loc[:, 'ema_9':] data_trimmed = pd.concat( [data_trimmed, data_trimmed.shift(1), data_trimmed.shift(2)], axis = 1 ) mm_scaler = MinMaxScaler(feature_range=(0, 1)) transformed_data = mm_scaler.fit_transform(data_trimmed[24:]) transformed_data = np.c_[ transformed_data, pd.to_numeric( data.labels[24:], downcast = 'signed' ).to_list() ] return transformed_data