QuantConnect/Lean · error · RegressionTestException
Unexpected sold quantity: {_boughtQuantity} and liquidated q
Error message
Unexpected sold quantity: {_boughtQuantity} and liquidated quantity: {_liquidatedQuantity} What it means
OnEndOfAlgorithm final trade check: the algorithm must have bought (_boughtQuantity > 0) and liquidated (_liquidatedQuantity < 0). If either is wrong sign/zero, the algorithm did not complete a full buy-then-liquidate cycle — it never traded, or never closed the position.
Source
Thrown at Algorithm.CSharp/BasicTemplateEurexFuturesAlgorithm.cs:176
}
}
public override void OnEndOfAlgorithm()
{
if (_mappingsCount == 0)
{
throw new RegressionTestException($"Unexpected number of symbol changed events (mappings): {_mappingsCount}. Expected 1.");
}
if (!_delisted)
{
throw new RegressionTestException("Contract was not delisted");
}
// Make sure we traded and that the position was liquidated on delisting
if (_boughtQuantity <= 0 || _liquidatedQuantity >= 0)
{
throw new RegressionTestException($"Unexpected sold quantity: {_boughtQuantity} and liquidated quantity: {_liquidatedQuantity}");
}
}
/// <summary>
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
/// </summary>
public bool CanRunLocally { get; } = true;
/// <summary>
/// This is used by the regression test system to indicate which languages this algorithm is written in.
/// </summary>
public List<Language> Languages { get; } = new() { Language.CSharp, Language.Python };
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 94326;
View on GitHub (pinned to d2c3659f87)
Solutions
- Verify Securities[_contractToTrade].Exchange.ExchangeOpen is true at some point after _contractToTrade is set so Buy() executes.
- Confirm the buy fill event passed the line-116 symbol guard and recorded _boughtQuantity.
- Confirm delisting liquidation produced the sell fill that recorded _liquidatedQuantity.
- Trace OnOrderEvent to ensure both buy and sell filled events reach the recording branches.
Defensive patterns
Strategy: validation
Validate before calling
public override void OnEndOfAlgorithm()
{
if (_boughtQuantity <= 0) Log("Never bought — check ExchangeOpen gate and order fills.");
if (_liquidatedQuantity >= 0) Log("Never liquidated — check delisting liquidation sell fill.");
} Prevention
- Confirm Exchange.ExchangeOpen is true so Buy() executes.
- Trace OnOrderEvent so both buy and sell fills are recorded.
- Ensure order-event symbol guards don't reject the legitimate fills.
When it happens
Trigger: OnEndOfAlgorithm runs with _boughtQuantity <= 0 (never bought) OR _liquidatedQuantity >= 0 (never liquidated). The order-event state machine never recorded both a positive buy fill and a negative sell fill.
Common situations: Exchange was never open when the buy gate (line 95) was checked, so no Buy() fired; the contract delisted before a buy; liquidation sell never arrived; buy/sell fills recorded under a different symbol and rejected by the line-116 guard.
Related errors
- {Time} - Unexpected order event symbol: {orderEvent.Symbol}.
- {Time} - Unexpected buy order event status: {orderEvent.Stat
- {Time} - Unexpected sell order event status: {orderEvent.Sta
- {Time} - Unexpected liquidated quantity: {_liquidatedQuantit
- Unexpected number of symbol changed events (mappings): {_map
AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13).
Data as JSON: /api/errors/09089c33dddf1c3e.
Report an issue: GitHub.