"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
feat(equity): model UK SDRT — 0.5% purchase-side stamp duty on LSE books
cgycorey3f2c70ebf6cd
diff --git a/agent/backtest/engines/global_equity.py b/agent/backtest/engines/global_equity.pyindex 9c0fe007f..b564ecb8e 100644--- a/agent/backtest/engines/global_equity.py+++ b/agent/backtest/engines/global_equity.py@@ -24,6 +24,7 @@ India (NSE/BSE) is handled by the dedicated ``backtest.engines.india_equity``from __future__ import annotationsimport math+from decimal import Decimal, ROUND_HALF_UPimport pandas as pd@@ -42,6 +43,10 @@ class GlobalEquityEngine(BaseEngine):- hk_settlement: default 0.00002 (CCASS)- slippage_ca: defaults to slippage_us- ca_commission: broker commission rate, default 0.0+ - slippage_uk: defaults to slippage_us+ - uk_stamp_tax: default 0.005 (0.5% Stamp Duty Reserve Tax on+ purchases only; chargeable on the buyer, paid on buying to close+ short positions too)"""def __init__(self, config: dict, market: str = "us"):@@ -61,6 +66,13 @@ class GlobalEquityEngine(BaseEngine):# rate instead of inventing an exchange-wide charge.self.slippage_ca: float = config.get("slippage_ca", self.slippage_us)self.ca_commission: float = config.get("ca_commission", 0.0)+ # UK defaults. LSE has no broker-commission model; the exchange-level+ # cost is SDRT: 0.5% on the buyer of Main Market equities, rounded to+ # the nearest penny (FA86/S99(13); exact ½p rounds up). Exemptions —+ # qualifying UCITS ETFs, AIM shares, gilts, new issues — are the+ # caller's concern; this engine applies the statutory Main Market rate.+ self.slippage_uk: float = config.get("slippage_uk", self.slippage_us)+ self.uk_stamp_tax: float = config.get("uk_stamp_tax", 0.005)def can_execute(self, symbol: str, direction: int, bar: pd.Series) -> bool:"""US/HK/Canada: same-session trading, both directions allowed."""@@ -80,11 +92,13 @@ class GlobalEquityEngine(BaseEngine):return float(math.floor(max(raw_size, 0.0)))return round(max(raw_size, 0.0), 2)- def calc_commission(self, size: float, price: float, _direction: int, is_open: bool) -> float:- """US: zero; HK: stamp tax + levies; Canada: configured broker rate.+ def calc_commission(self, size: float, price: float, direction: int, is_open: bool) -> float:+ """US: zero; HK: stamp tax + levies; Canada: configured broker rate;+ UK: SDRT on the buyer only.- ``_direction`` is unused — reserved for future short-borrow fees- (US Reg-T margin, HK SBL costs).+ ``direction`` is 1 for purchases (including buying to cover a short)+ and -1 for sales. HK charges its stamp tax bilaterally; UK SDRT is+ purchase-side only."""if self.market == "hk":notional = size * price@@ -95,6 +109,17 @@ class GlobalEquityEngine(BaseEngine):return commif self.market == "ca":return size * price * self.ca_commission+ if self.market == "uk":+ # SDRT is a purchase-side charge only: 0.5% of consideration on+ # the buyer, rounded to the nearest penny (FA86/S99(13); an exact+ # ½p rounds UP). Python's float round() is banker's rounding and+ # handles 1.005 inconsistently (-> 1.00), so use Decimal+ # ROUND_HALF_UP for the statutory direction.+ if direction > 0:+ consideration = Decimal(str(size)) * Decimal(str(price))+ tax = consideration * Decimal(str(self.uk_stamp_tax))+ return float(tax.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP))+ return 0.0# US: zero commission (SEC fee negligible)return 0.0diff --git a/agent/tests/test_uk_backtest_smoke.py b/agent/tests/test_uk_backtest_smoke.pyindex c5b05b700..d855c7c75 100644--- a/agent/tests/test_uk_backtest_smoke.py+++ b/agent/tests/test_uk_backtest_smoke.py@@ -5,12 +5,13 @@ Drives the real market-engine routing so a ``VOD.L`` backtest lands onbars. 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).+the regression this guards).All data is in-memory; no network access. LSE prices are quoted in GBp, sothe synthetic series uses penny-scale prices and whole-share sizes (thegeneric GlobalEquity rounding path is what UK inherits, unlike HK board lots-or Canada's tick grid).+or Canada's tick grid), and buys carry the statutory 0.5% SDRT (rounded to+the nearest penny, exact ½p up)."""from __future__ import annotations@@ -93,13 +94,19 @@ def test_uk_orders_are_whole_shares(tmp_path: Path) -> None: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.+def test_uk_sdrt_charged_on_buys_only() -> None:+ """LSE Main Market carries 0.5% SDRT on the buyer (purchase-side only).- 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.+ The engine's commission function is the fee surface the market= value+ selects. Sells pay nothing; buys (including covering a short) pay 0.5%+ of consideration rounded to the nearest penny (FA86/S99(13))."""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+ assert engine.calc_commission(1000.0, 110.0, 1, is_open=True) == 550.0+ assert engine.calc_commission(1000.0, 110.0, -1, is_open=True) == 0.0+ # Exact half-penny rounds UP per the HMRC manual (13.4547 -> 13.45,+ # 13.455 -> 13.46).+ assert engine.calc_commission(2690.94, 1.0, 1, is_open=True) == 13.45+ assert engine.calc_commission(2691.0, 1.0, 1, is_open=True) == 13.46