From 99e6c4d6fc76bd3bb5934841e92c94f5f6f7f918 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 26 Mar 2018 11:23:53 +0800 Subject: [PATCH] =?UTF-8?q?[Add]=E5=A2=9E=E5=8A=A0=E7=81=AB=E5=B8=81?= =?UTF-8?q?=E4=BA=A4=E6=98=93=E6=8E=A5=E5=8F=A3=E7=9A=84=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/huobi/vnhuobi.py | 13 ++++++++++--- .../gateway/huobiGateway/HUOBI_connect.json | 4 +++- vnpy/trader/gateway/huobiGateway/huobiGateway.py | 16 ++++++++++++---- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/vnpy/api/huobi/vnhuobi.py b/vnpy/api/huobi/vnhuobi.py index bc7a685f..24e1e544 100644 --- a/vnpy/api/huobi/vnhuobi.py +++ b/vnpy/api/huobi/vnhuobi.py @@ -93,6 +93,8 @@ class TradeApi(object): if mode: self.mode = mode + self.proxies = {} + return True #---------------------------------------------------------------------- @@ -250,7 +252,7 @@ class TradeApi(object): path = '/v1/common/timestamp' params = {} func = self.apiGet - callback = self.onGetCurrencys + callback = self.onGetTimestamp return self.addReq(path, params, func, callback) @@ -512,12 +514,17 @@ class DataApi(object): break #---------------------------------------------------------------------- - def connect(self, url): + def connect(self, url, proxyHost='', proxyPort=0): """连接""" self.url = url try: - self.ws = create_connection(self.url) + if not proxyHost: + self.ws = create_connection(self.url) + else: + self.ws = create_connection(self.url, + http_proxy_host=proxyHost, + http_proxy_port=proxyPort) self.active = True self.thread.start() diff --git a/vnpy/trader/gateway/huobiGateway/HUOBI_connect.json b/vnpy/trader/gateway/huobiGateway/HUOBI_connect.json index 150620c4..8b970997 100644 --- a/vnpy/trader/gateway/huobiGateway/HUOBI_connect.json +++ b/vnpy/trader/gateway/huobiGateway/HUOBI_connect.json @@ -2,5 +2,7 @@ "exchange": "hadax", "accessKey": "请在火币申请", "secretKey": "请在火币申请", - "symbols": ["aaceth"] + "symbols": ["aaceth"], + "proxyHost": "127.0.0.1", + "proxyPort": 1080 } \ No newline at end of file diff --git a/vnpy/trader/gateway/huobiGateway/huobiGateway.py b/vnpy/trader/gateway/huobiGateway/huobiGateway.py index a1f4aef4..72f9d755 100644 --- a/vnpy/trader/gateway/huobiGateway/huobiGateway.py +++ b/vnpy/trader/gateway/huobiGateway/huobiGateway.py @@ -75,6 +75,8 @@ class HuobiGateway(VtGateway): accessKey = str(setting['accessKey']) secretKey = str(setting['secretKey']) symbols = setting['symbols'] + proxyHost = str(setting['proxyHost']) + proxyPort = int(setting['proxyPort']) except KeyError: log = VtLogData() log.gatewayName = self.gatewayName @@ -83,7 +85,7 @@ class HuobiGateway(VtGateway): return # 创建行情和交易接口对象 - self.dataApi.connect(exchange) + self.dataApi.connect(exchange, proxyHost, proxyPort) self.tradeApi.connect(exchange, accessKey, secretKey, symbols) # 初始化并启动查询 @@ -180,14 +182,17 @@ class HuobiDataApi(DataApi): self.subscribeDict = {} #---------------------------------------------------------------------- - def connect(self, exchange): + def connect(self, exchange, proxyHost, proxyPort): """连接服务器""" if exchange == 'huobi': url = 'wss://api.huobi.pro/ws' else: url = 'wss://api.hadax.com/ws' - self.connectionStatus = super(HuobiDataApi, self).connect(url) + if proxyHost: + self.connectionStatus = super(HuobiDataApi, self).connect(url, proxyHost, proxyPort) + else: + self.connectionStatus = super(HuobiDataApi, self).connect(url) self.gateway.mdConnected = True if self.connectionStatus: @@ -360,6 +365,7 @@ class HuobiTradeApi(TradeApi): self.start() self.writeLog(u'交易服务器连接成功') + self.getTimestamp() self.getSymbols() #---------------------------------------------------------------------- @@ -473,7 +479,9 @@ class HuobiTradeApi(TradeApi): #---------------------------------------------------------------------- def onGetTimestamp(self, data, reqid): """查询时间回调""" - print reqid, data + event = Event(EVENT_LOG+'Time') + event.dict_['data'] = datetime.fromtimestamp(data/1000) + self.gateway.eventEngine.put(event) #---------------------------------------------------------------------- def onGetAccounts(self, data, reqid):