From d0553088b137d97dd3271efd39e6a393ccb76886 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Thu, 18 Oct 2018 11:27:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=94=AF=E6=8C=81=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/okex/Client.py | 34 ++++++++++++++-- vnpy/api/okex/OkcoinSpotAPI.py | 1 + vnpy/api/okex/vnokex.py | 40 +++++++++++++++++-- .../trader/gateway/okexGateway/okexGateway.py | 24 +++++++++-- 4 files changed, 88 insertions(+), 11 deletions(-) diff --git a/vnpy/api/okex/Client.py b/vnpy/api/okex/Client.py index c5a10e21..ce38e02b 100644 --- a/vnpy/api/okex/Client.py +++ b/vnpy/api/okex/Client.py @@ -5,7 +5,7 @@ from OkcoinSpotAPI import OKCoinSpot from OkcoinFutureAPI import OKCoinFuture - +from datetime import datetime #初始化apikey,secretkey,url apikey = 'XXXX' secretkey = 'XXXXX' @@ -23,8 +23,36 @@ okcoinFuture = OKCoinFuture(okcoinRESTURL,apikey,secretkey) #print (u' 现货深度 ') #print (okcoinSpot.depth('btc_usdt')) -print (u' K线数据 ') -print (okcoinSpot.kline(symbol='btc_usdt',type_='1day')) + +print('显示最后5条日线数据:') +klines = okcoinSpot.kline(symbol='eth_usdt', type_='1day') +for kl in klines[-5:]: + print('utc:{}, bjt:{} {}'.format(datetime.utcfromtimestamp(kl[0]/1e3),datetime.fromtimestamp(kl[0]/1e3),kl)) + +print('显示最后5条12小时线数据:') +klines = okcoinSpot.kline(symbol='eth_usdt', type_='12hour') +for kl in klines[-5:]: + print('utc:{}, bjt:{} {}'.format(datetime.utcfromtimestamp(kl[0]/1e3),datetime.fromtimestamp(kl[0]/1e3),kl)) + +print('显示最后5条6小时线数据:') +klines = okcoinSpot.kline(symbol='eth_usdt', type_='6hour') +for kl in klines[-5:]: + print('utc:{}, org:{} {}'.format(datetime.utcfromtimestamp(kl[0]/1e3),datetime.fromtimestamp(kl[0]/1e3),kl)) + +print('显示最后5条1小时线数据:') +klines = okcoinSpot.kline(symbol='eth_usdt', type_='1hour') +for kl in klines[-5:]: + print('utc:{}, bjt:{} {}'.format(datetime.utcfromtimestamp(kl[0]/1e3),datetime.fromtimestamp(kl[0]/1e3),kl)) + +print('显示最后5条30分钟线数据:') +klines = okcoinSpot.kline(symbol='eth_usdt', type_='30min') +for kl in klines[-5:]: + print('utc:{}, bjt:{} {}'.format(datetime.utcfromtimestamp(kl[0] / 1e3), datetime.fromtimestamp(kl[0] / 1e3), kl)) + +print('显示最后5条5分钟线数据:') +klines = okcoinSpot.kline(symbol='eth_usdt', type_='5min') +for kl in klines[-5:]: + print('utc:{}, bjt:{} {}'.format(datetime.utcfromtimestamp(kl[0] / 1e3), datetime.fromtimestamp(kl[0] / 1e3), kl)) #print (u' 现货历史交易信息 ') diff --git a/vnpy/api/okex/OkcoinSpotAPI.py b/vnpy/api/okex/OkcoinSpotAPI.py index 5a3f7146..16e91481 100644 --- a/vnpy/api/okex/OkcoinSpotAPI.py +++ b/vnpy/api/okex/OkcoinSpotAPI.py @@ -101,6 +101,7 @@ class OKCoinSpot: return httpPost(self.__url,CANCEL_ORDER_RESOURCE,params) #现货订单信息查询 + # orderId=-1,是查询未完成订单,否则查询相应订单号得订单 def orderinfo(self,symbol,orderId): ORDER_INFO_RESOURCE = "/api/v1/order_info.do" params = { diff --git a/vnpy/api/okex/vnokex.py b/vnpy/api/okex/vnokex.py index ed97910c..5fe39fcf 100644 --- a/vnpy/api/okex/vnokex.py +++ b/vnpy/api/okex/vnokex.py @@ -15,8 +15,8 @@ import sys from vnpy.api.okex.okexData import SPOT_TRADE_SIZE_DICT,SPOT_REST_ERROR_DICT, FUTURES_ERROR_DICT # OKEX网站 -OKEX_USD_SPOT = 'wss://real.okex.com:10441/websocket' # OKEX (币币交易)现货地址 -OKEX_USD_CONTRACT = 'wss://real.okex.com:10440/websocket/okexapi' # OKEX 期货地址 +OKEX_USD_SPOT = 'wss://real.okex.com:10441/websocket?compress=true' # OKEX (币币交易)现货地址 +OKEX_USD_CONTRACT = 'wss://real.okex.com:10440/websocket/okexapi?compress=true' # OKEX 期货地址 OKEX_CONTRACT_HOST = 'https://www.okex.com/api/v1/future_hold_amount.do?symbol=%s_usd&contract_type=%s' # 合约持仓查询地址 @@ -123,6 +123,8 @@ class OkexApi(object): :param evt: :return: """ + if isinstance(evt,bytes): + evt = evt.decode('utf-8') data = json.loads(evt) return data @@ -168,7 +170,7 @@ class OkexApi(object): """ print(u'vnokex.onClose') - #---------------------------------------------------------------------- + # ---------------------------------------------------------------------- def onOpen(self, ws): """ 接口打开事件 @@ -176,7 +178,22 @@ class OkexApi(object): :return: """ print(u'vnokex.onOpen') - + + # ---------------------------------------------------------------------- + def inflate(self,data): + """解压数据流""" + decompress = zlib.decompressobj( + -zlib.MAX_WBITS # see above + ) + inflated = decompress.decompress(data) + inflated += decompress.flush() + + # bytes=>string + if isinstance(inflated,bytes): + inflated = inflated.decode('utf-8') + + return inflated + #---------------------------------------------------------------------- def generateSign(self, params): """生成签名""" @@ -493,6 +510,21 @@ class WsFuturesApi(object): self.ws.close() self.thread.join() + # ---------------------------------------------------------------------- + def inflate(self, data): + """解压数据流""" + decompress = zlib.decompressobj( + -zlib.MAX_WBITS # see above + ) + inflated = decompress.decompress(data) + inflated += decompress.flush() + + # bytes=>string + if isinstance(inflated, bytes): + inflated = inflated.decode('utf-8') + + return inflated + # ---------------------------------------------------------------------- def readData(self, evt): """解压缩推送收到的数据""" diff --git a/vnpy/trader/gateway/okexGateway/okexGateway.py b/vnpy/trader/gateway/okexGateway/okexGateway.py index c158e383..309cb6fd 100644 --- a/vnpy/trader/gateway/okexGateway/okexGateway.py +++ b/vnpy/trader/gateway/okexGateway/okexGateway.py @@ -16,6 +16,7 @@ from queue import Queue from threading import Thread from time import sleep import traceback +import zlib # 新增解压功能 from vnpy.api.okex import WsSpotApi, WsFuturesApi, SPOT_SYMBOL_PAIRS, CONTRACT_SYMBOL, CONTRACT_TYPE, SPOT_CURRENCY from vnpy.api.okex.okexData import SPOT_TRADE_SIZE_DICT, SPOT_REST_ERROR_DICT, SPORT_WS_ERROR_DICT, FUTURES_ERROR_DICT @@ -437,7 +438,8 @@ class OkexSpotApi(WsSpotApi): :return: """ # str => json - ws_data = self.readData(evt) + decmp_evt = self.inflate(evt) + ws_data = self.readData(decmp_evt) if self.gateway.log_message: self.gateway.writeLog(u'SpotApi.onMessage:{}'.format(ws_data)) @@ -498,7 +500,8 @@ class OkexSpotApi(WsSpotApi): error = VtErrorData() error.gatewayName = self.gatewayName error.errorID = 0 - error.errorMsg = str(evt) + decom_evt = self.inflate(evt) + error.errorMsg = str(decom_evt) self.gateway.onError(error) # ---------------------------------------------------------------------- @@ -921,7 +924,16 @@ class OkexSpotApi(WsSpotApi): tick.date, tick.time,tick.datetime = self.generateDateTime(data['timestamp']) # print "Depth", tick.date, tick.time tick.tradingDay = tick.date + # 推送tick事件 + if tick.lastPrice == 0 and tick.askPrice1 != 0 and tick.bidPrice1 != 0: + tick.lastPrice = (tick.askPrice1 + tick.bidPrice1) / 2 + + if tick.lastPrice == 0 or tick.askPrice1 == 0 or tick.bidPrice1 == 0: + print('onDepth drop tick {},lastprice:{},askprice1={},bidPrice1:{}' + .format(tick.vtSymbol, tick.lastPrice, tick.askPrice1, tick.bidPrice1)) + return + newtick = copy(tick) self.gateway.onTick(newtick) @@ -1558,7 +1570,9 @@ class OkexFuturesApi(WsFuturesApi): :return: """ # str => json - ws_data = self.readData(evt) + decmp_evt = self.inflate(evt) + ws_data = self.readData(decmp_evt) + #ws_data = self.readData(evt) if self.gateway.log_message: self.gateway.writeLog(u'FutureApi.onMessage:{}'.format(ws_data)) @@ -1626,7 +1640,9 @@ class OkexFuturesApi(WsFuturesApi): """重载WsFutureApi.onError错误Event推送""" error = VtErrorData() error.gatewayName = self.gatewayName - error.errorMsg = str(evt) + #error.errorMsg = str(evt) + decom_evt = self.inflate(evt) + error.errorMsg = str(decom_evt) self.gateway.onError(error) # #----------------------------------------------------------------------