Repository: nautechsystems/nautilus_trader
File: nautilus_core/backtest/src/matching_engine/mod.rs (Rust)

Code before:
                _ => panic!("Invalid order side"),
            }
        } else {
            order.price().expect("Price must be set for passive order")
        };

        // Construct BookOrder from order
        let book_order = BookOrder::new(order.order_side(), price, order.quantity(), 1);

        self.book.simulate_fills(&book_order)
    }

    fn fill_market_order(&mut self, order: &mut OrderAny) {
        if let Some(filled_qty) = self.cached_filled_qty.get(&order.client_order_id()) {
            if filled_qty >= &order.quantity() {
                log::info!(
                        "Ignoring fill as already filled pending application of events: {:?}, {:?}, {:?}, {:?}",
                    filled_qty,
                    order.quantity(),
                    order.filled_qty(),
                    order.quantity()
                );
                return;
            }
        }

        let venue_position_id = self.ids_generator.get_position_id(order, Some(true));
        let position: Option<Position> = if let Some(venue_position_id) = venue_position_id {
            let cache = self.cache.as_ref().borrow();
            cache.position(&venue_position_id).cloned()
        } else {
            None
        };

        if self.config.use_reduce_only && order.is_reduce_only() && position.is_none() {
            log::warn!(
                "Canceling REDUCE_ONLY {} as would increase position",
                order.order_type()
            );
            self.cancel_order(order, None);
            return;
        }
        // set order side as taker
        order.set_liquidity_side(LiquiditySide::Taker);
        let fills = self.determine_market_price_and_volume(order);
        self.apply_fills(order, fills, LiquiditySide::Taker, None, position)
    }

    fn fill_limit_order(&mut self, order: &OrderAny) {
        todo!("fill_limit_order")
    }

Review comment (suggestion):
Cython version has `self._has_targets` variable, and Rust doesn't. But best to use native Option and check if the value is defined to achieve the same effect

Code after:
            if order.is_closed() {
                continue;
            };

            // Check expiration
            if self.config.support_gtd_orders {
                if let Some(expire_time) = order.expire_time() {
                    if timestamp_ns >= expire_time {
                        // SAFTEY: We know this order is in the core
                        self.core.delete_order(order).unwrap();
                        self.expire_order(order);
                    }
                }
            }

            // Manage trailing stop
            if let PassiveOrderAny::Stop(o) = order {
                match o {
                    StopOrderAny::TrailingStopMarket(o) => self.update_trailing_stop_market(o),
                    StopOrderAny::TrailingStopLimit(o) => self.update_trailing_stop_limit(o),
                    _ => {}
                }
            }

            // Move market back to targets
            if let Some(target_bid) = self.target_bid {
                self.core.bid = Some(target_bid);
                self.target_bid = None;
            }
            if let Some(target_ask) = self.target_ask {
                self.core.ask = Some(target_ask);
                self.target_ask = None;
            }
            if let Some(target_last) = self.target_last {
                self.core.last = Some(target_last);
                self.target_last = None;
            }
        }

        // Reset any targets after iteration
        self.target_bid = None;
        self.target_ask = None;
        self.target_last = None;
    }

    fn determine_limit_price_and_volume(&mut self, order: &OrderAny) -> Vec<(Price, Quantity)> {
        match order.price() {
            Some(order_price) => {
                // construct book order with price as passive with limit order price
                let book_order =
                    BookOrder::new(order.order_side(), order_price, order.quantity(), 1);