{"record":{"id":"7ab78ce87d95c094","repo":"StockSharp/StockSharp","slug":"active-order-cannot-have-zero-balance","errorCode":null,"errorMessage":"Active order cannot have zero balance.","messagePattern":"Active order cannot have zero balance\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algo.Strategies/StrategyPositionManager.cs","lineNumber":303,"sourceCode":"\t/// <param name=\"order\">Order to process.</param>\n\t/// <returns><see cref=\"OrderResults\"/></returns>\n\tpublic OrderResults ProcessOrder(Order order)\n\t{\n\t\tArgumentNullException.ThrowIfNull(order);\n\n\t\tvar txId = order.TransactionId;\n\n\t\tif (txId <= 0)\n\t\t\tthrow new ArgumentException(LocalizedStrings.TransactionInvalid, nameof(order));\n\n\t\tif (order.Balance < 0)\n\t\t\tthrow new ArgumentException(LocalizedStrings.OrderBalanceNotEnough.Put(order.TransactionId, order.Balance), nameof(order));\n\n\t\tif (order.Volume <= 0)\n\t\t\tthrow new ArgumentException(LocalizedStrings.WrongOrderVolume.Put(order.TransactionId), nameof(order));\n\n\t\tif (order.State == OrderStates.Active && order.Balance == 0)\n\t\t\tthrow new ArgumentException(\"Active order cannot have zero balance.\", nameof(order));\n\n\t\tif (order.State is OrderStates.None or OrderStates.Pending or OrderStates.Failed)\n\t\t\treturn OrderResults.InvalidStatus; // not yet active\n\n\t\tvar matchedAbs = order.GetMatchedVolume() ?? 0m; // cumulative executed volume (absolute)\n\t\tvar signSide = order.Side == Sides.Sell ? -1 : 1;\n\n\t\tPosition position;\n\t\tbool isNew;\n\n\t\tvar commission = order.Commission;\n\n\t\tusing (_lock.EnterScope())\n\t\t{\n\t\t\tvar key = (order.Security.ToSecurityId(), order.Portfolio);\n\t\t\tvar posExists = _positions.ContainsKey(key);\n\n\t\t\t// If there are no executions yet and the order state is not Active and no position exists yet, ignore.","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/StockSharp/StockSharp/blob/601a191de678bff83da28b14828f8885214ca71c/Algo.Strategies/StrategyPositionManager.cs#L285-L321","documentation":"Thrown by ProcessOrder when an order is in the Active state but its Balance is zero. An active order must still have remaining volume; a fully filled order should be in the Done state, so Active + zero balance is an inconsistent state that the position ledger refuses to process.","triggerScenarios":"Passing an Order whose State is OrderStates.Active while Balance == 0 — e.g. an execution feed that reported full fills but left the order state as Active, or a state-machine transition that forgot to flip to Done.","commonSituations":"Adapter/connector bugs reporting 100% execution without transitioning to Done; out-of-order messages where the fill arrived before the status update; replayed snapshots with mismatched State and Balance.","solutions":["Upstream, ensure fully filled orders are transitioned to Done (and Balance set to 0 only when Done).","Before calling ProcessOrder, reconcile: if Balance == 0 and State == Active, correct the state to Done (or reject the message).","If the source is an adapter, fix its state transitions so Active never coexists with zero balance."],"exampleFix":"// before\n_posManager.ProcessOrder(order); // State==Active && Balance==0 -> throws\n\n// after\nif (order.State == OrderStates.Active && order.Balance == 0)\n    order.State = OrderStates.Done; // reconcile inconsistent snapshot\n_posManager.ProcessOrder(order);","handlingStrategy":"validation","validationCode":"if (order.State == OrderStates.Active && order.Balance == 0)\n    order.State = OrderStates.Done; // reconcile inconsistent snapshot\n_posManager.ProcessOrder(order);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure the order state machine flips to Done on full fill before Balance hits zero.","Reject or reconcile Active+zero-balance snapshots at the feed boundary."],"tags":["position-management","orders","state-consistency","csharp"],"backgroundTag":null,"analyzedSha":"601a191de678bff83da28b14828f8885214ca71c","analyzedAt":"2026-08-13T20:43:24.460Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}