From 23a14a7a1976e02673414cc7c121f6b3e3e89cc8 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 14 Sep 2018 17:35:11 +0800 Subject: [PATCH] =?UTF-8?q?[Add]BitMEX=E6=8E=A5=E5=8F=A3=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=AF=B9testnet=E4=BB=BF=E7=9C=9F=E7=8E=AF=E5=A2=83=E7=9A=84?= =?UTF-8?q?=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 | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/vnpy/api/bitmex/vnbitmex.py b/vnpy/api/bitmex/vnbitmex.py index 962b0c33..bddb6a73 100644 --- a/vnpy/api/bitmex/vnbitmex.py +++ b/vnpy/api/bitmex/vnbitmex.py @@ -24,7 +24,8 @@ import websocket REST_HOST = 'https://www.bitmex.com/api/v1' WEBSOCKET_HOST = 'wss://www.bitmex.com/realtime' - +TESTNET_REST_HOST = 'https://testnet.bitmex.com/api/v1' +TESTNET_WEBSOCKET_HOST = 'wss://testnet.bitmex.com/realtime' ######################################################################## @@ -36,6 +37,7 @@ class BitmexRestApi(object): """Constructor""" self.apiKey = '' self.apiSecret = '' + self.host = '' self.active = False self.reqid = 0 @@ -49,11 +51,16 @@ class BitmexRestApi(object): } #---------------------------------------------------------------------- - def init(self, apiKey, apiSecret): + def init(self, apiKey, apiSecret, testnet=False): """初始化""" self.apiKey = apiKey self.apiSecret = apiSecret + if testnet: + self.host = TESTNET_REST_HOST + else: + self.host = REST_HOST + #---------------------------------------------------------------------- def start(self, n=3): """启动""" @@ -85,7 +92,7 @@ class BitmexRestApi(object): def processReq(self, req, i): """处理请求""" method, path, callback, params, postdict, reqid = req - url = REST_HOST + path + url = self.host + path expires = int(time() + 5) rq = requests.Request(url=url, data=postdict) @@ -161,12 +168,17 @@ class BitmexWebsocketApi(object): self.ws = None self.thread = None self.active = False + self.host = '' #---------------------------------------------------------------------- - def start(self): + def start(self, testnet=False): """启动""" - self.ws = websocket.create_connection(WEBSOCKET_HOST, - sslopt={'cert_reqs': ssl.CERT_NONE}) + if testnet: + self.host = TESTNET_WEBSOCKET_HOST + else: + self.host = WEBSOCKET_HOST + + self.ws = websocket.create_connection(self.host, sslopt={'cert_reqs': ssl.CERT_NONE}) self.active = True self.thread = Thread(target=self.run) @@ -177,8 +189,7 @@ class BitmexWebsocketApi(object): #---------------------------------------------------------------------- def reconnect(self): """重连""" - self.ws = websocket.create_connection(WEBSOCKET_HOST, - sslopt={'cert_reqs': ssl.CERT_NONE}) + self.ws = websocket.create_connection(self.host, sslopt={'cert_reqs': ssl.CERT_NONE}) self.onConnect()