diff --git a/vn.trader/ctpGateway/ctpGateway.py b/vn.trader/ctpGateway/ctpGateway.py index 01c6fb99..1619aac5 100644 --- a/vn.trader/ctpGateway/ctpGateway.py +++ b/vn.trader/ctpGateway/ctpGateway.py @@ -89,6 +89,8 @@ class CtpGateway(VtGateway): self.tdConnected = False # 交易API连接状态 self.qryEnabled = False # 是否要启动循环查询 + + self.requireAuthentication = False #---------------------------------------------------------------------- def connect(self): @@ -115,6 +117,14 @@ class CtpGateway(VtGateway): brokerID = str(setting['brokerID']) tdAddress = str(setting['tdAddress']) 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: log = VtLogData() log.gatewayName = self.gatewayName @@ -124,7 +134,7 @@ class CtpGateway(VtGateway): # 创建行情和交易接口对象 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() @@ -446,6 +456,7 @@ class CtpTdApi(TdApi): self.connectionStatus = False # 连接状态 self.loginStatus = False # 登录状态 + self.authStatus = False self.userID = EMPTY_STRING # 账号 self.password = EMPTY_STRING # 密码 @@ -458,6 +469,8 @@ class CtpTdApi(TdApi): self.posBufferDict = {} # 缓存持仓数据的字典 self.symbolExchangeDict = {} # 保存合约代码和交易所的印射关系 self.symbolSizeDict = {} # 保存合约代码和合约大小的印射关系 + + self.requireAuthentication = False #---------------------------------------------------------------------- def onFrontConnected(self): @@ -468,8 +481,10 @@ class CtpTdApi(TdApi): log.gatewayName = self.gatewayName log.logContent = u'交易服务器连接成功' self.gateway.onLog(log) - - self.login() + if self.requireAuthentication: + self.authenticate() + else: + self.login() #---------------------------------------------------------------------- def onFrontDisconnected(self, n): @@ -490,8 +505,13 @@ class CtpTdApi(TdApi): #---------------------------------------------------------------------- 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): @@ -1244,12 +1264,14 @@ class CtpTdApi(TdApi): pass #---------------------------------------------------------------------- - def connect(self, userID, password, brokerID, address): + def connect(self, userID, password, brokerID, address, authCode, userProductInfo): """初始化连接""" self.userID = userID # 账号 self.password = password # 密码 self.brokerID = brokerID # 经纪商代码 self.address = address # 服务器地址 + self.authCode = authCode #验证码 + self.userProductInfo = userProductInfo #产品信息 # 如果尚未建立服务器连接,则进行连接 if not self.connectionStatus: @@ -1271,8 +1293,12 @@ class CtpTdApi(TdApi): # 若已经连接但尚未登录,则进行登录 else: - if not self.loginStatus: - self.login() + if self.requireAuthentication: + if self.authStatus: + self.authenticate() + else: + if self.loginStatus: + self.login() #---------------------------------------------------------------------- def login(self): @@ -1285,7 +1311,17 @@ class CtpTdApi(TdApi): req['BrokerID'] = self.brokerID self.reqID += 1 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): """查询账户"""