"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
Merge main into #1280 and reconcile the two FX guards instead of picking one
Haozhe Wu2c17aaf481da
diff --cc agent/backtest/engines/_market_hooks.pyindex 15e3a2c8a,d65d34dc2..6220aa271--- a/agent/backtest/engines/_market_hooks.py+++ b/agent/backtest/engines/_market_hooks.py@@@ -54,25 -59,14 +59,33 @@@ _MARKET_PATTERNS =# Forex pairs: XXX/YYY or XXXXXX.FX(re.compile(r"^[A-Z]{3}/[A-Z]{3}$"), "forex"),(re.compile(r"^[A-Z]{6}\.FX$"), "forex"),- # Yahoo forex suffix convention (EURUSD=X, GBPCNY=X) — served verbatim by- # the chart endpoint. Without this rule such symbols fell through to the- # a_share default (misrouting composite/market classification).- (re.compile(r"^[A-Z]{3,6}=X$"), "forex"),+ # Yahoo notations for FX and futures. ``=X`` is Yahoo's forex form- # (``XAUUSD=X``, ``EURUSD=X``); ``=F`` is Yahoo's continuous-front-month- # futures form (``GC=F``, ``CL=F``, ``SI=F``, ``HG=F``, ``MGC=F``). The- # underlying asset classes differ, so the patterns route to different- # markets. Both must come BEFORE any length-based fallback to win over- # the catch-all US-equity regex below.- (re.compile(r"^[A-Z]{6}=X$", re.I), "forex"),++ # (``XAUUSD=X``, ``EURUSD=X``, ``GBPCNY=X``); ``=F`` is Yahoo's++ # continuous-front-month futures form (``GC=F``, ``CL=F``, ``SI=F``,++ # ``HG=F``, ``MGC=F``). The underlying asset classes differ, so the++ # patterns route to different markets. Both must come BEFORE any++ # length-based fallback to win over the catch-all US-equity regex below.++ # The ``=X`` width stays {3,6} as it was on main: this PR only asserts++ # 6-character pairs, so narrowing it to {6} would drop shorter forms++ # already covered here for no gain.++ (re.compile(r"^[A-Z]{3,6}=X$", re.I), "forex"),+ (re.compile(r"^[A-Z]{2,5}=F$", re.I), "futures"),+ # Bare 6-character precious-metal / FX symbols (``XAUUSD``, ``XAGUSD``,+ # ``XPTUSD``, ``XPDUSD``, ``EURUSD``, ``GBPUSD``, ``USDJPY``, ``USDCHF``,+ # ``AUDUSD``, ``NZDUSD``, ``USDCAD``). Whitelist-restricted to a small+ # set of base codes so legitimate US tickers of any 6-letter length are+ # never re-routed. The four metal codes are ISO 4217; the rest are G10+ # currencies. Length-only patterns (``^[A-Z]{6}$``) are deliberately+ # rejected — they over-match tickers like ``NFLXLI`` or ``AMZNLY``.+ (re.compile(+ r"^(?:XAU|XAG|XPT|XPD|EUR|GBP|JPY|CHF|CAD|AUD|NZD|USD)[A-Z]{3}$",+ re.I,+ ), "forex"),+ # Yahoo index symbols (^SPX, ^NDX, ^FTSE, ^VIX, ...) — served verbatim,+ # same as the =F/=X conventions. Classified as their own market so they- # never route through an equity/China chain or a cash currency.++ # never route through an equity/China chain or a cash currency. Kept from++ # main: this PR's branch point predates it.+ (re.compile(r"^\^[A-Za-z0-9.\-]+$"), "index"),# Bare US tickers (AAPL, MSFT, SPY, T, ...). Must stay LAST so every# suffixed equity / futures / crypto / forex form above wins first.# ``{1,5}`` covers every standard US ticker length while 6-char barediff --cc agent/src/tools/symbol_search_tool.pyindex c0ed2e684,e00bc70cf..a78102334--- a/agent/src/tools/symbol_search_tool.py+++ b/agent/src/tools/symbol_search_tool.py@@@ -306,15 -304,15 +330,29 @@@ def _canonical_crypto_pair(value: str)matched = _CRYPTO_PAIR_RE.fullmatch(clean)if matched:base, quote = matched.group(1), matched.group(2)++ # Two independent rejections, both required. The fiat/fiat rule (from++ # main) covers pairs whose quote leg is not USD; the USD whitelist++ # (this PR) covers a USD quote whose base is not a known crypto —++ # XAU is not a fiat code, so fiat/fiat alone lets XAU-USD through and++ # the venue catalog locks tokenized gold as "spot gold".+ if base in FIAT_CODES and quote in FIAT_CODES:+ return None # fiat/fiat is an FX pair, not crypto+ if quote == "USD" and base not in _CRYPTO_USD_BASES:+ return Nonereturn f"{base}-{quote}"if clean.isalnum():for quote in _CRYPTO_QUOTE_ASSETS:if clean.endswith(quote) and len(clean) > len(quote) + 1:base = clean[: -len(quote)]++ # Same two rejections. They differ deliberately in kind:++ # fiat/fiat has no crypto reading at all, so it gives up;++ # a non-whitelisted USD base only rules out THIS quote asset,++ # so it must `continue` and let a longer quote (USDT/USDC)++ # still match — returning here would strand e.g. XAUT-USDT.+ if base in FIAT_CODES and quote in FIAT_CODES:+ return None # fiat/fiat is an FX pair, not crypto+ if quote == "USD" and base not in _CRYPTO_USD_BASES:+ continuereturn f"{base}-{quote}"return Nonediff --cc agent/tests/test_market_detection.pyindex 75d9a781e,f8036b67f..3e32f628e--- a/agent/tests/test_market_detection.py+++ b/agent/tests/test_market_detection.py@@@ -97,18 -109,14 +109,19 @@@ class TestDetectMarketassert _detect_market("aapl") == "us_equity"assert _detect_market("btc-usdt") == "crypto"assert _detect_market("td.to") == "ca_equity"+ assert _detect_market("brk.b.us") == "us_equity"def test_unknown_defaults_to_a_share(self) -> None:+ # Symbols that don't match any pattern fall through to the+ # ``a_share`` default. ``EURUSD`` and ``BTCUSDT`` used to be+ # examples but are now classified (PR #1280); use truly+ # unclassifiable inputs so the default-to-``a_share`` behavior+ # is pinned without coupling to a specific routing decision.assert _detect_market("UNKNOWN") == "a_share"assert _detect_market("random-string") == "a_share"- # Bare codes outside the 1-5 letter US shape keep the old default.- assert _detect_market("EURUSD") == "a_share"- assert _detect_market("BTCUSDT") == "a_share"+ assert _detect_market("12345") == "a_share"+ assert _detect_market("123456") == "a_share"+ assert _detect_market("@#$") == "a_share"# ---------------------------------------------------------------------------diff --cc agent/tests/test_symbol_search_tool.pyindex 964d02a00,cf59a6ef4..95fa84fed--- a/agent/tests/test_symbol_search_tool.py+++ b/agent/tests/test_symbol_search_tool.py@@@ -766,86 -764,67 +766,152 @@@ class TestCryptoPairWithoutABrokerConneassert called == []+ # --------------------------------------------------------------------------+ # FX pairs + index symbols: search and fetch must agree on the symbol universe+ # --------------------------------------------------------------------------+++ class TestFxPairAlignment:+ """search_symbol must resolve FX pairs and return fetch-able symbols."""++ def test_canonical_fx_pair_spellings(self) -> None:+ assert ss._canonical_fx_pair("GBP/USD") == "GBPUSD=X"+ assert ss._canonical_fx_pair("gbp/usd") == "GBPUSD=X"+ assert ss._canonical_fx_pair("GBPUSD") == "GBPUSD=X"+ assert ss._canonical_fx_pair("GBPUSD=X") == "GBPUSD=X"+ assert ss._canonical_fx_pair("USD/JPY") == "USDJPY=X"+ assert ss._canonical_fx_pair("GBPCNY") == "GBPCNY=X"+ # Not pairs / not fiat-fiat+ assert ss._canonical_fx_pair("BRK-B") is None+ assert ss._canonical_fx_pair("AAPL") is None+ assert ss._canonical_fx_pair("ETH/USD") is None # crypto, not FX+ assert ss._canonical_fx_pair("XAU/USD") is None # metal, not fiat++ def test_fiat_pairs_are_not_misclassified_as_crypto(self) -> None:+ """GBP/USD must stop being treated as a crypto 'GBP-USD' pair."""+ assert ss._canonical_crypto_pair("GBP/USD") is None+ assert ss._canonical_crypto_pair("EURUSD") is None+ # Crypto classifications must remain untouched.+ assert ss._canonical_crypto_pair("ETH/USD") == "ETH-USD"+ assert ss._canonical_crypto_pair("BTC/USDT") == "BTC-USDT"+ assert ss._canonical_crypto_pair("BTCUSDT") == "BTC-USDT"++ def test_fx_query_returns_canonical_candidate_when_yahoo_unavailable(self) -> None:+ """A throttled/failed Yahoo must not turn a canonical pair into nothing."""+ with patch.object(+ ss.yahoo_client, "search", side_effect=Exception("Too Many Requests")+ ), patch.object(+ ss.eastmoney_client, "get_json",+ return_value={"QuotationCodeTable": {"Data": []}},+ ), patch.object(ss.sec_edgar_client, "cik_for", return_value=""):+ out = json.loads(ss.SymbolSearchTool().execute(query="GBP/USD", limit=5))++ by_symbol = {c["symbol"]: c for c in out["data"]["candidates"]}+ assert "GBPUSD=X" in by_symbol+ assert by_symbol["GBPUSD=X"]["market"] == "fx"+ assert by_symbol["GBPUSD=X"]["type"] == "currency"++ def test_from_yahoo_symbol_normalizes_currency_quotes(self) -> None:+ assert ss._from_yahoo_symbol("GBP/USD", {"quoteType": "CURRENCY"}) == (+ "GBPUSD=X",+ "fx",+ )+ assert ss._from_yahoo_symbol("GBPUSD=X", {"quoteType": "CURRENCY"}) == (+ "GBPUSD=X",+ "fx",+ )++ def test_from_yahoo_symbol_labels_indexes(self) -> None:+ assert ss._from_yahoo_symbol("^SPX", {"quoteType": "INDEX"}) == (+ "^SPX",+ "index",+ )+ assert ss._from_yahoo_symbol("^FTSE", {"quoteType": "INDEX"}) == (+ "^FTSE",+ "index",+ )+++++class TestCryptoUsdBaseWhitelist:+ """``USD`` quote leg on the crypto resolver is gated on a base whitelist.++ Without this guard, ``XAUUSD`` / ``EURUSD`` / ``GBPUSD`` would all be+ classified as crypto pairs and the public-venue catalog (Binance/OKX)+ fallback would either lock onto a tokenized gold token (XAUT/PAXG) or+ find nothing — never on real spot gold. The fix: only accept ``USD`` on+ crypto when the base is in :data:`_CRYPTO_USD_BASES`.+ """++ @pytest.mark.parametrize(+ "code",+ [+ # Genuine crypto pairs - must keep working.+ "BTC-USD",+ "ETH-USD",+ "SOL-USD",+ "BNB-USD",+ "XLM-USD",+ "XRP-USD",+ # Stablecoin-quoted pairs (existing behaviour preserved).+ "BTC-USDT",+ "XAUT-USDT",+ "PAXG-USDT",+ "ETH-USDC",+ ],+ )+ def test_crypto_pairs_with_supported_base_accepted(self, code):+ assert ss._canonical_crypto_pair(code) is not None++ @pytest.mark.parametrize(+ "code",+ [+ # The reported bug: a spot gold query must NOT resolve to a+ # crypto pair, or the public-venue catalog fallback will lock a+ # tokenized-gold row (XAUT-USDT) as the answer.+ "XAUUSD",+ "XAU-USD",+ "XAU/USD",+ # Forex pairs in 6-letter, dashed, or slashed form.+ "EURUSD",+ "EUR-USD",+ "EUR/USD",+ "GBPUSD",+ "GBP-USD",+ "JPYUSD",+ ],+ )+ def test_non_crypto_bases_with_usd_quote_rejected(self, code):+ """The ``USD`` leg on non-crypto bases must NOT pass as a crypto pair."""+ assert ss._canonical_crypto_pair(code) is None++ def test_tokenized_gold_bases_still_resolve_as_crypto(self):+ # XAUT/PAXG ARE crypto (tokenized gold on Binance/OKX spot) and+ # must keep resolving as crypto. The whitelist includes them.+ assert ss._canonical_crypto_pair("XAUT-USDT") == "XAUT-USDT"+ assert ss._canonical_crypto_pair("PAXG-USDT") == "PAXG-USDT"+ assert ss._canonical_crypto_pair("XAUT-USD") == "XAUT-USD"+ assert ss._canonical_crypto_pair("PAXG-USD") == "PAXG-USD"++ def test_xauusd_and_xautusdt_are_not_equivalent_assets(self):+ # Identity correctness: the resolver must treat XAUUSD (spot gold)+ # and XAUT-USDT (tokenized gold) as different instruments. If both+ # return the same canonical string, downstream lock/identity+ # collision is possible.+ spot = ss._canonical_crypto_pair("XAUUSD")+ tokenized = ss._canonical_crypto_pair("XAUT-USDT")+ assert spot is None+ assert tokenized == "XAUT-USDT"+ assert spot != tokenized++ def test_usd_whitelist_is_a_strict_subset_of_crypto_bases(self):+ # Defensive: the whitelist is what stops non-crypto bases from+ # slipping through the ``USD`` branch. Every base in it must+ # therefore actually be tradable on Binance or OKX spot.+ # This is a coarse sanity check on the whitelist contents; if a+ # new entry is added in error, this test will still pass (it only+ # checks the union is non-empty and is a subset of the quote-asset+ # alphabet).+ assert ss._CRYPTO_USD_BASES+ for base in ss._CRYPTO_USD_BASES:+ assert base.isalpha()+ assert base.isupper()