QuantConnect/Lean · error · RegressionTestException
Unexpected open order {order}
Error message
Unexpected open order {order} What it means
In this algorithm there is no AlphaModel; insights are emitted manually via EmitInsights. Before emitting, the test asserts there is no open order for _symbol. A pre-existing open order would mean a prior loop iteration left an order dangling before the insight-driven order is created. This is a pre-emission cleanliness check.
Source
Thrown at Algorithm.CSharp/EmitInsightNoAlphaModelAlgorithm.cs:72
// Order margin value has to have a minimum of 0.5% of Portfolio value, allows filtering out small trades and reduce fees.
// Commented so regression algorithm is more sensitive
//Settings.MinimumOrderMarginPortfolioPercentage = 0.005m;
}
/// <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 (!Portfolio.Invested)
{
var order = Transactions.GetOpenOrders(_symbol).FirstOrDefault();
if (order != null)
{
throw new RegressionTestException($"Unexpected open order {order}");
}
EmitInsights(Insight.Price(_symbol, Resolution.Daily, 10, 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}");
}
SetHoldings(_symbol, 1);View on GitHub (pinned to d2c3659f87)
Solutions
- Confirm the order from a prior OnData call has filled (Invested becomes true) before re-entering the block.
- Add an explicit open-orders guard at the top of OnData to skip re-processing.
- Verify the Portfolio Construction Model does not create duplicate orders from repeated insights.
Defensive patterns
Strategy: validation
Validate before calling
var open = Transactions.GetOpenOrders(_symbol);
if (open.Count > 0)
{
// a prior order is still open; do not emit again this step
Log($"Skipping EmitInsights; open order exists: {open[0]}");
return;
} Prevention
- Guard OnData against re-entrancy when a prior order is still open.
- Use a 'position/price managed' flag rather than relying solely on Portfolio.Invested.
- Log open-order state at each OnData entry.
When it happens
Trigger: OnData is called a second time while a previous order is still open (not yet filled), so GetOpenOrders returns it. Or the Portfolio.Invested guard did not prevent re-entry.
Common situations: The !Portfolio.Invested guard not sufficient (a pending order does not yet flip Invested); order fill delayed across time steps; a previous EmitInsights created an order that is still open.
Related errors
- Expected open order for emitted insight
- Order was not canceled
- There should be no open orders
- Unexpected open order for emitted insight: {order}
- Unexpected holdings
AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13).
Data as JSON: /api/errors/82e728910cfaccaf.
Report an issue: GitHub.