"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(ibkr): report the requested and the applied market-data tier separately
Haozhe Wub23aabf0a0b7
diff --git a/agent/src/trading/connectors/ibkr/local.py b/agent/src/trading/connectors/ibkr/local.pyindex 363a38c67..114a2a9b7 100644--- a/agent/src/trading/connectors/ibkr/local.py+++ b/agent/src/trading/connectors/ibkr/local.py@@ -349,7 +349,7 @@ def _tick_has_data(ticker: Any) -> bool:return False-def _apply_market_data_type(ib: Any, config: IBKRLocalConfig) -> None:+def _apply_market_data_type(ib: Any, config: IBKRLocalConfig) -> str | None:"""Select the TWS market-data tier before requesting prices.TWS defaults to live data (type 1). An account without a market-data@@ -357,16 +357,37 @@ def _apply_market_data_type(ib: Any, config: IBKRLocalConfig) -> None:fills, so the caller silently receives null prices after the full polltimeout. Setting type 3/4 falls back to the free delayed feed instead.- The call is best-effort: it is a no-op against SDK stubs that do not expose- ``reqMarketDataType``.+ Applying the tier is best-effort — an SDK build without+ ``reqMarketDataType`` must not fail an otherwise valid read — but the+ failure is never silent. The caller reports the requested and the applied+ tier separately: a quote served on TWS's own default tier is byte-identical+ to one served on the tier we asked for, so labelling an unapplied request+ as applied sends the user to debug a subscription that is not the problem.++ Args:+ ib: Connected ``ib_async`` client, or a stub in tests.+ config: Connector config carrying the requested tier.++ Returns:+ ``None`` when the tier was applied, otherwise a short reason why not."""fn = getattr(ib, "reqMarketDataType", None)if fn is None:- return+ return "the connected ib_async build does not expose reqMarketDataType"try:fn(int(config.market_data_type))- except Exception: # noqa: BLE001 - never fail a read over a tier hint- pass+ except Exception as exc: # noqa: BLE001 - never fail a read over a tier hint+ return f"reqMarketDataType({config.market_data_type}) failed: {exc}"+ return None+++def _unapplied_tier_warning(tier: str, reason: str) -> str:+ """Build the warning shown when TWS served a read on an unlabelled tier."""+ return (+ f"The '{tier}' market-data tier was NOT applied ({reason}). TWS served this "+ "request on whichever tier it already had selected, which defaults to live "+ "— treat these prices as unlabelled."+ )def _wait_for_tick(ib: Any, ticker: Any, *, timeout: float = 5.0, poll_interval: float = 0.1) -> bool:@@ -412,7 +433,7 @@ def get_quote(_assert_profile(cfg, accounts)contract = _make_contract(symbol, exchange=exchange, currency=currency, sec_type=sec_type)_qualify_contract(ib, contract)- _apply_market_data_type(ib, cfg)+ tier_unapplied = _apply_market_data_type(ib, cfg)ticker = ib.reqMktData(contract, "", True, False)received = _wait_for_tick(ib, ticker)tier = MARKET_DATA_TYPES.get(cfg.market_data_type, str(cfg.market_data_type))@@ -421,7 +442,8 @@ def get_quote("symbol": symbol.upper(),"exchange": exchange,"currency": currency,- "market_data_type": tier,+ "market_data_type_requested": tier,+ "market_data_type_applied": None if tier_unapplied else tier,"quote": {"bid": _obj_get(ticker, "bid"),"ask": _obj_get(ticker, "ask"),@@ -431,12 +453,21 @@ def get_quote("time": str(_obj_get(ticker, "time", "")),},}+ if tier_unapplied:+ result["warning"] = _unapplied_tier_warning(tier, tier_unapplied)if not received:result["error"] = (- f"No market data arrived for {symbol.upper()} on the '{tier}' tier. "- "Common causes: the instrument needs a market-data subscription, "- "the market is closed (try market_data_type 2 or 4 for frozen "- "last-known prices), or the symbol/exchange pair is wrong."+ f"No market data arrived for {symbol.upper()}. "+ + (+ f"The '{tier}' tier was not applied, so TWS most likely used its "+ "live tier, which needs a market-data subscription for this "+ "instrument. "+ if tier_unapplied+ else f"The '{tier}' tier was applied, so a missing market-data "+ "subscription is an unlikely cause. "+ )+ + "Other causes: the market is closed (try market_data_type 2 or 4 "+ "for frozen last-known prices), or the symbol/exchange pair is wrong.")return resultfinally:@@ -463,7 +494,7 @@ def get_historical_bars(_assert_profile(cfg, accounts)contract = _make_contract(symbol, exchange=exchange, currency=currency, sec_type=sec_type)_qualify_contract(ib, contract)- _apply_market_data_type(ib, cfg)+ tier_unapplied = _apply_market_data_type(ib, cfg)bars = ib.reqHistoricalData(contract,endDateTime="",@@ -473,13 +504,19 @@ def get_historical_bars(useRTH=use_rth,formatDate=1,)- return {+ tier = MARKET_DATA_TYPES.get(cfg.market_data_type, str(cfg.market_data_type))+ result = {"status": "ok","symbol": symbol.upper(),"duration": duration,"bar_size": bar_size,+ "market_data_type_requested": tier,+ "market_data_type_applied": None if tier_unapplied else tier,"bars": [_bar_to_dict(bar) for bar in bars],}+ if tier_unapplied:+ result["warning"] = _unapplied_tier_warning(tier, tier_unapplied)+ return resultfinally:_pool.release()diff --git a/agent/tests/test_ibkr_local.py b/agent/tests/test_ibkr_local.pyindex 616713213..3072c9fb0 100644--- a/agent/tests/test_ibkr_local.py+++ b/agent/tests/test_ibkr_local.py@@ -389,7 +389,9 @@ def test_quote_selects_tier_before_requesting_data(monkeypatch: pytest.MonkeyPatresult = local.get_quote("AAPL", config=local.IBKRLocalConfig(profile="paper", market_data_type=4))assert calls == [("tier", 4), ("mktdata", "AAPL")], calls- assert result["market_data_type"] == "delayed-frozen"+ assert result["market_data_type_requested"] == "delayed-frozen"+ assert result["market_data_type_applied"] == "delayed-frozen"+ assert "warning" not in resultdef test_historical_bars_select_tier_too(monkeypatch: pytest.MonkeyPatch) -> None:@@ -447,6 +449,81 @@ def test_tier_hint_is_best_effort_against_old_sdk(monkeypatch: pytest.MonkeyPatcassert local.get_quote("AAPL", config=local.IBKRLocalConfig(profile="paper"))["status"] == "ok"[email protected]("sdk_kind", ["raises", "missing"])+def test_unapplied_tier_is_never_reported_as_applied(+ monkeypatch: pytest.MonkeyPatch, sdk_kind: str+) -> None:+ """A tier we failed to select must not be echoed back as the tier in force.++ A quote served on TWS's own default tier is byte-identical to one served on+ the requested tier, so claiming the requested tier was applied is the one+ thing the response must never do.+ """++ class _RaisingIB(_FakeIB):+ def reqMarketDataType(self, tier):+ raise RuntimeError("not supported by this SDK build")++ _install_fake_ib(monkeypatch, _RaisingIB if sdk_kind == "raises" else _FakeIB)++ result = local.get_quote("AAPL", config=local.IBKRLocalConfig(profile="paper"))++ assert result["status"] == "ok"+ assert result["market_data_type_requested"] == "delayed"+ assert result["market_data_type_applied"] is None+ assert "was NOT applied" in result["warning"]+++def test_starved_quote_names_the_unapplied_tier_as_the_likely_cause(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """When the tier never applied, that is the diagnosis — not a missing feed."""++ class _RaisingStarvedIB(_FakeIB):+ def reqMarketDataType(self, tier):+ raise RuntimeError("not supported by this SDK build")++ def reqMktData(self, contract, genericTickList, snapshot, regulatorySnapshot):+ return SimpleNamespace(bid=None, ask=None, last=None, close=None, volume=None, time="")++ _install_fake_ib(monkeypatch, _RaisingStarvedIB)+ monkeypatch.setattr(local, "_wait_for_tick", lambda *_, **__: False)++ result = local.get_quote("AAPL", config=local.IBKRLocalConfig(profile="paper"))++ assert result["status"] == "no_data"+ assert result["market_data_type_applied"] is None+ assert "tier was not applied" in result["error"]+ assert "market-data subscription" in result["error"]+++def test_historical_bars_report_tier_provenance(monkeypatch: pytest.MonkeyPatch) -> None:+ """The history path carries the same requested/applied provenance as quotes."""++ class _TierIB(_FakeIB):+ def reqMarketDataType(self, tier):+ return None++ def reqHistoricalData(self, contract, **kwargs):+ return [SimpleNamespace(date="2026-05-29", open=1, high=2, low=0.5, close=1.5, volume=100)]++ _install_fake_ib(monkeypatch, _TierIB)+ applied = local.get_historical_bars("AAPL", config=local.IBKRLocalConfig(profile="paper"))+ assert applied["market_data_type_requested"] == "delayed"+ assert applied["market_data_type_applied"] == "delayed"+ assert "warning" not in applied++ class _RaisingIB(_TierIB):+ def reqMarketDataType(self, tier):+ raise RuntimeError("not supported by this SDK build")++ _install_fake_ib(monkeypatch, _RaisingIB)+ unapplied = local.get_historical_bars("AAPL", config=local.IBKRLocalConfig(profile="paper"))+ assert unapplied["status"] == "ok"+ assert unapplied["market_data_type_applied"] is None+ assert "was NOT applied" in unapplied["warning"]++@pytest.mark.parametrize("bad", [0, 5, -1, "banana", "0"])def test_invalid_market_data_type_is_rejected(bad) -> None:"""Config validation refuses tiers TWS does not define."""