"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): identity and symbol search know every market the data layer routes (#1565)
Haozhe Wu400f5d8b8aef
diff --git a/agent/src/agent/grounding/identity.py b/agent/src/agent/grounding/identity.pyindex 30f70bbf5..1619340f8 100644--- a/agent/src/agent/grounding/identity.py+++ b/agent/src/agent/grounding/identity.py@@ -122,7 +122,10 @@ _CANONICAL_SYMBOL_RE = re.compile(# match turns any "…/us.reuters/…" host inside a source URL into the# symbol REUTERS.US and fails the answer for an unsourced figure.r"(?-i:US\.[A-Z][A-Z0-9&-]{0,19})|"- r"[A-Z][A-Z0-9&.-]{0,19}\.(?:US|NS|BO|FX|TO|V|BA)|"+ # Every equity suffix the market-data layer routes (backtest.engines.+ # _market_hooks._MARKET_PATTERNS); test_market_identity_parity keeps the+ # two in step, because .L / .VN / .BA each landed there without landing here.+ r"[A-Z][A-Z0-9&.-]{0,19}\.(?:US|NS|BO|FX|TO|V|BA|L|VN)|"r"[A-Z0-9]{2,15}(?:-|/)(?:USDT|USDC|USD|BTC|ETH)|"r"[A-Z]{2,15}(?:" + "|".join(_JOINED_CRYPTO_QUOTE_SUFFIXES) + r")|"r"\^[A-Z0-9&.\-]{1,20}|"@@ -252,6 +255,8 @@ def _infer_venue(symbol: str) -> str | None:".TO": "toronto",".V": "tsx_venture",".BA": "buenos_aires",+ ".L": "lse",+ ".VN": "hose",}for suffix, venue in suffixes.items():if upper.endswith(suffix):@@ -309,6 +314,10 @@ def _infer_currency(symbol: str) -> str | None:".TO": "CAD",".V": "CAD",".BA": "ARS",+ # The UK loaders admit only a declared GBP / GBp quote and hand back+ # GBP, the same contract as the backtest's _MARKET_CURRENCY.+ ".L": "GBP",+ ".VN": "VND",}for suffix, currency in suffixes.items():if upper.endswith(suffix):diff --git a/agent/src/agent/resolution_context.py b/agent/src/agent/resolution_context.pyindex bed57861a..b8b0c2fa4 100644--- a/agent/src/agent/resolution_context.py+++ b/agent/src/agent/resolution_context.py@@ -154,8 +154,6 @@ def candidate_market(candidate: Mapping[str, Any]) -> str | None:return "hk"if suffix == "US":return "us"- if suffix == "BA":- return "ar"raw = (str(candidate.get("market") or candidate.get("exchange") or "").strip()@@ -170,10 +168,6 @@ def candidate_market(candidate: Mapping[str, Any]) -> str | None:"hong kong": "hk","us": "us","usa": "us",- "ar": "ar",- "argentina": "ar",- "buenos aires": "ar",- "bue": "ar",}.get(raw)diff --git a/agent/src/tools/symbol_search_tool.py b/agent/src/tools/symbol_search_tool.pyindex 61f7089ff..6f42c4a87 100644--- a/agent/src/tools/symbol_search_tool.py+++ b/agent/src/tools/symbol_search_tool.py@@ -142,13 +142,24 @@ _EASTMONEY_SUFFIX_BY_MARKET: Dict[str, str] = {"107": "US", # AMEX}-# Coarse market label for the candidate row, keyed by symbol suffix.+# Coarse market label for the candidate row, keyed by symbol suffix. Every+# market the data layer routes has a row, so none is labelled "global";+# test_market_identity_parity derives that list from the backtest's table._MARKET_BY_SUFFIX: Dict[str, str] = {"SH": "cn","SZ": "cn","BJ": "cn","HK": "hk","US": "us",+ "NS": "in",+ "BO": "in",+ "KS": "kr",+ "KQ": "kr",+ "TO": "ca",+ "V": "ca",+ "BA": "ar",+ "L": "uk",+ "VN": "vn",}# Hard caps so a broad query cannot bloat the envelope.@@ -788,10 +799,6 @@ def _from_yahoo_symbol(raw_symbol: str, quote: Dict[str, Any]) -> tuple[str, strif upper.endswith(".HK"):base = raw_symbol[: -len(".HK")].lstrip("0") or "0"return f"{base.zfill(5)}.HK", "hk"- if upper.endswith((".TO", ".V")):- return upper, "ca"- if upper.endswith(".BA"):- return upper, "ar"# Yahoo quotes Shanghai as ``.SS`` where this project (and Eastmoney) use# ``.SH``. Emitting both spellings published one listing as two rival# candidates, which the identity gate could not choose between, so every@@ -799,8 +806,9 @@ def _from_yahoo_symbol(raw_symbol: str, quote: Dict[str, Any]) -> tuple[str, str# sources merge and corroborate each other via ``also_from``.if upper.endswith(".SS"):return f"{upper[: -len('.SS')]}.SH", "cn"- if upper.endswith((".SH", ".SZ", ".BJ")):- return upper, "cn"+ suffix = upper.rsplit(".", 1)[-1] if "." in upper else ""+ if suffix in _MARKET_BY_SUFFIX:+ return upper, _MARKET_BY_SUFFIX[suffix]quote_type = str(quote.get("quoteType") or "").strip().upper()if quote_type == "CURRENCY":# FX pairs canonicalize to the ``XXXYYY=X`` form the fetch layerdiff --git a/agent/tests/test_market_identity_parity.py b/agent/tests/test_market_identity_parity.pynew file mode 100644index 000000000..71e6051d3--- /dev/null+++ b/agent/tests/test_market_identity_parity.py@@ -0,0 +1,48 @@+"""Every market the data layer routes is known to identity and symbol search (#1565).++A market is added in ``backtest.engines._market_hooks`` (pattern + currency) and+the backtest can then price it. The grounding identity layer and symbol search+keep their own suffix tables, and nothing tied them to that one: ``.L``, ``.VN``+and ``.BA`` each shipped with no canonical-symbol scan, no venue, no currency,+and search results labelled ``global``. This test makes the backtest's currency+table the list, so a new market fails here until it has a row.+"""++from __future__ import annotations++import pytest++from backtest.engines._market_hooks import _MARKET_CURRENCY, _detect_market+from src.agent.grounding.identity import _infer_currency, _infer_venue, _scan_symbols+from src.tools.symbol_search_tool import _from_yahoo_symbol++# market -> (project symbol, Yahoo's spelling of it, symbol-search label)+_SAMPLES: dict[str, tuple[str, str, str]] = {+ "a_share": ("600519.SH", "600519.SS", "cn"),+ "us_equity": ("AAPL.US", "AAPL", "us"),+ "hk_equity": ("00700.HK", "0700.HK", "hk"),+ "india_equity": ("RELIANCE.NS", "RELIANCE.NS", "in"),+ "kr_equity": ("005930.KS", "005930.KS", "kr"),+ "ca_equity": ("TD.TO", "TD.TO", "ca"),+ "ar_equity": ("GGAL.BA", "GGAL.BA", "ar"),+ "uk_equity": ("VOD.L", "VOD.L", "uk"),+ "vietnam_equity": ("VIC.VN", "VIC.VN", "vn"),+}++# Crypto has a currency row but no listing suffix: its pairs carry the quote.+_NOT_A_LISTING_MARKET = {"crypto"}+++def test_every_currency_market_has_a_sample() -> None:+ assert set(_SAMPLES) == set(_MARKET_CURRENCY) - _NOT_A_LISTING_MARKET++[email protected]("market", sorted(_SAMPLES))+def test_identity_and_search_know_the_market(market: str) -> None:+ symbol, yahoo_symbol, label = _SAMPLES[market]++ assert _detect_market(symbol) == market+ assert _scan_symbols(f"What did {symbol} close at yesterday?") == {symbol}+ assert _infer_venue(symbol) is not None+ assert _infer_currency(symbol) == _MARKET_CURRENCY[market]+ assert _from_yahoo_symbol(yahoo_symbol, {"quoteType": "EQUITY"}) == (symbol, label)