Beispiel #1
0
    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时查找处理
Beispiel #2
0
    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))