[Add]bitmexGateway增加仿真环境testnet的支持

This commit is contained in:
vn.py 2018-09-16 14:19:40 +08:00
parent 0a2d54e826
commit ed3ffd9ac2
2 changed files with 21 additions and 10 deletions

View File

@ -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):

View File

@ -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']