-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Description
Summary
After the IB Gateway nightly maintenance restart (~04:19 UTC), LEAN crashes with System.OverflowException in HandlePortfolioUpdates every time it reconnects. IB sends Double.MaxValue (1.7976931348623157E+308) as a sentinel value for MarketPrice, MarketValue, and AverageCost during the account download phase — LEAN then attempts System.Convert.ToDecimal(Double.MaxValue) which throws.
This affects all IB live/paper users after a nightly restart — the sentinel is sent for every symbol in the account snapshot, even symbols with zero position.
LEAN Version
- Version: 2.5.0.0, Build 17268
- Brokerage: InteractiveBrokers (IB Gateway via IBC)
- Mode: Live mode (paper account
DU...)
Stack Trace
ERROR:: InteractiveBrokersBrokerage.HandlePortfolioUpdates(): System.OverflowException: Value was either too large or too small for a Decimal.
at System.Number.ThrowOverflowException(String message)
at System.Decimal.DecCalc.VarDecFromR8(Double input, DecCalc& result)
at System.Convert.ToDecimal(Double value)
at QuantConnect.Brokerages.InteractiveBrokers.InteractiveBrokersBrokerage.CreateHolding(UpdatePortfolioEventArgs e)
at QuantConnect.Brokerages.InteractiveBrokers.InteractiveBrokersBrokerage.HandlePortfolioUpdates(Object sender, UpdatePortfolioEventArgs e)
Log Evidence
The TRACE lines immediately following the error show the exact values IB is sending. Note that Position is 0 for all symbols — the sentinel is not related to open positions:
TRACE:: InteractiveBrokersBrokerage.HandlePortfolioUpdates(): Contract: STK PAR USD , ConId: 11398, Position: 0, MarketPrice: 1.7976931348623157E+308, MarketValue: 1.7976931348623157E+308, AverageCost: 1.7976931348623157E+308, UnrealisedPnl: 13162.39, RealisedPnl: 0, AccountName: DU5968563
TRACE:: InteractiveBrokersBrokerage.HandlePortfolioUpdates(): Contract: STK QQQ USD , ConId: 320227571, Position: 0, MarketPrice: 1.7976931348623157E+308, MarketValue: 1.7976931348623157E+308, AverageCost: 1.7976931348623157E+308, UnrealisedPnl: -95.05, RealisedPnl: 0, AccountName: DU5968563
TRACE:: InteractiveBrokersBrokerage.HandlePortfolioUpdates(): Contract: STK SPY USD , ConId: 756733, Position: 0, MarketPrice: 1.7976931348623157E+308, MarketValue: 1.7976931348623157E+308, AverageCost: 1.7976931348623157E+308, UnrealisedPnl: -930.94, RealisedPnl: 0, AccountName: DU5968563
1.7976931348623157E+308 is Double.MaxValue — the IB TWS API convention for "data not yet available" during the account download sequence after a reconnect. IB sends this for all recently-traded symbols in the account history, regardless of current position.
Reproduction
- Run a live IB algo (paper or live account) that has traded any symbol recently
- Wait for IB Gateway nightly maintenance (~04:19 UTC) — gateway disconnects
- LEAN reconnects and calls
DownloadAccount()/HandlePortfolioUpdates() - IB sends
Double.MaxValueas placeholder values for market data during the account snapshot CreateHolding()callsSystem.Convert.ToDecimal(1.7976931348623157E+308)→OverflowException
The error occurs on every reconnect attempt, causing a crash-loop until the IB Gateway fully stabilizes (often 30–60 minutes). This happens nightly and affects all IB users — not just those with open overnight positions.
Root Cause
CreateHolding() in InteractiveBrokersBrokerage.cs passes raw double values from the IB API event args directly to Convert.ToDecimal() without checking for IB's sentinel values first.
The IB TWS API documents Double.MaxValue as the sentinel for "not yet computed" fields. These should be treated as null/zero rather than attempting decimal conversion.
Suggested Fix
In CreateHolding() (or HandlePortfolioUpdates()), guard before the decimal conversion:
// IB sentinel: Double.MaxValue means "not yet available"
private static decimal SafeDecimal(double value)
{
if (double.IsInfinity(value) || double.IsNaN(value) || value >= 1e300 || value <= -1e300)
return 0m;
return Convert.ToDecimal(value);
}Apply to MarketPrice, MarketValue, and AverageCost fields from UpdatePortfolioEventArgs.
Impact
- Crash-loop on every IB nightly restart
- Can cause missed trading days (algo not running at market open)
- Affects all live IB users — any account that has traded recently will hit this