"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(grounding): normalize every Futu venue prefix, not just HK
Haozhe Wu2a40fef90818
diff --git a/agent/src/agent/grounding.py b/agent/src/agent/grounding.pyindex 2dd10496f..55ecfbafe 100644--- a/agent/src/agent/grounding.py+++ b/agent/src/agent/grounding.py@@ -198,7 +198,17 @@ _JOINED_CRYPTO_RE = re.compile(_CANONICAL_SYMBOL_RE = re.compile(r"(?<![A-Za-z0-9_])(?:"r"\d{3,6}\.(?:SH|SZ|BJ|SS|HK|KS|KQ)|"- r"HK\.\d{3,6}|" # <-- added: HK-prefixed code (Futu connector format, e.g. HK.06693)+ # Futu writes the venue as a PREFIX (HK.00700 / SH.600519 / US.AAPL). The+ # suffix branch above cannot see it, so a user who pasted a connector code+ # got no identity lock at all and every market tool answered+ # identity_required. Handled for the whole prefix set, not just HK: the+ # connector emits all four, and one venue's fix leaves the same hole open+ # in the next.+ r"(?:HK|SH|SZ|BJ|SS)\.\d{3,6}|"+ # Case-SENSITIVE (the connector writes it uppercase): a case-folded+ # 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)|"r"[A-Z0-9]{2,15}(?:-|/)(?:USDT|USDC|USD|BTC|ETH)|"r"[A-Z]{2,15}(?:" + "|".join(_JOINED_CRYPTO_QUOTE_SUFFIXES) + r")|"@@ -813,6 +823,11 @@ def _utc_now() -> str:# for one listing, which no tie-break could resolve, so every Shanghai listing# resolved ``ambiguous`` and no market tool could run for the rest of the run._EXCHANGE_PREFIXED_RE = re.compile(r"^(SH|SZ|BJ)(\d{6})$")+# The dotted form of the same idea, as the Futu connector spells it+# (``HK.00700`` / ``US.AAPL``). ``SS`` is Yahoo's Shanghai alias and folds+# onto ``SH`` exactly as the suffix spelling does.+_VENUE_PREFIXES = frozenset({"HK", "SH", "SZ", "BJ", "SS", "US"})+_US_TICKER_RE = re.compile(r"[A-Z][A-Z0-9&-]{0,19}")def _normalize_symbol(value: Any) -> str:@@ -859,12 +874,19 @@ def _normalize_symbol(value: Any) -> str:if base_part.isalpha():return f"{base_part}-{quote}"return symbol- # HK-prefixed listing (Futu connector format, e.g. HK.06693 / HK.01288):- # rewrite to the canonical .HK suffix so identity matching agrees with the- # market-data chain (06693.HK) used by get_market_data.- if base == "HK" and suffix.replace(".", "", 1).isdigit():- digits = suffix.split(".")[0]- return f"{digits.zfill(5)}.HK"+ # Venue-prefixed listing (Futu connector format: HK.06693 / SH.600519 /+ # SZ.000001 / US.AAPL): rewrite to the canonical suffix spelling so+ # identity matching agrees with the market-data chain (06693.HK) that+ # get_market_data uses. Shanghai's .SS alias is folded onto .SH here too,+ # the same way the suffix branch below does it.+ if base in _VENUE_PREFIXES and suffix:+ venue = "SH" if base == "SS" else base+ if venue == "US":+ if _US_TICKER_RE.fullmatch(suffix):+ return f"{suffix}.US"+ elif suffix.isdigit():+ digits = suffix.zfill(5) if venue == "HK" else suffix+ return f"{digits}.{venue}"if suffix == "SS":suffix = "SH"if suffix == "HK" and base.isdigit():diff --git a/agent/tests/test_agent_grounding.py b/agent/tests/test_agent_grounding.pyindex 4141c21d9..cf923c069 100644--- a/agent/tests/test_agent_grounding.py+++ b/agent/tests/test_agent_grounding.py@@ -3630,3 +3630,50 @@ def test_generic_header_table_matches_prose_verdicts(tmp_path: Path) -> None:assert bare.validate_final_answer("| 指标 | 数值 |\n|---|---|\n| 预计夏普比率 | 1.2 |").valid is True+++ ("raw", "expected"),+ [+ # Futu writes the venue as a prefix; every one of them must land on+ # the same identity the market-data chain uses.+ ("HK.06693", "06693.HK"),+ ("HK.00700", "00700.HK"),+ ("HK.700", "00700.HK"), # zero-padded like the suffix spelling+ ("US.AAPL", "AAPL.US"),+ ("US.BRK-B", "BRK-B.US"),+ ("SH.600519", "600519.SH"),+ ("SS.600519", "600519.SH"), # Yahoo's Shanghai alias folds onto .SH+ ("SZ.000001", "000001.SZ"),+ ("BJ.430047", "430047.BJ"),+ # Negatives: a non-numeric venue code is not a listing (HK.HSI is an+ # index feed), and the suffix spellings stay untouched.+ ("HK.HSI", "HK.HSI"),+ ("06693.HK", "06693.HK"),+ ("AAPL.US", "AAPL.US"),+ ("600519.SH", "600519.SH"),+ ],+)+def test_normalize_venue_prefixed_symbols(raw: str, expected: str) -> None:+ """A Futu-style venue prefix normalizes onto the canonical suffix form."""+ assert _normalize_symbol(raw) == expected+++ ("text", "expected"),+ [+ ("分析港股 HK.06693 的走势", {"06693.HK"}),+ ("持仓 US.AAPL 与 HK.00700", {"AAPL.US", "00700.HK"}),+ ("对比 SH.600519 和 SZ.000001", {"600519.SH", "000001.SZ"}),+ # Negatives: prose and URLs must not become symbols. The US branch is+ # case-sensitive precisely so a "…/us.reuters/…" host cannot.+ ("Revenue grew in the US. Apple led the pack.", set()),+ ("Listed in the U.S. AAPL is the largest.", set()),+ ("see https://example.com/us.quotes for details", set()),+ ],+)+def test_scan_symbols_detects_venue_prefixed_symbols(+ text: str, expected: set[str]+) -> None:+ """A pasted connector code is locked as an identity, prose is not."""+ assert _scan_symbols(text) == expected