Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(backtest): India engine checks the right circuit band when covering a short

Lanre Shittud17f2ccbea88
diff --git a/agent/backtest/engines/india_equity.py b/agent/backtest/engines/india_equity.py
index bdebdba9f..e6c18c24b 100644
--- a/agent/backtest/engines/india_equity.py
+++ b/agent/backtest/engines/india_equity.py
@@ -137,17 +137,26 @@ def india_can_execute(state, rules, symbol: str, direction: int, bar: pd.Series)
return False
# 2. T+1: can't sell shares bought today (delivery).
- if direction == 0:
- pos = state.positions.get(symbol)
- if pos is not None:
- bar_date = _bar_date(bar)
- entry_date = pos.entry_time.date() if hasattr(pos.entry_time, "date") else None
- if bar_date is not None and entry_date is not None and bar_date == entry_date:
- return False
+ pos = state.positions.get(symbol) if direction == 0 else None
+ if pos is not None:
+ bar_date = _bar_date(bar)
+ entry_date = pos.entry_time.date() if hasattr(pos.entry_time, "date") else None
+ if bar_date is not None and entry_date is not None and bar_date == entry_date:
+ return False
# 3. Circuit bands, tested at execution time (see _blocked_by_limit).
+ # position_direction tells _blocked_by_limit which band a close is really
+ # testing: closing a short is a buy, blocked at the upper band, not the
+ # lower band a long-side sell would use. Without it a short-covering buy
+ # locked at the upper circuit (no sellers to buy from) was wrongly let
+ # through, tested against the wrong band with sell-side slippage.
if rules.price_limit and _blocked_by_limit(
- state, symbol, direction, bar, float(rules.price_limit)
+ state,
+ symbol,
+ direction,
+ bar,
+ float(rules.price_limit),
+ position_direction=pos.direction if pos is not None else None,
):
return False
diff --git a/agent/tests/test_india_equity_engine.py b/agent/tests/test_india_equity_engine.py
index 8fb2c2e2b..761ba24db 100644
--- a/agent/tests/test_india_equity_engine.py
+++ b/agent/tests/test_india_equity_engine.py
@@ -88,6 +88,33 @@ class TestCanExecute:
bar = _bar(close=120.0, pre_close=100.0)
assert engine.can_execute("RELIANCE.NS", 1, bar) is True
+ def test_upper_circuit_blocks_covering_a_short(self) -> None:
+ # Closing a short is a buy: with no sellers at the upper circuit, the
+ # cover can't actually fill there, same as a fresh buy can't (see
+ # test_upper_circuit_blocks_buy). Without forwarding the position's
+ # direction to _blocked_by_limit this was checked against the wrong
+ # (lower) band and wrongly allowed.
+ engine = _engine(allow_short=True, price_limit=0.20)
+ engine.positions["RELIANCE.NS"] = Position(
+ symbol="RELIANCE.NS", direction=-1, size=10, entry_price=100.0,
+ entry_time=pd.Timestamp("2024-04-01"),
+ )
+ bar = _bar(close=120.0, pre_close=100.0) # +20% -> upper band
+ bar.name = pd.Timestamp("2024-04-02")
+ assert engine.can_execute("RELIANCE.NS", 0, bar) is False
+
+ def test_lower_circuit_allows_covering_a_short(self) -> None:
+ # A short's cover buy at the lower band has willing sellers, so it
+ # must still fill (the lower band only blocks sells).
+ engine = _engine(allow_short=True, price_limit=0.20)
+ engine.positions["RELIANCE.NS"] = Position(
+ symbol="RELIANCE.NS", direction=-1, size=10, entry_price=100.0,
+ entry_time=pd.Timestamp("2024-04-01"),
+ )
+ bar = _bar(close=80.0, pre_close=100.0) # -20% -> lower band
+ bar.name = pd.Timestamp("2024-04-02")
+ assert engine.can_execute("RELIANCE.NS", 0, bar) is True
+
# ---------------------------------------------------------------------------
# round_size: 1-share lots