Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(loaders): a TSX line enters the CAD pool only when it is declared CAD

Haozhe Wu37a7e6681763
diff --git a/agent/backtest/loaders/base.py b/agent/backtest/loaders/base.py
index 32784d093..dcbf6a6ef 100644
--- a/agent/backtest/loaders/base.py
+++ b/agent/backtest/loaders/base.py
@@ -32,12 +32,21 @@ logger = logging.getLogger(__name__)
_GBP_PENCE_CURRENCY = "GBp"
_PRICE_COLUMNS = ("open", "high", "low", "close")
_UK_EQUITY_PATTERN = re.compile(r"^[A-Z0-9&.\-]+\.L$", re.I)
-# BYMA lists one issuer in pesos and again in dollars (GGAL.BA in ARS,
-# GGALD.BA in USD -- Yahoo declares each; the trailing D is not a rule, since
-# YPFD.BA is a peso line). The ar_equity pool is ARS, so the same contract as
-# LSE applies: read the declared currency, admit only ARS.
-_AR_EQUITY_PATTERN = re.compile(r"^[A-Z0-9&.\-]+\.BA$", re.I)
-_AR_EQUITY_CURRENCY = "ARS"
+# Venues that list lines in a second currency, whose market is one static
+# pool in the first. BYMA quotes GGAL.BA in ARS and GGALD.BA in USD (the
+# trailing D is not a rule: YPFD.BA is a peso line); the TSX quotes DLR.TO in
+# CAD and DLR-U.TO in USD. Yahoo declares each, and every priced source in
+# these markets' chains is Yahoo or yfinance, so the declared currency decides.
+_SINGLE_CURRENCY_VENUES: tuple[tuple[re.Pattern[str], str], ...] = (
+ (re.compile(r"^[A-Z0-9&.\-]+\.BA$", re.I), "ARS"),
+ (re.compile(r"^[A-Z0-9&.\-]+\.(?:TO|V)$", re.I), "CAD"),
+)
+
+
+def _venue_currency(code: str) -> str | None:
+ """Return the one currency a symbol's venue pool accepts, or None if unconstrained."""
+ code = str(code).strip()
+ return next((cur for pattern, cur in _SINGLE_CURRENCY_VENUES if pattern.match(code)), None)
def is_lse_symbol(code: str) -> bool:
@@ -79,8 +88,7 @@ def declared_currency_required(code: str) -> bool:
A loader must read the source's declared currency for such a symbol and
pass it to :func:`normalize_declared_quote_currency` before emitting bars.
"""
- code = str(code).strip()
- return bool(_UK_EQUITY_PATTERN.match(code) or _AR_EQUITY_PATTERN.match(code))
+ return is_lse_symbol(code) or _venue_currency(code) is not None
def normalize_declared_quote_currency(
@@ -98,17 +106,18 @@ def normalize_declared_quote_currency(
declared (GBP for an LSE line after pence scaling).
Raises:
- ValueError: If an LSE line is not declared GBP/GBp, or a BYMA line is
- not declared ARS -- either would enter a single-currency pool in
- the wrong unit.
+ ValueError: If an LSE line is not declared GBP/GBp, or a BYMA / TSX
+ line is not declared in its market's currency -- each would enter a
+ single-currency pool in the wrong unit.
"""
if is_lse_symbol(code):
return normalize_lse_quote_currency(frame, currency)
declared = currency.strip() if isinstance(currency, str) else ""
- if _AR_EQUITY_PATTERN.match(str(code).strip()) and declared != _AR_EQUITY_CURRENCY:
+ required = _venue_currency(code)
+ if required is not None and declared != required:
raise ValueError(
- f"BYMA quote currency must be declared as {_AR_EQUITY_CURRENCY}; "
- f"got {declared or 'missing'!r}"
+ f"{code} must be quoted in {required} to enter that market's pool; "
+ f"the source declared {declared or 'no currency'!r}"
)
if declared:
frame.attrs["quote_currency"] = declared
diff --git a/agent/tests/test_yahoo_loader.py b/agent/tests/test_yahoo_loader.py
index 49999ff4d..b1e99b16e 100644
--- a/agent/tests/test_yahoo_loader.py
+++ b/agent/tests/test_yahoo_loader.py
@@ -415,13 +415,26 @@ class TestLoaderMetadata:
assert loader.is_available() is True
[email protected]("declared", ["USD", ""])
-def test_fetch_rejects_a_byma_line_not_declared_in_pesos(declared: str) -> None:
- """GGALD.BA is BYMA's dollar line for Galicia; the ar_equity pool is ARS."""
+ ("symbol", "declared", "admitted"),
+ [
+ ("GGAL.BA", "ARS", True),
+ ("GGALD.BA", "USD", False), # BYMA's dollar line for Galicia
+ ("GGALD.BA", "", False),
+ ("DLR.TO", "CAD", True),
+ ("DLR-U.TO", "USD", False), # the TSX's US-dollar unit of the same fund
+ ],
+)
+def test_fetch_admits_a_line_only_in_its_markets_currency(
+ symbol: str, declared: str, admitted: bool
+) -> None:
+ """ar_equity is one ARS pool and ca_equity one CAD pool; both venues list USD lines."""
rows = [_row("2024-01-02", 4.1, 4.3, 4.0, 4.2, 1000)]
with patch(
"backtest.loaders.yahoo_loader.yahoo_client.get_chart",
return_value=(rows, declared),
):
- out = DataLoader().fetch(["GGALD.BA"], "2024-01-01", "2024-01-31")
- assert "GGALD.BA" not in out
+ out = DataLoader().fetch([symbol], "2024-01-01", "2024-01-31")
+ assert (symbol in out) is admitted
+ if admitted:
+ assert out[symbol].attrs["quote_currency"] == declared
diff --git a/agent/tests/test_yfinance_uk_scale.py b/agent/tests/test_yfinance_uk_scale.py
index d99fccd92..3a467837f 100644
--- a/agent/tests/test_yfinance_uk_scale.py
+++ b/agent/tests/test_yfinance_uk_scale.py
@@ -163,28 +163,37 @@ def test_fetch_declared_currency_failure_is_fail_closed(
assert yfl._declared_currency("VOD.L") is None
[email protected](("declared", "admitted"), [("ARS", True), ("USD", False), (None, False)])
-def test_fetch_admits_only_a_peso_quoted_byma_line(
- monkeypatch: pytest.MonkeyPatch, declared: str | None, admitted: bool
+ ("symbol", "declared", "admitted"),
+ [
+ ("GGALD.BA", "ARS", True),
+ ("GGALD.BA", "USD", False),
+ ("GGALD.BA", None, False),
+ ("DLR-U.TO", "USD", False),
+ ("DLR.TO", "CAD", True),
+ ],
+)
+def test_fetch_admits_a_byma_or_tsx_line_only_in_its_markets_currency(
+ monkeypatch: pytest.MonkeyPatch, symbol: str, declared: str | None, admitted: bool
) -> None:
- """BYMA lists GGAL.BA in ARS and GGALD.BA in USD; the ar_equity pool is ARS.
+ """BYMA and the TSX each list USD lines beside their ARS / CAD pools.
- Yahoo declared USD for SPYD.BA / GGALD.BA / AAPLD.BA (13.37 / 4.20 / 17.53,
- probed 2026-09-24) -- booked as pesos they are off by the exchange rate.
+ Yahoo declared USD for GGALD.BA (4.20) and DLR-U.TO (10.16) on 2026-09-24,
+ beside ARS for GGAL.BA and CAD for DLR.TO.
"""
monkeypatch.delenv("VIBE_TRADING_DATA_CACHE", raising=False)
asked: list[str] = []
- def declared_currency(symbol: str) -> str | None:
- asked.append(symbol)
+ def declared_currency(requested: str) -> str | None:
+ asked.append(requested)
return declared
monkeypatch.setattr(yfl, "_download_history", lambda *args: _download_frame())
monkeypatch.setattr(yfl, "_declared_currency", declared_currency)
- result = yfl.DataLoader().fetch(["GGALD.BA"], "2025-01-01", "2025-01-03")
+ result = yfl.DataLoader().fetch([symbol], "2025-01-01", "2025-01-03")
- assert asked == ["GGALD.BA"]
- assert ("GGALD.BA" in result) is admitted
+ assert asked == [symbol]
+ assert (symbol in result) is admitted
if admitted:
- assert result["GGALD.BA"].attrs["quote_currency"] == "ARS"
+ assert result[symbol].attrs["quote_currency"] == declared