Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(loaders): name FMP_API_KEY in the explicit-fmp refusal, and pin the open side of the gate

Haozhe Wud18c6bf13165
diff --git a/agent/backtest/loaders/registry.py b/agent/backtest/loaders/registry.py
index 690190be5..2ee04642f 100644
--- a/agent/backtest/loaders/registry.py
+++ b/agent/backtest/loaders/registry.py
@@ -453,6 +453,7 @@ def get_loader_cls_with_fallback(source: str) -> Type[Any]:
"(~/.vibe-trading/data-bridge/config.yaml) — it must exist and "
"list at least one source.",
"tickerall": "Set TICKERALL_API_KEY and TICKERALL_ACCOUNT_ID.",
+ "fmp": "Set FMP_API_KEY.",
}.get(source, "")
raise NoAvailableSourceError(
f"Data source '{source}' is unavailable and does not fall back to a "
diff --git a/agent/tests/test_fmp_stable_regression.py b/agent/tests/test_fmp_stable_regression.py
index b2b99fc80..c6116f5d6 100644
--- a/agent/tests/test_fmp_stable_regression.py
+++ b/agent/tests/test_fmp_stable_regression.py
@@ -89,3 +89,83 @@ def test_explicit_fmp_unavailable_raises_no_fallback(monkeypatch):
assert False, "should have raised NoAvailableSourceError"
except NoAvailableSourceError as exc:
assert "does not fall back" in str(exc)
+
+
+def test_explicit_fmp_unavailable_names_the_missing_credential(monkeypatch):
+ """The refusal must say how to fix it, the way local/tickerall already do.
+
+ ``fmp`` joined ``_NO_NETWORK_FALLBACK_SOURCES`` but not the hint table
+ beside it, so the user got "unavailable and does not fall back" with no
+ remedy — the one thing that error exists to deliver.
+ """
+ from backtest.loaders.registry import get_loader_cls_with_fallback
+ from backtest.loaders import registry
+
+ fmp_cls = registry.LOADER_REGISTRY["fmp"]
+ monkeypatch.setattr(fmp_cls, "is_available", lambda self: False)
+ try:
+ get_loader_cls_with_fallback("fmp")
+ assert False, "should have raised NoAvailableSourceError"
+ except NoAvailableSourceError as exc:
+ assert "FMP_API_KEY" in str(exc)
+
+
+def test_auto_chain_still_walks_past_an_unavailable_fmp():
+ """The other side of the gate: auto must NOT stop at fmp.
+
+ Every other test here asserts the gate closes (explicit fmp refuses to
+ substitute). None asserted it stays open, and that is the direction a
+ no-fallback rule breaks things: ``fmp`` sits inside the ``us_equity``
+ chain, so if adding it to ``_NO_NETWORK_FALLBACK_SOURCES`` had made the
+ walk abort at that link, every keyless user's auto US fetch would die
+ there. ``fmp`` is placed mid-chain on purpose — behind the detected head,
+ ahead of the source that actually serves — because a chain that reaches
+ fmp only after something already succeeded proves nothing.
+ """
+ from src.market_data import fetch_market_data
+
+ served = pd.DataFrame(
+ [{"trade_date": pd.Timestamp("2024-01-03"), "open": 1, "high": 1,
+ "low": 1, "close": 1, "volume": 1}]
+ ).set_index("trade_date")
+ attempted: list[str] = []
+
+ def resolver(name):
+ attempted.append(name)
+ if name == "fmp":
+ raise NoAvailableSourceError(
+ "Data source 'fmp' is unavailable and does not fall back to a "
+ "network source. Set FMP_API_KEY."
+ )
+
+ class Loader:
+ markets = {"us_equity"}
+
+ def __init__(self): pass
+
+ def is_available(self): return True
+
+ def fetch(self, codes, start_date, end_date, interval="1D"):
+ if name == "yahoo":
+ raise RuntimeError("yahoo 404")
+ return {codes[0]: served}
+
+ Loader.name = name
+ return Loader
+
+ result = fetch_market_data(
+ codes=["AAPL.US"],
+ start_date="2024-01-01",
+ end_date="2024-01-31",
+ source="auto",
+ interval="1D",
+ loader_resolver=resolver,
+ fallback_chain_provider=lambda src: ["yahoo", "fmp", "stooq"],
+ include_provenance=True,
+ )
+
+ assert "fmp" in attempted, "test is void unless the walk actually reached fmp"
+ assert attempted[-1] == "stooq", f"walk stopped at fmp: {attempted}"
+ assert "AAPL.US" in result, "an unavailable fmp must not abort the auto walk"
+ prov = result.get("_provenance", {}).get("AAPL.US", {})
+ assert prov.get("source") == "stooq"