{"record":{"id":"1370774c29915cea","repo":"QuantConnect/Lean","slug":"orderevent-quantity-is-not-expected-to-be-0-it-sh","errorCode":null,"errorMessage":"OrderEvent quantity is Not expected to be 0, it should hold the current order Quantity","messagePattern":"OrderEvent quantity is Not expected to be 0, it should hold the current order Quantity","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"Algorithm.Python/OrderTicketDemoAlgorithm.py","lineNumber":392,"sourceCode":"                self.__open_market_on_open_orders = []\n                return\n\n            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):","sourceCodeStart":374,"sourceCodeEnd":410,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Python/OrderTicketDemoAlgorithm.py#L374-L410","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["If you modified Lean, ensure every OrderEvent path sets Quantity to the order's current quantity.","Skip event types that legitimately have zero quantity (e.g. cancellation/commission events) before asserting.","Verify update_order_fields.quantity is never set to 0 in the demo's update logic.","Log order.type and order_event.status for the zero-quantity event to identify the path."],"exampleFix":"# before: asserts all events non-zero, including non-fill events\nif order_event.quantity == 0:\n    raise AssertionError('OrderEvent quantity is Not expected to be 0...')\n# after: ignore events that legitimately carry no quantity\nif order_event.quantity == 0 and order_event.status not in (OrderStatus.CANCELED, OrderStatus.NONE):\n    raise AssertionError('OrderEvent quantity unexpectedly 0')","handlingStrategy":"validation","validationCode":"# Skip event types that legitimately carry zero quantity\nif order_event.quantity == 0 and order_event.status not in (OrderStatus.CANCELED, OrderStatus.NONE):\n    raise AssertionError('OrderEvent quantity unexpectedly 0')","typeGuard":"def event_has_quantity(order_event) -> bool:\n    return order_event.quantity != 0","tryCatchPattern":null,"preventionTips":["Ensure every OrderEvent path sets Quantity to the order's current quantity.","Ignore cancellation/commission-only events before asserting non-zero.","Never set UpdateOrderFields.quantity to 0.","Log order.type/status for any zero-quantity event to find the path."],"tags":["orders","order-event","order-quantity","quantconnect","demo"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}