예제 #1
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy):
        """发单"""
        contract = self.mainEngine.getContract(vtSymbol)
        
        req = VtOrderReq()
        req.symbol = contract.symbol
        req.exchange = contract.exchange
        req.price = price
        req.volume = volume
        
        req.productClass = strategy.productClass
        req.currency = strategy.currency        
        
        # 设计为CTA引擎发出的委托只允许使用限价单
        req.priceType = PRICETYPE_LIMITPRICE    
        
        # CTA委托类型映射
        if orderType == CTAORDER_BUY:
            req.direction = DIRECTION_LONG
            req.offset = OFFSET_OPEN
            
        elif orderType == CTAORDER_SELL:
            req.direction = DIRECTION_SHORT
            
            # 只有上期所才要考虑平今平昨
            if contract.exchange != EXCHANGE_SHFE:
                req.offset = OFFSET_CLOSE
            else:
                # 获取持仓缓存数据
                posBuffer = self.posBufferDict.get(vtSymbol, None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    req.offset = OFFSET_CLOSE
                # 否则如果有多头今仓,则使用平今
                elif posBuffer.longToday:
                    req.offset= OFFSET_CLOSETODAY
                # 其他情况使用平昨
                else:
                    req.offset = OFFSET_CLOSE
                
        elif orderType == CTAORDER_SHORT:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_OPEN
            
        elif orderType == CTAORDER_COVER:
            req.direction = DIRECTION_LONG
            
            # 只有上期所才要考虑平今平昨
            if contract.exchange != EXCHANGE_SHFE:
                req.offset = OFFSET_CLOSE
            else:
                # 获取持仓缓存数据
                posBuffer = self.posBufferDict.get(vtSymbol, None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    req.offset = OFFSET_CLOSE
                # 否则如果有空头今仓,则使用平今
                elif posBuffer.shortToday:
                    req.offset= OFFSET_CLOSETODAY
                # 其他情况使用平昨
                else:
                    req.offset = OFFSET_CLOSE
        
        vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName)    # 发单
        self.orderStrategyDict[vtOrderID] = strategy        # 保存vtOrderID和策略的映射关系

        self.writeCtaLog(u'策略%s发送委托,%s,%s,%s@%s' 
                         %(strategy.name, vtSymbol, req.direction, volume, price))
        
        return vtOrderID
예제 #2
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy):
        """发单"""
        contract = self.mainEngine.getContract(vtSymbol)
        
        req = VtOrderReq()
        req.symbol = contract.symbol        # 合约代码
        req.exchange = contract.exchange    # 交易所
        req.price = price                   # 价格
        req.volume = volume                 # 数量

        if strategy:
            req.productClass = strategy.productClass
            req.currency = strategy.currency
        else:
            req.productClass = ''
            req.currency = ''


        # 设计为CTA引擎发出的委托只允许使用限价单
        req.priceType = PRICETYPE_LIMITPRICE     # 价格类型
        
        # CTA委托类型映射
        if orderType == CTAORDER_BUY:
            req.direction = DIRECTION_LONG      # 合约方向
            req.offset = OFFSET_OPEN            # 开/平
        elif orderType == CTAORDER_SELL:
            req.direction = DIRECTION_SHORT
            
            # 只有上期所才要考虑平今平昨
            if contract.exchange != EXCHANGE_SHFE:
                req.offset = OFFSET_CLOSE
            else:
                # 获取持仓缓存数据
                posBuffer = self.posBufferDict.get(vtSymbol, None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    req.offset = OFFSET_CLOSE

                # modified by IncenseLee 2016/11/08,改为优先平昨仓
                elif posBuffer.longYd :
                    req.offset = OFFSET_CLOSE
                else:
                    req.offset = OFFSET_CLOSETODAY

                # 否则如果有多头今仓,则使用平今
                #elif posBuffer.longToday:
                #    req.offset= OFFSET_CLOSETODAY
                # 其他情况使用平昨
                #else:
                #    req.offset = OFFSET_CLOSE
                
        elif orderType == CTAORDER_SHORT:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_OPEN
            
        elif orderType == CTAORDER_COVER:
            req.direction = DIRECTION_LONG
            
            # 只有上期所才要考虑平今平昨
            if contract.exchange != EXCHANGE_SHFE:
                req.offset = OFFSET_CLOSE
            else:
                # 获取持仓缓存数据
                posBuffer = self.posBufferDict.get(vtSymbol, None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    req.offset = OFFSET_CLOSE

                #modified by IncenseLee 2016/11/08,改为优先平昨仓
                elif posBuffer.shortYd:
                    req.offset = OFFSET_CLOSE
                else:
                    req.offset = OFFSET_CLOSETODAY

                # 否则如果有空头今仓,则使用平今
                #elif posBuffer.shortToday:
                #    req.offset= OFFSET_CLOSETODAY
                # 其他情况使用平昨
                #else:
                #    req.offset = OFFSET_CLOSE
        
        vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName)    # 发单

        if strategy:
            self.orderStrategyDict[vtOrderID] = strategy        # 保存vtOrderID和策略的映射关系

            self.writeCtaLog(u'策略%s发送委托,%s, %s,%s,%s@%s'
                         %(strategy.name, vtSymbol, req.offset, req.direction, volume, price))
        else:
            self.writeCtaLog(u'%s发送委托,%s, %s,%s,%s@%s'
                             % ('CtaEngine', vtSymbol, req.offset, req.direction, volume, price))
        return vtOrderID
예제 #3
0
파일: ctaEngine.py 프로젝트: 280185386/vnpy
 def sendOrder(self, vtSymbol, orderType, price, volume, strategy):
     """发单"""
     contract = self.mainEngine.getContract(vtSymbol)
     
     req = VtOrderReq()
     req.symbol = contract.symbol
     req.exchange = contract.exchange
     req.price = price
     req.volume = volume
     
     # 设计为CTA引擎发出的委托只允许使用限价单
     req.priceType = PRICETYPE_LIMITPRICE    
     
     # CTA委托类型映射
     if orderType == CTAORDER_BUY:
         req.direction = DIRECTION_LONG
         req.offset = OFFSET_OPEN
     elif orderType == CTAORDER_SELL:
         req.direction = DIRECTION_SHORT
         req.offset = OFFSET_CLOSE
     elif orderType == CTAORDER_SHORT:
         req.direction = DIRECTION_SHORT
         req.offset = OFFSET_OPEN
     elif orderType == CTAORDER_COVER:
         req.direction = DIRECTION_LONG
         req.offset = OFFSET_CLOSE
     
     vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName)    # 发单
     self.orderStrategyDict[vtOrderID] = strategy        # 保存vtOrderID和策略的映射关系
     
     #self.writeCtaLog(u'发送委托:' + str(req.__dict__))
     
     return vtOrderID
예제 #4
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy, isMKT):
        """发单"""
        contract = self.mainEngine.getContract(vtSymbol)
        if contract.gatewayName == "IB":
            return self.sendIBOrder(vtSymbol, orderType, price, volume,
                                    strategy, isMKT)
        req = VtOrderReq()
        req.symbol = contract.symbol
        req.exchange = contract.exchange
        priceTick = contract.priceTick
        req.price = self.getPrice(float(price), float(priceTick))
        req.volume = volume
        closeFirst = strategy.closeFirst
        req.productClass = strategy.productClass
        req.currency = strategy.currency
        # 设计为CTA引擎发出的委托只允许使用限价单
        req.priceType = PRICETYPE_LIMITPRICE
        if vtSymbol not in self.mPosInfo.keys():
            l = {}
            l['ytd'] = 0
            l['td'] = 0
            s = {}
            s['ytd'] = 0
            s['td'] = 0
            self.mPosInfo[vtSymbol] = {}
            self.mPosInfo[vtSymbol]['long'] = l
            self.mPosInfo[vtSymbol]['short'] = s
        if closeFirst != None and closeFirst == False:
            if orderType == CTAORDER_BUY:
                req.direction = DIRECTION_LONG
                req.offset = OFFSET_OPEN
                self.mPosInfo[vtSymbol]['long']['td'] += volume
            elif orderType == CTAORDER_SELL:
                req.direction = DIRECTION_SHORT
                if contract.exchange != EXCHANGE_SHFE:
                    req.offset = OFFSET_CLOSE
                    if self.mPosInfo[vtSymbol]['long']['td'] >= volume:
                        self.mPosInfo[vtSymbol]['long']['td'] -= volume
                    else:
                        self.mPosInfo[vtSymbol]['long']['ytd'] -= volume
                else:
                    # 如果获取持仓缓存失败,则默认平昨
                    if self.mPosInfo[vtSymbol]['long']['ytd'] >= volume:
                        req.offset = OFFSET_CLOSE
                        self.mPosInfo[vtSymbol]['long']['ytd'] -= volume
                # 否则如果有多头今仓,则使用平今
                    else:
                        req.offset = OFFSET_CLOSETODAY
                        self.mPosInfo[vtSymbol]['long']['td'] -= volume

            elif orderType == CTAORDER_SHORT:
                req.direction = DIRECTION_SHORT
                req.offset = OFFSET_OPEN
                self.mPosInfo[vtSymbol]['short']['td'] += volume
            elif orderType == CTAORDER_COVER:
                req.direction = DIRECTION_LONG

                # 只有上期所才要考虑平今平昨
                if contract.exchange != EXCHANGE_SHFE:
                    req.offset = OFFSET_CLOSE
                    if self.mPosInfo[vtSymbol]['short']['td'] >= volume:
                        self.mPosInfo[vtSymbol]['short']['td'] -= volume
                    else:
                        self.mPosInfo[vtSymbol]['short']['ytd'] -= volume
                else:
                    if self.mPosInfo[vtSymbol]['short']['ytd'] >= volume:
                        req.offset = OFFSET_CLOSE
                        self.mPosInfo[vtSymbol]['short']['ytd'] -= volume
                    elif self.mPosInfo[vtSymbol]['short']['td'] >= volume:
                        req.offset = OFFSET_CLOSETODAY
                        self.mPosInfo[vtSymbol]['short']['td'] -= volume

            vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName,
                                                  strategy)  # 发单
            self.orderStrategyDict[vtOrderID] = strategy  # 保存vtOrderID和策略的映射关系
            self.writeCtaLog(u'策略%s发送委托,%s,%s,%s, %s@%s' %
                             (strategy.name, vtSymbol, req.direction,
                              req.offset, volume, price))
            return vtOrderID

        if orderType == CTAORDER_BUY:
            req.direction = DIRECTION_LONG
            if self.mPosInfo[vtSymbol]['short']['ytd'] >= volume:
                req.offset = OFFSET_CLOSE
                self.mPosInfo[vtSymbol]['short']['ytd'] -= volume
            elif self.mPosInfo[vtSymbol]['short']['td'] >= volume:
                req.offset = OFFSET_CLOSETODAY
                self.mPosInfo[vtSymbol]['short']['td'] -= volume
            else:
                req.offset = OFFSET_OPEN
                self.mPosInfo[vtSymbol]['long']['td'] += volume
        elif orderType == CTAORDER_SELL:
            req.direction = DIRECTION_SHORT

            # 只有上期所才要考虑平今平昨
            if contract.exchange != EXCHANGE_SHFE:
                if self.mPosInfo[vtSymbol]['long']['ytd'] >= volume:
                    req.offset = OFFSET_CLOSE
                    self.mPosInfo[vtSymbol]['long']['ytd'] -= volume
                elif self.mPosInfo[vtSymbol]['long']['td'] >= volume:
                    req.offset = OFFSET_CLOSE
                    self.mPosInfo[vtSymbol]['long']['td'] -= volume
                else:
                    req.offset = OFFSET_OPEN
                    self.mPosInfo[vtSymbol]['short']['td'] += volume
            else:
                if self.mPosInfo[vtSymbol]['long']['ytd'] >= volume:
                    req.offset = OFFSET_CLOSE
                    self.mPosInfo[vtSymbol]['long']['ytd'] -= volume
                elif self.mPosInfo[vtSymbol]['long']['td'] >= volume:
                    req.offset = OFFSET_CLOSETODAY
                    self.mPosInfo[vtSymbol]['long']['td'] -= volume
                else:
                    req.offset = OFFSET_OPEN
                    self.mPosInfo[vtSymbol]['short']['td'] += volume
#=======================================================================
        elif orderType == CTAORDER_SHORT:
            req.direction = DIRECTION_SHORT
            if self.mPosInfo[vtSymbol]['long']['ytd'] >= volume:
                req.offset = OFFSET_CLOSE
                self.mPosInfo[vtSymbol]['long']['ytd'] -= volume
            elif self.mPosInfo[vtSymbol]['long']['td'] >= volume:
                req.offset = OFFSET_CLOSETODAY
                self.mPosInfo[vtSymbol]['long']['td'] -= volume
            else:
                req.offset = OFFSET_OPEN
                self.mPosInfo[vtSymbol]['short']['td'] += volume
        elif orderType == CTAORDER_COVER:
            req.direction = DIRECTION_LONG

            # 只有上期所才要考虑平今平昨
            if contract.exchange != EXCHANGE_SHFE:
                if self.mPosInfo[vtSymbol]['short']['ytd'] >= volume:
                    req.offset = OFFSET_CLOSE
                    self.mPosInfo[vtSymbol]['short']['ytd'] -= volume
                elif self.mPosInfo[vtSymbol]['short']['td'] >= volume:
                    req.offset = OFFSET_CLOSE
                    self.mPosInfo[vtSymbol]['short']['td'] -= volume
                else:
                    req.offset = OFFSET_OPEN
                    self.mPosInfo[vtSymbol]['long']['td'] += volume
            else:
                if self.mPosInfo[vtSymbol]['short']['ytd'] >= volume:
                    req.offset = OFFSET_CLOSE
                    self.mPosInfo[vtSymbol]['short']['ytd'] -= volume
                elif self.mPosInfo[vtSymbol]['short']['td'] >= volume:
                    req.offset = OFFSET_CLOSETODAY
                    self.mPosInfo[vtSymbol]['short']['td'] -= volume
                else:
                    req.offset = OFFSET_OPEN
                    self.mPosInfo[vtSymbol]['long']['td'] += volume

#=======================================================================
        vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName,
                                              strategy)  # 发单
        self.orderStrategyDict[vtOrderID] = strategy  # 保存vtOrderID和策略的映射关系

        self.writeCtaLog(
            u'策略%s发送委托,%s,%s,%s@%s' %
            (strategy.name, vtSymbol, req.direction, volume, price))
        return vtOrderID
예제 #5
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy):
        """发单"""
        contract = self.mainEngine.getContract(vtSymbol)

        req = VtOrderReq()
        req.symbol = contract.symbol
        req.exchange = contract.exchange
        req.price = self.roundToPriceTick(contract.priceTick, price)
        req.volume = volume

        req.productClass = strategy.productClass
        req.currency = strategy.currency

        # 设计为CTA引擎发出的委托只允许使用限价单
        req.priceType = PRICETYPE_LIMITPRICE

        # CTA委托类型映射
        if orderType == CTAORDER_BUY:
            req.direction = DIRECTION_LONG
            req.offset = OFFSET_OPEN

        elif orderType == CTAORDER_SELL:
            req.direction = DIRECTION_SHORT

            # 只有上期所才要考虑平今平昨
            if contract.exchange != EXCHANGE_SHFE:
                req.offset = OFFSET_CLOSE
            else:
                # 获取持仓缓存数据
                posBuffer = self.posBufferDict.get(vtSymbol, None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    req.offset = OFFSET_CLOSE
                # 否则如果有多头今仓,则使用平今
                elif posBuffer.longToday:
                    req.offset = OFFSET_CLOSETODAY
                # 其他情况使用平昨
                else:
                    req.offset = OFFSET_CLOSE

        elif orderType == CTAORDER_SHORT:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_OPEN

        elif orderType == CTAORDER_COVER:
            req.direction = DIRECTION_LONG

            # 只有上期所才要考虑平今平昨
            if contract.exchange != EXCHANGE_SHFE:
                req.offset = OFFSET_CLOSE
            else:
                # 获取持仓缓存数据
                posBuffer = self.posBufferDict.get(vtSymbol, None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    req.offset = OFFSET_CLOSE
                # 否则如果有空头今仓,则使用平今
                elif posBuffer.shortToday:
                    req.offset = OFFSET_CLOSETODAY
                # 其他情况使用平昨
                else:
                    req.offset = OFFSET_CLOSE

        vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName)  # 发单
        self.orderStrategyDict[vtOrderID] = strategy  # 保存vtOrderID和策略的映射关系

        self.writeCtaLog(
            u'策略%s发送委托,%s,%s,%s@%s' %
            (strategy.name, vtSymbol, req.direction, volume, price))

        return vtOrderID
예제 #6
0
    def sendIBOrder(self, vtSymbol, orderType, price, volume, strategy, isMKT):
        contract = self.mainEngine.getContract(vtSymbol)
        req = VtOrderReq()
        req.symbol = contract.symbol
        req.exchange = contract.exchange
        #req.price = self.getPrice(float(price), float(contract.priceTick))
        req.price = price
        req.volume = volume
        if isMKT:
            req.priceType = PRICETYPE_MARKETPRICE
        else:
            req.priceType = PRICETYPE_LIMITPRICE
        req.productClass = strategy.productClass
        req.currency = strategy.currency
        if orderType == CTAORDER_BUY or orderType == CTAORDER_COVER:
            req.direction = DIRECTION_LONG
        else:
            req.direction = DIRECTION_SHORT

        vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName,
                                              strategy)  # 发单
        self.orderStrategyDict[vtOrderID] = strategy  # 保存vtOrderID和策略的映射关系

        self.writeCtaLog(
            u'策略%s发送委托,%s,%s,%s@%s' %
            (strategy.name, vtSymbol, req.direction, volume, price))
        return vtOrderID
예제 #7
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy):
        """发单"""
        contract = self.mainEngine.getContract(vtSymbol)

        req = VtOrderReq()
        req.symbol = contract.symbol
        req.exchange = contract.exchange
        req.price = price
        req.volume = volume

        # 设计为CTA引擎发出的委托只允许使用限价单
        req.priceType = PRICETYPE_LIMITPRICE

        # CTA委托类型映射
        if orderType == CTAORDER_BUY:
            req.direction = DIRECTION_LONG
            req.offset = OFFSET_OPEN
        elif orderType == CTAORDER_SELL:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_CLOSE
        elif orderType == CTAORDER_SHORT:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_OPEN
        elif orderType == CTAORDER_COVER:
            req.direction = DIRECTION_LONG
            req.offset = OFFSET_CLOSE

        vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName)  # 发单
        self.orderStrategyDict[vtOrderID] = strategy  # 保存vtOrderID和策略的映射关系

        #self.writeCtaLog(u'发送委托:' + str(req.__dict__))

        return vtOrderID
예제 #8
0
 def pd_sendOrder(self):
     print "sendOrder"
     if self.gatewayName_1 in self.tickDict and self.gatewayName_2 in self.tickDict:
         if self.tickDict[self.gatewayName_1] > self.tickDict[self.gatewayName_2]:
             sellResult=getPosition("sell",self.positionDict_1,self.tickDict[self.gatewayName_1],self.lots)
             buyResult = getPosition("buy", self.positionDict_2, self.tickDict[self.gatewayName_2], self.lots)
             if sellResult and buyResult:
                 req = VtOrderReq()
                 req.symbol="BTC_CNY_SPOT"
                 req.direction=DIRECTION_SHORT
                 req.price=self.tickDict[self.gatewayName_1]-self.margin
                 req.volume=self.lots
                 self.writeLog(u'火币发送委托卖单,%s@%s' % (req.volume, req.price))
                 #print (u'HUOBI发送委托卖单,%s@%s' % (req.volume, req.price))
                 self.mainEngine.sendOrder(req,self.gatewayName_1)
                 self.orderCondition.acquire()
                 self.orderCondition.wait()
                 self.orderCondition.release()
                 if self.gatewayName_1 in self.orderDict:
                     orderData = self.orderDict[self.gatewayName_1]
                     if orderData.status == TRADER_STATUS_DEAL:
                         self.writeLog(u'火币卖单成交,%s@%s' % (req.volume, req.price))
                         req.symbol="BTC_CNY_SPOT"
                         req.direction=DIRECTION_LONG
                         req.priceType = PRICETYPE_LIMITPRICE
                         req.price = self.tickDict[self.gatewayName_2]+self.margin
                         req.volume = self.lots
                         self.writeLog(u'OKCOIN发送委托买单,%s@%s' % (req.volume, req.price))
                         # print (u'OKCOIN发送委托买单,%s@%s' % (req.volume, req.price))
                         self.mainEngine.sendOrder(req, self.gatewayName_2)
                     else:
                         self.writeLog(u'火币卖单未成交,%s@%s' % (req.volume, req.price))
             else:
                 if not sellResult:
                     self.writeLog(u'火币账户币不足无法执行卖出单')
                 if not buyResult:
                     self.writeLog(u'OKCOIN账户钱不足无法执行买入单')
         else:
             buyResult = getPosition("buy", self.positionDict_1, self.tickDict[self.gatewayName_1], self.lots)
             sellResult = getPosition("sell", self.positionDict_2, self.tickDict[self.gatewayName_2], self.lots)
             if sellResult and buyResult:
                 req = VtOrderReq()
                 req.symbol = "BTC_CNY_SPOT"
                 req.direction = DIRECTION_LONG
                 req.price = 5000#self.tickDict[self.gatewayName_1] + self.margin
                 req.volume = self.lots
                 self.writeLog(u'火币发送委托买单,%s@%s' % (req.volume, req.price))
                 #print (u'HUOBI发送委托买单,%s@%s' % (req.volume, req.price))
                 self.mainEngine.sendOrder(req, self.gatewayName_1)
                 # 等待发单回调推送委托号信息
                 self.orderCondition.acquire()
                 self.orderCondition.wait()
                 self.orderCondition.release()
                 if self.gatewayName_1 in self.orderDict:
                     orderData=self.orderDict[self.gatewayName_1]
                     if orderData.status==TRADER_STATUS_DEAL:
                         self.writeLog(u'火币买单成交,%s@%s' % (req.volume, req.price))
                         req.symbol = "BTC_CNY_SPOT"
                         req.direction = DIRECTION_SHORT
                         req.priceType = PRICETYPE_LIMITPRICE
                         req.price = self.tickDict[self.gatewayName_2] - self.margin
                         req.volume = self.lots
                         self.writeLog(u'OKCOIN发送委托卖单,%s@%s' % (req.volume, req.price))
                         #print (u'OKCOIN发送委托卖单,%s@%s' % (req.volume, req.price))
                         self.mainEngine.sendOrder(req, self.gatewayName_2)
                     else:
                         self.writeLog(u'火币买单未成交,%s@%s' % (req.volume, req.price))
             else:
                 if not sellResult:
                     self.writeLog(u'OKCOIN账户币不足无法执行卖出单')
                 if not buyResult:
                     self.writeLog(u'火币账户钱不足无法执行买入单')
예제 #9
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy):
        """发单"""
        contract = self.mainEngine.getContract(vtSymbol)

        req = VtOrderReq()
        req.symbol = contract.symbol  # 合约代码
        req.exchange = contract.exchange  # 交易所
        req.price = price  # 价格
        req.volume = volume  # 数量

        if strategy:
            req.productClass = strategy.productClass
            req.currency = strategy.currency
        else:
            req.productClass = ''
            req.currency = ''

        # 设计为CTA引擎发出的委托只允许使用限价单
        req.priceType = PRICETYPE_LIMITPRICE  # 价格类型

        # CTA委托类型映射
        if orderType == CTAORDER_BUY:
            req.direction = DIRECTION_LONG  # 合约方向
            req.offset = OFFSET_OPEN  # 开/平
        elif orderType == CTAORDER_SELL:
            req.direction = DIRECTION_SHORT

            # 只有上期所才要考虑平今平昨
            if contract.exchange != EXCHANGE_SHFE:
                req.offset = OFFSET_CLOSE
            else:
                # 获取持仓缓存数据
                posBuffer = self.posBufferDict.get(vtSymbol, None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    req.offset = OFFSET_CLOSE

                # modified by IncenseLee 2016/11/08,改为优先平昨仓
                elif posBuffer.longYd:
                    req.offset = OFFSET_CLOSE
                else:
                    req.offset = OFFSET_CLOSETODAY

                # 否则如果有多头今仓,则使用平今
                #elif posBuffer.longToday:
                #    req.offset= OFFSET_CLOSETODAY
                # 其他情况使用平昨
                #else:
                #    req.offset = OFFSET_CLOSE

        elif orderType == CTAORDER_SHORT:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_OPEN

        elif orderType == CTAORDER_COVER:
            req.direction = DIRECTION_LONG

            # 只有上期所才要考虑平今平昨
            if contract.exchange != EXCHANGE_SHFE:
                req.offset = OFFSET_CLOSE
            else:
                # 获取持仓缓存数据
                posBuffer = self.posBufferDict.get(vtSymbol, None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    req.offset = OFFSET_CLOSE

                #modified by IncenseLee 2016/11/08,改为优先平昨仓
                elif posBuffer.shortYd:
                    req.offset = OFFSET_CLOSE
                else:
                    req.offset = OFFSET_CLOSETODAY

                # 否则如果有空头今仓,则使用平今
                #elif posBuffer.shortToday:
                #    req.offset= OFFSET_CLOSETODAY
                # 其他情况使用平昨
                #else:
                #    req.offset = OFFSET_CLOSE

        vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName)  # 发单

        if strategy:
            self.orderStrategyDict[vtOrderID] = strategy  # 保存vtOrderID和策略的映射关系

            self.writeCtaLog(u'策略%s发送委托,%s, %s,%s,%s@%s' %
                             (strategy.name, vtSymbol, req.offset,
                              req.direction, volume, price))
        else:
            self.writeCtaLog(u'%s发送委托,%s, %s,%s,%s@%s' %
                             ('CtaEngine', vtSymbol, req.offset, req.direction,
                              volume, price))
        return vtOrderID
예제 #10
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy,gatewayName):
        """发单"""

        
        req = VtOrderReq()
        req.symbol = vtSymbol
        # CTA委托类型映射

        if orderType == CTAORDER_BUY:
            req.direction = DIRECTION_LONG
            req.offset = OFFSET_OPEN

        elif orderType == CTAORDER_SELL:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_CLOSE

        elif orderType == CTAORDER_SHORT:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_OPEN

        elif orderType == CTAORDER_COVER:
            req.direction = DIRECTION_LONG
            req.offset = OFFSET_CLOSE


        req.price = price
        req.volume = volume
        req.priceType=PRICETYPE_LIMITPRICE
        req.orderStyle=1
        vtOrderID = self.mainEngine.sendOrder(req, gatewayName)    # 发单
        self.orderStrategyDict[vtOrderID] = strategy        # 保存vtOrderID和策略的映射关系

        #print (u'策略%s发送委托,%s,%s,%s@%s,%s' %(strategy.name, vtSymbol, req.direction, volume, price,gatewayName))
        self.writeCtaLog(u'策略%s发送委托,%s,%s,%s@%s'
                         %(strategy.name, vtSymbol, req.direction, volume, price))
        
        return vtOrderID