diff --git a/vnpy/api/bitmex/vnbitmex.py b/vnpy/api/bitmex/vnbitmex.py index bddb6a73..ff83bf84 100644 --- a/vnpy/api/bitmex/vnbitmex.py +++ b/vnpy/api/bitmex/vnbitmex.py @@ -178,7 +178,7 @@ class BitmexWebsocketApi(object): else: self.host = WEBSOCKET_HOST - self.ws = websocket.create_connection(self.host, sslopt={'cert_reqs': ssl.CERT_NONE}) + self.connectWs() self.active = True self.thread = Thread(target=self.run) @@ -189,9 +189,13 @@ class BitmexWebsocketApi(object): #---------------------------------------------------------------------- def reconnect(self): """重连""" - self.ws = websocket.create_connection(self.host, sslopt={'cert_reqs': ssl.CERT_NONE}) - + self.connectWs() self.onConnect() + + #---------------------------------------------------------------------- + def connectWs(self): + """""" + self.ws = websocket.create_connection(self.host, sslopt={'cert_reqs': ssl.CERT_NONE}) #---------------------------------------------------------------------- def run(self): diff --git a/vnpy/trader/gateway/bitmexGateway/bitmexGateway.py b/vnpy/trader/gateway/bitmexGateway/bitmexGateway.py index 1e26e306..6aaa93c4 100644 --- a/vnpy/trader/gateway/bitmexGateway/bitmexGateway.py +++ b/vnpy/trader/gateway/bitmexGateway/bitmexGateway.py @@ -77,6 +77,7 @@ class BitmexGateway(VtGateway): apiSecret = str(setting['apiSecret']) sessionCount = int(setting['sessionCount']) symbols = setting['symbols'] + testnet = setting.get('testnet',False) except KeyError: log = VtLogData() log.gatewayName = self.gatewayName @@ -85,8 +86,8 @@ class BitmexGateway(VtGateway): return # 创建行情和交易接口对象 - self.restApi.connect(apiKey, apiSecret, sessionCount) - self.wsApi.connect(apiKey, apiSecret, symbols) + self.restApi.connect(apiKey, apiSecret, sessionCount, testnet) + self.wsApi.connect(apiKey, apiSecret, symbols, testnet) #---------------------------------------------------------------------- def subscribe(self, subscribeReq): @@ -167,9 +168,9 @@ class RestApi(BitmexRestApi): self.date = int(datetime.now().strftime('%y%m%d%H%M%S')) * self.orderId #---------------------------------------------------------------------- - def connect(self, apiKey, apiSecret, sessionCount): + def connect(self, apiKey, apiSecret, sessionCount, testnet): """连接服务器""" - self.init(apiKey, apiSecret) + self.init(apiKey, apiSecret, testnet) self.start(sessionCount) self.writeLog(u'REST API启动成功') @@ -262,7 +263,7 @@ class WebsocketApi(BitmexWebsocketApi): self.tradeSet = set() #---------------------------------------------------------------------- - def connect(self, apiKey, apiSecret, symbols): + def connect(self, apiKey, apiSecret, symbols, testnet): """""" self.apiKey = apiKey self.apiSecret = apiSecret @@ -275,7 +276,7 @@ class WebsocketApi(BitmexWebsocketApi): tick.vtSymbol = '.'.join([tick.symbol, tick.exchange]) self.tickDict[symbol] = tick - self.start() + self.start(testnet) #---------------------------------------------------------------------- def onConnect(self): @@ -286,7 +287,13 @@ class WebsocketApi(BitmexWebsocketApi): #---------------------------------------------------------------------- def onData(self, data): """数据回调""" - if 'request' in data: + if 'error' in data: + self.writeLog(u'Websocket API报错:%s' %data['error']) + + if 'not valid' in data['error']: + self.active = False + + elif 'request' in data: req = data['request'] success = data['success']