QuantConnect/Lean · error · AssertionError

OrderEvent quantity is Not expected to be 0, it should hold

Error message

OrderEvent quantity is Not expected to be 0, it should hold the current order Quantity

What it means

OrderTicketDemoAlgorithm asserts that every OrderEvent.quantity is non-zero. Lean's contract is that OrderEvent.quantity holds the order's current (target) quantity at the time of the event, including after UpdateOrderFields.quantity updates. A zero quantity on a live event indicates the event was emitted with an uninitialized/default quantity, or a quantity-update path set the field to 0 — an engine bug in order-event population.

Source

Thrown at Algorithm.Python/OrderTicketDemoAlgorithm.py:392

                self.__open_market_on_open_orders = []
                return

            quantity = ticket.quantity + 1
            self.log("Updating quantity  - New Quantity: {0}".format(quantity))

            # we can update the quantity and tag
            update_order_fields = UpdateOrderFields()
            update_order_fields.quantity = quantity
            update_order_fields.tag = "Update #{0}".format(len(ticket.update_requests) + 1)
            ticket.update(update_order_fields)


    def on_order_event(self, order_event):
        order = self.transactions.get_order_by_id(order_event.order_id)
        self.log("{0}: {1}: {2}".format(self.time, order.type, order_event))

        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):

View on GitHub (pinned to d2c3659f87)

Solutions

  1. If you modified Lean, ensure every OrderEvent path sets Quantity to the order's current quantity.
  2. Skip event types that legitimately have zero quantity (e.g. cancellation/commission events) before asserting.
  3. Verify update_order_fields.quantity is never set to 0 in the demo's update logic.
  4. Log order.type and order_event.status for the zero-quantity event to identify the path.

Example fix

# before: asserts all events non-zero, including non-fill events
if order_event.quantity == 0:
    raise AssertionError('OrderEvent quantity is Not expected to be 0...')
# after: ignore events that legitimately carry no quantity
if order_event.quantity == 0 and order_event.status not in (OrderStatus.CANCELED, OrderStatus.NONE):
    raise AssertionError('OrderEvent quantity unexpectedly 0')
Defensive patterns

Strategy: validation

Validate before calling

# Skip event types that legitimately carry zero quantity
if order_event.quantity == 0 and order_event.status not in (OrderStatus.CANCELED, OrderStatus.NONE):
    raise AssertionError('OrderEvent quantity unexpectedly 0')

Type guard

def event_has_quantity(order_event) -> bool:
    return order_event.quantity != 0

Prevention

When it happens

Trigger: on_order_event receives an event where order_event.quantity == 0. Happens when an event is constructed without setting Quantity (default 0), when a quantity update to 0 was applied, or when an intermediate event in an update sequence reports the pre-update quantity as 0.

Common situations: A Lean change to OrderEvent construction leaving Quantity unset; an UpdateOrderFields.quantity = 0 update; an event type (e.g. cancellation or commission-only) that legitimately has no quantity but is routed through this handler.

Related errors


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