def make_market(self, bars): """ 价格撮合""" #for order in self._open_orders: #print order.id fill_orders = set() for order in self._open_orders: if order.side == TradeSide.CANCEL: transact = Transaction(order) self.events.put(FillEvent(transact)) fill_orders.add(order) continue try: bar = bars[order.contract] except KeyError: logger.error('所交易的合约[%s]数据不存在' % order.contract) continue transact = Transaction(order) if self._strict: if order.price_type == PriceType.LMT: # 限价单以最高和最低价格为成交的判断条件. if (order.side == TradeSide.KAI and \ (order.direction == Direction.LONG and order.price >= bar.low or \ order.direction == Direction.SHORT and order.price <= bar.high)) or \ (order.side == TradeSide.PING and \ (order.direction == Direction.LONG and order.price <= bar.high or \ order.direction == Direction.SHORT and order.price >= bar.low)): transact.price = order.price # Bar的结束时间做为交易成交时间. transact.datetime = bar.datetime fill_orders.add(order) self.events.put(FillEvent(transact)) elif order.price_type == PriceType.MKT: # 市价单以最高或最低价格为成交价格. if order.side == TradeSide.KAI: if order.direction == Direction.LONG: transact.price = bar.high else: transact.price = bar.low elif order.side == TradeSide.PING: if order.direction == Direction.LONG: transact.price = bar.low else: transact.price = bar.high transact.datetime = bar.datetime fill_orders.add(order) self.events.put(FillEvent(transact)) else: transact.datetime = bar.datetime fill_orders.add(order) # self.events.put(FillEvent(transact)) # end of for if fill_orders: self._open_orders -= fill_orders
def make_market(self, bars): """ 价格撮合""" fill_orders = set() for order in self._open_orders: try: bar = bars[order.contract] except KeyError: logger.error('所交易的合约[%s]数据不存在' % order.contract) continue transact = Transaction(order) if self._strict: if order.price_type == PriceType.LMT: # 限价单以最高和最低价格为成交的判断条件. if (order.side == TradeSide.KAI and \ (order.direction == Direction.LONG and order.price >= bar.low or \ order.direction == Direction.SHORT and order.price <= bar.high)) or \ (order.side == TradeSide.PING and \ (order.direction == Direction.LONG and order.price <= bar.high or \ order.direction == Direction.SHORT and order.price >= bar.low)): transact.price = order.price # Bar的结束时间做为交易成交时间. transact.datetime = bar.datetime fill_orders.add(order) self.events.put(FillEvent(transact)) elif order.price_type == PriceType.MKT: # 市价单以最高或最低价格为成交价格. if order.side == TradeSide.KAI: if order.direction == Direction.LONG: transact.price = bar.high else: transact.price = bar.low elif order.side == TradeSide.PING: if order.direction == Direction.LONG: transact.price = bar.low else: transact.price = bar.high transact.datetime = bar.datetime fill_orders.add(order) self.events.put(FillEvent(transact)) else: transact.datetime = bar.datetime fill_orders.add(order) # self.events.put(FillEvent(transact)) # end of for if fill_orders: self._open_orders -= fill_orders
def _force_close(self): """ 在回测的最后一根k线以close价格强平持仓位。 """ force_trans = [] if self._all_transactions: price_type = self._all_transactions[-1].price_type else: price_type = PriceType.LMT for pos in self.positions.values(): order = Order( self._datetime, pos.contract, price_type, TradeSide.PING, pos.direction, self._bars[pos.contract].close, pos.quantity ) force_trans.append(Transaction(order)) for trans in force_trans: self._update_holding(trans) self._update_positions(trans) if force_trans: self.update_status(trans.datetime, False, False) self.positions = {} return
def make_market(self, bar): ## @bug 开仓资金是否足够的验证 ## @todo """ 价格撮合""" if self._open_orders: fill_orders = set() for order in self._open_orders: transact = Transaction(order) if self._strict: if order.price_type == PriceType.LMT: # 限价单以最高和最低价格为成交的判断条件. if (order.side == TradeSide.KAI and \ (order.direction == Direction.LONG and order.price >= bar.low or \ order.direction == Direction.SHORT and order.price <= bar.high)) or \ (order.kpp == TradeSide.PING and \ (order.direction == Direction.LONG and order.price <= bar.high or \ order.direction == Direction.SHORT and order.price >= bar.low)): transact.price = order.price # Bar的结束时间做为交易成交时间. transact.datetime = bar.datetime fill_orders.add(order) self.events.put(FillEvent(transact)) elif order.type == PriceType.MKT: # 市价单以最高或最低价格为成交价格. if order.side == TradeSide.KAI: if order.direction == Direction.LONG: transact.price = bar.high else: transact.price = bar.low elif order.side == TradeSide.PING: if order.direction == Direction.LONG: transact.price = bar.low else: transact.price = bar.high transact.datetime = bar.datetime fill_orders.add(order) self.events.put(FillEvent(transact)) else: transact.datetime = bar.datetime fill_orders.add(order) # self.events.put(FillEvent(transact)) if fill_orders: self._open_orders -= fill_orders
def make_market(self, bars, at_baropen): """ 价格撮合""" if len(self._open_orders) == 0: return fill_orders = set() for order in self._open_orders: if order.side == TradeSide.CANCEL: fill_orders.add(order) transact = Transaction(order) self.events.put(FillEvent(transact)) continue try: bar = bars[order.contract] except KeyError: log.error('所交易的合约[%s]数据不存在' % order.contract) continue transact = Transaction(order) if self._strict: if at_baropen: if order.price_type == PriceType.LMT: price = bar.open if (order.side == TradeSide.OPEN and \ (order.direction == Direction.LONG and order.price >= price or \ order.direction == Direction.SHORT and order.price <= price)) or \ (order.side == TradeSide.CLOSE and \ (order.direction == Direction.LONG and order.price <= price or \ order.direction == Direction.SHORT and order.price >= price)): transact.price = order.price transact.datetime = bar.datetime fill_orders.add(order) self.events.put(FillEvent(transact)) elif order.price_type == PriceType.MKT: transact.price = bar.open transact.datetime = bar.datetime # recompute commission when price changed transact.compute_commission() fill_orders.add(order) self.events.put(FillEvent(transact)) else: if order.price_type == PriceType.LMT: # 限价单以最高和最低价格为成交的判断条件. if (order.side == TradeSide.OPEN and \ (order.direction == Direction.LONG and order.price >= bar.low or \ order.direction == Direction.SHORT and order.price <= bar.high)) or \ (order.side == TradeSide.CLOSE and \ (order.direction == Direction.LONG and order.price <= bar.high or \ order.direction == Direction.SHORT and order.price >= bar.low)): transact.price = order.price # Bar的结束时间做为交易成交时间. transact.datetime = bar.datetime fill_orders.add(order) self.events.put(FillEvent(transact)) elif order.price_type == PriceType.MKT: # 市价单以最高或最低价格为成交价格. if order.side == TradeSide.OPEN: if order.direction == Direction.LONG: transact.price = bar.high else: transact.price = bar.low elif order.side == TradeSide.CLOSE: if order.direction == Direction.LONG: transact.price = bar.low else: transact.price = bar.high transact.datetime = bar.datetime # recompute commission when price changed transact.compute_commission() fill_orders.add(order) self.events.put(FillEvent(transact)) else: transact.datetime = bar.datetime fill_orders.add(order) # self.events.put(FillEvent(transact)) # end of for if fill_orders: self._open_orders -= fill_orders