def __OnRspQryAccount(self, pTradingAccount=CThostFtdcTradingAccountField, pRspInfo=CThostFtdcRspInfoField, nRequestID=int, bIsLast=bool): """""" if not self.Account: self.Account = TradingAccount() self.Account.Available = pTradingAccount.getAvailable() self.Account.CloseProfit = pTradingAccount.getCloseProfit() self.Account.Commission = pTradingAccount.getCommission() self.Account.CurrMargin = pTradingAccount.getCurrMargin() self.Account.FrozenCash = pTradingAccount.getFrozenCash() self.Account.PositionProfit = pTradingAccount.getPositionProfit() self.Account.PreBalance = pTradingAccount.getPreBalance( ) + pTradingAccount.getDeposit() + pTradingAccount.getWithdraw() self.Account.Fund = self.Account.PreBalance + pTradingAccount.getCloseProfit( ) + pTradingAccount.getPositionProfit( ) - pTradingAccount.getCommission() self.Account.Risk = self.Account.CurrMargin / self.Account.Fund if not self.IsLogin: self.IsLogin = True info = InfoField() info.ErrorID = 0 info.ErrorMsg = '正确' self.OnRspUserLogin(info)
def __OnRspUserLogin(self, pRspUserLogin=CThostFtdcRspUserLoginField, pRspInfo=CThostFtdcRspInfoField, nRequestID=int, bIsLast=bool): """""" info = InfoField() info.ErrorID = pRspInfo.getErrorID() info.ErrorMsg = pRspInfo.getErrorMsg() self.IsLogin = True _thread.start_new_thread(self.OnRspUserLogin, (info, ))
def __OnRspOrderAction(self, pInputOrderAction=CThostFtdcInputOrderActionField, pRspInfo=CThostFtdcRspInfoField, nRequestID=int, bIsLast=bool): id = "{0}|{1}|{2}".format(pInputOrderAction.getSessionID(), pInputOrderAction.getFrontID(), pInputOrderAction.getOrderRef()) if self.IsLogin and id in self.DicOrderField: info = InfoField() info.ErrorID = pRspInfo.ErrorID info.ErrorMsg = pRspInfo.ErrorMsg self.OnErrCancel(self.DicOrderField[id], info)
def __OnErrOrder(self, pInputOrder=CThostFtdcInputOrderField, pRspInfo=CThostFtdcRspInfoField): """""" id = '{0}|{1}|{2}'.format(self.SessionID, '0', pInputOrder.getOrderRef()) of = self.DicOrderField.get(id) info = InfoField() info.ErrorID = pRspInfo.getErrorID() info.ErrorMsg = pRspInfo.getErrorMsg() if of and of.IsLocal: of.Status = OrderStatus.Error of.StatusMsg = '{0}:{1}'.format(pRspInfo.getErrorID(), pRspInfo.getErrorMsg()) _thread.start_new_thread(self.OnRtnErrOrder, (of, info))
def __OnRtnOrder(self, pOrder=CThostFtdcOrderField): """""" id = '{0}|{1}|{2}'.format(pOrder.getSessionID(), pOrder.getFrontID(), pOrder.getOrderRef()) # of = OrderField() of = self.DicOrderField.get(id) if not of: of = OrderField() l = int(pOrder.getOrderRef()) of.Custom = l % 1000000 of.InstrumentID = pOrder.getInstrumentID() of.InsertTime = pOrder.getInsertTime() of.Direction = DirectType.Buy if DirectionType(pOrder.getDirection( )) == DirectionType.Buy else DirectType.Sell ot = OffsetFlagType(ord(pOrder.getCombOffsetFlag()[0])) of.Offset = OffsetType.Open if ot == OffsetFlagType.Open else ( OffsetType.CloseToday if ot == OffsetFlagType.CloseToday else OffsetType.Close) of.Status = OrderStatus.Normal of.StatusMsg = pOrder.getStatusMsg() of.IsLocal = pOrder.getSessionID() == self.SessionID of.LimitPrice = pOrder.getLimitPrice() of.OrderID = id of.Volume = pOrder.getVolumeTotalOriginal() of.VolumeLeft = of.Volume self.DicOrderField[id] = of self.OnRtnOrder(of) # _thread.start_new_thread(self.OnRtnOrder, (of,)) # call client OnRtnOrder event elif pOrder.getOrderStatus() == OrderStatusType.Canceled: of.Status = OrderStatus.Canceled of.StatusMsg = pOrder.getStatusMsg() if of.StatusMsg.find('被拒绝') >= 0: info = InfoField() info.ErrorID = -1 info.ErrorMsg = of.StatusMsg self.OnRtnErrOrder(of, info) else: self.OnRtnCancel(of) else: if pOrder.getOrderSysID(): of.SysID = pOrder.getOrderSysID() self.__dic_orderid_sysid[pOrder.getOrderSysID( )] = id # 记录sysid与orderid关联,方便Trade时查找处理
def __OnRspOrder(self, pInputOrder=CThostFtdcInputOrderField, pRspInfo=CThostFtdcRspInfoField, nRequestID=int, bIsLast=bool): """""" info = InfoField() info.ErrorID = pRspInfo.getErrorID() info.ErrorMsg = pRspInfo.getErrorMsg() id = '{0}|{1}|{2}'.format(self.SessionID, '0', pInputOrder.getOrderRef()) of = self.DicOrderField.get(id) if not of: of = OrderField() l = int(pInputOrder.getOrderRef()) of.Custom = l % 1000000 of.InstrumentID = pInputOrder.getInstrumentID() of.InsertTime = time.strftime('%Y%M%d %H:%M:%S', time.localtime()) # 对direction需特别处理(具体见ctp_struct) of.Direction = DirectType.Buy if DirectionType( pInputOrder.getDirection( )) == DirectionType.Buy else DirectType.Sell ot = OffsetFlagType(ord(pInputOrder.getCombOffsetFlag()[0])) of.Offset = OffsetType.Open if ot == OffsetFlagType.Open else ( OffsetType.CloseToday if ot == OffsetFlagType.CloseToday else OffsetType.Close) # of.Status = OrderStatus.Normal # of.StatusMsg = f.getStatusMsg() of.IsLocal = True of.LimitPrice = pInputOrder.getLimitPrice() of.OrderID = id of.Volume = pInputOrder.getVolumeTotalOriginal() of.VolumeLeft = of.Volume self.DicOrderField[id] = of of.Status = OrderStatus.Error of.StatusMsg = '{0}:{1}'.format(info.ErrorID, info.ErrorMsg) _thread.start_new_thread(self.OnRtnErrOrder, (of, info))
def __OnRspUserLogin(self, pRspUserLogin=CThostFtdcRspUserLoginField(), pRspInfo=CThostFtdcRspInfoField, nRequestID=int, bIsLast=bool): self.Investor = pRspUserLogin.getUserID() self.BrokerID = pRspUserLogin.getBrokerID() self.SessionID = pRspUserLogin.getSessionID() self.TradingDay = pRspUserLogin.getTradingDay() if pRspInfo.getErrorID() != 0: info = InfoField() info.ErrorID = pRspInfo.getErrorID() info.ErrorMsg = pRspInfo.getErrorMsg() self.OnRspUserLogin(info) else: self.t.ReqSettlementInfoConfirm(self.BrokerID, self.Investor) if not self.qryStart: time.sleep(0.5) """查询持仓与权益""" _thread.start_new_thread(self.__qry, ()) # 开启查询