"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
Fix silent IBKR market-data failure; add 6 regression tests
jay79-boop76310cea0bb3
diff --git a/agent/src/trading/connectors/ibkr/local.py b/agent/src/trading/connectors/ibkr/local.pyindex 8e4dfcbca..363a38c67 100644--- a/agent/src/trading/connectors/ibkr/local.py+++ b/agent/src/trading/connectors/ibkr/local.py@@ -32,6 +32,19 @@ PROFILE_DEFAULT_PORTS = {"live-readonly": 7496,}+# TWS market-data tiers. Type 1 (live) requires a paid market-data subscription+# for the instrument; without one TWS answers error 354 and never sends a tick.+# Types 3/4 fall back to the free 15-minute delayed feed, so they are the safe+# default for a research/backtest connector.+MARKET_DATA_TYPES = {+ 1: "live",+ 2: "frozen",+ 3: "delayed",+ 4: "delayed-frozen",+}++DEFAULT_MARKET_DATA_TYPE = 3+class IBKRDependencyError(RuntimeError):"""Raised when the optional ``ib_async`` package is not installed."""@@ -57,6 +70,9 @@ class IBKRLocalConfig:account: Optional account code to filter requests.timeout: Connection timeout in seconds.readonly: Always passed as true when the SDK supports it.+ market_data_type: TWS market-data tier (1 live, 2 frozen, 3 delayed,+ 4 delayed-frozen). Defaults to delayed so quotes work without a+ paid market-data subscription."""host: str = "127.0.0.1"@@ -66,6 +82,7 @@ class IBKRLocalConfig:account: str | None = Nonetimeout: float = 8.0readonly: bool = True+ market_data_type: int = DEFAULT_MARKET_DATA_TYPE@classmethoddef from_mapping(cls, data: Mapping[str, Any] | None = None) -> "IBKRLocalConfig":@@ -90,6 +107,9 @@ class IBKRLocalConfig:account=_clean_optional_str(payload.get("account")),timeout=float(payload.get("timeout") or 8.0),readonly=bool(payload.get("readonly", True)),+ market_data_type=_clean_market_data_type(+ payload.get("market_data_type", payload.get("marketDataType"))+ ),)def with_overrides(@@ -100,9 +120,12 @@ class IBKRLocalConfig:client_id: int | None = None,profile: str | None = None,account: str | None = None,+ market_data_type: int | None = None,) -> "IBKRLocalConfig":"""Return a copy with CLI/tool overrides applied."""payload = asdict(self)+ if market_data_type is not None:+ payload["market_data_type"] = market_data_typeif profile is not None:payload["profile"] = profileif port is None:@@ -326,7 +349,27 @@ def _tick_has_data(ticker: Any) -> bool:return False-def _wait_for_tick(ib: Any, ticker: Any, *, timeout: float = 5.0, poll_interval: float = 0.1) -> None:+def _apply_market_data_type(ib: Any, config: IBKRLocalConfig) -> None:+ """Select the TWS market-data tier before requesting prices.++ TWS defaults to live data (type 1). An account without a market-data+ subscription for the instrument then gets error 354 and the ticker never+ fills, so the caller silently receives null prices after the full poll+ timeout. 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``.+ """+ fn = getattr(ib, "reqMarketDataType", None)+ if fn is None:+ return+ try:+ fn(int(config.market_data_type))+ except Exception: # noqa: BLE001 - never fail a read over a tier hint+ pass+++def _wait_for_tick(ib: Any, ticker: Any, *, timeout: float = 5.0, poll_interval: float = 0.1) -> bool:"""Pump the ib_async event loop until the snapshot ticker fills.``reqMktData(..., snapshot=True)`` returns immediately but the ticker@@ -339,14 +382,18 @@ def _wait_for_tick(ib: Any, ticker: Any, *, timeout: float = 5.0, poll_interval:reliably processes all pending socket messages on each call, while``waitOnUpdate`` can return True for unrelated messages, causingpremature exit with NaN fields.++ Returns:+ True if real market data arrived before the deadline, False on timeout."""import time as _timedeadline = _time.monotonic() + timeoutwhile _time.monotonic() < deadline:if _tick_has_data(ticker):- return+ return True_sleep(ib, poll_interval)+ return _tick_has_data(ticker)def get_quote(@@ -365,13 +412,16 @@ 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)ticker = ib.reqMktData(contract, "", True, False)- _wait_for_tick(ib, ticker)- return {- "status": "ok",+ received = _wait_for_tick(ib, ticker)+ tier = MARKET_DATA_TYPES.get(cfg.market_data_type, str(cfg.market_data_type))+ result = {+ "status": "ok" if received else "no_data","symbol": symbol.upper(),"exchange": exchange,"currency": currency,+ "market_data_type": tier,"quote": {"bid": _obj_get(ticker, "bid"),"ask": _obj_get(ticker, "ask"),@@ -381,6 +431,14 @@ def get_quote("time": str(_obj_get(ticker, "time", "")),},}+ 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."+ )+ return resultfinally:_pool.release()@@ -405,6 +463,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)bars = ib.reqHistoricalData(contract,endDateTime="",@@ -600,6 +659,28 @@ def _obj_get(obj: Any, name: str, default: Any = None) -> Any:return getattr(obj, name, default)+def _clean_market_data_type(value: Any) -> int:+ """Normalize a market-data tier, accepting either the code or its name."""+ if value is None or value == "":+ return DEFAULT_MARKET_DATA_TYPE+ if isinstance(value, str):+ text = value.strip().lower()+ for code, name in MARKET_DATA_TYPES.items():+ if text == name:+ return code+ try:+ value = int(text)+ except ValueError as exc:+ raise ValueError(+ f"market_data_type must be one of {sorted(MARKET_DATA_TYPES)} "+ f"or {sorted(MARKET_DATA_TYPES.values())}"+ ) from exc+ code = int(value)+ if code not in MARKET_DATA_TYPES:+ raise ValueError(f"market_data_type must be one of {sorted(MARKET_DATA_TYPES)}")+ return code++def _clean_optional_str(value: Any) -> str | None:if value is None:return Nonediff --git a/agent/tests/test_ibkr_local.py b/agent/tests/test_ibkr_local.pyindex 964d7660f..616713213 100644--- a/agent/tests/test_ibkr_local.py+++ b/agent/tests/test_ibkr_local.py@@ -345,3 +345,120 @@ def test_pool_release_idempotent_no_connection(fake_ib_async) -> None:local._pool._local.ib = None# Must not raise.local._pool.release()+++# -- market-data tier regression tests -----------------------------------+# TWS defaults to live data (tier 1), which needs a paid per-exchange+# subscription. Without one IBKR answers error 354, no tick ever arrives, and+# get_quote used to return status "ok" with null prices — a silent failure the+# tool layer passed straight to the LLM agent.+++def _install_fake_ib(monkeypatch: pytest.MonkeyPatch, ib_cls) -> None:+ module = types.ModuleType("ib_async")+ module.IB = ib_cls+ module.Stock = _FakeStock+ module.Contract = _FakeContract+ monkeypatch.setitem(sys.modules, "ib_async", module)+ monkeypatch.setattr(local, "tcp_port_open", lambda *_, **__: True)+ monkeypatch.setattr(local._pool._local, "refcount", 0)+ monkeypatch.setattr(local._pool._local, "ib", None)+++def test_default_market_data_type_is_free_delayed() -> None:+ """Default tier must not be 1 (live), which requires a paid subscription."""+ assert local.DEFAULT_MARKET_DATA_TYPE == 3+ assert local.IBKRLocalConfig().market_data_type == 3+ assert local.MARKET_DATA_TYPES[3] == "delayed"+++def test_quote_selects_tier_before_requesting_data(monkeypatch: pytest.MonkeyPatch) -> None:+ """reqMarketDataType is called with the configured tier before reqMktData."""+ calls: list[tuple[str, object]] = []++ class _TierIB(_FakeIB):+ def reqMarketDataType(self, tier):+ calls.append(("tier", tier))++ def reqMktData(self, contract, genericTickList, snapshot, regulatorySnapshot):+ calls.append(("mktdata", contract.symbol))+ return SimpleNamespace(bid=1.0, ask=1.1, last=1.05, close=1.0, volume=1, time="")++ _install_fake_ib(monkeypatch, _TierIB)++ result = 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"+++def test_historical_bars_select_tier_too(monkeypatch: pytest.MonkeyPatch) -> None:+ """The tier hint is applied ahead of reqHistoricalData as well."""+ calls: list[str] = []++ class _TierIB(_FakeIB):+ def reqMarketDataType(self, tier):+ calls.append(f"tier:{tier}")++ def reqHistoricalData(self, contract, **kwargs):+ calls.append("history")+ return [SimpleNamespace(date="2026-05-29", open=1, high=2, low=0.5, close=1.5, volume=100)]++ _install_fake_ib(monkeypatch, _TierIB)++ local.get_historical_bars("AAPL", config=local.IBKRLocalConfig(profile="paper"))++ assert calls == ["tier:3", "history"], calls+++def test_starved_ticker_reports_no_data_not_ok(monkeypatch: pytest.MonkeyPatch) -> None:+ """A ticker that never fills must not be reported as a successful quote."""++ class _StarvedIB(_FakeIB):+ def reqMarketDataType(self, tier):+ return None++ def reqMktData(self, contract, genericTickList, snapshot, regulatorySnapshot):+ return SimpleNamespace(bid=None, ask=None, last=None, close=None, volume=None, time="")++ _install_fake_ib(monkeypatch, _StarvedIB)+ # Keep the poll short — the real default is a 5s wall-clock timeout.+ 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["quote"]["last"] is None+ assert "market-data subscription" in result["error"]+++def test_tier_hint_is_best_effort_against_old_sdk(monkeypatch: pytest.MonkeyPatch) -> None:+ """A stub without reqMarketDataType, or one that raises, must not break reads."""++ class _RaisingIB(_FakeIB):+ def reqMarketDataType(self, tier):+ raise RuntimeError("not supported by this SDK build")++ _install_fake_ib(monkeypatch, _RaisingIB)+ assert local.get_quote("AAPL", config=local.IBKRLocalConfig(profile="paper"))["status"] == "ok"++ # _FakeIB itself has no reqMarketDataType at all — the getattr path.+ _install_fake_ib(monkeypatch, _FakeIB)+ assert local.get_quote("AAPL", config=local.IBKRLocalConfig(profile="paper"))["status"] == "ok"++[email protected]("bad", [0, 5, -1, "banana", "0"])+def test_invalid_market_data_type_is_rejected(bad) -> None:+ """Config validation refuses tiers TWS does not define."""+ with pytest.raises(ValueError, match="market_data_type"):+ local.IBKRLocalConfig.from_mapping({"profile": "paper", "market_data_type": bad})+++ ("given", "expected"),+ [("live", 1), ("frozen", 2), ("delayed", 3), ("delayed-frozen", 4), ("3", 3), (None, 3), ("", 3)],+)+def test_market_data_type_accepts_names_codes_and_blanks(given, expected) -> None:+ """Tier may be given as its TWS name or code; blank falls back to the free tier."""+ cfg = local.IBKRLocalConfig.from_mapping({"profile": "paper", "market_data_type": given})+ assert cfg.market_data_type == expected