class Trader: new_table = False SMA_PERIOD = 20 EMA_PERIOD = 10 def __init__(self, api_key=None, api_secret=None, api_passphrase=None, ticker_span=100): self.api_key = api_key self.api_secret = api_secret self.api_passphrase = api_passphrase self.client = Client(api_key, api_secret, api_passphrase, sandbox=False) self.ticker_span = ticker_span #Number of candlestick data points to acquire self.kline_data = [] self.historical_data = [] now = int(time.time()) self.kline_data = np.array( self.client.get_kline_data('BTC-USDT', '1min', (now - self.ticker_span * 60), now)) self.historical_data = pd.DataFrame(self.kline_data, columns=[ 'TimeStamp', 'Open', 'Close', 'High', 'Low', 'Tx Amount', 'Tx Volume' ]) self.candle_stick = self.kline_data[0] #Most recenr candle stick info self.update_indicators() def update_indicators( self): #Call this to update the CP, SMA, and EMA arrays self.get_cp() self.get_sma() self.get_ema() return self.cp, self.sma, self.ema def get_ema(self, period=EMA_PERIOD): closing_price = [] for items in reversed(list(self.historical_data.loc[:, 'Close'])): closing_price.append(float(items)) #Gives a list var = np.array(closing_price) self.ema = np.flip(talib.EMA(var, timeperiod=period)).tolist() def get_sma(self, period=SMA_PERIOD): closing_price = [] for items in reversed(list(self.historical_data.loc[:, 'Close'])): closing_price.append(float(items)) #Gives a list var = np.array(closing_price) self.sma = np.flip(talib.SMA(var, timeperiod=period)).tolist() def get_cp(self): closing_price = [] for items in reversed(list(self.historical_data.loc[:, 'Close'])): closing_price.append(float(items)) #Gives a list self.cp = np.flip(closing_price).tolist()
def get_historical_klines_tv(symbol, interval, start_str, end_str=None): """Get Historical Klines from Kucoin (Trading View) See dateparse docs for valid start and end string formats http://dateparser.readthedocs.io/en/latest/ If using offset strings for dates add "UTC" to date string e.g. "now UTC", "11 hours ago UTC" :param symbol: Name of symbol pair e.g BNBBTC :type symbol: str :param interval: Trading View Kline interval :type interval: str :param start_str: Start date string in UTC format :type start_str: str :param end_str: optional - end date string in UTC format :type end_str: str :return: list of OHLCV values """ # init our array for klines klines = [] client = Client("", "", "") # convert our date strings to seconds start_ts = date_to_seconds(start_str) # if an end time was not passed we need to use now if end_str is None: end_str = 'now UTC' end_ts = date_to_seconds(end_str) kline_res = client.get_kline_data(symbol, interval, start_ts, end_ts) # print(kline_res) # check if we got a result if 't' in kline_res and len(kline_res['t']): # now convert this array to OHLCV format and add to the array for i in range(1, len(kline_res['t'])): klines.append( (kline_res['t'][i], kline_res['o'][i], kline_res['h'][i], kline_res['l'][i], kline_res['c'][i], kline_res['v'][i])) # finally return our converted klines return klines
#sets beginning and ending timestamp now = datetime.datetime.now() nowtime = now.timestamp() start = now - datetime.timedelta(days=2) starttime = start.timestamp() start72 = now - datetime.timedelta(days=3) starttime72 = start72.timestamp() endTime = math.floor(nowtime) beginTime = math.floor(starttime) beginTime72 = math.floor(starttime72) #first list that runs through selection of swing trading coins c = ['ARPA-USDT', 'BEPRO-USDT', 'ONE-USDT', 'DAPPT-USDT'] #brings in OHLC Data for percent change metrics for i in c: kline48 = client.get_kline_data(i, '1day', beginTime72, endTime) kline1h = client.get_kline_data(i, '1hour', beginTime, endTime) data1h = pd.DataFrame(kline1h) data1h = data1h[[0, 2]] data1h[0] = pd.to_datetime(data1h[0], unit='s', origin='unix') data1h[2] = data1h[2].astype(float) data1h = data1h.reindex(index=data1h.index[::-1]) closeprice = data1h[2] data48 = pd.DataFrame(kline48) data48 = data48[[0, 2]] data48[0] = pd.to_datetime(data48[0], unit='s', origin='unix') data48[2] = data48[2].astype(float) data48 = data48.reindex(index=data48.index[::-1]) price48 = data48[2] thirdDay = price48.iloc[0] secondDay = price48.iloc[1]