Exemplo n.º 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

        # 设计为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
Exemplo n.º 2
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 = price - price % contract.priceTick
        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
Exemplo n.º 3
0
    def sendOrderOriginal(self, vtSymbol, direction, offset, price, volume, strategy):
        """CTP原始发单"""
        contract = self.mainEngine.getContract(vtSymbol)
        
        req = VtOrderReq()
        req.symbol = contract.symbol
        req.exchange = contract.exchange
        req.price = price
        req.volume = volume
        req.direction = direction
        req.offset = offset
        
        req.productClass = strategy.productClass
        req.currency = strategy.currency   
        # 设计为CTA引擎发出的委托只允许使用限价单
        req.priceType = PRICETYPE_LIMITPRICE                 
        
        vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName)    # 发单

        if __name__ == '__main__':
            self.orderStrategyDict[vtOrderID] = strategy        # 保存vtOrderID和策略的映射关系
        #     这样做之后,收到的委托回报和成交回报就可以正确的提交到对应的策略,不至于乱套.

        self.writeCtaLog(u'%s:下单委托,标的--%s,方向--%s,开平--%s,下单量--%s,下单价--%s' 
                         %(strategy.name, vtSymbol, req.direction, req.offset, req.volume, req.price))
 
        return vtOrderID
Exemplo n.º 4
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy,
                  gatewayName):
        """发单"""

        req = VtOrderReq()
        req.symbol = vtSymbol
        # CTA委托类型映射
        if orderType == CTAORDER_BUY:
            req.direction = DIRECTION_LONG

        elif orderType == CTAORDER_SELL:
            req.direction = DIRECTION_SHORT
        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
Exemplo n.º 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.vtSymbol = contract.vtSymbol
        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
            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

        # 委托转换
        reqList = self.mainEngine.convertOrderReq(req)
        vtOrderIDList = []

        if not reqList:
            return vtOrderIDList

        for convertedReq in reqList:
            vtOrderID = self.mainEngine.sendOrder(convertedReq,
                                                  contract.gatewayName)  # 发单
            self.orderStrategyDict[vtOrderID] = strategy  # 保存vtOrderID和策略的映射关系
            self.strategyOrderDict[strategy.name].add(vtOrderID)  # 添加到策略委托号集合中
            vtOrderIDList.append(vtOrderID)

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

        return vtOrderIDList
Exemplo n.º 6
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
Exemplo n.º 7
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
        req.price = price - price % contract.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
Exemplo n.º 8
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy,
                  priceType, parked, alt, kwargs):
        """send order"""
        #if alt:
        #    return self.sendOrder2(vtSymbol, orderType, price, volume, strategy, priceType, kwargs)
        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

        req.priceType = priceType  # market or limit order

        # 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:
                    print 'close today'
                    req.offset = OFFSET_CLOSETODAY
                # 其他情况使用平昨
                else:
                    print 'close yesterday'
                    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:
                    print 'close today'
                    req.offset = OFFSET_CLOSETODAY
                # 其他情况使用平昨
                else:
                    print 'close yesterday'
                    req.offset = OFFSET_CLOSE
        if not parked:
            vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName)
            self.orderStrategyDict[vtOrderID] = strategy
            if 'append_info' in kwargs:
                self.orderAppendInfoDict[vtOrderID] = kwargs['append_info']
        else:
            vtOrderID = self.mainEngine.sendParkedOrder(
                req, contract.gatewayName)
            po = ParkedOrder()
            po.vtSymbol = vtSymbol
            po.orderType = orderType
            po.price = self.roundToPriceTick(contract.priceTick, price)
            po.volume = volume
            po.strategy = strategy
            po.localOrderID = vtOrderID

            if orderType == CTAORDER_BUY:
                po.direction = DIRECTION_LONG
                po.offset = OFFSET_OPEN
            elif orderType == CTAORDER_SELL:
                po.direction = DIRECTION_SHORT
                po.offset = OFFSET_CLOSE
            elif orderType == CTAORDER_SHORT:
                po.direction = DIRECTION_SHORT
                po.offset = OFFSET_OPEN
            elif orderType == CTAORDER_COVER:
                po.direction = DIRECTION_LONG
                po.offset = OFFSET_CLOSE
            self.parkedOrderSet.add(po)
            self.workingParkedOrderSet.add(po)

        self.writeCtaLog(
            u'策略%s发送委托, %s, %s, %s@%s' %
            (strategy.name, vtSymbol, req.direction, volume, price))
        return vtOrderID
Exemplo n.º 9
0
    def sendOrder2(self, vtSymbol, orderType, price, volume, strategy,
                   priceType, kwargs):
        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
        req.priceType = priceType

        #CTA OrderType Map
        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

        if contract.exchange == EXCHANGE_SHFE:
            posBuffer = self.posBufferDict.get(vtSymbol, None)
            if not posBuffer:
                posBuffer = PositionBuffer()
                posBuffer.vtSymbol = vtSymbol
                self.posBufferDict[vtSymbol] = posBuffer
                posBuffer.longToday, posBuffer.longYd, posBuffer.shortToday, posBuffer.shortYd = self.getPosition(
                    vtSymbol)

            if req.direction == DIRECTION_LONG:
                print 'long'
                if posBuffer.shortYd >= req.volume:
                    print 'close shortYd'
                    req.offset = OFFSET_CLOSE
                else:
                    print 'open today'
                    req.offset = OFFSET_OPEN
            else:
                print 'short'
                if posBuffer.longYd >= req.volume:
                    print 'close longYd'
                    req.offset = OFFSET_CLOSE
                else:
                    print 'open today'
                    req.offset = OFFSET_OPEN

        vtOrderID = self.mainEngine.sendOrder(req, contract.gatewayName)
        self.orderStrategyDict[vtOrderID] = strategy
        if 'append_info' in kwargs:
            print kwargs['append_info']
            self.orderAppendInfoDict[vtOrderID] = kwargs['append_info']
        self.writeCtaLog(
            u'策略%s发送委托, %s, %s, %s@%s' %
            (strategy.name, vtSymbol, req.direction, volume, price))
        return vtOrderID
Exemplo n.º 10
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 contract.exchange != EXCHANGE_SHFE:
            if orderType == CTAORDER_BUY:
                req.direction = DIRECTION_LONG
                req.offset = OFFSET_OPEN
            elif orderType == CTAORDER_SHORT:
                req.direction = DIRECTION_SHORT
                req.offset = OFFSET_OPEN
            elif orderType == CTAORDER_SELL:
                req.direction = DIRECTION_SHORT
                req.offset = OFFSET_CLOSE
            elif orderType == CTAORDER_COVER:
                req.direction = DIRECTION_LONG
                req.offset = OFFSET_CLOSE

            return self.SubsendOrder(req,strategy,contract.gatewayName)

        # 针对可能发生的平昨、平今进行订单拆分
        else:
            if orderType == CTAORDER_BUY:
                req.direction = DIRECTION_LONG
                req.offset = OFFSET_OPEN

                return self.SubsendOrder(req,strategy,contract.gatewayName)

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

                return self.SubsendOrder(req,strategy,contract.gatewayName)

            elif orderType == CTAORDER_SELL:
                req.direction = DIRECTION_SHORT

                if strategy.posTD[vtSymbol]['long'] > 0 :
                    if volume <= strategy.posTD[vtSymbol]['long']:
                        req.offset = OFFSET_CLOSETODAY

                        return self.SubsendOrder(req,strategy,contract.gatewayName)

                    else:
                        req.volume = strategy.posTD[vtSymbol]['long']
                        req.offset = OFFSET_CLOSETODAY
                        vtOrderID1 = self.SubsendOrder(req,strategy,contract.gatewayName)
                        req.volume = volume - strategy.posTD[vtSymbol]['long']
                        req.offset = OFFSET_CLOSE
                        vtOrderID2 = self.SubsendOrder(req,strategy,contract.gatewayName)

                        return [vtOrderID1,vtOrderID2]
                elif strategy.posYD[vtSymbol]['long'] > 0 :
                    req.volume = volume
                    req.offset = OFFSET_CLOSE
                    return self.SubsendOrder(req,strategy,contract.gatewayName)
                else :
                    self.writeCtaLog(u'%s:下单委托,报单与当前持仓不匹配!' 
                         %(strategy.name))

            elif orderType == CTAORDER_COVER:
                req.direction = DIRECTION_LONG

                if strategy.posTD[vtSymbol]['short'] > 0:
                    if volume <= strategy.posTD[vtSymbol]['short']:
                        req.offset = OFFSET_CLOSETODAY

                        return self.SubsendOrder(req,strategy,contract.gatewayName)

                    else:
                        req.volume = strategy.posTD[vtSymbol]['short']
                        req.offset = OFFSET_CLOSETODAY
                        vtOrderID1 = self.SubsendOrder(req,strategy,contract.gatewayName)
                        req.volume = volume - strategy.posTD[vtSymbol]['short']
                        req.offset = OFFSET_CLOSE
                        vtOrderID2 = self.SubsendOrder(req,strategy,contract.gatewayName)

                        return [vtOrderID1,vtOrderID2]
                elif strategy.posYD[vtSymbol]['short'] > 0:
                    req.volume = volume
                    req.offset = OFFSET_CLOSE
                    return self.SubsendOrder(req,strategy,contract.gatewayName)
                else:
                    self.writeCtaLog(u'%s:下单委托,报单与当前持仓不匹配!' 
                         %(strategy.name))
Exemplo n.º 11
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
Exemplo n.º 12
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'火币账户钱不足无法执行买入单')