Beispiel #1
0
    def crossLimitOrder(self):
        """基于最新数据撮合限价单"""
        # 先确定会撮合成交的价格
        if self.mode == self.BAR_MODE:
            buyCrossPrice = self.bar.low  # 若买入方向限价单价格高于该价格,则会成交
            sellCrossPrice = self.bar.high  # 若卖出方向限价单价格低于该价格,则会成交
            buyBestCrossPrice = self.bar.open  # 在当前时间点前发出的买入委托可能的最优成交价
            sellBestCrossPrice = self.bar.open  # 在当前时间点前发出的卖出委托可能的最优成交价
        else:
            buyCrossPrice = self.tick.askPrice1
            sellCrossPrice = self.tick.bidPrice1
            buyBestCrossPrice = self.tick.askPrice1
            sellBestCrossPrice = self.tick.bidPrice1

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

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

                # 以买入为例:
                # 1. 假设当根K线的OHLC分别为:100, 125, 90, 110
                # 2. 假设在上一根K线结束(也是当前K线开始)的时刻,策略发出的委托为限价105
                # 3. 则在实际中的成交价会是100而不是105,因为委托发出时市场的最优价格是100
                if buyCross:
                    trade.price = min(order.price, buyBestCrossPrice)
                    self.strategy.pos += order.totalVolume
                else:
                    trade.price = max(order.price, sellBestCrossPrice)
                    self.strategy.pos -= order.totalVolume

                trade.volume = order.totalVolume
                trade.tradeTime = str(self.dt)
                trade.dt = self.dt
                self.strategy.onTrade(trade)

                self.tradeDict[tradeID] = trade

                # 推送委托数据
                order.tradedVolume = order.totalVolume
                order.status = STATUS_ALLTRADED
                self.strategy.onOrder(order)

                # 从字典中删除该限价单
                del self.workingLimitOrderDict[orderID]
Beispiel #2
0
 def crossLimitOrder(self):
     """基于最新数据撮合限价单"""
     # 先确定会撮合成交的价格
     if self.mode == self.BAR_MODE:
         buyCrossPrice = self.bar.low        # 若买入方向限价单价格高于该价格,则会成交
         sellCrossPrice = self.bar.high      # 若卖出方向限价单价格低于该价格,则会成交
         buyBestCrossPrice = self.bar.open   # 在当前时间点前发出的买入委托可能的最优成交价
         sellBestCrossPrice = self.bar.open  # 在当前时间点前发出的卖出委托可能的最优成交价
     else:
         buyCrossPrice = self.tick.askPrice1
         sellCrossPrice = self.tick.bidPrice1
         buyBestCrossPrice = self.tick.askPrice1
         sellBestCrossPrice = self.tick.bidPrice1
     
     # 遍历限价单字典中的所有限价单
     for orderID, order in self.workingLimitOrderDict.items():
         # 判断是否会成交
         buyCross = order.direction==DIRECTION_LONG and order.price>=buyCrossPrice
         sellCross = order.direction==DIRECTION_SHORT and order.price<=sellCrossPrice
         
         # 如果发生了成交
         if buyCross or sellCross:
             # 推送成交数据
             self.tradeCount += 1            # 成交编号自增1
             tradeID = str(self.tradeCount)
             trade = VtTradeData()
             trade.vtSymbol = order.vtSymbol
             trade.tradeID = tradeID
             trade.vtTradeID = tradeID
             trade.orderID = order.orderID
             trade.vtOrderID = order.orderID
             trade.direction = order.direction
             trade.offset = order.offset
             
             # 以买入为例:
             # 1. 假设当根K线的OHLC分别为:100, 125, 90, 110
             # 2. 假设在上一根K线结束(也是当前K线开始)的时刻,策略发出的委托为限价105
             # 3. 则在实际中的成交价会是100而不是105,因为委托发出时市场的最优价格是100
             if buyCross:
                 trade.price = min(order.price, buyBestCrossPrice)
                 self.strategy.pos += order.totalVolume
             else:
                 trade.price = max(order.price, sellBestCrossPrice)
                 self.strategy.pos -= order.totalVolume
             
             trade.volume = order.totalVolume
             trade.tradeTime = str(self.dt)
             trade.dt = self.dt
             self.strategy.onTrade(trade)
             
             self.tradeDict[tradeID] = trade
             
             # 推送委托数据
             order.tradedVolume = order.totalVolume
             order.status = STATUS_ALLTRADED
             self.strategy.onOrder(order)
             
             # 从字典中删除该限价单
             del self.workingLimitOrderDict[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]
Beispiel #4
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]        
Beispiel #5
0
    def crossLimitOrder(self):
        """基于最新数据撮合限价单"""
        # 先确定会撮合成交的价格
        if self.mode == self.BAR_MODE:
            buyCrossPrice = self.bar.low  # 若买入方向限价单价格高于该价格,则会成交
            sellCrossPrice = self.bar.high  # 若卖出方向限价单价格低于该价格,则会成交
        else:
            buyCrossPrice = self.tick.lastPrice
            sellCrossPrice = self.tick.lastPrice

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

            # 如果发生了成交
            if buyCross or sellCross:
                # 推送成交数据
                self.tradeCount += 1  # 成交编号自增1
                tradeID = str(self.tradeCount)
                trade = VtTradeData()
                trade.vtSymbol = order.vtSymbol
                trade.tradeID = tradeID
                trade.vtTradeID = tradeID
                trade.orderID = order.orderID
                trade.vtOrderID = order.orderID
                trade.direction = order.direction
                trade.offset = order.offset
                trade.price = order.price
                trade.volume = order.totalVolume
                trade.tradeTime = str(self.dt)
                trade.dt = self.dt
                self.strategy.onTrade(trade)

                self.tradeDict[tradeID] = trade

                # 推送委托数据
                order.tradedVolume = order.totalVolume
                order.status = STATUS_ALLTRADED
                self.strategy.onOrder(order)

                # 从字典中删除该限价单
                del self.workingLimitOrderDict[orderID]
    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]
Beispiel #7
0
 def crossLimitOrder(self):
     """基于最新数据撮合限价单"""
     if not self.tradeCount:
         output=open('trade.log','w')
     # 先确定会撮合成交的价格
     if self.mode == self.BAR_MODE:
         buyCrossPrice = self.bar.low    # 若买入方向限价单价格高于该价格,则会成交
         sellCrossPrice = self.bar.high  # 若卖出方向限价单价格低于该价格,则会成交
         bestCrossPrice = self.bar.open  # 在当前时间点前发出的委托可能的最优成交价
     else:
         buyCrossPrice = self.tick.lastPrice
         sellCrossPrice = self.tick.lastPrice
         bestCrossPrice = self.tick.lastPrice
     
     # 遍历限价单字典中的所有限价单
     for orderID, order in self.workingLimitOrderDict.items():
         # 判断是否会成交
         buyCross = order.direction==DIRECTION_LONG and order.price>=buyCrossPrice
         sellCross = order.direction==DIRECTION_SHORT and order.price<=sellCrossPrice
         
         # 如果发生了成交
         if buyCross or sellCross:
             # 推送成交数据
             self.tradeCount += 1            # 成交编号自增1
             tradeID = str(self.tradeCount)
             trade = VtTradeData()
             trade.vtSymbol = order.vtSymbol
             trade.tradeID = tradeID
             trade.vtTradeID = tradeID
             trade.orderID = order.orderID
             trade.vtOrderID = order.orderID
             trade.direction = order.direction
             trade.offset = order.offset
             
             # 以买入为例:
             # 1. 假设当根K线的OHLC分别为:100, 125, 90, 110
             # 2. 假设在上一根K线结束(也是当前K线开始)的时刻,策略发出的委托为限价105
             # 3. 则在实际中的成交价会是100而不是105,因为委托发出时市场的最优价格是100
             if buyCross:
                 trade.price = order.price
                 self.strategy.pos += order.totalVolume
             else:
                 trade.price = order.price
                 self.strategy.pos -= order.totalVolume
             
             trade.volume = order.totalVolume
             trade.tradeTime = str(self.dt)
             trade.dt = self.dt
             self.strategy.onTrade(trade)
             
             self.tradeDict[tradeID] = trade
             
             output=open('trade.log','a')
             output.write(u'策略%s发送委托,%s,%s,%s,%s@%s' 
                      %('Turtle',trade.dt, trade.vtSymbol, trade.direction, trade.volume, trade.price)+'\n')
             output.close
             # 推送委托数据
             order.tradedVolume = order.totalVolume
             order.status = STATUS_ALLTRADED
             self.strategy.onOrder(order)
             
             # 从字典中删除该限价单
             del self.workingLimitOrderDict[orderID]
Beispiel #8
0
    def crossLimitOrder(self):
        """基于最新数据撮合限价单"""
        """Check LimitOrder base on newest market data"""

        # 先确定会撮合成交的价格
        if self.mode == self.BAR_MODE:
            buyCrossPrice = self.bar.low  # 若买入方向限价单价格高于该价格,则会成交
            sellCrossPrice = self.bar.high  # 若卖出方向限价单价格低于该价格,则会成交
            buyBestCrossPrice = self.bar.open  # 在当前时间点前发出的买入委托可能的最优成交价
            sellBestCrossPrice = self.bar.open  # 在当前时间点前发出的卖出委托可能的最优成交价
        else:
            buyCrossPrice = self.tick.askPrice1
            sellCrossPrice = self.tick.bidPrice1
            buyBestCrossPrice = self.tick.askPrice1
            sellBestCrossPrice = self.tick.bidPrice1

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

            # 如果发生了成交
            # If transaction happens
            if buyCross or sellCross:
                # 推送成交数据
                # Update trade data
                self.tradeCount += 1  # TradeID increase by 1
                tradeID = str(self.tradeCount)
                trade = VtTradeData()
                trade.vtSymbol = order.vtSymbol
                trade.tradeID = tradeID
                trade.vtTradeID = tradeID
                trade.orderID = order.orderID
                trade.vtOrderID = order.orderID
                trade.direction = order.direction
                trade.offset = order.offset

                # Buy as example:
                # 1. Suppose the OHLC of current Bar are 100, 125, 90, 110 (Open = 100)
                # 2. Suppose at the end of last Bar(not the start of current Bar), the price of limit order is 105,
                #    (Last Close = 105)
                # 3. In real trading, the trade price will be 100 instead of 105, because the best market price is 100
                if buyCross:
                    trade.price = min(order.price, buyBestCrossPrice)
                    self.strategy.pos += order.totalVolume
                else:
                    trade.price = max(order.price, sellBestCrossPrice)
                    self.strategy.pos -= order.totalVolume

                trade.volume = order.totalVolume
                trade.tradeTime = str(self.dt)
                trade.dt = self.dt
                self.strategy.onTrade(trade)

                self.tradeDict[tradeID] = trade

                # 推送委托数据
                # Upadte order data
                order.tradedVolume = order.totalVolume
                order.status = STATUS_ALLTRADED
                self.strategy.onOrder(order)

                # 从字典中删除该限价单
                # Remove this order from "working limit order dictionary"
                del self.workingLimitOrderDict[orderID]