"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(equity): SDRT buy-side detection on position-side close path
cgycorey333bc2980a22
diff --git a/agent/backtest/engines/global_equity.py b/agent/backtest/engines/global_equity.pyindex b564ecb8e..394cec405 100644--- a/agent/backtest/engines/global_equity.py+++ b/agent/backtest/engines/global_equity.py@@ -96,9 +96,10 @@ class GlobalEquityEngine(BaseEngine):"""US: zero; HK: stamp tax + levies; Canada: configured broker rate;UK: SDRT on the buyer only.- ``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.+ ``direction`` is the order/position side (1=buy-long/short-cover,+ -1=sell/short). Trade side is direction on opens and -direction on+ closes (closing a long sells; covering a short buys). UK SDRT applies+ to purchases of every kind: opening long and buying to cover."""if self.market == "hk":notional = size * price@@ -115,7 +116,12 @@ class GlobalEquityEngine(BaseEngine):# ½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:+ #+ # While open orders carry buyer/seller as the sign directly, a+ # CLOSE passes the *position* side: closing a long (direction=1)+ # is a sale, covering a short (direction=-1) is a purchase.+ trade_is_buy = direction > 0 if is_open else direction < 0+ if trade_is_buy: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))diff --git a/agent/tests/test_uk_backtest_smoke.py b/agent/tests/test_uk_backtest_smoke.pyindex d855c7c75..b1a31b5aa 100644--- a/agent/tests/test_uk_backtest_smoke.py+++ b/agent/tests/test_uk_backtest_smoke.py@@ -106,6 +106,10 @@ def test_uk_sdrt_charged_on_buys_only() -> None:assert engine.market == "uk"assert engine.calc_commission(1000.0, 110.0, 1, is_open=True) == 550.0assert engine.calc_commission(1000.0, 110.0, -1, is_open=True) == 0.0+ # Close path: the engine passes the POSITION side, so closing a long+ # (direction=1) is a sale and covering a short (direction=-1) is a buy.+ assert engine.calc_commission(1000.0, 110.0, 1, is_open=False) == 0.0+ assert engine.calc_commission(1000.0, 110.0, -1, is_open=False) == 550.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