class RealtimeInstrumentData: def __init__(self, symbol, window_size=10, keys=('timestamps', 'open', 'high', 'low', 'close', 'volume')): self.symbol = symbol self.window_size = window_size self.window = None self.__binance_client = Client(api_key=BINANCE_API_KEY, api_secret=BINANCE_API_SECRET) self.keys = keys self.init_timestamp_unix = None def retrieve_latest_window(self): # TODO: Need to change to support multi-timescale convolutions return NotImplementedError last_timestep = self.window['timestamps'][-1] kline = self.__binance_client.get_klines(symbol=self.symbol, limit=1, interval='1m')[0] curr_timestamp = self.__get_relative_timestamp( kline[binance_klines_indices['timestamps']]) if curr_timestamp == last_timestep: self.window['close'][-1] = self.get_latest_price() return self.window, False for k in self.keys: point = float(kline[binance_klines_indices[k]]) if k == 'timestamps': self.window['timestamps'].append( self.__get_relative_timestamp(point)) else: self.window[k].append(point) return self.window, True def init_window(self): klines = self.__binance_client.get_klines(symbol=self.symbol, limit=self.window_size, interval='1m') self.window = { k: [float(step[binance_klines_indices[k]]) for step in klines] for k in self.keys } self.init_timestamp_unix = self.window['timestamps'][0] self.window['timestamps'] = [ self.__get_relative_timestamp(t) for t in self.window['timestamps'] ] return self.window def get_latest_price(self): return float( self.__binance_client.get_symbol_ticker( symbol=self.symbol)['price']) def __get_relative_timestamp(self, unix_timestamp): return get_minute_difference(self.init_timestamp_unix, unix_timestamp)
def get(self, name): coinList = [ "BTCUSDT", "ETHUSDT", "BNBUSDT", "LTCUSDT", "NEOUSDT", "XRPUSDT", "ADAUSDT", "BELUSDT", "XLMUSDT", "BATUSDT", "LINKUSDT", "BANDUSDT", "ONTUSDT", "IOTAUSDT" ] client = Client() dict_result = {} if name in coinList and name != "BTCUSDT": df_predict = pd.DataFrame() for i in coinList: list_pred = [] if i == name: continue candles = client.get_klines(symbol=i, interval="1h", limit=2) list_pred.append(candles[0][4]) df_predict[i] = list_pred model = pickle.load(open("MLBinanceAPI/" + name, 'rb')) result = model.predict(df_predict)[0] name_last_value = client.get_klines(symbol=name, interval="1h", limit=2) dict_result.update({ name + " 1 SAATLIK MUM ACILISI": round(float(name_last_value[1][1]), 3), name + " ANLIK DEGER": round(float(name_last_value[1][4]), 3), # "Interval": "1 hour", name + " 1 SAATLIK MUM KAPANIS TAHMINI": round(result, 3) }) return dict_result else: return {name: "NULL"}
def screen(): client = Client("api-key", "api-secret", {"verify": False, "timeout": 20}) positiveCoin = get_positive_coin.positiveCoin bullishKScoin = [] for m in positiveCoin: candles = client.get_klines(symbol=m, interval=client.KLINE_INTERVAL_30MINUTE) if candles[-2][1] < candles[-2][4] <= candles[-1][1] < candles[-1][4]: bullishKScoin.append(m) macdCoin = [] for m in bullishKScoin: candles = client.get_klines(symbol=m, interval=client.KLINE_INTERVAL_1HOUR) close = [] for n in candles: close.append(float(n[4])) mac = macd(close, 12, 26) macs = macds(mac) mach = macdh(mac, macs) # if mach[-1] > 0 and (macs[-1] - macs[-2]) > 0 and (mac[-1] - mac[-2]) > 0: # macdCoin.append(m) if mach[-1] > 0 and (macs[-1] - macs[-2]) > 0 and (mac[-1] - mac[-2]) > 0: macdCoin.append(m) bollCoin = [] for m in macdCoin: candles = client.get_klines(symbol=m, interval=client.KLINE_INTERVAL_1HOUR) close = [] for n in candles: close.append(float(n[4])) lb = lbb(close, 20, 2) mb = mbb(close, 20, 2) ub = ubb(close, 20, 2) if lb[-1] < close[-1] < ((0.8 * ub[-1]) + (0.2 * mb[-1])): bollCoin.append(m) maxDemandRatio = 0 buyingCoin = '' for m in bollCoin: depth = client.get_order_book(symbol=m) buyingVol = 0 sellingVol = 0 for n in depth['bids'][0:20]: buyingVol = buyingVol + float(n[1]) for n in depth['asks'][0:20]: sellingVol = sellingVol + float(n[1]) demandRatio = buyingVol / sellingVol print(demandRatio) print(maxDemandRatio) if demandRatio > maxDemandRatio: maxDemandRatio = demandRatio buyingCoin = m print(bullishKScoin) print(macdCoin) print(bollCoin) print(buyingCoin) return buyingCoin
def get_binance_chart_data(token_name, t_from, t_to): delta = round(t_to - t_from) if delta < 6 * 3600: res = "1m" elif delta < 13 * 3600: res = "5m" elif delta < 24 * 3600 + 100: res = "5m" elif delta < 24 * 3600 * 7 + 100: res = "1h" elif delta < 24 * 3600 * 30 + 100: res = "6h" else: res = "1d" t_from_ms = t_from * 1000 t_to_ms = t_to * 1000 print("token: " + token_name + "f_from: " + str(t_from) + " - t_to: " + str(t_to) + " - resolution = " + str(res)) clef = os.environ.get('BINANCE_API_KEY') secret = os.environ.get('BINANCE_API_SECRET') client = Client(clef, secret) candles = client.get_klines(symbol=token_name, interval=res, startTime=t_from_ms, endTime=t_to_ms) return candles
def GetVolume(cr): client = Client(os.getenv("secret1"), os.getenv("secret2")) trades = client.get_klines(symbol=cr, interval=Client.KLINE_INTERVAL_1HOUR) tickers = client.get_ticker(symbol=cr) helper0 = [] helper1 = [] helper2 = [] helper3 = [] helper5 = [] for tr in trades: helper0.append(tr) x = 0 while x < constants.NUMBER: helper5.append( pd.to_datetime(float(trades[len(trades) - x - 1][0]), unit='ms').to_pydatetime()) helper1.append(round(float(helper0[len(helper0) - 1 - x][9]), 2)) helper2.append(round(float(helper0[len(helper0) - 1 - x][5]), 2)) helper3.append( round(float(helper0[len(helper0) - 1 - x][5]), 2) - round(float(helper0[len(helper0) - 1 - x][9]), 2)) x = x + 1 Data = { "Date": helper5, "Buy": helper1, "Sell": helper3, "Total": helper2, } df = pd.DataFrame(Data) return df
def binance_price(): client = Client(api_key= keys.ApiKey, api_secret=keys.SecretKey) candles = client.get_klines(symbol='XRPUSDT', interval=Client.KLINE_INTERVAL_1HOUR) # print(candles) # https://python-binance.readthedocs.io/en/latest/binance.html#binance.client.Client.get_klines # adresinde ne oldukları yazıyor candles_data_frame = df(candles) # print (candles_data_frame) # üsttekini datafreme içine koduk candles_data_frame_date = candles_data_frame[0] # ilk sütün zaman damgası final_date = [] for time in candles_data_frame_date: readable = datetime.fromtimestamp(int(time/1000)) # binance time milisecond cinsinden olduğu için 1000 e böl final_date.append(readable) candles_data_frame.pop(11) # 11 sutunu çıkardık candles_data_frame[0] = final_date # tarihi okunabilir yaptık candles_data_frame.rename(columns={0:'date'}, inplace=True) # tarih sütünü ismi date oldu candles_data_frame.set_index('date', inplace=True) candles_data_frame.columns=['open', 'high', 'low', 'close', 'volume', 'close_time', 'asset_volume', 'trade_number', 'taker_buy_base', 'taker_buy_quote'] return candles_data_frame
def get_historical_klines(symbol, interval, start_str, end_str=None): client = Client(None, None) output_data = [] limit = 500 timeframe = interval_to_milliseconds(interval) start_ts = date_to_milliseconds(start_str) end_ts = None if end_str: end_ts = date_to_milliseconds(end_str) idx = 0 symbol_existed = False while True: temp_data = client.get_klines(symbol=symbol,interval=interval,limit=limit,startTime=start_ts,endTime=end_ts) if not symbol_existed and len(temp_data): symbol_existed = True if symbol_existed: output_data += temp_data start_ts = temp_data[len(temp_data) - 1][0] + timeframe else: start_ts += timeframe idx += 1 if len(temp_data) < limit: break if idx % 3 == 0: time.sleep(1) return output_data
def price_binance(): #api_keys and api_secret are stored in keys.py file in the same folder, donot post it on github. client = Client(api_key=keys.pkey, api_secret=keys.skey) #getting kline/candelstics from python-binance documentation candles = client.get_klines(symbol='LTCUSDT', interval=Client.KLINE_INTERVAL_1HOUR) candles_data_frame = df(candles) candles_data_frame_date = candles_data_frame[0] final_date = [] for time in candles_data_frame_date.unique(): readable = datetime.fromtimestamp(int(time / 1000)) final_date.append(readable) candles_data_frame.pop(0) candles_data_frame.pop(11) dataframe_final_date = df(final_date) dataframe_final_date.columns = ['date'] final_df = candles_data_frame.join(dataframe_final_date) final_df.set_index('date', inplace=True) final_df.columns = [ 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_asset_value', 'no_of_trades', 'taker_by_base', 'taker_by_quotes' ] return final_df
def binance_price(): client = Client(api_key=keys['Pkey'], api_secret=keys['Skey']) candles = client.get_klines(symbol='LTCUSDT', interval=Client.KLINE_INTERVAL_1HOUR) candles_data_frame = df(candles) candles_data_frame_date = candles_data_frame[0] final_date = [] for time in candles_data_frame_date.unique(): readable = datetime.fromtimestamp(int(time / 1000)) final_date.append(readable) candles_data_frame.pop(0) candles_data_frame.pop(11) dataframe_final_date = df(final_date) dataframe_final_date.columns = ['date'] final_dataframe = candles_data_frame.join(dataframe_final_date) final_dataframe.set_index('date', inplace=True) final_dataframe.columns = [ 'open', 'high', 'low', 'close', 'volume', 'close_time', 'asset_volume', 'trade_number', 'taker_buy_base', 'taker_buy_quote' ] return final_dataframe
def main(): now = datetime.datetime.now() currentDate = str(now.year) + "-" + str(now.month) + "-" + str(now.day) if len(sys.argv) != 2: sys.exit(1) Find_Stocks = cmdCheck() credentials = getHeaders(Find_Stocks) symbols = [] if Find_Stocks: symbols = getStockSymbols() STOCKS_BASE_URL = "https://paper-api.alpaca.markets/v2/orders" # getStocks() else: client = Client(credentials['keyId'], credentials['secretKey']) symbols = getCryptoSymbols() print("CURRENT DATE: " + str(datetime.datetime.fromtimestamp(time.time()))) for i in range(len(symbols)): candle = client.get_klines(symbol=symbols[i], interval=Client.KLINE_INTERVAL_30MINUTE) offset = int(time.time() - (candle[0][6] // 1000)) openTime = datetime.datetime.fromtimestamp((candle[0][0] // 1000) + offset) closeTime = datetime.datetime.fromtimestamp((candle[0][6] // 1000) + offset) print(symbols[i]) print("Open " + str(openTime)) print("Close: " + str(closeTime))
class Binance: def __init__(self, config_path): with open(config_path, 'r') as f: config = yaml.load(f) self.binance_api_key = config['binance_key'] self.binance_api_secret = config['binance_secret'] ### CONSTANTS self.binsizes = {"1m": 1, "5m": 5, "1h": 60, "1d": 1440} self.batch_size = 750 self.binance_client = Client(api_key = self.binance_api_key, api_secret = self.binance_api_secret) def minutes_of_new_data(self, symbol, kline_size, data, source): if len(data) > 0: old = parser.parse(data["timestamp"].iloc[-1]) elif source == "binance": old = datetime.strptime('1 Jan 2017', '%d %b %Y') if source == "binance": new = pd.to_datetime(self.binance_client.get_klines(symbol = symbol, interval=kline_size)[-1][0], unit='ms') return old, new def get_binance_data(self, symbol, kline_size, start, end): klines = self.binance_client.get_historical_klines(symbol, kline_size, start, end) data = pd.DataFrame(klines, columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore' ]) data['timestamp'] = pd.to_datetime(data['timestamp'], unit='ms') return data def get_all_binance(self, symbol, kline_size, save = False): filename = '%s-%s-data.csv' % (symbol, kline_size) if os.path.isfile(filename): data_df = pd.read_csv(filename) else: data_df = pd.DataFrame() oldest_point, newest_point = self.__class__minutes_of_new_data(symbol, kline_size, data_df, source = "binance") delta_min = (newest_point - oldest_point).total_seconds()/60 available_data = math.ceil(delta_min / self.binsizes[kline_size]) if oldest_point == datetime.strptime('1 Jan 2017', '%d %b %Y'): print('Downloading all available %s data for %s. Be patient..!' % (kline_size, symbol)) else: print('Downloading %d minutes of new data available for %s, i.e. %d instances of %s data.' % (delta_min, symbol, available_data, kline_size)) klines = self.binance_client.get_historical_klines(symbol, kline_size, oldest_point.strftime("%d %b %Y %H:%M:%S"), newest_point.strftime("%d %b %Y %H:%M:%S")) data = pd.DataFrame(klines, columns = ['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_av', 'trades', 'tb_base_av', 'tb_quote_av', 'ignore' ]) data['timestamp'] = pd.to_datetime(data['timestamp'], unit='ms') if len(data_df) > 0: temp_df = pd.DataFrame(data) data_df = data_df.append(temp_df) else: data_df = data data_df.set_index('timestamp', inplace=True) if save: data_df.to_csv(filename) print('All caught up..!') return data_df
def binanceToJsonMinute(self): clientBinance = Client(keys.public_key, keys.secret_key) candles_eu = [] candles_us = [] for index in self.indexs: symbol = self.cryptos.get(index) try: non_error = 0 candles_us = clientBinance.get_klines( symbol=symbol + 'USDT', interval=Client.KLINE_INTERVAL_1MINUTE, limit=2) print(len(candles_us)) non_error = 1 #candles_eu=clientBinance.get_klines(symbol=symbol+'EUR', interval=Client.KLINE_INTERVAL_1MINUTE, limit=2) #non_error = 2 except BinanceAPIException as e: print(str(e) + ' ' + symbol) finally: if (non_error == 1 or len(candles_eu) == 0): for i in range(0, len(candles_us)): line = tools.binance_dataparser_us(candles_us[i]) taux = self.getRate(crypto=index, value=line[2], time=candles_us[i][0] / 1000) self.append(time=line[0], measurement=index, eur=line[1], usd=line[2], low_eur=line[3], low_usd=line[4], high_eur=line[5], high_usd=line[6], volume=line[7], taux_h=taux[0], taux_j=taux[1], taux_s=taux[2]) elif (non_error == 2): print(symbol + str(len(candles_us)) + ' ' + str(len(candles_eu))) for i in range(0, len(candles_us)): line = tools.binance_dataparser( candles_eu[i], candles_us[i]) taux = self.getRate(crypto=index, value=line[2], time=candles_us[i][0] / 1000) self.append(time=line[0], measurement=index, eur=line[1], usd=line[2], low_eur=line[3], low_usd=line[4], high_eur=line[5], high_usd=line[6], volume=line[7], taux_h=taux[0], taux_j=taux[1], taux_s=taux[2])
class Aquisition(object): def __init__(self): self.client = Client('', '') self.df = pd.DataFrame(columns= ['Open_time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close_time']) def cast_to_dataframe(self, opentime, lopen, lhigh, llow, lclose, lvol, closetime): self.df['Open_time'] = opentime self.df["date"] = opentime self.df['Open'] = np.array(lopen).astype(np.float) self.df['High'] = np.array(lhigh).astype(np.float) self.df['Low'] = np.array(llow).astype(np.float) self.df['Close'] = np.array(lclose).astype(np.float) self.df['Volume'] = np.array(lvol).astype(np.float) self.df['Close_time'] = closetime self.df["date"] = pd.to_datetime(self.df['date'],unit='ms') def parse(self, candles): opentime, lopen, lhigh, llow, lclose, lvol, closetime = [], [], [], [], [], [], [] for candle in candles: opentime.append(candle[0]) lopen.append(candle[1]) lhigh.append(candle[2]) llow.append(candle[3]) lclose.append(candle[4]) lvol.append(candle[5]) closetime.append(candle[6]) self.cast_to_dataframe(opentime, lopen, lhigh, llow, lclose, lvol, closetime) def get_candles(self): #candles = self.client.get_historical_klines("BTCUSDT", Client.KLINE_INTERVAL_1HOUR, "1 Ago, 2018") candles = self.client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_1MINUTE) self.parse(candles) return self.df def get_price_now(self): r = requests.get("https://www.binance.com/api/v3/ticker/price?symbol=BTCUSDT") r = r.content jsonResponse = json.loads(r.decode('utf-8')) return float(jsonResponse['price']) def plot_candles(self): df = self.df[450:] df["date"] = df["Open_time"] df["date"] = pd.to_datetime(self.df['date'],unit='ms') inc = df.Close > df.Open dec = df.Open > df.Close w = 12*60*60*1000 # half day in ms TOOLS = "pan,wheel_zoom,box_zoom,reset,save" p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000, title = "BITCOIN Candlestick") p.xaxis.major_label_orientation = pi/4 p.grid.grid_line_alpha=0.3 p.segment(df.date, df.High, df.date, df.Low, color="black") p.vbar(df.date[inc], w, df.Open[inc], df.Close[inc], fill_color="#006400", line_color="black") p.vbar(df.date[dec], w, df.Open[dec], df.Close[dec], fill_color="#F2583E", line_color="black") output_file("candlestick.html", title="candlestick.py Grafico de Candles") show(p)
def get_historical_klines(symbol, interval, start_str, end_str=None): # create the Binance client, no need for api key client = Client("", "") # init our list output_data = [] # setup the max limit limit = 500 # convert interval to useful value in seconds timeframe = interval_to_milliseconds(interval) # convert our date strings to milliseconds start_ts = date_to_milliseconds(start_str) # if an end time was passed convert it end_ts = None if end_str: end_ts = date_to_milliseconds(end_str) idx = 0 # it can be difficult to know when a symbol was listed on Binance so allow start time to be before list date symbol_existed = False while True: # fetch the klines from start_ts up to max 500 entries or the end_ts if set temp_data = client.get_klines(symbol=symbol, interval=interval, limit=limit, startTime=start_ts, endTime=end_ts) # handle the case where our start date is before the symbol pair listed on Binance if not symbol_existed and len(temp_data): symbol_existed = True if symbol_existed: # append this loops data to our output data output_data += temp_data # update our start timestamp using the last value in the array and add the interval timeframe start_ts = temp_data[len(temp_data) - 1][0] + timeframe else: # it wasn't listed yet, increment our start date start_ts += timeframe idx += 1 # check if we received less than the required limit and exit the loop if len(temp_data) < limit: # exit the while loop break # sleep after every 3rd call to be kind to the API if idx % 3 == 0: time.sleep(1) return output_data
def main(argv): period = 10 ########### API KEYS ########### #Binance b_api_key = 'newb' b_api_secret = 'noob' #HitBTC h_api_key = 'nub' h_api_secret = '...' #### Client Declarations #### b_client = Client(b_api_key, b_api_secret) h_client = H_Client("https://api.hitbtc.com", h_api_key, h_api_secret) ############# #Get Market Depth depth = b_client.get_order_book(symbol='BNBBTC') #Get Recent Trades trades_recent = b_client.get_recent_trades(symbol='BNBBTC') #Get Historical Trades trades_hist = b_client.get_historical_trades(symbol='BNBBTC') #Get Aggregate trades trades_agg = b_client.get_aggregate_trades(symbol='BNBBTC') #Get Kline/Candlesticks candles = b_client.get_klines(symbol='BNBBTC', interval=KLINE_INTERVAL_30MINUTE) #Get 24hr Ticker tickers_24 = b_client.get_ticker() #Get All Prices: Get last price for all markets prices = b_client.get_all_tickers() #Get Orderbook Tickers: Get first bid and ask entry in the order book for all markets tickers_order = b_client.get_orderbook_tickers() ############ # print(h_client.get_orderbook("ETHBTC")) ethbtc_orderbook = h_client.get_orderbook("ETHBTC") #Check to see which coins can be traded and withdrawn h_cryptolist = [] h_currencies = h_client.currencies() for currency in h_currencies: if currency['id'] == 'BTC': print(currency) minimum_ask = 0 minimum_bid = 0 for order, price in ethbtc_orderbook.items(): print(order, ":", price) print(len(price)) # for i in price.length(): if order == 'ask': pass # print("minimum ask price is: ") if order == 'bid': pass
def screen(): client = Client("api-key", "api-secret", {"verify": False, "timeout": 20}) positiveCoin = get_positive_coin.positiveCoin bullishKScoin = [] for m in positiveCoin: candles = client.get_klines(symbol=m, interval=client.KLINE_INTERVAL_30MINUTE) if candles[-2][1] < candles[-2][4] <= candles[-1][1] < candles[-1][4]: bullishKScoin.append(m) atrCoin = [] for m in bullishKScoin: candles = client.get_klines(symbol=m, interval=client.KLINE_INTERVAL_5MINUTE) close = [] for n in candles: close.append(float(n[4])) avgtr = atr(close, 14) if avgtr[-1] / close[-1] > 0.01: atrCoin.append(m) maxDemandRatio = 0 buyingCoin = '' for m in atrCoin: depth = client.get_order_book(symbol=m) buyingVol = 0 sellingVol = 0 for n in depth['bids'][0:20]: buyingVol = buyingVol + float(n[1]) for n in depth['asks'][0:20]: sellingVol = sellingVol + float(n[1]) demandRatio = buyingVol / sellingVol print(demandRatio) print(maxDemandRatio) if demandRatio > maxDemandRatio: maxDemandRatio = demandRatio buyingCoin = m if maxDemandRatio < 1.5: buyingCoin = '' print(bullishKScoin) print(atrCoin) print(buyingCoin) return buyingCoin
def query_api(pair, freq, start=None, end=None, force=False): """Get Historical Klines (candles) from Binance. @freq: Binance kline frequency: 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M] m -> minutes; h -> hours; d -> days; w -> weeks; M -> months @force: if False, only query unstored data (faster). If True, query all. Return: list of OHLCV value """ t1 = Timer() limit = 500 idx = 0 results = [] periodlen = intrvl_to_ms(freq) end_ts = datestr_to_ms(end) if end else dt_to_ms(now()) start_ts = datestr_to_ms(start) if start else end_ts - (periodlen * 20) # Skip queries for records already stored if force == False: query = {"pair":pair, "freq":freq} if start: query["open_time"] = {"$gt": datestr_to_dt(start)} newer = app.get_db().candles.find(query).sort('open_time',-1).limit(1) if newer.count() > 0: dt = list(newer)[0]['open_time'] start_ts = int(dt.timestamp()*1000 + periodlen) if start_ts > end_ts: log.debug("All records for %s already stored.", pair) return [] client = Client("", "") #while len(results) < 500 and start_ts < end_ts: while start_ts < end_ts: try: data = client.get_klines(symbol=pair, interval=freq, limit=limit, startTime=start_ts, endTime=end_ts) if len(data) == 0: start_ts += periodlen else: # Don't want candles that aren't closed yet if data[-1][6] >= dt_to_ms(now()): results += data[:-1] break results += data start_ts = data[-1][0] + periodlen except Exception as e: log.exception("Binance API request error. e=%s", str(e)) log.debug('%s %s %s queried [%ss].', len(results), freq, pair, t1.elapsed(unit='s')) return results
class General: def __init__(self, is_it_for_real=False): self.iv_sec = { '1m': 60, '3m': 60 * 3, '5m': 60 * 5, '15m': 60 * 15, '30m': 60 * 30, '1h': 60 * 60, '2h': 60 * 60 * 2, '4h': 60 * 60 * 4, '8h': 60 * 60 * 8, '1d': 60 * 60 * 24, '3d': 60 * 60 * 24 * 3, '1w': 60 * 60 * 24 * 7 } if is_it_for_real: from binance.client import Client self.client = Client("", "") def wait_till(self, interval, advance=0, skip_till=None): lag = self.client.get_server_time()['serverTime'] / 1000 - time.time( ) + 0.5 #taka kalibracja, żeby serwer otrzymał nasz request ułamek sekundy przed rządaną przez nas chwilą print('Lag: ', round(lag, 3)) to_wait = (-time.time() - advance - lag) % self.iv_sec[interval] print(f"Candle ({interval}) closing in {round(to_wait,3)} seconds") if skip_till: if to_wait > skip_till: print("Skipping the wait") return to_wait time.sleep(to_wait) print( f"Server time: {datetime.fromtimestamp(self.client.get_server_time()['serverTime']/1000)}" ) return 0 def get_candles(self, symbol, interval, limit, market='spot'): if market == 'spot': candles = pd.DataFrame( self.client.get_klines(symbol=symbol + 'USDT', interval=interval, limit=limit + 1)) elif market == 'future' or market == 'futures': candles = pd.DataFrame( self.client.futures_klines(symbol=symbol + 'USDT', interval=interval, limit=limit + 1)) candles = candles[[0, 1, 2, 3, 4, 5]].astype(float) return candles
def minutes_of_new_data(self, symbol, kline_size, data, source, startDate="01 Nov 2017"): if len(data) > 0: old = parser.parse(data["timestamp"].iloc[-1]) elif source == "binance": old = datetime.strptime(startDate, '%d %b %Y') if source == "binance": binance_client = Client( api_key=self.binance_api_key, api_secret=self.binance_api_secret) new = pd.to_datetime(binance_client.get_klines( symbol=symbol, interval=kline_size)[-1][0], unit='ms') return old, new
def get_klines_data(symbol: str, interval: str, start_ts: int, end_ts: int, output_location: str): ''' :param symbol: traiding pair: example, BTCUSDT :param interval: time interval, 1m, 3m, 1h ... :param start_ts: start timestamp :param end_ts: end timestamp :param output_location: output file location ends with / :return: file on file system Example: get_data('BTCUSDT', '1m', 1630683540000, 1630683599999) ''' try: BINANCE_API_KEY = os.environ["BINANCE_API_KEY"] BINANCE_SECRET_KEY = os.environ["BINANCE_SECRET_KEY"] except KeyError: raise KeyError client = Client(BINANCE_API_KEY, BINANCE_SECRET_KEY) candles = client.get_klines(symbol=symbol, interval=interval, startTime=start_ts, endTime=end_ts) #pandas dataframe data = [sub[:len(sub) - 3] for sub in candles] # removes unused data [sub.append(symbol) for sub in data] # add symbol as column columns = [ "open_time", "open_price", "high_price", "low_price", "close_price", "BTC_volume", "close_time", "USD_volume", "number_of_trades", "trading_pair" ] output_file = f'{output_location}{symbol}_{start_ts}_{end_ts}.parquet' df = pd.DataFrame(data=data, columns=columns) # write out parquet file table = pa.Table.from_pandas(df) pq.write_table(table, output_file, compression='GZIP') # print dataframe. print(df) ''' #spark dataframe #loads data to the Spark Dataframe spark = SparkSession.builder.appName("Binance").getOrCreate() dataframe = spark.createDataFrame(data, columns) dataframe.show() dataframe.write.parquet(output_file) ''' return f"Done. File location: {output_file}"
class Market: def __init__(self, k, s): self.k, self.s = k, s self.client = Client(self.k, self.k) self.request_count = 0 def info(self, pair='BTCUSDT'): rp = self.client.get_symbol_info(symbol=pair) df = pd.DataFrame.from_dict(rp, orient='index', columns=['info']) self.request_count += 1 del rp return df def ticker(self, pair='BTCUSDT'): rp = self.client.get_ticker(symbol=pair) df = pd.DataFrame.from_dict(rp, orient='index', columns=['ticker']) df.ticker.openTime = Converter.unix_to_timestamp(df.ticker.openTime) df.ticker.closeTime = Converter.unix_to_timestamp(df.ticker.closeTime) df = df.ticker.map(Formatter.tidy_string_decimal) df = pd.DataFrame(df, columns=['ticker']) self.request_count += 1 del rp return df def kline(self, symbol, cycle='1h'): rp = self.client.get_klines(symbol=symbol, interval=cycle) kn = ['date', 'open', 'high', 'low', 'close', 'volume', 'closeTime', 'quoteAssetVolume', 'numberOfTrades', 'takerBuyBasesAssetVolume', 'takerBuyQuotAssetVolume', 'Ignore'] df = pd.DataFrame(data=rp, columns=kn) df['date'] = df['date'].map(Converter.unix_to_timestamp) df['closeTime'] = df['closeTime'].map(Converter.unix_to_timestamp) for i in df.columns: df[i] = df[i].map(Formatter.tidy_string_decimal) df = df.drop(['Ignore'], axis=1) df = df.reset_index(drop=True) self.request_count += 1 del rp return df
def main(tradepair): client = Client(api_key, api_secret) candles = client.get_klines(symbol=tradepair, interval=KLINE_INTERVAL_5MINUTE, limit=400) x_values = [float(i[4]) for i in candles] result = mavg.exp_mavg(x_values, 20) result_ = mavg.s_mavg(x_values, 20) plt.plot(x_values, label='BTC') plt.plot(result, label='EXPMAVG') plt.plot(result_, label='SMAVG') plt.legend() plt.show()
def get_klines(request): '''Parameters: symbol (str) – required interval (str) – limit (int) – Default 500; max 500. startTime (int) – endTime (int) – ''' if request.method == "POST": cli = Client() data = request.data try: agg = cli.get_klines(**data) return JsonResponse(agg, safe=False) except BinanceAPIException as e: return JsonResponse({"MessageError": e.message}, safe=False)
def get_klines(self, period=Client.KLINE_INTERVAL_1HOUR, base_coin='BTC', alt_coin='XRP', localize_time=True, index_time_column_name='closeTime', start_date=None, end_date=None): self.set_coin_pair(base_coin, alt_coin) client = Client(self.key, self.secret) if start_date is None and end_date is None: candles = client.get_klines(symbol=self.coin_pair, interval=period) else: start_str = Binance.get_date_string(start_date) end_str = Binance.get_date_string(end_date) candles = client.get_historical_klines(symbol=self.coin_pair, interval=period, start_str=start_str, end_str=end_str) df = pd.DataFrame(candles) df.columns = self.kline_columns df['open'] = pd.to_numeric(df['open']) df['close'] = pd.to_numeric(df['close']) df['volume'] = pd.to_numeric(df['volume']) if localize_time: mytz = get_localzone() df['openTime'] = pd.to_datetime( df['openTime'], unit='ms').dt.tz_localize('UTC').dt.tz_convert(mytz) df['closeTime'] = pd.to_datetime( df['closeTime'], unit='ms').dt.tz_localize('UTC').dt.tz_convert(mytz) if index_time_column_name == 'closeTime': df.set_index('closeTime', inplace=True) if index_time_column_name == 'openTime': df.set_index('openTime', inplace=True) return df
def screen(): client = Client("api-key", "api-secret", {"verify": False, "timeout": 20}) positiveCoin = get_positive_coin.positiveCoin pbCoin = [] for m in positiveCoin: candles = client.get_klines(symbol=m, interval=client.KLINE_INTERVAL_15MINUTE) close = [] for n in candles: close.append(float(n[4])) pb = pbb(close, 20, 2.0, 2.0) if 50 > pb[-1] > 10: pbCoin.append(m) # bullishKScoin = [] # for m in pbCoin: # candles = client.get_klines(symbol=m, interval=client.KLINE_INTERVAL_30MINUTE) # if candles[-2][1] < candles[-2][4] <= candles[-1][1] < candles[-1][4]: # bullishKScoin.append(m) maxDemandRatio = 0 buyingCoin = '' for m in pbCoin: depth = client.get_order_book(symbol=m) buyingVol = 0 sellingVol = 0 for n in depth['bids'][0:20]: buyingVol = buyingVol + float(n[1]) for n in depth['asks'][0:20]: sellingVol = sellingVol + float(n[1]) demandRatio = buyingVol / sellingVol print(demandRatio) print(maxDemandRatio) if demandRatio > maxDemandRatio: maxDemandRatio = demandRatio buyingCoin = m if maxDemandRatio < 1: buyingCoin = '' print(pbCoin) print(buyingCoin) return buyingCoin
def screen(): client = Client("api-key", "api-secret", {"verify": False, "timeout": 20}) positiveCoin = get_positive_coin.positiveCoin bollCoin = [] for m in positiveCoin: candles = client.get_klines(symbol=m, interval=client.KLINE_INTERVAL_15MINUTE) close = [] for n in candles: close.append(float(n[4])) pb = pbb(close, 20, 2.0, 2.0) lb = lbb(close, 20, 2.0) ub = ubb(close, 20, 0.2) if pb[-1] > 50 and ((ub[-1] - lb[-1]) / lb[-1]) > 0.03: if candles[-2][1] < candles[-2][4]: if candles[-2][4] <= candles[-1][1]: if candles[-1][1] < candles[-1][4]: bollCoin.append(m) buyingCoin = '' maxDemandRatio = 0 for m in bollCoin: depth = client.get_order_book(symbol=m) buyingVol = 0 sellingVol = 0 for n in depth['bids'][0:20]: buyingVol = buyingVol + float(n[1]) for n in depth['asks'][0:20]: sellingVol = sellingVol + float(n[1]) demandRatio = buyingVol / sellingVol print(demandRatio) print(maxDemandRatio) if demandRatio > maxDemandRatio: maxDemandRatio = demandRatio buyingCoin = m if maxDemandRatio < 1.5: buyingCoin = '' print(bollCoin) print(buyingCoin) return buyingCoin
def GetCandles(symbol): client = Client(apiKey, apiSecret) candles = client.get_klines(symbol=symbol, interval=Client.KLINE_INTERVAL_30MINUTE) for candle in candles: openTime = candle[0] openTimeUTC = datetime.utcfromtimestamp(openTime/1000).strftime('%Y-%m-%d %H:%M:%S') open = candle[1] high = candle[2] low = candle[3] close = candle[4] volume = candle[5] closeTime = candle[6] closeTimeUTC = datetime.utcfromtimestamp(closeTime / 1000).strftime('%Y-%m-%d %H:%M:%S') quoteAssetVolume = candle[7] numTrades = candle[8] takerBuyBase = candle[9] takerBuyQuote = candle[10] ignore = candle[11]
import config import csv from binance.client import Client client = Client(config.API_KEY, config.API_SECRET) # prices = client.get_all_tickers() # for i in prices: # print(i) candles = client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_15MINUTE) csvfile = open('daily.csv', 'w', newline='') candlestick_writer = csv.writer(csvfile, delimiter=',',) # for candle in candles: # print(candle) # candlestick_writer.writerow(candle) candles = client.get_historical_klines("BTCUSDT", Client.KLINE_INTERVAL_1DAY, "1 Jan, 2019", "7 Feb, 2021") for candle in candles: candle[0] = candle[0] / 1000 candlestick_writer.writerow(candle) csvfile.close
def get_historical_klines(symbol, interval, start_str, end_str=None): """Get Historical Klines from Binance 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: Biannce 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 """ # create the Binance client, no need for api key client = Client("", "") # init our list output_data = [] # setup the max limit limit = 500 # convert interval to useful value in seconds timeframe = interval_to_milliseconds(interval) # convert our date strings to milliseconds start_ts = date_to_milliseconds(start_str) # if an end time was passed convert it end_ts = None if end_str: end_ts = date_to_milliseconds(end_str) idx = 0 # it can be difficult to know when a symbol was listed on Binance so allow start time to be before list date symbol_existed = False while True: # fetch the klines from start_ts up to max 500 entries or the end_ts if set temp_data = client.get_klines(symbol=symbol, interval=interval, limit=limit, startTime=start_ts, endTime=end_ts) # handle the case where our start date is before the symbol pair listed on Binance if not symbol_existed and len(temp_data): symbol_existed = True if symbol_existed: # append this loops data to our output data output_data += temp_data # update our start timestamp using the last value in the array and add the interval timeframe start_ts = temp_data[len(temp_data) - 1][0] + timeframe else: # it wasn't listed yet, increment our start date start_ts += timeframe idx += 1 # check if we received less than the required limit and exit the loop if len(temp_data) < limit: # exit the while loop break # sleep after every 3rd call to be kind to the API if idx % 3 == 0: time.sleep(1) #print(output_data) return output_data
class ApiCalls: """Collection of api related methods.""" def __init__(self, mw, tp): self.mw = mw self.client = Client(mw.cfg_manager.api_key, mw.cfg_manager.api_secret, { "verify": True, "timeout": 10 }) app.client = self.client self.threadpool = tp def initialize(self): # print("setting client: " + str(self.client)) try: val["coins"] = self.availablePairs() val["accHoldings"] = self.getHoldings() val["tickers"] = self.getTickers() val["apiCalls"] += 3 # userMsg = dict() # accHoldings = dict() self.set_pair_values() self.mw.is_connected = True except (BinanceAPIException, NameError) as e: print("API ERROR") print(str(e)) if "code=-1003" in str(e): print("ja ein api error :)") elif "code=-2014": print("API KEY INVALID") # def get_tether(client): # tether_info = client.get_ticker(symbol="BTCUSDT") # return tether_info def set_pair_values(self): """Set various values based on the chosen pair.""" pair = self.mw.cfg_manager.pair val["decimals"] = len(str(val["coins"][pair]["tickSize"])) - 2 if int(val["coins"][pair]["minTrade"]) == 1: val["assetDecimals"] = 0 else: val["assetDecimals"] = len(str(val["coins"][pair]["minTrade"])) - 2 def availablePairs(self): """ Create a dictonary containing all BTC tradepairs excluding USDT. Keys are: {'symbol': 'ETHBTC', 'tradedMoney': 3024.89552855, 'baseAssetUnit': 'Ξ', 'active': True, 'minTrade': '0.00100000', 'baseAsset': 'ETH', 'activeSell': 66254.102, 'withdrawFee': '0', 'tickSize': '0.000001', 'prevClose': 0.044214, 'activeBuy': 0, 'volume': '66254.102000', 'high': '0.047998', 'lastAggTradeId': 2809764, 'decimalPlaces': 8, 'low': '0.043997', 'quoteAssetUnit': '฿', 'matchingUnitType': 'STANDARD', 'close': '0.047656', 'quoteAsset': 'BTC', 'open': '0.044214', 'status': 'TRADING', 'minQty': '1E-8'} """ # create a local dictionary coins = dict() # API Call products = self.client.get_products() # For every entry in API answer: for i, pair in enumerate(products["data"]): # Check if pair contains BTC, does not contain USDT and if volume is >0 if "BTC" in pair["symbol"] and "USDT" not in pair[ "symbol"] and float(products["data"][i]["volume"]) > 0.0: # Create a temporary dictionary to store keys and values tempdict = dict() # Add every key-value pair to the temp dictionary for key, value in pair.items(): tempdict[key] = value # Add every temp dictionary to the coin dictionary coins[tempdict["symbol"]] = tempdict return coins def getHoldings(self): """Make an inital API call to get BTC and coin holdings.""" # API Call: order = self.client.get_account() accHoldings = dict() for i in range(len(order["balances"])): accHoldings[order["balances"][i]["asset"]] = { "free": order["balances"][i]["free"], "locked": order["balances"][i]["locked"] } return accHoldings def getTickers(self): """Make an initial API call to get ticker data.""" ticker = self.client.get_ticker() # print(str(ticker)) all_tickers = dict() for _, ticker_data in enumerate(ticker): if "BTC" in ticker_data["symbol"]: # print(str(ticker_data)) all_tickers[ticker_data["symbol"]] = ticker_data return all_tickers def getTradehistory(self, pair): """Make an initial API call to get the trade history of a given pair. This is used until updated by websocket data.""" # API call globalList = list() trades = self.client.get_aggregate_trades(symbol=pair, limit=50) for _, trade in enumerate(reversed(trades)): globalList.insert( 0, { "price": str(trade["p"]), "quantity": str(trade["q"]), "maker": bool(trade["m"]), "time": str(trade["T"]) }) return list(reversed(globalList)) def getDepth(self, symbol): """Make an initial API call to get market depth (bids and asks).""" # API Call depth = self.client.get_order_book(symbol=symbol, limit=20) asks = depth["asks"] bids = depth["bids"] return {"bids": bids, "asks": asks} def api_create_order(self, side, pair, price, amount, progress_callback): print("create order: " + str(price) + " " + str(amount)) try: if side == "Buy": order = self.client.order_limit_buy(symbol=pair, quantity=str(amount), price=str(price)) elif side == "Sell": order = self.client.order_limit_sell(symbol=pair, quantity=str(amount), price=str(price)) print("order status: " + str(order)) return order except BinanceAPIException as e: print("create order failed: " + str(e)) print(str(order)) def api_cancel_order(self, client, order_id, symbol, progress_callback): print("cancel order " + str(symbol) + " " + str(order_id)) try: self.client.cancel_order(symbol=symbol, orderId=order_id) except BinanceAPIException as e: print("cancel failed " + str(e)) print(str(self.client)) print(str(symbol)) print(str(order_id)) def api_order_history(self, pair, progress_callback): orders = self.client.get_all_orders(symbol=pair) progress_callback.emit(orders) val["apiCalls"] += 1 def api_history(self, progress_callback): trade_history = self.getTradehistory(self.mw.cfg_manager.pair) progress_callback.emit({"history": reversed(trade_history)}) val["apiCalls"] += 1 def api_depth(self, progress_callback): depth = self.getDepth(self.mw.cfg_manager.pair) val["asks"] = depth["asks"] progress_callback.emit({"asks": val["asks"]}) val["bids"] = depth["bids"] progress_callback.emit({"bids": val["bids"]}) val["apiCalls"] += 1 def api_all_orders(self, progress_callback): print("CLEINT;" + str(self.client)) orders = self.client.get_open_orders() progress_callback.emit(orders) numberPairs = sum(val["pairs"].values()) print("number pairs: " + str(numberPairs)) def api_calls(self): """Inital and coin specific api calls""" worker = Worker(self.api_history) worker.signals.progress.connect(self.mw.live_data.batch_history) self.mw.threadpool.start(worker) worker = Worker(self.api_depth) worker.signals.progress.connect(self.mw.live_data.batch_orderbook) worker.signals.finished.connect(self.mw.limit_pane.t_complete) self.mw.threadpool.start(worker) self.get_trade_history(self.mw.cfg_manager.pair) def get_trade_history(self, pair): worker = Worker(partial(self.api_order_history, pair)) worker.signals.progress.connect(self.mw.history_table.orders_received) self.mw.threadpool.start(worker) def get_kline(self, pair, progress_callback): """Make an API call to get historical data of a coin pair.""" interval = "1m" try: # try since this is used heavily klines = self.client.get_klines(symbol=pair, interval=interval) except (ConnectionError, BinanceAPIException) as e: print(str(e)) progress_callback.emit([klines, pair, interval]) val["apiCalls"] += 1 def cancel_order_byId(self, order_id, symbol): """Cancel an order by id from within a separate thread.""" worker = Worker( partial(self.mw.api_manager.api_cancel_order, app.client, order_id, symbol)) # worker.signals.progress.connect(self.cancel_callback) self.threadpool.start(worker)
def get_historical_klines(symbol, interval, start_str, end_str=None): """Get Historical Klines from Binance 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: Biannce 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 """ # create the Binance client, no need for api key client = Client("", "") # init our list output_data = [] # setup the max limit limit = 500 # convert interval to useful value in seconds timeframe = interval_to_milliseconds(interval) # convert our date strings to milliseconds start_ts = date_to_milliseconds(start_str) # if an end time was passed convert it end_ts = None if end_str: end_ts = date_to_milliseconds(end_str) idx = 0 # it can be difficult to know when a symbol was listed on Binance so allow start time to be before list date symbol_existed = False while True: # fetch the klines from start_ts up to max 500 entries or the end_ts if set temp_data = client.get_klines( symbol=symbol, interval=interval, limit=limit, startTime=start_ts, endTime=end_ts ) # handle the case where our start date is before the symbol pair listed on Binance if not symbol_existed and len(temp_data): symbol_existed = True if symbol_existed: # append this loops data to our output data output_data += temp_data # update our start timestamp using the last value in the array and add the interval timeframe start_ts = temp_data[len(temp_data) - 1][0] + timeframe else: # it wasn't listed yet, increment our start date start_ts += timeframe idx += 1 # check if we received less than the required limit and exit the loop if len(temp_data) < limit: # exit the while loop break # sleep after every 3rd call to be kind to the API if idx % 3 == 0: time.sleep(1) return output_data