Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(market-data): derive source allow-list from loader registry

cgycoreyd1c465e5f720
diff --git a/agent/src/tools/market_data_tool.py b/agent/src/tools/market_data_tool.py
index 73d57a7e6..6c8bf769d 100644
--- a/agent/src/tools/market_data_tool.py
+++ b/agent/src/tools/market_data_tool.py
@@ -10,6 +10,7 @@ from typing import Any
from src.agent.tools import BaseTool
from src.market_data import DEFAULT_MAX_ROWS, fetch_market_data_json
+from backtest.loaders.registry import VALID_SOURCES
from backtest.runner import _VALID_INTERVALS
# Canonical-case lookup: `_VALID_INTERVALS` mixes cases ("1m" minutes vs "1H"
@@ -18,6 +19,11 @@ from backtest.runner import _VALID_INTERVALS
# form the loaders expect ("1d" -> "1D", "30M" -> "30m").
_INTERVAL_CANON = {v.upper(): v for v in _VALID_INTERVALS}
+# Source allow-list derived from the shared loader registry (the same set the
+# backtest tool validates against), so the MCP/agent-facing surface can never
+# silently drop a loader the registry serves. Sorted for a stable schema.
+_SOURCE_ENUM = sorted(VALID_SOURCES)
+
_ISO_DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
@@ -74,41 +80,24 @@ class MarketDataTool(BaseTool):
},
"source": {
"type": "string",
- "enum": [
- "auto",
- "longbridge",
- "yfinance",
- "yahoo",
- "okx",
- "ccxt",
- "tushare",
- "baostock",
- "tencent",
- "akshare",
- "mootdx",
- "eastmoney",
- "sina",
- "stooq",
- "finnhub",
- "alphavantage",
- "tiingo",
- "fmp",
- "mt5",
- "pykrx",
- ],
+ "enum": _SOURCE_ENUM,
"description": (
"Data source. 'auto' detects from symbol format with fallback. "
"Use 'longbridge' explicitly for US/HK OHLCV through the "
"Longbridge OpenAPI (requires Longbridge credentials). "
"Free, no key: yfinance/yahoo (US/HK/Canada equities; "
- "Canada uses .TO/.V), okx/ccxt "
+ "Canada uses .TO/.V), okx/ccxt/binance "
"(crypto), baostock/tencent/eastmoney/sina/akshare/mootdx "
- "(China A-shares), stooq (global EOD), pykrx (Korea KRX daily "
+ "(China A-shares), futu (HK/A via local FutuOpenD), stooq "
+ "(global EOD), pykrx (Korea KRX daily "
"bars for <CODE>.KS / <CODE>.KQ; needs the optional pykrx "
"package, else Korea falls back to yahoo/yfinance). Key-gated "
"REST: tushare (China A-shares), finnhub/alphavantage/tiingo/fmp "
- "(US/global). mt5: forex/metals from a local MetaTrader 5 "
- "terminal (Windows; e.g. EUR/USD, XAUUSD.FX)."
+ "(US/global), qveris (premium marketplace). india_broker: "
+ "read-only Shoonya/Dhan bars for .NS/.BO. mt5: forex/metals "
+ "from a local MetaTrader 5 terminal (Windows; e.g. EUR/USD, "
+ "XAUUSD.FX); tickerall: the same feed hosted, no terminal, "
+ "any OS. local: your own CSV/Parquet/DuckDB files."
),
"default": "auto",
},
@@ -163,9 +152,8 @@ class MarketDataTool(BaseTool):
)
source = kwargs.get("source", "auto")
- source_enum = self.parameters["properties"]["source"]["enum"]
- if source not in source_enum:
- return _error(f"source must be one of {list(source_enum)}")
+ if source not in _SOURCE_ENUM:
+ return _error(f"source must be one of {_SOURCE_ENUM}")
interval = kwargs.get("interval", "1D")
if not isinstance(interval, str):
diff --git a/agent/tests/test_market_data_tool.py b/agent/tests/test_market_data_tool.py
index 13e303d4c..bb5d5afa3 100644
--- a/agent/tests/test_market_data_tool.py
+++ b/agent/tests/test_market_data_tool.py
@@ -309,9 +309,40 @@ def test_market_data_tool_rejects_compact_date_form():
def test_market_data_tool_rejects_unknown_source():
from src.tools.market_data_tool import MarketDataTool
+ from backtest.loaders.registry import VALID_SOURCES
out = json.loads(MarketDataTool().execute(codes=["AAPL.US"], start_date="2026-01-01", end_date="2026-02-01", source="bogus"))
- assert out == {"ok": False, "error": "source must be one of ['auto', 'longbridge', 'yfinance', 'yahoo', 'okx', 'ccxt', 'tushare', 'baostock', 'tencent', 'akshare', 'mootdx', 'eastmoney', 'sina', 'stooq', 'finnhub', 'alphavantage', 'tiingo', 'fmp', 'mt5', 'pykrx']"}
+ assert out == {"ok": False, "error": f"source must be one of {sorted(VALID_SOURCES)}"}
+
+
+def test_market_data_tool_accepts_every_registered_source():
+ """Every source in the loader registry must pass MarketDataTool validation.
+
+ Regression (#1185): the source allow-list was a hardcoded subset that
+ rejected registered, documented sources (binance, local, futu, qveris,
+ india_broker, tickerall). The tool's enum must match VALID_SOURCES so the
+ tool can never silently drop a loader the registry serves.
+ """
+ import src.tools.market_data_tool as mod
+ from backtest.loaders.registry import VALID_SOURCES
+ from unittest import mock
+
+ enum = set(mod.MarketDataTool.parameters["properties"]["source"]["enum"])
+ assert enum == VALID_SOURCES
+
+ calls = []
+ with mock.patch.object(
+ mod, "fetch_market_data_json", side_effect=lambda **kw: calls.append(kw) or "{}"
+ ):
+ for source in sorted(VALID_SOURCES):
+ out = mod.MarketDataTool().execute(
+ codes=["BTC-USDT"],
+ start_date="2026-08-20",
+ end_date="2026-08-21",
+ source=source,
+ )
+ assert json.loads(out) == {}, f"source={source!r} was rejected"
+ assert len(calls) == len(VALID_SOURCES)
def test_market_data_tool_rejects_garbage_interval():