class BinanceData(object): # ---------------------------------------------------------------------- def __init__(self, strategy): """ 构造函数 :param strategy: 上层策略,主要用与使用strategy.writeCtaLog() """ self.strategy = strategy self.client = Client('-', '-') def get_bars(self, symbol, period, callback, bar_is_completed=False, bar_freq=1, start_dt=None): """ 返回k线数据 symbol:合约 period: 周期: 1min,3min,5min,15min,30min,1day,3day,1hour,2hour,4hour,6hour,12hour """ ret_bars = [] if symbol not in SYMBOL_LIST: self.strategy.writeCtaError(u'{} 合约{}不在下载清单中'.format( datetime.now(), symbol)) return False, ret_bars if period not in PERIOD_MAPPING: self.strategy.writeCtaError(u'{} 周期{}不在下载清单中'.format( datetime.now(), period)) return False, ret_bars if self.client is None: return False, ret_bars binance_symbol = symbol.upper().replace('_', '') binance_period = PERIOD_MAPPING.get(period) self.strategy.writeCtaLog('{}开始下载binance:{} {}数据.'.format( datetime.now(), binance_symbol, binance_period)) try: bars = self.client.get_klines(symbol=binance_symbol, interval=binance_period) for i, bar in enumerate(bars): add_bar = CtaBarData() try: add_bar.vtSymbol = symbol add_bar.symbol = symbol add_bar.datetime = datetime.fromtimestamp(bar[0] / 1000) add_bar.date = add_bar.datetime.strftime('%Y-%m-%d') add_bar.time = add_bar.datetime.strftime('%H:%M:%S') add_bar.tradingDay = add_bar.date add_bar.open = float(bar[1]) add_bar.high = float(bar[2]) add_bar.low = float(bar[3]) add_bar.close = float(bar[4]) add_bar.volume = float(bar[5]) except Exception as ex: self.strategy.writeCtaError( 'error when convert bar:{},ex:{},t:{}'.format( bar, str(ex), traceback.format_exc())) return False if start_dt is not None and bar.datetime < start_dt: continue ret_bars.append(add_bar) if callback is not None: callback(add_bar, bar_is_completed, bar_freq) return True, ret_bars except Exception as ex: self.strategy.writeCtaError('exception in get:{},{},{}'.format( binance_symbol, str(ex), traceback.format_exc())) return False, ret_bars
class BinanceData(object): # ---------------------------------------------------------------------- def __init__(self, strategy=None): """ 构造函数 :param strategy: 上层策略,主要用与使用writeLog() """ self.strategy = strategy self.client = Client('', '') def writeLog(self, content): if self.strategy and hasattr(self.strategy, 'writeCtaLog'): self.strategy.writeCtaLog(content) else: print(content) def writeError(self, content): if self.strategy and hasattr(self.strategy, 'writeCtaError'): self.strategy.writeCtaError(content) else: print(content, file=sys.stderr) def get_bars(self, symbol, period, callback, bar_is_completed=False, bar_freq=1, start_dt=None): """ 返回k线数据 symbol:合约 period: 周期: 1min,3min,5min,15min,30min,1day,3day,1hour,2hour,4hour,6hour,12hour """ ret_bars = [] if symbol not in SYMBOL_LIST: self.writeError(u'{} 合约{}不在下载清单中'.format(datetime.now(), symbol)) return False, ret_bars if period not in PERIOD_MAPPING: self.writeError(u'{} 周期{}不在下载清单中'.format(datetime.now(), period)) return False, ret_bars if self.client is None: return False, ret_bars binance_symbol = symbol.upper().replace('_', '') # 转换一下 binance_symbol = symbolFromOtherExchangesToBinance(binance_symbol) binance_period = PERIOD_MAPPING.get(period) self.writeLog('{}开始下载binance:{} {}数据.'.format(datetime.now(), binance_symbol, binance_period)) bars = [] try: bars = self.client.get_klines(symbol=binance_symbol, interval=binance_period) bar_len = len(bars) for i, bar in enumerate(bars): add_bar = CtaBarData() try: add_bar.vtSymbol = symbol add_bar.symbol = symbol add_bar.datetime = datetime.fromtimestamp(bar[0] / 1000) utcdatetime = datetime.utcfromtimestamp(bar[0] / 1e3) add_bar.date = add_bar.datetime.strftime('%Y-%m-%d') add_bar.time = add_bar.datetime.strftime('%H:%M:%S') # 币安的交易日,是按照utc来计算的 add_bar.tradingDay = utcdatetime.strftime('%Y-%m-%d') add_bar.open = float(bar[1]) add_bar.high = float(bar[2]) add_bar.low = float(bar[3]) add_bar.close = float(bar[4]) add_bar.volume = float(bar[5]) ret_bars.append(add_bar) except Exception as ex: self.writeError( 'error when convert bar:{},ex:{},t:{}'.format( bar, str(ex), traceback.format_exc())) return False, ret_bars if start_dt is not None and bar.datetime < start_dt: continue if callback is not None: # 最后一个bar,可能是不完整的,强制修改 if i == bar_len - 1 and bar_is_completed: # 根据秒数算的话,要+1,例如13:31,freq=31,第31根bar freq = int((datetime.now() - add_bar.datetime).total_seconds() / 60) + 1 callback(add_bar, False, freq) else: callback(add_bar, bar_is_completed, bar_freq) return True, ret_bars except Exception as ex: self.writeError('exception in get:{},{},{}'.format( binance_symbol, str(ex), traceback.format_exc())) return False, ret_bars def download_bars(self, symbol, period, start_dt=None): """ 返回k线数据 symbol:合约 period: 周期: 1min,3min,5min,15min,30min,1day,3day,1hour,2hour,4hour,6hour,12hour """ ret_bars = [] if symbol not in SYMBOL_LIST: self.writeError(u'{} 合约{}不在下载清单中'.format(datetime.now(), symbol)) return ret_bars if period not in PERIOD_MAPPING: self.writeError(u'{} 周期{}不在下载清单中'.format(datetime.now(), period)) return ret_bars if self.client is None: return ret_bars binance_symbol = symbol.upper().replace('_', '') # 转换一下 binance_symbol = symbolFromOtherExchangesToBinance(binance_symbol) binance_period = PERIOD_MAPPING.get(period) self.writeLog('{}开始下载binance:{} {}数据.'.format(datetime.now(), binance_symbol, binance_period)) if isinstance(start_dt, datetime): start_timestamp = int(start_dt.timestamp() * 1e3) else: start_timestamp = None try: bars = [] if start_timestamp is not None: end_timestamp = int(datetime.now().timestamp() * 1e3) while start_timestamp <= end_timestamp: try: self.writeLog(u'从{} ~{} 下载 {}数据'.format( datetime.fromtimestamp(start_timestamp / 1e3), datetime.fromtimestamp(end_timestamp / 1e3), binance_symbol)) bars_per_loop = self.client.get_klines( symbol=binance_symbol, interval=binance_period, startTime=start_timestamp, endTime=end_timestamp, limit=1000) if len(bars_per_loop) == 0: self.writeLog(u'数据长度为0,结束') break # 更新开始时间,为这次取得最后一个值 start_timestamp = bars_per_loop[-1][0] if len(bars) > 0: while bars[-1][0] >= bars_per_loop[0][0]: if len(bars_per_loop) == 1: bars_per_loop = [] start_timestamp = end_timestamp + 1 break bars_per_loop = bars_per_loop[1:] # 追加bars bars.extend(bars_per_loop) except Exception as ex: self.writeError(u'下载数据{} {} 异常:{},{}'.format( binance_symbol, binance_period, str(ex), traceback.format_exc())) break else: bars = self.client.get_klines(symbol=binance_symbol, interval=binance_period) for i, bar in enumerate(bars): add_bar = {} try: bar_datetime = datetime.fromtimestamp(bar[0] / 1e3) utc_datetime = datetime.utcfromtimestamp(bar[0] / 1e3) add_bar['datetime'] = bar_datetime.strftime( '%Y-%m-%d %H:%M:%S') add_bar['date'] = bar_datetime.strftime('%Y-%m-%d') add_bar['time'] = bar_datetime.strftime('%H:%M:%S') add_bar['open'] = float(bar[1]) add_bar['high'] = float(bar[2]) add_bar['low'] = float(bar[3]) add_bar['close'] = float(bar[4]) add_bar['volume'] = float(bar[5]) add_bar['tradingDay'] = utc_datetime.strftime('%Y-%m-%d') ret_bars.append(add_bar) except Exception as ex: self.writeError( 'error when convert bar:{},ex:{},t:{}'.format( bar, str(ex), traceback.format_exc())) return ret_bars if start_dt is not None and bar_datetime < start_dt: continue return ret_bars except Exception as ex: self.writeError('exception in get:{},{},{}'.format( binance_symbol, str(ex), traceback.format_exc())) return ret_bars
class BinanceData(object): # ---------------------------------------------------------------------- def __init__(self, strategy): """ 构造函数 :param strategy: 上层策略,主要用与使用strategy.writeCtaLog() """ self.strategy = strategy self.client = None self.init_client() def init_client(self): fileName = globalSetting.get('gateway_name', '') + '_connect.json' filePath = getJsonPath(fileName, __file__) try: with open(filePath, 'r') as f: # 解析json文件 setting = json.load(f) apiKey = setting.get('accessKey',None) secretKey = setting.get('secretKey',None) if apiKey is not None and secretKey is not None: self.client = Client(apiKey, secretKey) except IOError: self.strategy.writeCtaError(u'BINANCE读取连接配置{}出错,请检查'.format(filePath)) return def get_bars(self, symbol, period, callback, bar_is_completed=False,bar_freq=1, start_dt=None): """ 返回k线数据 symbol:合约 period: 周期: 1min,3min,5min,15min,30min,1day,3day,1hour,2hour,4hour,6hour,12hour """ if symbol not in SYMBOL_LIST: self.strategy.writeCtaError(u'{} 合约{}不在下载清单中'.format(datetime.now(), symbol)) return False if period not in PERIOD_MAPPING: self.strategy.writeCtaError(u'{} 周期{}不在下载清单中'.format(datetime.now(), period)) return False if self.client is None: return False binance_symbol = symbol.upper().replace('_' , '') binance_period = PERIOD_MAPPING.get(period) self.strategy.writeCtaLog('{}开始下载binance:{} {}数据.'.format(datetime.now(), binance_symbol, binance_period)) bars = [] try: bars = self.client.get_klines(symbol=binance_symbol, interval=binance_period) for i, bar in enumerate(bars): add_bar = CtaBarData() try: add_bar.vtSymbol = symbol add_bar.symbol = symbol add_bar.datetime = datetime.fromtimestamp(bar[0] / 1000) add_bar.date = add_bar.datetime.strftime('%Y-%m-%d') add_bar.time = add_bar.datetime.strftime('%H:%M:%S') add_bar.tradingDay = add_bar.date add_bar.open = float(bar[1]) add_bar.high = float(bar[2]) add_bar.low = float(bar[3]) add_bar.close = float(bar[4]) add_bar.volume = float(bar[5]) except Exception as ex: self.strategy.writeCtaError( 'error when convert bar:{},ex:{},t:{}'.format(bar, str(ex), traceback.format_exc())) return False if start_dt is not None and bar.datetime < start_dt: continue if callback is not None: callback(add_bar, bar_is_completed, bar_freq) return True except Exception as ex: self.strategy.writeCtaError('exception in get:{},{},{}'.format(binance_symbol,str(ex), traceback.format_exc())) return False
# # start aggregated trade websocket for BNBBTC def process_message(msg): print("message type: {}".format(msg['e'])) print(msg) # do something #from vnpy.api.binance.websockets import BinanceSocketManager #bm = BinanceSocketManager(client) #bm.start_aggtrade_socket('BNBBTC', process_message) #bm.start() # get historical kline data from any date range from datetime import datetime,timedelta # fetch 1 minute klines for the last day up until now start_time = datetime.now() start_time = start_time - timedelta(hours=1) t =int(start_time.timestamp()*1000) # startTime =t klines = client.get_klines(symbol="BTCUSDT",interval= Client.KLINE_INTERVAL_1MINUTE) for kl in klines: print(datetime.fromtimestamp(kl[0]/1e3)) # fetch 30 minute klines for the last month of 2017 # klines = client.get_historical_klines("ETHBTC", Client.KLINE_INTERVAL_30MINUTE, "1 Dec, 2017", "1 Jan, 2018") # fetch weekly klines since it listed # klines = client.get_historical_klines("NEOBTC", KLINE_INTERVAL_1WEEK, "1 Jan, 2017")
class BinanceData(object): # ---------------------------------------------------------------------- def __init__(self, strategy): """ 构造函数 :param strategy: 上层策略,主要用与使用strategy.writeCtaLog() """ self.strategy = strategy self.client = None self.init_client() def init_client(self): fileName = globalSetting.get('gateway_name', '') + '_connect.json' filePath = getJsonPath(fileName, __file__) try: with open(filePath, 'r') as f: # 解析json文件 setting = json.load(f) apiKey = setting.get('accessKey', None) secretKey = setting.get('secretKey', None) if apiKey is not None and secretKey is not None: self.client = Client(apiKey, secretKey) except IOError: self.strategy.writeCtaError( u'BINANCE读取连接配置{}出错,请检查'.format(filePath)) return def get_bars(self, symbol, period, callback, bar_is_completed=False, bar_freq=1, start_dt=None): """ 返回k线数据 symbol:合约 period: 周期: 1min,3min,5min,15min,30min,1day,3day,1hour,2hour,4hour,6hour,12hour """ if symbol not in SYMBOL_LIST: self.strategy.writeCtaError(u'{} 合约{}不在下载清单中'.format( datetime.now(), symbol)) return False if period not in PERIOD_MAPPING: self.strategy.writeCtaError(u'{} 周期{}不在下载清单中'.format( datetime.now(), period)) return False if self.client is None: return False binance_symbol = symbol.upper().replace('_', '') binance_period = PERIOD_MAPPING.get(period) self.strategy.writeCtaLog('{}开始下载binance:{} {}数据.'.format( datetime.now(), binance_symbol, binance_period)) bars = [] try: bars = self.client.get_klines(symbol=binance_symbol, interval=binance_period) for i, bar in enumerate(bars): add_bar = CtaBarData() try: add_bar.vtSymbol = symbol add_bar.symbol = symbol add_bar.datetime = datetime.fromtimestamp(bar[0] / 1000) add_bar.date = add_bar.datetime.strftime('%Y-%m-%d') add_bar.time = add_bar.datetime.strftime('%H:%M:%S') add_bar.tradingDay = add_bar.date add_bar.open = float(bar[1]) add_bar.high = float(bar[2]) add_bar.low = float(bar[3]) add_bar.close = float(bar[4]) add_bar.volume = float(bar[5]) except Exception as ex: self.strategy.writeCtaError( 'error when convert bar:{},ex:{},t:{}'.format( bar, str(ex), traceback.format_exc())) return False if start_dt is not None and bar.datetime < start_dt: continue if callback is not None: callback(add_bar, bar_is_completed, bar_freq) return True except Exception as ex: self.strategy.writeCtaError('exception in get:{},{},{}'.format( binance_symbol, str(ex), traceback.format_exc())) return False