From 6b564c9f01277b4ab5c2ff6d716b5e0d10424587 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Thu, 27 Dec 2018 13:05:24 +0800 Subject: [PATCH] =?UTF-8?q?[Mod]=E8=B0=83=E6=95=B4=E9=83=A8=E5=88=86?= =?UTF-8?q?=E7=81=AB=E5=B8=81=E6=8E=A5=E5=8F=A3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gateway/huobiGateway/huobiGateway.py | 65 ++++++++++++++----- 1 file changed, 48 insertions(+), 17 deletions(-) diff --git a/vnpy/trader/gateway/huobiGateway/huobiGateway.py b/vnpy/trader/gateway/huobiGateway/huobiGateway.py index 74187a1b..b1931e37 100644 --- a/vnpy/trader/gateway/huobiGateway/huobiGateway.py +++ b/vnpy/trader/gateway/huobiGateway/huobiGateway.py @@ -13,14 +13,15 @@ import json import re import urllib import zlib +from copy import copy from vnpy.api.rest import Request, RestClient from vnpy.api.websocket import WebsocketClient from vnpy.trader.vtGateway import * from vnpy.trader.vtFunction import getTempPath, getJsonPath -#REST_HOST = 'https://api.huobipro.com' -REST_HOST = 'https://api.huobi.pro/v1' +REST_HOST = 'https://api.huobipro.com' +#REST_HOST = 'https://api.huobi.pro/v1' WEBSOCKET_MARKET_HOST = 'wss://api.huobi.pro/ws' # Global站行情 WEBSOCKET_TRADE_HOST = 'wss://api.huobi.pro/ws/v1' # 资产和订单 @@ -181,7 +182,18 @@ class HuobiGateway(VtGateway): def setQryEnabled(self, qryEnabled): """设置是否要启动循环查询""" self.qryEnabled = qryEnabled - + + #---------------------------------------------------------------------- + def writeLog(self, msg): + """""" + log = VtLogData() + log.logContent = msg + log.gatewayName = self.gatewayName + + event = Event(EVENT_LOG) + event.dict_['data'] = log + self.eventEngine.put(event) + ######################################################################## @@ -238,8 +250,11 @@ class HuobiRestApi(RestClient): host, path = _split_url(REST_HOST) self.init(REST_HOST) + self.signHost = host self.start(sessionCount) + + self.queryContract() #---------------------------------------------------------------------- def queryAccount(self): @@ -318,15 +333,17 @@ class HuobiRestApi(RestClient): #---------------------------------------------------------------------- def onQueryAccount(self, data, request): # type: (dict, Request)->None """""" - for d in data: + for d in data['data']: if str(d['type']) == 'spot': self.accountid = str(d['id']) self.gateway.writeLog(u'交易账户%s查询成功' %self.accountid) + + self.queryAccountBalance() #---------------------------------------------------------------------- def onQueryAccountBalance(self, data, request): # type: (dict, Request)->None """""" - for d in data['list']: + for d in data['data']['list']: currency = d['currency'] account = self.accountDict.get(currency, None) @@ -346,11 +363,16 @@ class HuobiRestApi(RestClient): account.balance = account.margin + account.available for account in self.accountDict.values(): - self.gateway.onAccount(account) + self.gateway.onAccount(account) + + self.gateway.writeLog(u'账户资金信息查询成功') + self.queryOrder() #---------------------------------------------------------------------- def onQueryOrder(self, data, request): # type: (dict, Request)->None """""" + print(data) + data.reverse() for d in data: @@ -390,11 +412,13 @@ class HuobiRestApi(RestClient): self.orderDict[orderID] = order self.gateway.onOrder(order) + + self.gateway.writeLog(u'委托信息查询成功') #---------------------------------------------------------------------- def onQueryContract(self, data, request): # type: (dict, Request)->None """""" - for d in data: + for d in data['data']: contract = VtContractData() contract.gatewayName = self.gatewayName @@ -409,7 +433,7 @@ class HuobiRestApi(RestClient): self.gateway.onContract(contract) - self.gateway.writeLog(u'交易代码查询成功') + self.gateway.writeLog(u'合约信息查询成功') self.queryAccount() #---------------------------------------------------------------------- @@ -477,6 +501,11 @@ class HuobiWebsocketApiBase(WebsocketClient): """""" pass + #---------------------------------------------------------------------- + @staticmethod + def unpackData(data): + return json.loads(zlib.decompress(data, 31)) + #---------------------------------------------------------------------- def onPacket(self, packet): """""" @@ -485,10 +514,10 @@ class HuobiWebsocketApiBase(WebsocketClient): return if 'err-msg' in packet: - return self.onError(packet) + return self.onErrorMsg(packet) if "op" in packet and packet["op"] == "auth": - return self.onLogin(packet) + return self.onLogin() self.onData(packet) @@ -500,7 +529,7 @@ class HuobiWebsocketApiBase(WebsocketClient): #---------------------------------------------------------------------- def onErrorMsg(self, packet): # type: (dict)->None """""" - self.gateway.writeLog(packet['err-msg'])) + self.gateway.writeLog(packet['err-msg']) ######################################################################## @@ -557,6 +586,8 @@ class HuobiTradeWebsocketApi(HuobiWebsocketApiBase): #---------------------------------------------------------------------- def onLogin(self): """""" + self.gateway.writeLog(u'交易Websocket服务器登录成功') + self.subscribeTopic() #---------------------------------------------------------------------- @@ -772,12 +803,12 @@ class HuobiMarketWebsocketApi(HuobiWebsocketApiBase): #---------------------------------------------------------------------- def onData(self, packet): # type: (dict)->None """""" - if 'ch' in data: - if 'depth.step' in data['ch']: - self.onMarketDepth(data) - elif 'detail' in data['ch']: - self.onMarketDetail(data) - elif 'err-code' in data: + if 'ch' in packet: + if 'depth.step' in packet['ch']: + self.onMarketDepth(packet) + elif 'detail' in packet['ch']: + self.onMarketDetail(packet) + elif 'err-code' in packet: self.gateway.writeLog(u'错误代码:%s, 信息:%s' %(data['err-code'], data['err-msg'])) #----------------------------------------------------------------------