Пример #1
0
    def sendOrder(self, vtSymbol, orderType, price, volume, marketPrice,
                  strategy):
        """发单"""

        contract = self.mainEngine.getContract(vtSymbol)
        req = VtOrderReq()

        req.symbol = contract.symbol
        req.contractType = req.symbol[4:]
        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
        req.bystrategy = strategy.name

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

        req.priceType = marketPrice  #OKEX 用number作priceType

        if marketPrice:
            type_shown = PRICETYPE_MARKETPRICE
        else:
            type_shown = 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('策略%s: 发送%s委托%s, 交易:%s,%s,数量:%s @ %s' %
                         (strategy.name, type_shown, vtOrderID, vtSymbol,
                          orderType, volume, price))

        return vtOrderIDList
Пример #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.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))
        flt = {'name': strategy.name, 'vtSymbol': strategy.vtSymbol}
        d = copy(flt)
        d['order_time'] = datetime.now()
        d['direction'] = req.direction
        d['volume'] = volume
        d['price'] = price
        self.mainEngine.dbInsert(ORDER_DB_NAME, strategy.name, d)  #记录每次下单的情况
        return vtOrderIDList
Пример #3
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

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

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

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

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

        elif orderType == MMKORDER_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.writeMmkLog(
            u'策略%s发送委托,%s,%s,%s@%s' %
            (strategy.name, vtSymbol, req.direction, volume, price))

        return vtOrderIDList
Пример #4
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
Пример #5
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy):
        """发单"""
        contract = self.mainEngine.getContract(vtSymbol)

        if contract.gatewayName == "IB":
            return self.sendIBOrder(vtSymbol, orderType, price, volume,
                                    strategy, False)

        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

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

        # ARB委托类型映射
        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_SELL_TODAY:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_CLOSETODAY
        elif orderType == CTAORDER_SHORT:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_OPEN
        elif orderType == CTAORDER_COVER:
            req.direction = DIRECTION_LONG
            req.offset = OFFSET_CLOSE
        elif orderType == CTAORDER_COVER_TODAY:
            req.direction = DIRECTION_LONG
            req.offset = OFFSET_CLOSETODAY

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

        self.writeArbLog(
            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,
                    isMktOrder=False):
        """IB 发单"""
        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

        if isMktOrder:
            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)  # 发单
        self.orderStrategyDict[vtOrderID] = strategy  # 保存vtOrderID和策略的映射关系

        self.writeArbLog(
            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 = 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(
            '策略%s发送委托,%s,%s,%s@%s' %
            (strategy.name, vtSymbol, req.direction, volume, price))

        return vtOrderID
Пример #8
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  # 数量

        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
Пример #9
0
    def sendMarketOrder(self, vtSymbol, orderType, price, volume, strategy):
        """发单"""
        contract = self.mainEngine.getContract(vtSymbol)
        req = VtOrderReq()
        req.symbol = contract.symbol
        req.exchange = contract.exchange

        # print req.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
        req.priceType = PRICETYPE_MARKETPRICE     # 需要增加一个市价单的方法。或者看懂停止单
        msg_str = ''

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

        elif orderType == CTAORDER_SELL:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_CLOSE
            msg_str = '平昨多'

        elif orderType == CTAORDER_SELL_TODAY:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_CLOSETODAY
            msg_str = '平今多'

        elif orderType == CTAORDER_SHORT:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_OPEN
            msg_str = '做空'

        elif orderType == CTAORDER_COVER:
            req.direction = DIRECTION_LONG
            req.offset = OFFSET_CLOSE
            msg_str = '平昨空'

        elif orderType == CTAORDER_COVER_TODAY:
            req.direction = DIRECTION_LONG
            req.offset = OFFSET_CLOSETODAY
            msg_str = '平今空'

        # 委托转换
        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, msg_str, volume, price))

        # self.send_ding_msg(u'策略%s发送委托,%s,%s,%s@%s'
        #                  %(strategy.name, vtSymbol, msg_str, volume, price))

        # if contract.gatewayName == 'OKEX':
        #     account_right = self.query_account().info[vtSymbol[:3].lower()]['account_rights']
        # balance = round(account_right * price, 2)
        # weixin = WeixinMessage()
        # result = weixin.send(contract.gatewayName, strategy.name, vtSymbol,
        #                  '%s%s张$%s,账户权益%s≈$%s,%s' %
        #                  (msg_str, volume, price, account_right,
        #                   balance, datetime.now().strftime('%Y-%m-%d %H:%M:%S')),
        #                  self.user_list)
        # self.writeCtaLog('weixin.send: %s' % result)
        # else:
        #     account_right = self.query_account().info[vtSymbol[:3].lower()]['account_rights']

        return vtOrderIDList
Пример #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 = 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
Пример #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.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[req.symbol + strategy.className].add(
                vtOrderID)  # 添加到策略委托号集合中
            vtOrderIDList.append(vtOrderID)

            ##写数据库
            traderOrd = TraderOrder()
            traderOrd.orderID = vtOrderID
            traderOrd.orderUuid = datetime.now().strftime(
                "%Y%m%d%H%M%S") + vtOrderID
            traderOrd.symbol = contract.symbol
            traderOrd.strategyName = strategy.className
            traderOrd.direction = req.direction
            traderOrd.offset = req.offset
            traderOrd.orderVolume = convertedReq.volume
            traderOrd.orderPrice = convertedReq.price

            self.mainEngine.mysqlClient.dbInsert(SQL_TABLENAME_TRADER,
                                                 traderOrd)

            if traderOrd.offset == OFFSET_OPEN:
                self.lastOpenTrade[traderOrd.symbol] = traderOrd

            self.traderDict[vtOrderID] = traderOrd

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

        return vtOrderIDList
Пример #12
0
    def sendOrder(self, vtSymbol, orderType, price, volume, priceType,
                  strategy):
        """发单"""

        contract = self.mainEngine.getContract(vtSymbol)
        req = VtOrderReq()
        reqList = []

        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
        req.byStrategy = strategy.name
        req.priceType = priceType

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

        elif orderType == CTAORDER_SELL:
            req.direction = constant.DIRECTION_SHORT
            # 只有上期所才要考虑平今平昨
            if contract.exchange != constant.EXCHANGE_SHFE:
                req.offset = constant.OFFSET_CLOSE
            else:
                # 获取持仓缓存数据
                posBuffer = self.ydPositionDict.get(vtSymbol + '_LONG', None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    self.writeCtaLog(u'获取昨持多仓为0, 发出平今指令')
                    req.offset = constant.OFFSET_CLOSETODAY

                elif posBuffer:
                    if volume <= posBuffer:
                        req.offset = constant.OFFSET_CLOSE
                        self.writeCtaLog(
                            f'{vtSymbol}优先平昨, 昨多仓:{posBuffer}, 平仓数:{volume}')
                        req.offset = constant.OFFSET_CLOSE
                        if (posBuffer - volume) > 0:
                            self.writeCtaLog(
                                f'{vtSymbol}剩余昨多仓{(posBuffer - volume)}')
                    else:
                        req.offset = constant.OFFSET_CLOSE
                        req.volume = posBuffer
                        self.writeCtaLog(
                            f'{vtSymbol}平仓量{volume}, 大于昨多仓, 拆分优先平昨仓数:{posBuffer}'
                        )
                        req2 = copy(req)
                        req2.offset = constant.OFFSET_CLOSETODAY
                        req2.volume = volume - posBuffer
                        self.writeCtaLog(
                            f'{vtSymbol}平仓量大于昨多仓, 拆分到平今仓数:{req2.volume}')
                        reqList.append(req2)

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

        elif orderType == CTAORDER_COVER:
            req.direction = constant.DIRECTION_LONG
            # # 只有上期所才要考虑平今平昨
            if contract.exchange != constant.EXCHANGE_SHFE:
                req.offset = constant.OFFSET_CLOSE
            else:
                # 获取持仓缓存数据
                posBuffer = self.ydPositionDict.get(vtSymbol + '_SHORT', None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    self.writeCtaLog(u'获取昨持空仓为0, 发出平今指令')
                    req.offset = constant.OFFSET_CLOSETODAY

                elif posBuffer:
                    if volume <= posBuffer:
                        req.offset = constant.OFFSET_CLOSE
                        self.writeCtaLog(
                            f'{vtSymbol}优先平昨,昨空仓:{posBuffer}, 平仓数:{volume}')
                        req.offset = constant.OFFSET_CLOSE
                        if (posBuffer - volume) > 0:
                            self.writeCtaLog(
                                f'{vtSymbol}剩余昨空仓{(posBuffer - volume)}')
                    else:
                        req.offset = constant.OFFSET_CLOSE
                        req.volume = posBuffer
                        self.writeCtaLog(
                            f'{vtSymbol}平仓量{volume}, 大于昨空仓, 拆分优先平昨仓数:{posBuffer}'
                        )
                        req2 = copy(req)
                        req2.offset = constant.OFFSET_CLOSETODAY
                        req2.volume = volume - posBuffer
                        self.writeCtaLog(
                            f'{vtSymbol}平仓量大于昨空仓, 拆分到平今仓数:{req2.volume}')
                        reqList.append(req2)

        # 委托转换
        # reqList = self.mainEngine.convertOrderReq(req) # 不转了

        reqList.append(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('策略%s: 发送%s委托%s, 交易:%s,%s,数量:%s @ %s' %
                             (strategy.name, priceType, vtOrderID, vtSymbol,
                              orderType, volume, price))

        return vtOrderIDList
Пример #13
0
    def sendOrder(self, vtSymbol, orderType, price, volume, priceType, strategy):
        """发单"""
        
        contract = self.mainEngine.getContract(vtSymbol)
        req = VtOrderReq()
        reqcount = 1 
        
        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
        req.byStrategy = strategy.name

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

        req.priceType = priceType
        

        # 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.ydPositionDict.get(vtSymbol+'_LONG', None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    self.writeCtaLog(u'获取昨持多仓为0,发出平今指令')
                    req.offset = OFFSET_CLOSETODAY

                elif posBuffer:
                    if volume <= posBuffer:
                        req.offset = OFFSET_CLOSE
                        self.writeCtaLog(u'{}优先平昨,昨多仓:{},平仓数:{}'.format(vtSymbol, posBuffer, volume))
                        req.offset = OFFSET_CLOSE
                        if (posBuffer - volume)>0:
                            self.writeCtaLog(u'{}剩余昨多仓{}'.format(vtSymbol,(posBuffer - volume)))
                    else:
                        req.offset = OFFSET_CLOSE
                        req.volume = posBuffer
                        self.writeCtaLog(u'{}平仓量{},大于昨多仓,拆分优先平昨仓数:{}'.format(vtSymbol, volume, posBuffer))
                        req2 = copy(req)
                        req2.offset = OFFSET_CLOSETODAY
                        req2.volume = volume - posBuffer
                        self.writeCtaLog(u'{}平仓量大于昨多仓,拆分到平今仓数:{}'.format(vtSymbol, req2.volume))
                        reqcount = 2

        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.ydPositionDict.get(vtSymbol+'_SHORT', None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    self.writeCtaLog(u'获取昨持空仓为0,发出平今指令')
                    req.offset = OFFSET_CLOSETODAY

                elif posBuffer:
                    if volume <= posBuffer:
                        req.offset = OFFSET_CLOSE
                        self.writeCtaLog(u'{}优先平昨,昨空仓:{},平仓数:{}'.format(vtSymbol, posBuffer, volume))
                        req.offset = OFFSET_CLOSE
                        if (posBuffer - volume)>0:
                            self.writeCtaLog(u'{}剩余昨空仓{}'.format(vtSymbol,(posBuffer - volume)))
                    else:
                        req.offset = OFFSET_CLOSE
                        req.volume = posBuffer
                        self.writeCtaLog(u'{}平仓量{},大于昨空仓,拆分优先平昨仓数:{}'.format(vtSymbol, volume, posBuffer))
                        req2 = copy(req)
                        req2.offset = OFFSET_CLOSETODAY
                        req2.volume = volume - posBuffer
                        self.writeCtaLog(u'{}平仓量大于昨空仓,拆分到平今仓数:{}'.format(vtSymbol, req2.volume))
                        reqcount = 2

        # 委托转换
        # reqList = self.mainEngine.convertOrderReq(req) # 不转了

        if reqcount == 1:
            reqList = [req]
        else:
            reqList = [req,req2]

        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('策略%s: 发送%s委托%s, 交易:%s,%s,数量:%s @ %s'
                         %(strategy.name, priceType, vtOrderID, vtSymbol, orderType, volume, price ))

        return vtOrderIDList
Пример #14
0
    def sendOrder(self, vtSymbol, orderType, price, volume, priceType,
                  strategy):
        """发单
        cta引擎之中所有的操作都是基于引擎的,具体数据流为 strategy --->ctatemple----->ctaenging
        在ctaenging 之中进行四个交易方向的order 分别为"买开" "卖开" "买平" "卖平"
        这块是非常重要的,首先在存储的reqorder list 列表之中进行循环,调用底层接口进行发单,返回vtOrder;维护两个列表 orderStrategyDict[vtOrderID]
        保存vtOrderID和strategy对象映射的字典(用于推送order和trade数据)   key为vtOrderID,value为strategy对象; 保存策略名称和委托号列表的字典  
        key为name,value为保存orderID(限价+本地停止)的集合
        """

        contract = self.mainEngine.getContract(vtSymbol)
        req = VtOrderReq()
        reqcount = 1

        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
        req.byStrategy = strategy.name

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

        # CTA委托类型映射
        """
        cta策略底层委托映射 可以根据传入的ordertype求出来相应的direction 和 offset,进而判断开平仓方向
        注意这里使用的bitfinex 由于bitfinex gateway api 没有开平,所以需要在gateway 之中进行定义转换
        
        """
        if orderType == CTAORDER_BUY:
            req.direction = constant.DIRECTION_LONG
            req.offset = constant.OFFSET_OPEN

        elif orderType == CTAORDER_SELL:
            req.direction = constant.DIRECTION_SHORT
            # 只有上期所才要考虑平今平昨,上期所映射
            if contract.exchange != constant.EXCHANGE_SHFE:
                req.offset = constant.OFFSET_CLOSE
            else:
                # 获取持仓缓存数据
                posBuffer = self.ydPositionDict.get(vtSymbol + '_LONG', None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    self.writeCtaLog(u'获取昨持多仓为0,发出平今指令')
                    req.offset = constant.OFFSET_CLOSETODAY

                elif posBuffer:
                    if volume <= posBuffer:
                        req.offset = constant.OFFSET_CLOSE
                        self.writeCtaLog(u'{}优先平昨,昨多仓:{},平仓数:{}'.format(
                            vtSymbol, posBuffer, volume))
                        req.offset = constant.OFFSET_CLOSE
                        if (posBuffer - volume) > 0:
                            self.writeCtaLog(u'{}剩余昨多仓{}'.format(
                                vtSymbol, (posBuffer - volume)))
                    else:
                        req.offset = constant.OFFSET_CLOSE
                        req.volume = posBuffer
                        self.writeCtaLog(u'{}平仓量{},大于昨多仓,拆分优先平昨仓数:{}'.format(
                            vtSymbol, volume, posBuffer))
                        req2 = copy(req)
                        req2.offset = constant.OFFSET_CLOSETODAY
                        req2.volume = volume - posBuffer
                        self.writeCtaLog(u'{}平仓量大于昨多仓,拆分到平今仓数:{}'.format(
                            vtSymbol, req2.volume))
                        reqcount = 2

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

        elif orderType == CTAORDER_COVER:
            req.direction = constant.DIRECTION_LONG
            # # 只有上期所才要考虑平今平昨
            if contract.exchange != constant.EXCHANGE_SHFE:
                req.offset = constant.OFFSET_CLOSE
            else:
                # 获取持仓缓存数据
                posBuffer = self.ydPositionDict.get(vtSymbol + '_SHORT', None)
                # 如果获取持仓缓存失败,则默认平昨
                if not posBuffer:
                    self.writeCtaLog(u'获取昨持空仓为0,发出平今指令')
                    req.offset = constant.OFFSET_CLOSETODAY

                elif posBuffer:
                    if volume <= posBuffer:
                        req.offset = constant.OFFSET_CLOSE
                        self.writeCtaLog(u'{}优先平昨,昨空仓:{},平仓数:{}'.format(
                            vtSymbol, posBuffer, volume))
                        req.offset = constant.OFFSET_CLOSE
                        if (posBuffer - volume) > 0:
                            self.writeCtaLog(u'{}剩余昨空仓{}'.format(
                                vtSymbol, (posBuffer - volume)))
                    else:
                        req.offset = constant.OFFSET_CLOSE
                        req.volume = posBuffer
                        self.writeCtaLog(u'{}平仓量{},大于昨空仓,拆分优先平昨仓数:{}'.format(
                            vtSymbol, volume, posBuffer))
                        req2 = copy(req)
                        req2.offset = constant.OFFSET_CLOSETODAY
                        req2.volume = volume - posBuffer
                        self.writeCtaLog(u'{}平仓量大于昨空仓,拆分到平今仓数:{}'.format(
                            vtSymbol, req2.volume))
                        reqcount = 2

        # 委托转换
        # reqList = self.mainEngine.convertOrderReq(req) # 不转了

        if reqcount == 1:
            reqList = [req]
        else:
            reqList = [req, req2]

        vtOrderIDList = []  # 维系一个列表  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('策略%s: 发送%s委托%s, 交易:%s,%s,数量:%s @ %s' %
                             (strategy.name, priceType, vtOrderID, vtSymbol,
                              orderType, volume, price))

        return vtOrderIDList
Пример #15
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy):
        """发单"""
        print(orderType)
        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  #PRICETYPE_MARKETPRICE

        direction = 'LONG'
        offset = 'OPEN'
        # 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
            #req.priceType = PRICETYPE_MARKETPRICE
            direction = 'SHORT'
            offset = 'CLOSE'

        elif orderType == CTAORDER_SHORT:
            req.direction = DIRECTION_SHORT
            req.offset = OFFSET_OPEN
            direction = 'SHORT'
            offset = 'OPEN'

        elif orderType == CTAORDER_COVER:
            req.direction = DIRECTION_LONG
            req.offset = OFFSET_CLOSE
            #req.priceType = PRICETYPE_MARKETPRICE
            direction = 'LONG'
            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@%s,%s' %
                         (strategy.name, vtSymbol, req.direction, req.offset,
                          volume, price, vtOrderID))

        date = dt.datetime.now().strftime("%Y%m%d")
        time = dt.datetime.now().strftime("%H:%M:%S")

        dict1 = {
            'symbol': req.symbol,
            'date': date,
            'time': time,
            'price': req.price,
            'volume': 1,
            'direction': direction,
            'offset': offset
        }
        self.mainEngine.dbUpdateTradeRecord(Trade_DB_NAME, strategy.className,
                                            dict1)
        #addTradeRecord(req) req.symbol,req.vtSymbol,req.offset,req.direction,req.price,req.volume

        return vtOrderIDList