Skip to content

"Vibe-Trading: Your Personal Trading Agent"

test(market-data): UK equity parity tests + volume-unit declaration

cgycorey74658b9974a0
diff --git a/agent/backtest/loaders/yahoo_loader.py b/agent/backtest/loaders/yahoo_loader.py
index eeae1b0ab..a3e3a29b8 100644
--- a/agent/backtest/loaders/yahoo_loader.py
+++ b/agent/backtest/loaders/yahoo_loader.py
@@ -185,7 +185,7 @@ class DataLoader:
# Yahoo chart volume is single shares for US/HK equities
# (HKUDS/Vibe-Trading#1062; HK verified 2026-08-11, 00700.HK ratio 1.00
# vs tencent/eastmoney). Other equity markets stay undeclared.
- volume_units = {"us_equity": "shares", "hk_equity": "shares"}
+ volume_units = {"us_equity": "shares", "hk_equity": "shares", "uk_equity": "shares"}
requires_auth = False
def is_available(self) -> bool:
diff --git a/agent/backtest/loaders/yfinance_loader.py b/agent/backtest/loaders/yfinance_loader.py
index 7fe97b42a..6c215ddf7 100644
--- a/agent/backtest/loaders/yfinance_loader.py
+++ b/agent/backtest/loaders/yfinance_loader.py
@@ -238,7 +238,7 @@ class DataLoader:
# yfinance volume is single shares for US/HK equities
# (HKUDS/Vibe-Trading#1062; HK verified 2026-08-11, 00700.HK ratio 1.00
# vs tencent/eastmoney). Crypto base-asset units stay undeclared.
- volume_units = {"us_equity": "shares", "hk_equity": "shares"}
+ volume_units = {"us_equity": "shares", "hk_equity": "shares", "uk_equity": "shares"}
requires_auth = False
def is_available(self) -> bool:
diff --git a/agent/tests/test_uk_backtest_smoke.py b/agent/tests/test_uk_backtest_smoke.py
new file mode 100644
index 000000000..c5b05b700
--- /dev/null
+++ b/agent/tests/test_uk_backtest_smoke.py
@@ -0,0 +1,105 @@
+"""End-to-end smoke test: backtest runs on UK (LSE) symbols.
+
+Drives the real market-engine routing so a ``VOD.L`` backtest lands on
+``GlobalEquityEngine(market="uk")`` and executes against in-memory LSE-style
+bars. This is the path the routing tables feed: ``source=auto`` ->
+``_MARKET_TO_SOURCE`` -> yahoo -> GlobalEquity, submarket ``uk``. Without the
+``uk_equity`` entries the same call silently produced a CryptoEngine (the
+regression this guards).
+
+All data is in-memory; no network access. LSE prices are quoted in GBp, so
+the synthetic series uses penny-scale prices and whole-share sizes (the
+generic GlobalEquity rounding path is what UK inherits, unlike HK board lots
+or Canada's tick grid).
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pandas as pd
+
+from backtest.engines.global_equity import GlobalEquityEngine
+from backtest.runner import _create_market_engine
+
+CODE = "VOD.L"
+
+_BARS = pd.DataFrame(
+ {
+ "open": [110.0 + 2 * i for i in range(9)],
+ "high": [112.0 + 2 * i for i in range(9)],
+ "low": [108.0 + 2 * i for i in range(9)],
+ "close": [111.0 + 2 * i for i in range(9)],
+ "volume": [1_000_000] * 9,
+ },
+ index=pd.bdate_range("2026-03-02", periods=9),
+)
+
+
+class _FakeLoader:
+ def fetch(self, *args, **kwargs):
+ return {CODE: _BARS.copy()}
+
+
+class _WeightSignal:
+ """Replay a fixed target-weight path, one weight per bar."""
+
+ def __init__(self, weights: list[float]) -> None:
+ self._weights = weights
+
+ def generate(self, data_map):
+ return {CODE: pd.Series(self._weights, index=data_map[CODE].index)}
+
+
+def _run(weights: list[float], run_dir: Path) -> GlobalEquityEngine:
+ config = {
+ "codes": [CODE],
+ "start_date": "2026-03-02",
+ "end_date": "2026-03-20",
+ "source": "auto",
+ "initial_cash": 1_000_000,
+ "slippage": 0.0,
+ "position_adjustment": "rebalance",
+ }
+ engine = _create_market_engine("auto", config, [CODE])
+ assert isinstance(engine, GlobalEquityEngine), type(engine)
+ engine.run_backtest(config, _FakeLoader(), _WeightSignal(weights), run_dir)
+ return engine
+
+
+def test_uk_routes_to_global_equity_engine() -> None:
+ engine = _create_market_engine("auto", {"initial_cash": 100_000}, [CODE])
+ assert isinstance(engine, GlobalEquityEngine)
+ assert engine.market == "uk"
+
+
+def test_backtest_completes_on_lse_bars(tmp_path: Path) -> None:
+ # Half weight: a fully invested target cannot fund its own commissions
+ # once equity drifts, which is BaseEngine behaviour and not under test.
+ engine = _run([0.5] * 9, tmp_path)
+ assert engine.fill_records
+ assert engine.fill_records[0].action == "open"
+
+
+def test_uk_orders_are_whole_shares(tmp_path: Path) -> None:
+ """LSE has no fractional orders; the generic equity path rounds to cents.
+
+ Unlike HK (board lots) and Canada (tick grid), UK inherits the plain
+ GlobalEquity rounding — but must still execute on every bar.
+ """
+ engine = _run([0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5], tmp_path)
+ fills = engine.fill_records
+ assert fills
+ assert all(abs(f.signed_quantity) > 0 for f in fills)
+
+
+def test_uk_zero_commission_default() -> None:
+ """The generic GlobalEquity path charges no commission for US-style books.
+
+ A UK book must not inherit HK stamp tax or Canada's broker rate. The
+ engine's calc_commission is the fee surface the market= value selects.
+ """
+ engine = _create_market_engine("auto", {"initial_cash": 100_000}, [CODE])
+ assert isinstance(engine, GlobalEquityEngine)
+ assert engine.market == "uk"
+ assert engine.calc_commission(100.0, 110.0, 1, is_open=True) == 0.0
diff --git a/agent/tests/test_uk_equity_support.py b/agent/tests/test_uk_equity_support.py
new file mode 100644
index 000000000..00c32c28f
--- /dev/null
+++ b/agent/tests/test_uk_equity_support.py
@@ -0,0 +1,99 @@
+"""Test UK/Irish equity (LSE .L / ISE .IL) market support end to end.
+
+Regression suite for issue #1205: UK symbols used to fall through the
+source/market detection tables to the tushare default and the China
+fallback chain, surfacing as ``_unresolved`` after several seconds of
+network attempts. They must now route as a first-class market with the
+same parity as Canada/US: yahoo source, ``uk_equity`` market, GBP
+settlement, and GlobalEquityEngine with ``market="uk"``.
+"""
+
+from __future__ import annotations
+
+from backtest.engines.global_equity import GlobalEquityEngine
+from backtest.engines._market_hooks import _detect_market, _detect_submarket, code_currency
+from backtest.loaders.registry import FALLBACK_CHAINS
+from backtest.runner import _create_market_engine, _MARKET_TO_SOURCE
+from src.market_data import detect_source
+
+
+class TestUKSourceDetection:
+ def test_lse_routes_to_yahoo(self) -> None:
+ assert detect_source("VOD.L") == "yahoo"
+ assert detect_source("SHEL.L") == "yahoo"
+
+ def test_ise_routes_to_yahoo(self) -> None:
+ assert detect_source("DCC.IL") == "yahoo"
+
+ def test_lowercase_suffix_routes_to_yahoo(self) -> None:
+ assert detect_source("vod.l") == "yahoo"
+
+
+class TestUKMarketClassification:
+ def test_lse_classifies_as_uk_equity(self) -> None:
+ assert _detect_market("VOD.L") == "uk_equity"
+ assert _detect_market("HSBA.L") == "uk_equity"
+
+ def test_ise_classifies_as_uk_equity(self) -> None:
+ assert _detect_market("DCC.IL") == "uk_equity"
+
+ def test_lowercase_classifies_as_uk_equity(self) -> None:
+ assert _detect_market("shel.l") == "uk_equity"
+
+ def test_submarket_detects_uk(self) -> None:
+ assert _detect_submarket(["VOD.L", "SHEL.L"]) == "uk"
+ assert _detect_submarket(["DCC.IL"]) == "uk"
+
+ def test_submarket_still_detects_other_markets(self) -> None:
+ assert _detect_submarket(["AAPL.US"]) == "us"
+ assert _detect_submarket(["TD.TO"]) == "ca"
+ assert _detect_submarket(["700.HK"]) == "hk"
+
+
+class TestUKSettlementCurrency:
+ def test_lse_settles_in_gbp(self) -> None:
+ assert code_currency("VOD.L") == "GBP"
+ assert code_currency("SHEL.L") == "GBP"
+
+ def test_ise_settles_in_gbp(self) -> None:
+ assert code_currency("DCC.IL") == "GBP"
+
+ def test_uk_market_cost_is_separate_from_cad(self) -> None:
+ # The composite engine refuses mixed-currency sets; UK must not
+ # collapse into the CAD or USD bucket.
+ assert code_currency("VOD.L") != code_currency("TD.TO")
+ assert code_currency("VOD.L") != code_currency("AAPL.US")
+
+
+class TestUKFallbackChain:
+ def test_uk_chain_prefers_yahoo(self) -> None:
+ assert FALLBACK_CHAINS["uk_equity"] == ["yahoo", "yfinance", "local"]
+
+ def test_uk_chain_is_a_member_of_global_routing(self) -> None:
+ # get_market_data's _chain_for resolves any source in any chain;
+ # yahoo must find uk_equity the same way it finds us_equity.
+ assert "yahoo" in FALLBACK_CHAINS["uk_equity"]
+
+
+class TestUKBacktestRouting:
+ def test_lse_engine_is_global_equity(self) -> None:
+ engine = _create_market_engine("yahoo", {"initial_cash": 100_000}, ["VOD.L"])
+ assert isinstance(engine, GlobalEquityEngine)
+
+ def test_lse_engine_gets_uk_submarket(self) -> None:
+ engine = _create_market_engine("yahoo", {"initial_cash": 100_000}, ["VOD.L"])
+ assert engine.market == "uk"
+
+ def test_auto_source_lse_engine_is_global_equity(self) -> None:
+ # source=auto resolves to yahoo via _MARKET_TO_SOURCE; the engine
+ # must land on GlobalEquity, never CryptoEngine (the silent wrong
+ # routing a uk_equity gap used to produce).
+ assert _MARKET_TO_SOURCE["uk_equity"] == "yahoo"
+ engine = _create_market_engine("auto", {"initial_cash": 100_000}, ["SHEL.L"])
+ assert isinstance(engine, GlobalEquityEngine)
+ assert engine.market == "uk"
+
+ def test_ise_engine_is_global_equity(self) -> None:
+ engine = _create_market_engine("auto", {"initial_cash": 100_000}, ["DCC.IL"])
+ assert isinstance(engine, GlobalEquityEngine)
+ assert engine.market == "uk"