def onRspAuthenticate(self, data, error, n, last): """ 服务端返回的身份认证的响应 :param data: :param error: :param n: :param last: :return: """ if error['ErrorID'] == 0: self.authStatus = True log = Log() log.gatewayName = self.gatewayName log.content = u"交易服务器认证完成" self.gateway.onLog(log) # 登录 self.login() else: err = Error() err.gatewayName = self.gatewayName err.errorID = error['ErrorID'] err.errorMsg = error['ErrorMsg'].decode('gbk') self.gateway.onError(err)
def writeDrLog(self, content): """快速发出日志事件""" log = Log() log.content = content event = Event(type_=EVENT_DATARECORDER_LOG) event.dict_['data'] = log self.eventEngine.put(event)
def onRspUserLogin(self, data, error, n, last): """登录回报""" # 如果登录成功,推送日志信息 if error['ErrorID'] == 0: self.frontID = str(data['FrontID']) self.sessionID = str(data['SessionID']) self.loginStatus = True self.gateway.tdConnected = True log = Log() log.gatewayName = self.gatewayName log.content = u'交易服务器登录完成' self.gateway.onLog(log) # 确认结算信息 req = {} req['BrokerID'] = self.brokerID req['InvestorID'] = self.userID self.reqID += 1 self.reqSettlementInfoConfirm(req, self.reqID) # 否则,推送错误信息 else: err = Error() err.gatewayName = self.gatewayName err.errorID = error['ErrorID'] err.errorMsg = error['ErrorMsg'].decode('gbk') self.gateway.onError(err)
def on_get_position(self): self.engine.qryPosition() # socketio.emit('update_position', positions.__dict__) log = Log() log.content = u"已经开始查询持仓信息" self.emit('log', log.__dict__, namespace='/system')
def writeRiskLog(self, content): """快速发出日志事件""" # 发出日志事件 log = Log() log.content = content log.gatewayName = self.name event = Event(type_=EVENT_LOG) event.dict_['data'] = log self.eventEngine.put(event)
def onFrontConnected(self): """服务器连接""" self.connectionStatus = True log = Log() log.gatewayName = self.gatewayName log.content = u'行情服务器连接成功' self.gateway.onLog(log) self.login()
def onFrontDisconnected(self, n): """服务器断开""" self.connectionStatus = False self.loginStatus = False self.gateway.tdConnected = False log = Log() log.gatewayName = self.gatewayName log.content = u'交易服务器连接断开' self.gateway.onLog(log)
def onRspSettlementInfoConfirm(self, data, error, n, last): """确认结算信息回报""" log = Log() log.gatewayName = self.gatewayName log.content = u'结算信息确认完成' self.gateway.onLog(log) # 查询合约代码 self.reqID += 1 self.reqQryInstrument({}, self.reqID)
def on_start(self, config): # todo 依据传入的柜台服务器地址建立连接 self.engine.connect('CTP', config) # connect Database self.engine.dbConnect() log = Log() log.content = "system starting" self.emit('log', log.__dict__)
def onFrontConnected(self): """服务器连接""" self.connectionStatus = True log = Log() log.gatewayName = self.gatewayName log.content = u'交易服务器连接成功' self.gateway.onLog(log) # 服务器连接之后进行身份认证 if not self.authStatus: self.auth() elif not self.loginStatus: self.login()
def onRspUserLogout(self, data, error, n, last): """登出回报""" # 如果登出成功,推送日志信息 if error['ErrorID'] == 0: self.loginStatus = False self.gateway.tdConnected = False log = Log() log.gatewayName = self.gatewayName log.content = u'交易服务器登出完成' self.gateway.onLog(log) # 否则,推送错误信息 else: err = Error() err.gatewayName = self.gatewayName err.errorID = error['ErrorID'] err.errorMsg = error['ErrorMsg'].decode('gbk') self.gateway.onError(err)
def onRspUserLogin(self, data, error, n, last): """登陆回报""" # 如果登录成功,推送日志信息 if error['ErrorID'] == 0: self.loginStatus = True self.gateway.mdConnected = True log = Log() log.gatewayName = self.gatewayName log.content = u'行情服务器登录完成' self.gateway.onLog(log) # 重新订阅之前订阅的合约 for subscribeReq in self.subscribedSymbols: self.subscribe(subscribeReq) # 否则,推送错误信息 else: err = Error() err.gatewayName = self.gatewayName err.errorID = error['ErrorID'] err.errorMsg = error['ErrorMsg'].decode('gbk') self.gateway.onError(err)
def onRspQryInstrument(self, data, error, n, last): """合约查询回报""" contract = Contract() contract.gatewayName = self.gatewayName contract.symbol = data['InstrumentID'] contract.exchange = exchangeMapReverse[data['ExchangeID']] contract.vtSymbol = contract.symbol # '.'.join([contract.symbol, contract.exchange]) contract.name = data['InstrumentName'].decode('GBK') # 合约数值 contract.size = data['VolumeMultiple'] contract.priceTick = data['PriceTick'] contract.strikePrice = data['StrikePrice'] contract.underlyingSymbol = data['UnderlyingInstrID'] contract.productClass = productClassMapReverse.get( data['ProductClass'], PRODUCT_UNKNOWN) # 期权类型 if data['OptionsType'] == '1': contract.optionType = OPTION_CALL elif data['OptionsType'] == '2': contract.optionType = OPTION_PUT # 缓存代码和交易所的印射关系 self.symbolExchangeDict[contract.symbol] = contract.exchange self.symbolSizeDict[contract.symbol] = contract.size # 推送 self.gateway.onContract(contract) if last: log = Log() log.gatewayName = self.gatewayName log.content = u'交易合约信息获取完成' self.gateway.onLog(log)
def on_close(self): self.engine.exit() log = Log() log.content = "system closed" self.emit('log', log.__dict__)