From 70ed2dfd66561f9ef1eae5d6415645ef0364e084 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 10 Jun 2018 11:53:09 +0800 Subject: [PATCH 1/9] =?UTF-8?q?[Fix]Close=20#906,Bitfinex=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=96=B0=E5=A2=9EonConnect=E5=9B=9E=E8=B0=83=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/bitfinex/vnbitfinex.py | 12 +++++++++++- .../gateway/bitfinexGateway/bitfinexGateway.py | 7 +++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/vnpy/api/bitfinex/vnbitfinex.py b/vnpy/api/bitfinex/vnbitfinex.py index 8c55b08a..4e1b10bb 100644 --- a/vnpy/api/bitfinex/vnbitfinex.py +++ b/vnpy/api/bitfinex/vnbitfinex.py @@ -38,11 +38,15 @@ class BitfinexApi(object): self.restThread = Thread(target=self.runRest) self.restThread.start() + + self.onConnect() #---------------------------------------------------------------------- def reconnect(self): """""" - self.ws = websocket.create_connection(WEBSOCKET_V2_URL) + self.ws = websocket.create_connection(WEBSOCKET_V2_URL) + + self.onConnect() #---------------------------------------------------------------------- def run(self): @@ -57,6 +61,12 @@ class BitfinexApi(object): self.onError(msg) self.reconnect() + #---------------------------------------------------------------------- + def onConnect(self): + """""" + print 'connected' + + #---------------------------------------------------------------------- def onData(self, data): """""" diff --git a/vnpy/trader/gateway/bitfinexGateway/bitfinexGateway.py b/vnpy/trader/gateway/bitfinexGateway/bitfinexGateway.py index 5aef41be..005ca85b 100644 --- a/vnpy/trader/gateway/bitfinexGateway/bitfinexGateway.py +++ b/vnpy/trader/gateway/bitfinexGateway/bitfinexGateway.py @@ -182,10 +182,13 @@ class GatewayApi(BitfinexApi): self.start() self.writeLog(u'交易API启动成功') - for symbol in symbols: + #---------------------------------------------------------------------- + def onConnect(self): + """""" + for symbol in self.symbols: self.subscribe(symbol, 'ticker') self.subscribe(symbol, 'book') - self.writeLog(u'行情推送订阅成功') + self.writeLog(u'行情推送订阅成功') self.authenticate() self.writeLog(u'交易推送订阅成功') From c42ecb6ec87af14fba2e632ee5f757980e9b8056 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 10 Jun 2018 14:59:03 +0800 Subject: [PATCH 2/9] =?UTF-8?q?[Fix]=E4=BF=AE=E5=A4=8D=E5=B8=81=E5=AE=89AP?= =?UTF-8?q?I=E5=85=B3=E9=97=AD=E6=97=B6=E7=9A=84=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=B1=A0=E5=85=B3=E9=97=AD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/binance/vnbinance.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/vnpy/api/binance/vnbinance.py b/vnpy/api/binance/vnbinance.py index 8e9ade96..9d9decfd 100644 --- a/vnpy/api/binance/vnbinance.py +++ b/vnpy/api/binance/vnbinance.py @@ -85,8 +85,10 @@ class BinanceApi(object): def close(self): """""" self.active = False - self.pool.close() - self.pool.join() + + if self.pool: + self.pool.close() + self.pool.join() #---------------------------------------------------------------------- def request(self, method, path, params=None, signed=False, stream=False): From bbabd742ff3add740dc329489b99c3dd09aeca5b Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 10 Jun 2018 14:59:33 +0800 Subject: [PATCH 3/9] =?UTF-8?q?[Add]BitfinexApi=E6=96=B0=E5=A2=9Eclose?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/bitfinex/vnbitfinex.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/vnpy/api/bitfinex/vnbitfinex.py b/vnpy/api/bitfinex/vnbitfinex.py index 4e1b10bb..bd5aa189 100644 --- a/vnpy/api/bitfinex/vnbitfinex.py +++ b/vnpy/api/bitfinex/vnbitfinex.py @@ -32,7 +32,7 @@ class BitfinexApi(object): """""" self.ws = websocket.create_connection(WEBSOCKET_V2_URL) - self.active =True + self.active = True self.thread = Thread(target=self.run) self.thread.start() @@ -61,11 +61,21 @@ class BitfinexApi(object): self.onError(msg) self.reconnect() + #---------------------------------------------------------------------- + def close(self): + """""" + self.active = False + + if self.thread: + self.thread.join() + + if self.restThread: + self.thread.join() + #---------------------------------------------------------------------- def onConnect(self): """""" print 'connected' - #---------------------------------------------------------------------- def onData(self, data): From 8cdfcef62ba5dc3b3c87277b16edee96f981f42d Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 10 Jun 2018 15:00:39 +0800 Subject: [PATCH 4/9] =?UTF-8?q?[Mod]=E7=81=AB=E5=B8=81=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=BB=A3=E7=90=86=E6=94=AF=E6=8C=81,?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=8A=A8=E8=AE=A2=E9=98=85=E8=A1=8C?= =?UTF-8?q?=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/CryptoTrader/VT_setting.json | 6 ++-- vnpy/api/huobi/vnhuobi.py | 21 ++--------- .../gateway/huobiGateway/huobiGateway.py | 35 ++++++++++--------- 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/examples/CryptoTrader/VT_setting.json b/examples/CryptoTrader/VT_setting.json index 473017fb..40495eca 100644 --- a/examples/CryptoTrader/VT_setting.json +++ b/examples/CryptoTrader/VT_setting.json @@ -9,12 +9,12 @@ "darkStyle": true, "language": "chinese", - "logActive": true, + "logActive": false, "logLevel": "debug", "logConsole": true, "logFile": true, - "tdPenalty": ["IF", "IH", "IC"], + "tdPenalty": [], - "maxDecimal": 4 + "maxDecimal": 10 } \ No newline at end of file diff --git a/vnpy/api/huobi/vnhuobi.py b/vnpy/api/huobi/vnhuobi.py index ca9db9a3..f6b37697 100644 --- a/vnpy/api/huobi/vnhuobi.py +++ b/vnpy/api/huobi/vnhuobi.py @@ -504,8 +504,6 @@ class DataApi(object): self.subDict = {} self.url = '' - self.proxyHost = '' - self.proxyPort = 0 #---------------------------------------------------------------------- def run(self): @@ -532,12 +530,7 @@ class DataApi(object): def reconnect(self): """重连""" try: - if not self.proxyHost: - self.ws = create_connection(self.url) - else: - self.ws = create_connection(self.url, - http_proxy_host=self.proxyHost, - http_proxy_port=self.proxyPort) + self.ws = create_connection(self.url) return True except: msg = traceback.format_exc() @@ -553,20 +546,12 @@ class DataApi(object): self.subTopic(topic) #---------------------------------------------------------------------- - def connect(self, url, proxyHost='', proxyPort=0): + def connect(self, url): """连接""" self.url = url - self.proxyHost = proxyHost - self.proxyPort = proxyPort try: - if not self.proxyHost: - self.ws = create_connection(self.url) - else: - self.ws = create_connection(self.url, - http_proxy_host=self.proxyHost, - http_proxy_port=self.proxyPort) - + self.ws = create_connection(self.url) self.active = True self.thread.start() diff --git a/vnpy/trader/gateway/huobiGateway/huobiGateway.py b/vnpy/trader/gateway/huobiGateway/huobiGateway.py index 2b84a7ee..7e50940c 100644 --- a/vnpy/trader/gateway/huobiGateway/huobiGateway.py +++ b/vnpy/trader/gateway/huobiGateway/huobiGateway.py @@ -75,8 +75,6 @@ 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 @@ -85,7 +83,7 @@ class HuobiGateway(VtGateway): return # 创建行情和交易接口对象 - self.dataApi.connect(exchange, proxyHost, proxyPort) + self.dataApi.connect(exchange, symbols) self.tradeApi.connect(exchange, accessKey, secretKey, symbols) # 初始化并启动查询 @@ -94,7 +92,8 @@ class HuobiGateway(VtGateway): #---------------------------------------------------------------------- def subscribe(self, subscribeReq): """订阅行情""" - self.dataApi.subscribe(subscribeReq) + pass + #self.dataApi.subscribe(subscribeReq) #---------------------------------------------------------------------- def sendOrder(self, orderReq): @@ -179,38 +178,40 @@ class HuobiDataApi(DataApi): self.tickDict = {} - self.subscribeDict = {} + #self.subscribeDict = {} #---------------------------------------------------------------------- - def connect(self, exchange, proxyHost, proxyPort): + def connect(self, exchange, symbols): """连接服务器""" if exchange == 'huobi': url = 'wss://api.huobi.pro/ws' else: url = 'wss://api.hadax.com/ws' + + self.symbols = symbols - if proxyHost: - self.connectionStatus = super(HuobiDataApi, self).connect(url, proxyHost, proxyPort) - else: - self.connectionStatus = super(HuobiDataApi, self).connect(url) + self.connectionStatus = super(HuobiDataApi, self).connect(url) self.gateway.mdConnected = True if self.connectionStatus: self.writeLog(u'行情服务器连接成功') - + + for symbol in self.symbols: + self.subscribe(symbol) # 订阅所有之前订阅过的行情 - for req in self.subscribeDict.values(): - self.subscribe(req) + #for req in self.subscribeDict.values(): + # self.subscribe(req) + #---------------------------------------------------------------------- - def subscribe(self, subscribeReq): + def subscribe(self, symbol): """订阅合约""" - self.subscribeDict[subscribeReq.symbol] = subscribeReq + #self.subscribeDict[subscribeReq.symbol] = subscribeReq if not self.connectionStatus: return - symbol = subscribeReq.symbol + #symbol = subscribeReq.symbol if symbol in self.tickDict: return @@ -354,7 +355,7 @@ class HuobiTradeApi(TradeApi): #self.activeOrderSet = set() # 活动委托集合 #---------------------------------------------------------------------- - def connect(self, exchange, accessKey, secretKey, symbols=''): + def connect(self, exchange, symbols, accessKey, secretKey): """初始化连接""" if not self.connectionStatus: self.symbols = symbols From a80f44100f197a2904ec0c1b6e05a62768f47cb9 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 10 Jun 2018 16:03:31 +0800 Subject: [PATCH 5/9] =?UTF-8?q?[Mod]HuobiGateway=E5=8F=AA=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=8E=B0=E8=B4=A7=E4=BA=A4=E6=98=93=E8=B4=A6=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/gateway/huobiGateway/huobiGateway.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/vnpy/trader/gateway/huobiGateway/huobiGateway.py b/vnpy/trader/gateway/huobiGateway/huobiGateway.py index 7e50940c..64041e7d 100644 --- a/vnpy/trader/gateway/huobiGateway/huobiGateway.py +++ b/vnpy/trader/gateway/huobiGateway/huobiGateway.py @@ -84,7 +84,7 @@ class HuobiGateway(VtGateway): # 创建行情和交易接口对象 self.dataApi.connect(exchange, symbols) - self.tradeApi.connect(exchange, accessKey, secretKey, symbols) + self.tradeApi.connect(exchange, symbols, accessKey, secretKey) # 初始化并启动查询 self.initQuery() @@ -495,8 +495,9 @@ class HuobiTradeApi(TradeApi): def onGetAccounts(self, data, reqid): """查询账户回调""" for d in data: - self.accountid = str(d['id']) - self.writeLog(u'交易账户%s查询成功' %self.accountid) + if str(d['type']) == 'spot': + self.accountid = str(d['id']) + self.writeLog(u'交易账户%s查询成功' %self.accountid) #---------------------------------------------------------------------- def onGetAccountBalance(self, data, reqid): From 8ba6206be07dd068f3648d99c4cf073239051d0d Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 10 Jun 2018 16:06:12 +0800 Subject: [PATCH 6/9] =?UTF-8?q?[Mod]=E7=81=AB=E5=B8=81=E5=80=9F=E5=8F=A3?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E8=AF=B7=E6=B1=82=E8=B6=85=E6=97=B6429?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/gateway/huobiGateway/huobiGateway.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vnpy/trader/gateway/huobiGateway/huobiGateway.py b/vnpy/trader/gateway/huobiGateway/huobiGateway.py index 64041e7d..d4b72e90 100644 --- a/vnpy/trader/gateway/huobiGateway/huobiGateway.py +++ b/vnpy/trader/gateway/huobiGateway/huobiGateway.py @@ -452,6 +452,10 @@ class HuobiTradeApi(TradeApi): #---------------------------------------------------------------------- def onError(self, msg, reqid): """错误回调""" + # 忽略请求超时错误 + if '429' in msg: + return + err = VtErrorData() err.gatewayName = self.gatewayName err.errorID = 'Trade' From 603d9e855f3613654502ad137226a69c53103542 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 10 Jun 2018 16:08:57 +0800 Subject: [PATCH 7/9] =?UTF-8?q?[Mod]vnhuobi=E7=9A=84=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E8=B6=85=E6=97=B6=E6=94=B9=E4=B8=BA10?= =?UTF-8?q?=E7=A7=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/huobi/vnhuobi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vnpy/api/huobi/vnhuobi.py b/vnpy/api/huobi/vnhuobi.py index f6b37697..bb82d80f 100644 --- a/vnpy/api/huobi/vnhuobi.py +++ b/vnpy/api/huobi/vnhuobi.py @@ -20,7 +20,7 @@ from websocket import create_connection, _exceptions # 常量定义 -TIMEOUT = 5 +TIMEOUT = 10 HUOBI_API_HOST = "api.huobi.pro" HADAX_API_HOST = "api.hadax.com" LANG = 'zh-CN' From 50609547e8fedfc1638476206d1a211b84226599 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 10 Jun 2018 16:09:25 +0800 Subject: [PATCH 8/9] =?UTF-8?q?[Mod]HuobiGateway=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E7=94=B1=E4=BA=8E=E8=AF=B7=E6=B1=82=E8=B6=85=E6=97=B6=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84API=E7=AD=BE=E5=90=8D=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/gateway/huobiGateway/huobiGateway.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vnpy/trader/gateway/huobiGateway/huobiGateway.py b/vnpy/trader/gateway/huobiGateway/huobiGateway.py index d4b72e90..c1c587c9 100644 --- a/vnpy/trader/gateway/huobiGateway/huobiGateway.py +++ b/vnpy/trader/gateway/huobiGateway/huobiGateway.py @@ -453,7 +453,7 @@ class HuobiTradeApi(TradeApi): def onError(self, msg, reqid): """错误回调""" # 忽略请求超时错误 - if '429' in msg: + if '429' in msg or 'api-signature-not-valid' in msg: return err = VtErrorData() From ff8a6c96028061a51cf92dc32ad60a9e145ab201 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Sun, 10 Jun 2018 16:19:36 +0800 Subject: [PATCH 9/9] =?UTF-8?q?[Mod]=E5=8D=87=E7=BA=A7=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E8=B4=A7=E5=B8=81=E6=8E=A5=E5=8F=A3=E4=BD=BF=E7=94=A8=E7=9A=84?= =?UTF-8?q?websocket-client=E5=88=B0=E6=9C=80=E6=96=B0=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/api/binance/vnbinance.py | 7 +++++-- vnpy/api/bitfinex/vnbitfinex.py | 7 +++++-- vnpy/api/huobi/vnhuobi.py | 2 +- vnpy/api/okex/vnokex.py | 4 +++- vnpy/trader/app/riskManager/RM_setting.json | 4 ++-- vnpy/trader/gateway/binanceGateway/binanceGateway.py | 3 --- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/vnpy/api/binance/vnbinance.py b/vnpy/api/binance/vnbinance.py index 9d9decfd..da67de8e 100644 --- a/vnpy/api/binance/vnbinance.py +++ b/vnpy/api/binance/vnbinance.py @@ -5,6 +5,7 @@ import requests import hmac import hashlib import traceback +import ssl from queue import Queue, Empty from threading import Thread @@ -535,7 +536,8 @@ class BinanceApi(object): def connectDataStream(self): """""" try: - self.dataStreamWs = create_connection(self.dataStreamUrl) + self.dataStreamWs = create_connection(self.dataStreamUrl, + sslopt={'cert_reqs': ssl.CERT_NONE}) return True except: msg = traceback.format_exc() @@ -595,7 +597,8 @@ class BinanceApi(object): def connectUserStream(self): """""" try: - self.userStreamWs = create_connection(self.userStreamUrl) + self.userStreamWs = create_connection(self.userStreamUrl, + sslopt={'cert_reqs': ssl.CERT_NONE}) return True except: msg = traceback.format_exc() diff --git a/vnpy/api/bitfinex/vnbitfinex.py b/vnpy/api/bitfinex/vnbitfinex.py index bd5aa189..ee308d68 100644 --- a/vnpy/api/bitfinex/vnbitfinex.py +++ b/vnpy/api/bitfinex/vnbitfinex.py @@ -3,6 +3,7 @@ import json import requests import traceback +import ssl from threading import Thread from queue import Queue, Empty @@ -30,7 +31,8 @@ class BitfinexApi(object): #---------------------------------------------------------------------- def start(self): """""" - self.ws = websocket.create_connection(WEBSOCKET_V2_URL) + self.ws = websocket.create_connection(WEBSOCKET_V2_URL, + sslopt={'cert_reqs': ssl.CERT_NONE}) self.active = True self.thread = Thread(target=self.run) @@ -44,7 +46,8 @@ class BitfinexApi(object): #---------------------------------------------------------------------- def reconnect(self): """""" - self.ws = websocket.create_connection(WEBSOCKET_V2_URL) + self.ws = websocket.create_connection(WEBSOCKET_V2_URL, + sslopt={'cert_reqs': ssl.CERT_NONE}) self.onConnect() diff --git a/vnpy/api/huobi/vnhuobi.py b/vnpy/api/huobi/vnhuobi.py index bb82d80f..8877a371 100644 --- a/vnpy/api/huobi/vnhuobi.py +++ b/vnpy/api/huobi/vnhuobi.py @@ -551,7 +551,7 @@ class DataApi(object): self.url = url try: - self.ws = create_connection(self.url) + self.ws = create_connection(self.url, sslopt={'cert_reqs': ssl.CERT_NONE}) self.active = True self.thread.start() diff --git a/vnpy/api/okex/vnokex.py b/vnpy/api/okex/vnokex.py index 0c17d3eb..9a21ef4b 100644 --- a/vnpy/api/okex/vnokex.py +++ b/vnpy/api/okex/vnokex.py @@ -2,6 +2,7 @@ from __future__ import print_function +import ssl import hashlib import json import traceback @@ -136,7 +137,8 @@ class OkexApi(object): on_close=self.onCloseCallback, on_open=self.onOpenCallback) - self.wsThread = Thread(target=self.ws.run_forever) + kwargs = {'sslopt': {'cert_reqs': ssl.CERT_NONE}} + self.wsThread = Thread(target=self.ws.run_forever, kwargs=kwargs) self.wsThread.start() #---------------------------------------------------------------------- diff --git a/vnpy/trader/app/riskManager/RM_setting.json b/vnpy/trader/app/riskManager/RM_setting.json index 20625b38..6dc35464 100644 --- a/vnpy/trader/app/riskManager/RM_setting.json +++ b/vnpy/trader/app/riskManager/RM_setting.json @@ -1,10 +1,10 @@ { "orderFlowClear": 1, "orderCancelLimit": 10, + "marginRatioLimit": 0.95, "workingOrderLimit": 20, "tradeLimit": 1000, "orderSizeLimit": 100, "active": true, - "orderFlowLimit": 50, - "marginRatioLimit": 0.95 + "orderFlowLimit": 50 } \ No newline at end of file diff --git a/vnpy/trader/gateway/binanceGateway/binanceGateway.py b/vnpy/trader/gateway/binanceGateway/binanceGateway.py index 897a7877..229c2494 100644 --- a/vnpy/trader/gateway/binanceGateway/binanceGateway.py +++ b/vnpy/trader/gateway/binanceGateway/binanceGateway.py @@ -223,9 +223,6 @@ class GatewayApi(BinanceApi): def onQueryExchangeInfo(self, data, reqid): """""" for d in data['symbols']: - if str(d['symbol']) == 'ETHUSDT': - print d - contract = VtContractData() contract.gatewayName = self.gatewayName