Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(backtest): charge india stamp duty on the buy side of a short

Rudy Celekli773e506708a0
diff --git a/agent/backtest/engines/india_equity.py b/agent/backtest/engines/india_equity.py
index e6c18c24b..b2b922d34 100644
--- a/agent/backtest/engines/india_equity.py
+++ b/agent/backtest/engines/india_equity.py
@@ -79,11 +79,13 @@ class IndiaEquityEngine(BaseEngine):
"""Cash equity trades in 1-share lots."""
return float(max(int(raw_size), 0))
- def calc_commission(self, size: float, price: float, _direction: int, is_open: bool) -> float:
+ def calc_commission(self, size: float, price: float, direction: int, is_open: bool) -> float:
"""India delivery cost stack (see module docstring).
- ``_direction`` is unused — reserved for future asymmetric long/short
- (intraday MIS) schedules.
+ Stamp duty and the DP charge follow the actual trade side rather than
+ open/close: with ``allow_short`` an opening short is a sale and its
+ cover is the purchase. ``BaseEngine`` passes the position direction on
+ both hooks, so the side is derivable.
"""
notional = size * price
brokerage = notional * self.in_brokerage
@@ -92,7 +94,8 @@ class IndiaEquityEngine(BaseEngine):
gst = (brokerage + exchange_txn + sebi_fee) * self.in_gst
stt = notional * self.in_stt # bilateral (delivery)
comm = brokerage + exchange_txn + sebi_fee + gst + stt
- if is_open:
+ is_buy = (direction > 0) if is_open else (direction < 0)
+ if is_buy:
comm += notional * self.in_stamp_duty # stamp duty: buy-only
else:
comm += self.in_dp_charge # DP charge: sell-only, flat
diff --git a/agent/tests/test_india_equity_engine.py b/agent/tests/test_india_equity_engine.py
index a2f3db1b9..8d7440fe6 100644
--- a/agent/tests/test_india_equity_engine.py
+++ b/agent/tests/test_india_equity_engine.py
@@ -167,6 +167,17 @@ class TestCommission:
)
+ def test_short_round_trip_charges_stamp_duty_on_the_cover_buy(self) -> None:
+ engine = _engine(allow_short=True, in_dp_charge=13.5)
+ long_buy = engine.calc_commission(100, 1000.0, 1, is_open=True)
+ long_sell = engine.calc_commission(100, 1000.0, 1, is_open=False)
+ short_sell = engine.calc_commission(100, 1000.0, -1, is_open=True)
+ short_cover = engine.calc_commission(100, 1000.0, -1, is_open=False)
+ # Opening a short is a sale and covering it is a purchase.
+ assert short_sell == pytest.approx(long_sell, abs=1e-6)
+ assert short_cover == pytest.approx(long_buy, abs=1e-6)
+
+
# ---------------------------------------------------------------------------
# apply_slippage + leverage
# ---------------------------------------------------------------------------