Skip to content

"Vibe-Trading: Your Personal Trading Agent"

feat(swarm): ground bare US tickers mentioned in swarm prompts

Haozhe Wudfd71b4bc218
diff --git a/agent/src/swarm/grounding.py b/agent/src/swarm/grounding.py
index e3ab1191b..72edd22ea 100644
--- a/agent/src/swarm/grounding.py
+++ b/agent/src/swarm/grounding.py
@@ -19,11 +19,28 @@ What this module does
swallowed per-symbol so they do not poison the whole run.
* Renders a compact markdown block the worker prompt can splice in.
-What this module **does not** do
---------------------------------
-* It does not match bare tickers (``NVDA`` without ``.US``) — too easy
- to false-positive on common English words. Users supply suffixed
- symbols already in every shipped preset.
+Bare US tickers
+---------------
+Suffixed symbols are matched verbatim. Bare all-caps tokens (``NVDA``
+without ``.US``) are *promoted* to ``<TOKEN>.US`` under guards, because
+auto-built swarm variables routinely carry the user's raw prompt and
+real prompts say "long or short on NVDA", not "NVDA.US" (#198):
+
+* only 2–5 uppercase letters on word boundaries (never lowercase,
+ never single letters — too collision-prone);
+* a stopword list drops common finance/English acronyms (``ETF``,
+ ``CEO``, ``GDP``, ``USD``, bare crypto symbols, …);
+* text already matched by a suffixed pattern is blanked first, so
+ ``BTC-USDT`` never leaks a bogus ``BTC.US``;
+* promotions sort *after* explicit symbols, so explicit symbols win
+ the ``DEFAULT_MAX_SYMBOLS`` cap;
+* the per-symbol fetch remains the final validator — a promoted token
+ that is not a real Yahoo ticker returns no data and is dropped.
+
+A residual risk stays by design: an all-caps non-ticker word that
+collides with a real listed product (e.g. ``MOAT``) grounds an
+irrelevant table. That costs prompt budget, not correctness — workers
+are told to cite only symbols they analyze.
* It does not refresh data mid-run. The block is a snapshot taken once
when the background run starts; long-running swarms will see stale data after
many minutes, but that is still strictly better than training-data
@@ -62,21 +79,66 @@ _SYMBOL_PATTERNS: tuple[re.Pattern[str], ...] = (
re.compile(r"\b[A-Z]{2,6}-USDT\b"),
)
+# Bare-ticker promotion: 2–5 uppercase letters. Single letters (A, F, T …)
+# collide with ordinary prose far too often to be worth grounding. The
+# lookarounds reject dotted compounds on either side (FOO.USDA promotes
+# neither FOO nor USDA) while still matching a sentence-ending "NVDA.".
+_BARE_US_TICKER_PATTERN = re.compile(r"(?<![\w.])[A-Z]{2,5}(?!\w)(?!\.\w)")
+
+# All-caps tokens that show up in finance prompts but must never be promoted
+# to a .US symbol — either not tickers at all, or colliding with unrelated
+# listed products (CEO and MSCI are both real Yahoo symbols).
+_BARE_TICKER_STOPWORDS = frozenset({
+ # geography / venues / index & data providers
+ "US", "USA", "UK", "EU", "HK", "CN", "JP", "NYSE", "AMEX", "SSE", "SZSE",
+ "HKEX", "SPX", "NDX", "DJI", "DJIA", "HSI", "CSI", "FTSE", "MSCI", "VIX",
+ # instruments / structures
+ "ETF", "ETN", "ADR", "IPO", "REIT", "BOND", "SWAP", "PERP",
+ # macro / institutions
+ "FED", "FOMC", "SEC", "IMF", "GDP", "CPI", "PPI", "PMI", "PCE", "OPEC",
+ "YOY", "QOQ", "MOM", "YTD", "EOD",
+ # metrics / indicators
+ "PE", "PB", "PS", "EPS", "ROE", "ROA", "ROI", "EBIT", "EV", "DCF",
+ "CAGR", "IRR", "NAV", "AUM", "ATH", "ATL", "RSI", "MACD", "EMA", "SMA",
+ "KDJ", "BOLL", "OHLC", "ADV", "PNL",
+ # currencies / crypto traded under other loaders
+ "USD", "EUR", "JPY", "GBP", "CNY", "CNH", "RMB", "KRW", "INR", "AUD",
+ "CAD", "CHF", "FX", "BTC", "ETH", "SOL", "XRP", "BNB", "ADA", "DOGE",
+ "USDT", "USDC", "DEFI", "NFT", "DAO",
+ # trading verbs / order words
+ "BUY", "SELL", "HOLD", "LONG", "SHORT", "CALL", "PUT", "STOP", "LIMIT",
+ "TP", "SL", "DCA",
+ # tech / prose acronyms
+ "AI", "ML", "LLM", "API", "JSON", "CSV", "PDF", "URL", "HTML", "CEO",
+ "CFO", "CTO", "COO", "CIO", "VP", "OK", "FAQ", "ASAP", "AM", "PM",
+ "EST", "PST", "UTC", "GMT",
+})
+
def extract_symbols_from_user_vars(user_vars: dict[str, str]) -> list[str]:
"""Return the deduplicated list of symbols mentioned anywhere in *user_vars*.
- Order is preserved from first occurrence so the worker prompt is
- deterministic when nothing else changes.
+ Explicit suffixed symbols come first (in first-occurrence order),
+ followed by guarded bare-ticker promotions (``NVDA`` → ``NVDA.US``),
+ so explicit symbols always win the grounding cap. See the module
+ docstring for the promotion guards.
"""
- seen: dict[str, None] = {} # ordered set
+ explicit: dict[str, None] = {} # ordered set
+ promoted: dict[str, None] = {}
for value in user_vars.values():
if not isinstance(value, str):
continue
+ remainder = value
for pattern in _SYMBOL_PATTERNS:
- for match in pattern.findall(value):
- seen.setdefault(match, None)
- return list(seen.keys())
+ for match in pattern.findall(remainder):
+ explicit.setdefault(match, None)
+ # Blank matched spans so the bare scan can't split a suffixed
+ # symbol into bogus fragments (BTC-USDT -> BTC.US).
+ remainder = pattern.sub(" ", remainder)
+ for token in _BARE_US_TICKER_PATTERN.findall(remainder):
+ if token not in _BARE_TICKER_STOPWORDS:
+ promoted.setdefault(f"{token}.US", None)
+ return list(explicit) + [s for s in promoted if s not in explicit]
def max_grounding_symbols() -> int:
diff --git a/agent/tests/test_swarm_grounding.py b/agent/tests/test_swarm_grounding.py
index 9f49275e5..b5e4a1d3f 100644
--- a/agent/tests/test_swarm_grounding.py
+++ b/agent/tests/test_swarm_grounding.py
@@ -58,13 +58,52 @@ def test_extract_returns_empty_when_no_symbol_present() -> None:
def test_extract_skips_non_string_values() -> None:
user_vars = {
- "ticker": "TSM", # bare ticker — intentionally not matched
"weight": 0.5, # type: ignore[dict-item] — not a str
"real_target": "TSLA.US",
}
assert grounding.extract_symbols_from_user_vars(user_vars) == ["TSLA.US"]
+def test_extract_promotes_bare_us_ticker() -> None:
+ # The #198 reporter's exact shape: investment_committee target text with
+ # a bare US ticker and no loader suffix anywhere.
+ user_vars = {
+ "target": "Evaluate whether to go long or short on NVDA given current market conditions",
+ "market": "A-shares",
+ }
+ assert grounding.extract_symbols_from_user_vars(user_vars) == ["NVDA.US"]
+
+
+def test_extract_bare_ticker_skips_common_acronyms() -> None:
+ user_vars = {
+ "goal": "US CPI and FED policy impact on AI ETF flows; CEO guidance, PE ratios, USD strength",
+ }
+ assert grounding.extract_symbols_from_user_vars(user_vars) == []
+
+
+def test_extract_bare_ticker_does_not_duplicate_suffixed_symbol() -> None:
+ user_vars = {"goal": "Compare NVDA.US against a bare NVDA mention"}
+ assert grounding.extract_symbols_from_user_vars(user_vars) == ["NVDA.US"]
+
+
+def test_extract_bare_scan_does_not_split_suffixed_symbols() -> None:
+ # BTC-USDT must stay one crypto pair; neither BTC.US nor USDT.US may leak.
+ user_vars = {"goal": "Hedge BTC-USDT exposure into quarter end"}
+ assert grounding.extract_symbols_from_user_vars(user_vars) == ["BTC-USDT"]
+
+
+def test_extract_explicit_symbols_rank_before_bare_promotions() -> None:
+ # Explicit suffixed symbols must win the max-symbols cap, so they sort
+ # first even when a bare ticker appears earlier in the text.
+ user_vars = {"goal": "MSTR leverage versus 600519.SH stability"}
+ assert grounding.extract_symbols_from_user_vars(user_vars) == ["600519.SH", "MSTR.US"]
+
+
+def test_extract_ignores_lowercase_and_single_letter_tokens() -> None:
+ user_vars = {"goal": "buy nvda now, grade A balance sheet"}
+ assert grounding.extract_symbols_from_user_vars(user_vars) == []
+
+
def test_extract_does_not_match_substrings_inside_words() -> None:
user_vars = {
# \b boundary should keep "FOO.USDA" / "BLAH.USA" from matching .US