Merge pull request #216 from tesla1060/master

增加CTP接口的认证功能,支持部分需要程序化备案的期货公司。

非常感谢!
This commit is contained in:
vn.py 2017-02-20 09:25:03 +08:00 committed by GitHub
commit 926d093924

View File

@ -89,6 +89,8 @@ class CtpGateway(VtGateway):
self.tdConnected = False # 交易API连接状态 self.tdConnected = False # 交易API连接状态
self.qryEnabled = False # 是否要启动循环查询 self.qryEnabled = False # 是否要启动循环查询
self.requireAuthentication = False
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def connect(self): def connect(self):
@ -115,6 +117,14 @@ class CtpGateway(VtGateway):
brokerID = str(setting['brokerID']) brokerID = str(setting['brokerID'])
tdAddress = str(setting['tdAddress']) tdAddress = str(setting['tdAddress'])
mdAddress = str(setting['mdAddress']) mdAddress = str(setting['mdAddress'])
if 'authCode' in setting: #如果json文件提供了验证码
authCode = str(setting['authCode'])
userProductInfo = str(setting['userProductInfo'])
self.tdApi.requireAuthentication = True
else:
authCode = None
userProductInfo = None
except KeyError: except KeyError:
log = VtLogData() log = VtLogData()
log.gatewayName = self.gatewayName log.gatewayName = self.gatewayName
@ -124,7 +134,7 @@ class CtpGateway(VtGateway):
# 创建行情和交易接口对象 # 创建行情和交易接口对象
self.mdApi.connect(userID, password, brokerID, mdAddress) self.mdApi.connect(userID, password, brokerID, mdAddress)
self.tdApi.connect(userID, password, brokerID, tdAddress) self.tdApi.connect(userID, password, brokerID, tdAddress,authCode, userProductInfo)
# 初始化并启动查询 # 初始化并启动查询
self.initQuery() self.initQuery()
@ -446,6 +456,7 @@ class CtpTdApi(TdApi):
self.connectionStatus = False # 连接状态 self.connectionStatus = False # 连接状态
self.loginStatus = False # 登录状态 self.loginStatus = False # 登录状态
self.authStatus = False
self.userID = EMPTY_STRING # 账号 self.userID = EMPTY_STRING # 账号
self.password = EMPTY_STRING # 密码 self.password = EMPTY_STRING # 密码
@ -458,6 +469,8 @@ class CtpTdApi(TdApi):
self.posBufferDict = {} # 缓存持仓数据的字典 self.posBufferDict = {} # 缓存持仓数据的字典
self.symbolExchangeDict = {} # 保存合约代码和交易所的印射关系 self.symbolExchangeDict = {} # 保存合约代码和交易所的印射关系
self.symbolSizeDict = {} # 保存合约代码和合约大小的印射关系 self.symbolSizeDict = {} # 保存合约代码和合约大小的印射关系
self.requireAuthentication = False
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onFrontConnected(self): def onFrontConnected(self):
@ -468,8 +481,10 @@ class CtpTdApi(TdApi):
log.gatewayName = self.gatewayName log.gatewayName = self.gatewayName
log.logContent = u'交易服务器连接成功' log.logContent = u'交易服务器连接成功'
self.gateway.onLog(log) self.gateway.onLog(log)
if self.requireAuthentication:
self.login() self.authenticate()
else:
self.login()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onFrontDisconnected(self, n): def onFrontDisconnected(self, n):
@ -490,8 +505,13 @@ class CtpTdApi(TdApi):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onRspAuthenticate(self, data, error, n, last): def onRspAuthenticate(self, data, error, n, last):
"""""" """验证客户端回报"""
pass if error['ErrorID'] == 0:
log = VtLogData()
log.gatewayName = self.gatewayName
log.logContent = u'交易服务器验证成功'
self.gateway.onLog(log)
self.login()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onRspUserLogin(self, data, error, n, last): def onRspUserLogin(self, data, error, n, last):
@ -1244,12 +1264,14 @@ class CtpTdApi(TdApi):
pass pass
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def connect(self, userID, password, brokerID, address): def connect(self, userID, password, brokerID, address, authCode, userProductInfo):
"""初始化连接""" """初始化连接"""
self.userID = userID # 账号 self.userID = userID # 账号
self.password = password # 密码 self.password = password # 密码
self.brokerID = brokerID # 经纪商代码 self.brokerID = brokerID # 经纪商代码
self.address = address # 服务器地址 self.address = address # 服务器地址
self.authCode = authCode #验证码
self.userProductInfo = userProductInfo #产品信息
# 如果尚未建立服务器连接,则进行连接 # 如果尚未建立服务器连接,则进行连接
if not self.connectionStatus: if not self.connectionStatus:
@ -1271,8 +1293,12 @@ class CtpTdApi(TdApi):
# 若已经连接但尚未登录,则进行登录 # 若已经连接但尚未登录,则进行登录
else: else:
if not self.loginStatus: if self.requireAuthentication:
self.login() if self.authStatus:
self.authenticate()
else:
if self.loginStatus:
self.login()
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def login(self): def login(self):
@ -1285,7 +1311,17 @@ class CtpTdApi(TdApi):
req['BrokerID'] = self.brokerID req['BrokerID'] = self.brokerID
self.reqID += 1 self.reqID += 1
self.reqUserLogin(req, self.reqID) self.reqUserLogin(req, self.reqID)
def authenticate(self):
if self.userID and self.brokerID and self.authCode and self.userProductInfo:
req = {}
req['UserID'] = self.userID
req['BrokerID'] = self.brokerID
req['AuthCode'] = self.authCode
req['UserProductInfo'] = self.userProductInfo
self.reqID +=1
self.reqAuthenticate(req, self.reqID)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def qryAccount(self): def qryAccount(self):
"""查询账户""" """查询账户"""