[Add]BitMEX接口增加对testnet仿真环境的支持

This commit is contained in:
vn.py 2018-09-14 17:35:11 +08:00
parent 4bf6f333fa
commit 23a14a7a19

View File

@ -24,7 +24,8 @@ import websocket
REST_HOST = 'https://www.bitmex.com/api/v1' REST_HOST = 'https://www.bitmex.com/api/v1'
WEBSOCKET_HOST = 'wss://www.bitmex.com/realtime' 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""" """Constructor"""
self.apiKey = '' self.apiKey = ''
self.apiSecret = '' self.apiSecret = ''
self.host = ''
self.active = False self.active = False
self.reqid = 0 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.apiKey = apiKey
self.apiSecret = apiSecret self.apiSecret = apiSecret
if testnet:
self.host = TESTNET_REST_HOST
else:
self.host = REST_HOST
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def start(self, n=3): def start(self, n=3):
"""启动""" """启动"""
@ -85,7 +92,7 @@ class BitmexRestApi(object):
def processReq(self, req, i): def processReq(self, req, i):
"""处理请求""" """处理请求"""
method, path, callback, params, postdict, reqid = req method, path, callback, params, postdict, reqid = req
url = REST_HOST + path url = self.host + path
expires = int(time() + 5) expires = int(time() + 5)
rq = requests.Request(url=url, data=postdict) rq = requests.Request(url=url, data=postdict)
@ -161,12 +168,17 @@ class BitmexWebsocketApi(object):
self.ws = None self.ws = None
self.thread = None self.thread = None
self.active = False self.active = False
self.host = ''
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def start(self): def start(self, testnet=False):
"""启动""" """启动"""
self.ws = websocket.create_connection(WEBSOCKET_HOST, if testnet:
sslopt={'cert_reqs': ssl.CERT_NONE}) 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.active = True
self.thread = Thread(target=self.run) self.thread = Thread(target=self.run)
@ -177,8 +189,7 @@ class BitmexWebsocketApi(object):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def reconnect(self): def reconnect(self):
"""重连""" """重连"""
self.ws = websocket.create_connection(WEBSOCKET_HOST, self.ws = websocket.create_connection(self.host, sslopt={'cert_reqs': ssl.CERT_NONE})
sslopt={'cert_reqs': ssl.CERT_NONE})
self.onConnect() self.onConnect()