QuantConnect/Lean · error · RegressionTestException

Unexpected open order {order}

Error message

Unexpected open order {order}

What it means

Thrown by the EmitInsightsAlgorithm regression test (a C# sample algorithm) inside OnData. It guards the Alpha→Portfolio→Execution pipeline: right before the test manually emits an Insight, there must be NO open order for the symbol. If Transactions.GetOpenOrders(_symbol) returns an order at that checkpoint, the insight-to-order plumbing produced a spurious order, so the test fails fast with the offending order printed.

Source

Thrown at Algorithm.CSharp/EmitInsightsAlgorithm.cs:70

            SetAlpha(new ConstantAlphaModel(InsightType.Price, InsightDirection.Up, TimeSpan.FromDays(1), 0.025, null));
            SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel());
            SetRiskManagement(new MaximumDrawdownPercentPerSecurity(0.01m));
        }

        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">Slice object keyed by symbol containing the stock data</param>
        public override void OnData(Slice slice)
        {
            if (_toggle)
            {
                _toggle = false;
                var order = Transactions.GetOpenOrders(_symbol).FirstOrDefault();

                if (order != null)
                {
                    throw new RegressionTestException($"Unexpected open order {order}");
                }

                // we manually emit an insight
                EmitInsights(Insight.Price(_symbol, Resolution.Daily, 1, InsightDirection.Down));

                // emitted insight should have triggered a new order
                order = Transactions.GetOpenOrders(_symbol).FirstOrDefault();

                if (order == null)
                {
                    throw new RegressionTestException("Expected open order for emitted insight");
                }
                if (order.Direction != OrderDirection.Sell
                    || order.Symbol != _symbol)
                {
                    throw new RegressionTestException($"Unexpected open order for emitted insight: {order}");
                }
            }

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Inspect the printed order object to see which direction/type leaked through, then trace which earlier insight/order created it.
  2. If you modified the Execution/Portfolio/Risk framework, verify the execution model is not double-submitting or carrying orders across bars.
  3. Ensure the previous order fills or is cancelled before the toggle bar; adjust the regression's expected fill timing / data set if fills genuinely arrive later.
  4. Run the algorithm in the same data/timezone configuration the regression expects (QC cloud or local lean-cli) so fill timing matches.

Example fix

// before
var order = Transactions.GetOpenOrders(_symbol).FirstOrDefault();
if (order != null) { throw new RegressionTestException($"Unexpected open order {order}"); }

// diagnostic fix: log/cancel the stale order before asserting
foreach (var o in Transactions.GetOpenOrders(_symbol))
{
    Log($"Cancelling stale order {o.Id} {o.Type} qty={o.Quantity}");
    Transactions.CancelOrder(o.Id, "stale before insight emit");
}
Defensive patterns

Strategy: validation

Validate before calling

// Before emitting the insight, cancel any stale open orders for the symbol
var open = Transactions.GetOpenOrders(_symbol);
if (open.Any())
{
    foreach (var o in open) Transactions.CancelOrder(o.Id);
    // optionally wait a bar before re-checking
}
// only then:
// EmitInsights(Insight.Price(_symbol, Resolution.Daily, 1, InsightDirection.Down));

Prevention

When it happens

Trigger: OnData runs while the per-bar _toggle flag is true, and an order for _symbol is still in the open-orders list (unfilled/uncancelled) when GetOpenOrders(_symbol).FirstOrDefault() is called. Typically a previously emitted insight's order was never filled or a stale order survived into this bar.

Common situations: Regression test data changed (fills arriving a bar late), a brokerage/execution-model change that queues orders differently, or market data with no liquidity so the prior order never filled before the toggle branch ran.

Related errors


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