def cross_order(self, order: OrderData, tick: TickData): """""" contract = self.main_engine.get_contract(order.vt_symbol) trade_price = 0 # Cross market order immediately after received if order.type == OrderType.MARKET: if order.direction == Direction.LONG: trade_price = tick.ask_price_1 + self.trade_slippage * contract.pricetick else: trade_price = tick.bid_price_1 - self.trade_slippage * contract.pricetick # Cross limit order only if price touched elif order.type == OrderType.LIMIT: if order.direction == Direction.LONG: if order.price >= tick.ask_price_1: trade_price = tick.ask_price_1 else: if order.price <= tick.bid_price_1: trade_price = tick.bid_price_1 # Cross limit order only if price broken elif order.type == OrderType.STOP: if order.direction == Direction.LONG: if tick.ask_price_1 >= order.price: trade_price = tick.ask_price_1 + self.trade_slippage * contract.pricetick else: if tick.bid_price_1 <= order.price: trade_price = tick.bid_price_1 - self.trade_slippage * contract.pricetick if trade_price: order.status = Status.ALLTRADED order.traded = order.volume self.put_event(EVENT_ORDER, order) trade = TradeData( symbol=order.symbol, exchange=order.exchange, orderid=order.orderid, tradeid=order.orderid, direction=order.direction, offset=order.offset, price=trade_price, volume=order.volume, datetime=datetime.now(LOCAL_TZ), gateway_name=order.gateway_name ) self.put_event(EVENT_TRADE, trade) self.update_position(trade, contract)
def check_order_valid(self, order: OrderData, contract: ContractData) -> Optional[PositionData]: """""" # Reject unsupported order type if order.type in {OrderType.FAK, OrderType.FOK, OrderType.RFQ}: order.status = Status.REJECTED elif order.type == OrderType.STOP and not contract.stop_supported: order.status = Status.REJECTED if order.status == Status.REJECTED: self.write_log(f"委托被拒单,不支持的委托类型{order.type.value}") # Reject close order if no more available position if contract.net_position or order.offset == Offset.OPEN: return if order.direction == Direction.LONG: short_position = self.get_position(order.vt_symbol, Direction.SHORT) available = short_position.volume - short_position.frozen if order.volume > available: order.status = Status.REJECTED else: short_position.frozen += order.volume return short_position else: long_position = self.get_position(order.vt_symbol, Direction.LONG) available = long_position.volume - long_position.frozen if order.volume > available: order.status = Status.REJECTED else: long_position.frozen += order.volume return long_position if order.status == Status.REJECTED: self.write_log(f"委托被拒单,可平仓位不足")