QuantConnect/Lean · error · AssertionError
OrderEvent quantity should hold the current order Quantity
Error message
OrderEvent quantity should hold the current order Quantity
What it means
OrderTicketDemoAlgorithm further asserts that the OrderEvent.quantity equals the current order.quantity retrieved via self.transactions.get_order_by_id(order_event.order_id).quantity. This enforces that each event reflects the order's up-to-date quantity, including after UpdateOrderFields.quantity updates applied between events. A mismatch means the event's quantity is stale relative to the order — an event/order synchronization bug.
Source
Thrown at Algorithm.Python/OrderTicketDemoAlgorithm.py:395
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):
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.")View on GitHub (pinned to d2c3659f87)
Solutions
- Ensure order updates are applied to the order object before the corresponding OrderEvent is dispatched.
- If updates are intentionally async, tolerate transient mismatches for in-flight update events instead of asserting strict equality.
- Verify get_order_by_id returns the same order instance/snapshot the event was generated against.
- Log both quantities with the event status to distinguish stale-event from stale-order cases.
Example fix
# before: strict equality, breaks on in-flight async updates
if order_event.quantity != order.quantity:
raise AssertionError('OrderEvent quantity should hold the current order Quantity')
# after: tolerate transient mismatch during pending updates
if order_event.quantity != order.quantity and order_event.status != OrderStatus.UPDATE_SUBMITTED:
raise AssertionError(f'quantity mismatch: event={order_event.quantity} order={order.quantity}') Defensive patterns
Strategy: validation
Validate before calling
# Tolerate transient mismatch during in-flight updates
if order_event.quantity != order.quantity and order_event.status != OrderStatus.UPDATE_SUBMITTED:
raise AssertionError(f'quantity mismatch: event={order_event.quantity} order={order.quantity}') Prevention
- Apply order updates before dispatching the corresponding OrderEvent.
- For async updates, tolerate transient event/order quantity mismatches.
- Ensure get_order_by_id returns the snapshot the event was generated against.
- Log both quantities with status to distinguish stale-event from stale-order.
When it happens
Trigger: on_order_event with order_event.quantity != order.quantity, typically after a ticket.update(UpdateOrderFields(quantity=...)) changed the order's quantity but the subsequent event still reported the old value (or vice versa). Also possible if the order lookup returns a different snapshot than the event.
Common situations: A Lean change making order updates asynchronous so the event fires before the order object is updated; a quantity update race; the order object being reloaded/refreshed between the update and the event so its quantity differs from the event's.
Related errors
- OrderEvent quantity is Not expected to be 0, it should hold
- Expected order ticket in order event to not be null
- Field self.ticket not expected no be assigned on the first o
- Expected the portfolio to have holdings and to have {self.tr
- OrderEvent LimitPrice is Not expected to be 0 for LimitOrder
AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13).
Data as JSON: /api/errors/f9b78b1a55b66ca2.
Report an issue: GitHub.