QuantConnect/Lean · error · AssertionError

The Algorithms was not handled any StopMarketOrders

Error message

The Algorithms was not handled any StopMarketOrders

What it means

End-of-algorithm assertion that every STOP_MARKET order created by the scheduled method reached FILLED status. on_end_of_algorithm iterates transactions.get_orders filtered by OrderType.STOP_MARKET and fails if any order is not FILLED. It guards the prior extended-hours fill behaviour end-to-end.

Source

Thrown at Algorithm.Python/FutureStopMarketOrderOnExtendedHoursRegressionAlgorithm.py:70

    # An order fill update the resulting information is passed to this method.
    def on_order_event(self, order_event: OrderEvent) -> None:
        if self.transactions.get_order_by_id(order_event.order_id).type is not OrderType.STOP_MARKET:
            return None

        if order_event.status == OrderStatus.FILLED:
            # Get Exchange Hours for specific security
            exchange_hours = self.market_hours_database.get_exchange_hours(self._sp_500_e_mini.subscription_data_config)

            # Validate, Exchange is opened explicitly
            if (not exchange_hours.is_open(order_event.utc_time, self._sp_500_e_mini.is_extended_market_hours)):
                raise AssertionError("The Exchange hours was closed, verify 'extended_market_hours' flag in Initialize() when added new security(ies)")

    def on_end_of_algorithm(self) -> None:
        self.stop_market_orders = self.transactions.get_orders(lambda o: o.type is OrderType.STOP_MARKET)

        for o in self.stop_market_orders:
            if o.status != OrderStatus.FILLED:
                raise AssertionError("The Algorithms was not handled any StopMarketOrders")

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Inspect transactions.get_orders() at on_end_of_algorithm to see each stop order's actual status.
  2. Ensure the stop price (self._sp_500_e_mini.price * 1.1) is actually crossed during the backtest window.
  3. Confirm the scheduler's early-return guard (end_date - 1 or not mapped) is not skipping every bar.
  4. Re-check extended_market_hours=True so the stop can fill in the session the test exercises.
Defensive patterns

Strategy: validation

Validate before calling

# Before on_end_of_algorithm, summarise stop order statuses
statuses = [o.status for o in self.transactions.get_orders(lambda o: o.type is OrderType.STOP_MARKET)]
if any(s != OrderStatus.FILLED for s in statuses):
    self.debug(f"Unfilled stop orders: {statuses}")

Type guard

def all_stops_filled(algo) -> bool:
    orders = algo.transactions.get_orders(lambda o: o.type is OrderType.STOP_MARKET)
    return len(orders) > 0 and all(o.status == OrderStatus.FILLED for o in orders)

Prevention

When it happens

Trigger: Stop-market orders left SUBMITTED/PARTIALLY_FILLED/CANCELED because the market was closed at the stop price; the scheduler never placed orders (make_market_and_stop_market_order early-returned); fills happened but were registered under a different order type after symbol mapping.

Common situations: extended_market_hours flag missing so stops cannot trigger; stop price set beyond the data range for the backtest window; data gaps preventing the stop from being touched; mapping/rollover changing the ticket symbol so get_orders filter misses them.

Related errors


AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13). Data as JSON: /api/errors/d2d726a3c0b22cdc. Report an issue: GitHub.