{"record":{"id":"f9b78b1a55b66ca2","repo":"QuantConnect/Lean","slug":"orderevent-quantity-should-hold-the-current-order","errorCode":null,"errorMessage":"OrderEvent quantity should hold the current order Quantity","messagePattern":"OrderEvent quantity should hold the current order Quantity","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"Algorithm.Python/OrderTicketDemoAlgorithm.py","lineNumber":395,"sourceCode":"            quantity = ticket.quantity + 1\n            self.log(\"Updating quantity  - New Quantity: {0}\".format(quantity))\n\n            # we can update the quantity and tag\n            update_order_fields = UpdateOrderFields()\n            update_order_fields.quantity = quantity\n            update_order_fields.tag = \"Update #{0}\".format(len(ticket.update_requests) + 1)\n            ticket.update(update_order_fields)\n\n\n    def on_order_event(self, order_event):\n        order = self.transactions.get_order_by_id(order_event.order_id)\n        self.log(\"{0}: {1}: {2}\".format(self.time, order.type, order_event))\n\n        if order_event.quantity == 0:\n            raise AssertionError(\"OrderEvent quantity is Not expected to be 0, it should hold the current order Quantity\")\n\n        if order_event.quantity != order.quantity:\n            raise AssertionError(\"OrderEvent quantity should hold the current order Quantity\")\n\n        if (type(order) is LimitOrder and order_event.limit_price == 0 or\n            type(order) is StopLimitOrder and order_event.limit_price == 0):\n            raise AssertionError(\"OrderEvent LimitPrice is Not expected to be 0 for LimitOrder and StopLimitOrder\")\n\n        if type(order) is StopMarketOrder and order_event.stop_price == 0:\n            raise AssertionError(\"OrderEvent StopPrice is Not expected to be 0 for StopMarketOrder\")\n\n        # We can access the order ticket from the order event\n        if order_event.ticket is None:\n            raise AssertionError(\"OrderEvent Ticket was not set\")\n        if order_event.order_id != order_event.ticket.order_id:\n            raise AssertionError(\"OrderEvent.ORDER_ID and order_event.ticket.order_id do not match\")\n\n    def check_pair_orders_for_fills(self, long_order, short_order):\n        if long_order.status == OrderStatus.FILLED:\n            self.log(\"{0}: Cancelling short order, long order is filled.\".format(short_order.order_type))\n            short_order.cancel(\"Long filled.\")","sourceCodeStart":377,"sourceCodeEnd":413,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Python/OrderTicketDemoAlgorithm.py#L377-L413","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before: strict equality, breaks on in-flight async updates\nif order_event.quantity != order.quantity:\n    raise AssertionError('OrderEvent quantity should hold the current order Quantity')\n# after: tolerate transient mismatch during pending updates\nif order_event.quantity != order.quantity and order_event.status != OrderStatus.UPDATE_SUBMITTED:\n    raise AssertionError(f'quantity mismatch: event={order_event.quantity} order={order.quantity}')","handlingStrategy":"validation","validationCode":"# Tolerate transient mismatch during in-flight updates\nif order_event.quantity != order.quantity and order_event.status != OrderStatus.UPDATE_SUBMITTED:\n    raise AssertionError(f'quantity mismatch: event={order_event.quantity} order={order.quantity}')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["orders","order-event","order-quantity","order-update","quantconnect","demo"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}