QuantConnect/Lean · error · AssertionError

OrderEvent.ORDER_ID and order_event.ticket.order_id do not m

Error message

OrderEvent.ORDER_ID and order_event.ticket.order_id do not match

What it means

Self-test assertion verifying internal consistency: the OrderId on the OrderEvent must equal the OrderId on the OrderTicket referenced by that same event. Both should identify the same order, since the ticket is the handle created for the order and the event describes a state change of that order. The assertion fires when the two ids disagree.

Source

Thrown at Algorithm.Python/OrderTicketDemoAlgorithm.py:408

        if order_event.quantity == 0:
            raise AssertionError("OrderEvent quantity is Not expected to be 0, it should hold the current order Quantity")

        if order_event.quantity != order.quantity:
            raise AssertionError("OrderEvent quantity should hold the current order Quantity")

        if (type(order) is LimitOrder and order_event.limit_price == 0 or
            type(order) is StopLimitOrder and order_event.limit_price == 0):
            raise AssertionError("OrderEvent LimitPrice is Not expected to be 0 for LimitOrder and StopLimitOrder")

        if type(order) is StopMarketOrder and order_event.stop_price == 0:
            raise AssertionError("OrderEvent StopPrice is Not expected to be 0 for StopMarketOrder")

        # We can access the order ticket from the order event
        if order_event.ticket is None:
            raise AssertionError("OrderEvent Ticket was not set")
        if order_event.order_id != order_event.ticket.order_id:
            raise AssertionError("OrderEvent.ORDER_ID and order_event.ticket.order_id do not match")

    def check_pair_orders_for_fills(self, long_order, short_order):
        if long_order.status == OrderStatus.FILLED:
            self.log("{0}: Cancelling short order, long order is filled.".format(short_order.order_type))
            short_order.cancel("Long filled.")
            return True

        if short_order.status == OrderStatus.FILLED:
            self.log("{0}: Cancelling long order, short order is filled.".format(long_order.order_type))
            long_order.cancel("Short filled")
            return True

        return False


    def time_is(self, day, hour, minute):
        return self.time.day == day and self.time.hour == hour and self.time.minute == minute

View on GitHub (pinned to d2c3659f87)

Solutions

  1. As an engine regression: inspect the transaction manager's ticket lookup to confirm it keys tickets by the same OrderId used to build the event.
  2. If you build/reassign tickets manually: never attach a ticket from one order to an event of another; create the ticket from the order being submitted.
  3. In a custom handler: cross-check using self.transactions.get_order_by_id(order_event.order_id) and report the mismatch rather than asserting, if full integrity is not guaranteed.
  4. For cloned events: use OrderEvent.Clone() and never reassign OrderId without also re-resolving the ticket.

Example fix

# before
if order_event.order_id != order_event.ticket.order_id:
    raise AssertionError("OrderEvent.ORDER_ID and order_event.ticket.order_id do not match")

# after (fail with enough context to diagnose the mismatch)
if order_event.order_id != order_event.ticket.order_id:
    raise AssertionError(
        f"OrderEvent id {order_event.order_id} does not match "
        f"ticket id {order_event.ticket.order_id} for symbol {order_event.symbol}")
Defensive patterns

Strategy: validation

Validate before calling

# Validate id consistency before using the ticket
ticket = order_event.ticket
if ticket is None or order_event.order_id != ticket.order_id:
    self.debug(f"Event/ticket id mismatch for order {order_event.order_id}")
    # fall back to a fresh lookup by the event's authoritative order id
    order = self.transactions.get_order_by_id(order_event.order_id)
    # proceed using `order`, not the mismatched ticket

Type guard

def event_ticket_ids_match(order_event):
    """True when the event carries a ticket whose order id equals the event's order id."""
    return (order_event.ticket is not None
            and order_event.order_id == order_event.ticket.order_id)

Prevention

When it happens

Trigger: order_event.order_id != order_event.ticket.order_id, i.e. the ticket attached to the event belongs to a different order than the event itself. This is an integrity violation that can arise if the transaction processor attaches the wrong ticket to an event (a lookup-by-index bug), if an event is cloned/reused across orders, or if the ticket was manually assigned.

Common situations: Lean contributors hit this after changing how the transaction manager maps order ids to tickets. Users hit it when they construct or mutate OrderEvent/OrderTicket objects manually, or when a brokerage integration reports events against the wrong internal order id. Rare in normal use; indicates a real data-integrity bug when seen.

Related errors


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