Example #1
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy):
        """发单"""
        self.limitOrderCount += 1
        orderID = str(self.limitOrderCount)

        order = VtOrderData()
        order.vtSymbol = vtSymbol
        order.price = self.roundToPriceTick(price)
        order.totalVolume = volume
        order.status = STATUS_NOTTRADED  # 刚提交尚未成交
        order.orderID = orderID
        order.vtOrderID = orderID
        order.orderTime = str(self.dt)

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

        # 保存到限价单字典中
        self.workingLimitOrderDict[orderID] = order
        self.limitOrderDict[orderID] = order

        return orderID
Example #2
0
 def sendOrder(self, vtSymbol, orderType, price, volume, strategy):
     """发单"""
     self.limitOrderCount += 1
     orderID = str(self.limitOrderCount)
     
     order = VtOrderData()
     order.vtSymbol = vtSymbol
     order.price = price
     order.totalVolume = volume
     order.status = STATUS_NOTTRADED     # 刚提交尚未成交
     order.orderID = orderID
     order.vtOrderID = orderID
     order.orderTime = str(self.dt)
     
     # CTA委托类型映射
     if orderType == CTAORDER_BUY:
         order.direction = DIRECTION_LONG
         order.offset = OFFSET_OPEN
     elif orderType == CTAORDER_SELL:
         order.direction = DIRECTION_SHORT
         order.offset = OFFSET_CLOSE
     elif orderType == CTAORDER_SHORT:
         order.direction = DIRECTION_SHORT
         order.offset = OFFSET_OPEN
     elif orderType == CTAORDER_COVER:
         order.direction = DIRECTION_LONG
         order.offset = OFFSET_CLOSE     
     
     # 保存到限价单字典中
     self.workingLimitOrderDict[orderID] = order
     self.limitOrderDict[orderID] = order
     
     return orderID
Example #3
0
    def sendOrder(self, vtSymbol, orderType, price, volume, strategy):
        """发单"""
        """send order"""

        self.limitOrderCount += 1
        orderID = str(self.limitOrderCount)

        order = VtOrderData()
        order.vtSymbol = vtSymbol
        order.price = price
        order.totalVolume = volume
        order.status = STATUS_NOTTRADED  # the status of new limit order is not traded
        order.orderID = orderID
        order.vtOrderID = orderID
        order.orderTime = str(self.dt)

        # CTA order type
        if orderType == CTAORDER_BUY:
            order.direction = DIRECTION_LONG
            order.offset = OFFSET_OPEN
        elif orderType == CTAORDER_SELL:
            order.direction = DIRECTION_SHORT
            order.offset = OFFSET_CLOSE
        elif orderType == CTAORDER_SHORT:
            order.direction = DIRECTION_SHORT
            order.offset = OFFSET_OPEN
        elif orderType == CTAORDER_COVER:
            order.direction = DIRECTION_LONG
            order.offset = OFFSET_CLOSE

        # record this order to "limit order dictionary" and "working limit order dictionary"
        self.workingLimitOrderDict[orderID] = order
        self.limitOrderDict[orderID] = order

        return orderID
    def crossStopOrder(self):
        """基于最新数据撮合停止单"""
        # 先确定会撮合成交的价格,这里和限价单规则相反
        if self.mode == self.BAR_MODE:
            buyCrossPrice = self.bar.high  # 若买入方向停止单价格低于该价格,则会成交
            sellCrossPrice = self.bar.low  # 若卖出方向限价单价格高于该价格,则会成交
        else:
            buyCrossPrice = self.tick.lastPrice
            sellCrossPrice = self.tick.lastPrice

        # 遍历限价单字典中的所有限价单
        for stopOrderID, so in self.workingStopOrderDict.items():
            # 判断是否会成交
            buyCross = so.direction == DIRECTION_LONG and so.price <= buyCrossPrice
            sellCross = so.direction == DIRECTION_SHORT and so.price >= sellCrossPrice

            # 如果发生了成交
            if buyCross or sellCross:
                # 推送成交数据
                self.tradeCount += 1  # 成交编号自增1
                tradeID = str(self.tradeCount)
                trade = VtTradeData()
                trade.vtSymbol = so.vtSymbol
                trade.tradeID = tradeID
                trade.vtTradeID = tradeID

                self.limitOrderCount += 1
                orderID = str(self.limitOrderCount)
                trade.orderID = orderID
                trade.vtOrderID = orderID

                trade.direction = so.direction
                trade.offset = so.offset
                trade.price = so.price
                trade.volume = so.volume
                trade.tradeTime = str(self.dt)
                self.strategy.onTrade(trade)

                self.tradeDict[tradeID] = trade

                # 推送委托数据
                so.status = STOPORDER_TRIGGERED

                order = VtOrderData()
                order.vtSymbol = so.vtSymbol
                order.symbol = so.vtSymbol
                order.orderID = orderID
                order.vtOrderID = orderID
                order.direction = so.direction
                order.offset = so.offset
                order.price = so.price
                order.totalVolume = so.volume
                order.tradedVolume = so.volume
                order.status = STATUS_ALLTRADED
                order.orderTime = trade.tradeTime
                self.strategy.onOrder(order)

                # 从字典中删除该限价单
                del self.workingStopOrderDict[stopOrderID]
Example #5
0
 def crossStopOrder(self):
     """基于最新数据撮合停止单"""
     # 先确定会撮合成交的价格,这里和限价单规则相反
     if self.mode == self.BAR_MODE:
         buyCrossPrice = self.bar.high    # 若买入方向停止单价格低于该价格,则会成交
         sellCrossPrice = self.bar.low    # 若卖出方向限价单价格高于该价格,则会成交
     else:
         buyCrossPrice = self.tick.lastPrice
         sellCrossPrice = self.tick.lastPrice
     
     # 遍历限价单字典中的所有限价单
     for stopOrderID, so in self.workingStopOrderDict.items():
         # 判断是否会成交
         buyCross = so.direction==DIRECTION_LONG and so.price<=buyCrossPrice
         sellCross = so.direction==DIRECTION_SHORT and so.price>=sellCrossPrice
         
         # 如果发生了成交
         if buyCross or sellCross:
             # 推送成交数据
             self.tradeCount += 1            # 成交编号自增1
             tradeID = str(self.tradeCount)
             trade = VtTradeData()
             trade.vtSymbol = so.vtSymbol
             trade.tradeID = tradeID
             trade.vtTradeID = tradeID
             
             self.limitOrderCount += 1
             orderID = str(self.limitOrderCount)
             trade.orderID = orderID
             trade.vtOrderID = orderID
             
             trade.direction = so.direction
             trade.offset = so.offset
             trade.price = so.price
             trade.volume = so.volume
             trade.tradeTime = str(self.dt)
             self.strategy.onTrade(trade)
             
             self.tradeDict[tradeID] = trade
             
             # 推送委托数据
             so.status = STOPORDER_TRIGGERED
             
             order = VtOrderData()
             order.vtSymbol = so.vtSymbol
             order.symbol = so.vtSymbol
             order.orderID = orderID
             order.vtOrderID = orderID
             order.direction = so.direction
             order.offset = so.offset
             order.price = so.price
             order.totalVolume = so.volume
             order.tradedVolume = so.volume
             order.status = STATUS_ALLTRADED
             order.orderTime = trade.tradeTime
             self.strategy.onOrder(order)
             
             # 从字典中删除该限价单
             del self.workingStopOrderDict[stopOrderID]        
    def crossStopOrder(self):
        """基于最新数据撮合停止单"""
        # 先确定会撮合成交的价格,这里和限价单规则相反
        if self.mode == self.BAR_MODE:
            buyCrossPrice = self.bar.high  # 若买入方向停止单价格低于该价格,则会成交
            sellCrossPrice = self.bar.low  # 若卖出方向限价单价格高于该价格,则会成交
            bestCrossPrice = self.bar.open  # 最优成交价,买入停止单不能低于,卖出停止单不能高于
        else:
            buyCrossPrice = self.tick.lastPrice
            sellCrossPrice = self.tick.lastPrice
            bestCrossPrice = self.tick.lastPrice

        # 遍历停止单字典中的所有停止单
        for stopOrderID, so in self.workingStopOrderDict[
                self.bar.symbol].items():
            # 判断是否会成交
            buyCross = so.direction == DIRECTION_LONG and so.price <= buyCrossPrice
            sellCross = so.direction == DIRECTION_SHORT and so.price >= sellCrossPrice

            # 如果发生了成交
            if buyCross or sellCross:
                # 推送成交数据
                self.tradeCount += 1  # 成交编号自增1
                tradeID = str(self.tradeCount)
                trade = VtTradeData()
                trade.vtSymbol = so.vtSymbol
                trade.tradeID = tradeID
                trade.vtTradeID = tradeID

                if buyCross:
                    self.strategyDict[self.bar.symbol].pos += so.volume
                    trade.price = max(bestCrossPrice, so.price)
                else:
                    self.strategyDict[self.bar.symbol].pos -= so.volume
                    trade.price = min(bestCrossPrice, so.price)

                self.limitOrderCount += 1
                orderID = str(self.limitOrderCount)
                trade.orderID = orderID
                trade.vtOrderID = orderID

                trade.direction = so.direction
                trade.offset = so.offset
                trade.volume = so.volume
                trade.tradeTime = str(self.dt)
                trade.dt = self.dt
                trade.symbol = self.bar.symbol
                stra = self.strategyDict[self.bar.symbol]
                stra.onTrade(trade)

                self.tradeDict[trade.vtSymbol][tradeID] = trade
                '''#在此处更新回测账户'''
                #print REDPREFIX + str(trade.symbol) + "\t"+  "%.1f"%trade.price + "\t" +\
                #        trade.direction + "\t" + trade.offset + "\t" + str(trade.volume)
                self.updateAccount(trade)

                # 推送委托数据
                so.status = STOPORDER_TRIGGERED

                order = VtOrderData()
                order.vtSymbol = so.vtSymbol
                order.symbol = so.vtSymbol
                order.orderID = orderID
                order.vtOrderID = orderID
                order.direction = so.direction
                order.offset = so.offset
                order.price = so.price
                order.totalVolume = so.volume
                order.tradedVolume = so.volume
                order.status = STATUS_ALLTRADED
                order.orderTime = trade.tradeTime

                stra = self.strategyDict[self.bar.symbol]
                stra.onOrder(order)

                self.limitOrderDict[order.symbol][orderID] = order

                # 从字典中删除该限价单
                del self.workingStopOrderDict[order.symbol][stopOrderID]