Merge branch 'dev' of https://github.com/vnpy/vnpy into dev

This commit is contained in:
vn.py 2018-06-11 13:06:46 +08:00
commit 6ae70b391d
9 changed files with 78 additions and 57 deletions

View File

@ -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
}

View File

@ -5,6 +5,7 @@ import requests
import hmac
import hashlib
import traceback
import ssl
from queue import Queue, Empty
from threading import Thread
@ -85,8 +86,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):
@ -533,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()
@ -593,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()

View File

@ -3,6 +3,7 @@
import json
import requests
import traceback
import ssl
from threading import Thread
from queue import Queue, Empty
@ -30,19 +31,25 @@ 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.active = True
self.thread = Thread(target=self.run)
self.thread.start()
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,
sslopt={'cert_reqs': ssl.CERT_NONE})
self.onConnect()
#----------------------------------------------------------------------
def run(self):
@ -57,6 +64,22 @@ 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):
""""""

View File

@ -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'
@ -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, sslopt={'cert_reqs': ssl.CERT_NONE})
self.active = True
self.thread.start()

View File

@ -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()
#----------------------------------------------------------------------

View File

@ -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
}

View File

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

View File

@ -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'交易推送订阅成功')

View File

@ -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,8 +83,8 @@ class HuobiGateway(VtGateway):
return
# 创建行情和交易接口对象
self.dataApi.connect(exchange, proxyHost, proxyPort)
self.tradeApi.connect(exchange, accessKey, secretKey, symbols)
self.dataApi.connect(exchange, symbols)
self.tradeApi.connect(exchange, symbols, accessKey, secretKey)
# 初始化并启动查询
self.initQuery()
@ -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
@ -451,6 +452,10 @@ class HuobiTradeApi(TradeApi):
#----------------------------------------------------------------------
def onError(self, msg, reqid):
"""错误回调"""
# 忽略请求超时错误
if '429' in msg or 'api-signature-not-valid' in msg:
return
err = VtErrorData()
err.gatewayName = self.gatewayName
err.errorID = 'Trade'
@ -494,8 +499,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):