Skip to content

"Vibe-Trading: Your Personal Trading Agent"

Merge pull request #1037 from wiliao/main

Haozhe Wub4d7ce493fcc
diff --cc agent/src/agent/grounding.py
index ec9b6b96b,52026ff40..4e71e3b49
--- a/agent/src/agent/grounding.py
+++ b/agent/src/agent/grounding.py
@@@ -364,46 -333,35 +382,70 @@@ def _utc_now() -> str
return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
+# Provider spellings that denote one instrument. Shanghai is quoted as ``.SH``
+# by Eastmoney and ``.SS`` by Yahoo, A-share tools also accept an exchange
+# prefix (``sh600519``), Hong Kong codes are zero-padded to five digits, and
+# ccxt writes a crypto pair with a slash. Every one of these is a spelling, not
+# an identity: ``_infer_venue`` and ``_infer_currency`` below already map ``.SS``
+# and ``.SH`` to the same venue and the same currency. Treating them as
+# different identities made ``search_symbol("600519")`` return two candidates
+# 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})$")
+
+
def _normalize_symbol(value: Any) -> str:
- """Normalize a symbol for exact identity comparison."""
- return str(value or "").strip().upper()
+ """Normalize a symbol onto one canonical identity for exact comparison.
+
+ Args:
+ value: Any provider- or model-supplied symbol spelling.
+
+ Returns:
+ The canonical spelling — uppercased, with Shanghai's ``.SS`` alias
+ folded onto ``.SH``, an exchange prefix rewritten as a suffix, a Hong
+ Kong code zero-padded, and a crypto pair hyphenated. Text that is not a
+ symbol is returned uppercased and otherwise untouched.
+ """
+ symbol = str(value or "").strip().upper().replace("/", "-")
+ if not symbol:
+ return ""
+ prefixed = _EXCHANGE_PREFIXED_RE.match(symbol)
+ if prefixed:
+ return f"{prefixed.group(2)}.{prefixed.group(1)}"
+ base, dot, suffix = symbol.rpartition(".")
+ if not dot:
+ return symbol
+ if suffix == "SS":
+ suffix = "SH"
+ if suffix == "HK" and base.isdigit():
+ base = base.zfill(5)
+ return f"{base}.{suffix}"
+ def _symbol_from_csv_filename(stem: str) -> str | None:
+ """Map a run-dir CSV stem back to a canonical project symbol.
+
+ The bash workaround writes filesystem-safe stems: ``BYN_V.csv`` -> ``BYN.V``,
+ ``PDI_TO.csv`` -> ``PDI.TO``, ``GC_F.csv`` -> ``GC=F``. A stem without a
+ recognized suffix (e.g. a bare US name ``AAPL``) maps to None because the
+ project convention requires an explicit venue suffix.
+
+ Args:
+ stem: CSV filename without the ``.csv`` extension.
+
+ Returns:
+ The canonical symbol, or ``None`` when the stem has no recognizable
+ venue suffix.
+ """
+ upper = (stem or "").strip().upper()
+ if not upper:
+ return None
+ for raw, canonical in _CSV_FILENAME_SUFFIX_MAP:
+ if upper.endswith(raw) and len(upper) > len(raw):
+ return upper[: -len(raw)] + canonical
+ return None
+
+
def _query_key(value: Any) -> str:
"""Normalize resolver queries into stable state-machine keys."""
return " ".join(str(value or "").casefold().split())