NaN (Not a Number) for bid, ask, and last prices.
Here's the catch: the individual legs of the combo still quote perfectly fine. The market data subscription isn't broken, your network connection is stable, and the API isn't throwing a hard error. It just silently degrades into NaN.
This is a known issue in the IBKR API ecosystem, specifically affecting combo contracts. If you're relying on real-time pricing for algorithmic trading, this bug can cause your strategies to stall or trade off stale data. In this guide, we'll break down why this happens, how to identify it before it ruins your trades, and the exact workarounds to fix it without restarting your Gateway.
About this guide: I'm Lawrence, the writer behind supa.is. Between February and May 2026 I've published 150+ articles on supa.is across crypto and brokerage tooling — including 60+ IBKR-specific guides (recent examples: Interactive Brokers TWS API: Market Data Not Subscribed, Fix IBKR API calculateImpliedVolatility Timeout, Fix IBKR API Timezone Mismatch). The most-repeated reader question across that IBKR API archive is exactly why combo market data silently drops to NaN, which is why I'm publishing this standardized guide instead of answering one-off.
What is the IBKR API Combo (BAG) NaN Bug?
In the IBKR API, a "Combo" refers to an options combination—like a vertical spread, iron condor, or straddle—where you buy and sell multiple legs simultaneously. When you request market data for a combo using the API (e.g., via reqMktData with a BAG contract), IBKR is supposed to calculate the aggregate bid, ask, and last price based on the individual legs.
The bug occurs when the Gateway or TWS has been running for an extended period. The API continues to receive ticks for the individual legs, but the calculated market data for the combo contract itself silently switches to NaN.
Because it's a silent failure, there's no error code thrown by the API. Your code keeps running, but your pricing logic suddenly sees NaN and either crashes, skips the trade, or makes a bad decision based on stale data.
I've seen this exact issue pop up in the IBKR forums repeatedly, and it's a headache for anyone running algo strategies.
Why Does the Combo Market Data Become NaN?
IBKR hasn't given us a detailed post-mortem on this, but based on how the API processes combo ticks, here are the likely culprits:
- Tick Accumulation Overflow: The API calculates combo prices by aggregating the ticks of the individual legs. Over time, if the internal tick buffers aren't flushed properly, the calculation can overflow or lose reference to the current state of the legs.
- Stale Leg Data: If one of the legs temporarily stops sending ticks (e.g., due to a brief market pause or a low-liquidity strike), the combo calculation might fail to update. Instead of reverting to the last known good price, it defaults to
NaN. - Gateway Memory Leaks: TWS and Gateway are known to suffer from memory leaks after long uptimes. As memory fills up, background processes like combo price calculation can silently fail.
How to Identify the Bug Early
Since the bug is silent, you need to implement safeguards in your trading scripts. Here are the best ways to catch it before it impacts your PnL:
1. Validate for NaN in Your Tick Handlers
Whenever you receive a tick for a combo contract, explicitly check if the bid, ask, or last price isNaN.
In Python (using ib_async or the official ibapi), you can use math.isnan():
import math
def on_tick(self, tick):
if tick.field == 1: # Bid
if math.isnan(tick.price):
print(f"WARNING: Combo {tick.contract} bid is NaN!")
# Trigger a fallback or alert
2. Compare Combo Price to Individual Legs
Like what you're reading? Try it yourself — this link supports ChartedTrader at no cost to you.
Open an Interactive Brokers Account →NaN, you know the bug has triggered.
3. Monitor the Uptime of Your Gateway
If you notice combo prices dropping toNaN consistently after the Gateway has been running for several hours, you've hit the uptime threshold for this bug.
How to Fix the IBKR API Combo NaN Bug
Since this is a bug on IBKR's side, there is no permanent "patch" you can apply to your code. However, there are several effective workarounds to keep your trading running smoothly.
Workaround 1: Restart the Gateway Regularly
The most straightforward fix is to restart your TWS or IB Gateway periodically. Since the bug is tied to the uptime of the application, a daily restart (or even a restart every 6-8 hours) will clear the internal buffers and reset the combo price calculations.If you're running an automated trading system, you can script a daily restart of the Gateway at a time when markets are closed (e.g., 6:00 AM EST).
Workaround 2: Cancel and Resubscribe to Combo Market Data
If you don't want to restart the entire Gateway, you can cancel the market data subscription for the combo contract and resubscribe to it. This forces the API to recalculate the combo price from scratch.In your code, you can implement a periodic resubscription:
import time
def resubscribe_combo(contract, api_client):
api_client.cancelMktData(contract)
time.sleep(1) # Give the API a moment to process the cancellation
api_client.reqMktData(contract, '', False, False)
You can run this function every few hours or trigger it specifically when you detect a NaN value.
Workaround 3: Calculate the Combo Price Yourself
Instead of relying on IBKR's internal combo calculation, you can request market data for the individual legs and calculate the combo price in your own code. This is the most robust solution, as it completely bypasses the buggy combo calculation engine.For example, if you're trading a bull call spread (buying a lower strike call, selling a higher strike call), your combo bid is the bid of the long leg minus the ask of the short leg.
def calculate_combo_price(long_leg_bid, short_leg_ask):
return long_leg_bid - short_leg_ask
This completely bypasses the buggy engine, and gives you more control over your pricing logic (like accounting for slippage or using mid-prices).
Workaround 4: Use the ib_async Library's Built-in Features
If you're using the popular ib_async Python library, it has some built-in utilities for handling combo contracts. The library attempts to handle some of the edge cases with combo ticks, and it provides easier ways to resubscribe to market data.
However, even ib_async is not immune to the NaN bug if the underlying API data is corrupted. You still need to implement the math.isnan() checks mentioned earlier.
Best Practices for IBKR API Combo Trading
If you trade combo options via the IBKR API, here are some best practices to minimize downtime and ensure data integrity:
- Always validate your data: Never assume that the API will always return valid numbers. Implement
NaNchecks for all price fields. - Prefer leg-level data: If your strategy allows, request market data for the individual legs rather than the combo. This gives you more flexibility and avoids the combo calculation bug entirely.
- Keep Gateway updated: Make sure you're running the latest version of TWS or Gateway. IBKR occasionally releases patches that fix API bugs.
- Use Paper Trading to test: Before deploying any new API logic to a live account, test it extensively in paper trading mode. The
NaNbug is more likely to appear after long uptimes, so let your paper account run for a few days to see if the issue occurs.
FAQ
Why does the IBKR API return NaN for combo market data?
The IBKR API calculates combo prices by aggregating the ticks of individual legs. Over long uptimes, internal buffers can overflow or lose reference to the current state of the legs, causing the calculated price to silently default toNaN.
Can I fix the combo NaN bug without restarting the Gateway?
Yes. You can cancel and resubscribe to the combo market data usingcancelMktData followed by reqMktData. Alternatively, you can calculate the combo price yourself using the individual leg prices.
Is this bug specific to the Python API?
No. The bug is in the IBKR API's internal combo calculation engine, so it affects all API languages (Python, Java, C++, etc.) and both TWS and Gateway.How often should I restart my IB Gateway to prevent this bug?
Most users report the bug appearing after several hours of uptime. Restarting the Gateway once a day (during market hours or at the start of the trading day) is usually sufficient.Will IBKR fix this bug in a future update?
IBKR frequently updates TWS and Gateway, but the comboNaN bug has persisted for years. It's best to assume it will not be fixed soon and implement workarounds in your code.
Risk Warning
Risk Warning: Crypto trading involves substantial risk of loss. Never invest more than you can afford to lose. This is not financial advice. Trading options involves a high degree of risk and is not suitable for all investors. You should carefully consider your investment objectives, level of experience, and risk appetite before trading options.
If you're looking to set up your IBKR API environment properly, or if you're new to the platform, check out our guide on Interactive Brokers Account Setup: Application to First Trade. For more API troubleshooting, see our article on Fix IBKR API calculateImpliedVolatility Timeout.
Ready to start trading with Interactive Brokers? Open an Interactive Brokers Account today.