From ed3ffd9ac233f2ba9f6c24802252ffca5e84c895 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 16 Sep 2018 14:19:40 +0800 Subject: [PATCH] =?UTF-8?q?[Add]bitmexGateway=E5=A2=9E=E5=8A=A0=E4=BB=BF?= =?UTF-8?q?=E7=9C=9F=E7=8E=AF=E5=A2=83testnet=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/bitmex/vnbitmex.py | 10 ++++++--- .../gateway/bitmexGateway/bitmexGateway.py | 21 ++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) 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']