Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(uk): scale .L prices only on Yahoo's declared currency, never the suffix

cgycorey91aa40152a02
diff --git a/agent/backtest/loaders/yfinance_loader.py b/agent/backtest/loaders/yfinance_loader.py
index d7d65c432..76a26ecf8 100644
--- a/agent/backtest/loaders/yfinance_loader.py
+++ b/agent/backtest/loaders/yfinance_loader.py
@@ -96,6 +96,23 @@ def _to_yfinance_exclusive_end(end_date: str) -> str:
return (pd.Timestamp(end_date).normalize() + pd.Timedelta(days=1)).strftime("%Y-%m-%d")
+def _declared_currency(symbol: str) -> Optional[str]:
+ """Return Yahoo's declared currency for ``symbol``, or ``None`` when absent.
+
+ ``yf.Ticker(...).history_metadata`` carries the exchange's declared quote
+ currency. Absence (or any probe failure) MUST NOT be treated as GBp: a
+ missing currency means "do not scale", not "assume pence" — the suffix
+ heuristic alone would wrongly ÷100 every LSE line priced in GBP or USD
+ (e.g. VUSA.L=GBP, VUSD.L=USD).
+ """
+ try:
+ meta = yf.Ticker(symbol).history_metadata
+ except Exception: # noqa: BLE001 — a metadata probe failure is not data
+ return None
+ currency = meta.get("currency") if isinstance(meta, dict) else None
+ return currency if isinstance(currency, str) and currency else None
+
+
def _download_history(
tickers: Union[List[str], str],
start_date: str,
@@ -330,10 +347,16 @@ class DataLoader:
logger.warning("yfinance returned no usable data for %s", symbol)
continue
- # Yahoo-family data: .L UK names quote in GBp (pence).
- # Normalize ÷100 so code_currency's GBP matches the values.
+ # Yahoo-family data: LSE names may quote in GBp (pence) —
+ # normalize ÷100 so code_currency's GBP matches the values.
+ # Scale ONLY on Yahoo's declared currency (GBp/p). A GBP or
+ # USD-priced .L line (VUSA.L=GBP, VUSD.L=USD) must pass
+ # through unscaled; a missing currency means "do not scale"
+ # (fail closed — never assume pence from the suffix).
if is_gbp_pence_symbol(symbol):
- normalized, _ = scale_pence_to_currency(normalized, "GBp")
+ declared = _declared_currency(symbol)
+ if declared in ("GBp", "p"):
+ normalized, _ = scale_pence_to_currency(normalized, "GBp")
loader_cache_put(
source=self.name,
diff --git a/agent/tests/test_yfinance_uk_scale.py b/agent/tests/test_yfinance_uk_scale.py
index e3fbd67f3..57a18a069 100644
--- a/agent/tests/test_yfinance_uk_scale.py
+++ b/agent/tests/test_yfinance_uk_scale.py
@@ -70,3 +70,73 @@ def test_fetch_leaves_us_prices_untouched(monkeypatch: pytest.MonkeyPatch) -> No
result = yfl.DataLoader().fetch(["AAPL.US"], "2025-01-01", "2025-01-03")
assert result["AAPL.US"]["close"].iloc[0] == pytest.approx(117.5)
+
+
+def test_fetch_leaves_gbp_quoted_lse_unscaled(monkeypatch: pytest.MonkeyPatch) -> None:
+ # Reviewer finding: .L is not uniformly GBp — VUSA.L is priced GBP and
+ # must NOT be ÷100'd. Scale only on the declared currency.
+ monkeypatch.delenv("VIBE_TRADING_DATA_CACHE", raising=False)
+
+ def fake_download(tickers, start_date, end_date, interval):
+ assert tickers == ["VUSA.L"]
+ return _download_frame()
+
+ monkeypatch.setattr(yfl, "_download_history", fake_download)
+ monkeypatch.setattr(yfl, "_declared_currency", lambda symbol: "GBP")
+
+ result = yfl.DataLoader().fetch(["VUSA.L"], "2025-01-01", "2025-01-03")
+
+ frame = result["VUSA.L"]
+ assert frame["close"].iloc[0] == pytest.approx(117.5) # untouched
+
+
+def test_fetch_leaves_usd_quoted_lse_unscaled(monkeypatch: pytest.MonkeyPatch) -> None:
+ # VUSD.L is USD-priced .L: suffix alone would wrongly ÷100.
+ monkeypatch.delenv("VIBE_TRADING_DATA_CACHE", raising=False)
+
+ def fake_download(tickers, start_date, end_date, interval):
+ assert tickers == ["VUSD.L"]
+ return _download_frame()
+
+ monkeypatch.setattr(yfl, "_download_history", fake_download)
+ monkeypatch.setattr(yfl, "_declared_currency", lambda symbol: "USD")
+
+ result = yfl.DataLoader().fetch(["VUSD.L"], "2025-01-01", "2025-01-03")
+
+ frame = result["VUSD.L"]
+ assert frame["close"].iloc[0] == pytest.approx(117.5) # untouched
+
+
+def test_fetch_fails_closed_when_currency_absent(monkeypatch: pytest.MonkeyPatch) -> None:
+ # A missing declared currency means "do not scale", not "assume pence":
+ # the suffix heuristic must never be the fallback.
+ monkeypatch.delenv("VIBE_TRADING_DATA_CACHE", raising=False)
+
+ def fake_download(tickers, start_date, end_date, interval):
+ assert tickers == ["VOD.L"]
+ return _download_frame()
+
+ monkeypatch.setattr(yfl, "_download_history", fake_download)
+ monkeypatch.setattr(yfl, "_declared_currency", lambda symbol: None)
+
+ result = yfl.DataLoader().fetch(["VOD.L"], "2025-01-01", "2025-01-03")
+
+ frame = result["VOD.L"]
+ assert frame["close"].iloc[0] == pytest.approx(117.5) # NOT ÷100'd
+
+
+def test_fetch_scales_only_on_gbp_pence(monkeypatch: pytest.MonkeyPatch) -> None:
+ # GBp remains the only scaling trigger: real pence names still ÷100.
+ monkeypatch.delenv("VIBE_TRADING_DATA_CACHE", raising=False)
+
+ def fake_download(tickers, start_date, end_date, interval):
+ assert tickers == ["VOD.L"]
+ return _download_frame()
+
+ monkeypatch.setattr(yfl, "_download_history", fake_download)
+ monkeypatch.setattr(yfl, "_declared_currency", lambda symbol: "GBp")
+
+ result = yfl.DataLoader().fetch(["VOD.L"], "2025-01-01", "2025-01-03")
+
+ frame = result["VOD.L"]
+ assert frame["close"].iloc[0] == pytest.approx(1.175) # still scaled