Skip to content

"Vibe-Trading: Your Personal Trading Agent"

7.2 KB
Raw Blame
"""India equity (NSE / BSE) backtest engine.

Models the Indian cash-equity **delivery** segment on daily bars. Intraday
(MIS) mechanics are not represented by a daily-bar engine, so the defaults
reflect overnight delivery rules; the knobs below let advanced users approximate
intraday behaviour.

Market rules:
  - T+1 settlement: shares bought today cannot be sold the same bar (delivery).
  - No short selling by default: retail cannot hold overnight short delivery
    positions. Set ``allow_short=True`` to model intraday (MIS) shorting.
  - Circuit bands: per-scrip price bands vary (2/5/10/20%); the exact band is
    not derivable from the symbol alone, so a single configurable band applies
    (default ±20%, the widest common band). Set ``price_limit`` to ``0`` /
    ``None`` to disable.
  - Lot size: 1 share for cash equity (F&O lot sizes are not modelled here).

Cost stack (delivery, discount-broker defaults; all config-driven). NOTE: SEBI/
exchange tariffs change periodically — verify ``in_*`` rates against a current
broker schedule before relying on absolute cost figures:
  - Brokerage: ₹0 (delivery on discount brokers)            [in_brokerage]
  - STT: 0.1% on buy + 0.1% on sell (bilateral)             [in_stt]
  - Exchange transaction charge: NSE ~0.00297% (bilateral)  [in_exchange_txn]
  - SEBI turnover fee: ₹10/crore = 0.0001% (bilateral)      [in_sebi_fee]
  - Stamp duty: 0.015% on buy only                          [in_stamp_duty]
  - GST: 18% on (brokerage + exchange txn + SEBI fee)       [in_gst]
  - DP charge: flat per-scrip on sell (default ₹0)          [in_dp_charge]
"""

from __future__ import annotations

import pandas as pd

from backtest.engines.base import BaseEngine
from backtest.engines.china_a import _blocked_by_limit


class IndiaEquityEngine(BaseEngine):
    """NSE / BSE cash-equity (delivery) engine.

    Config keys (all optional; defaults shown in the module docstring):
      - allow_short: bool, default False
      - price_limit: float fraction or None, default 0.20
      - slippage: default 0.001
      - in_brokerage / in_stt / in_exchange_txn / in_sebi_fee /
        in_stamp_duty / in_gst / in_dp_charge
    """

    def __init__(self, config: dict):
        config = {**config, "leverage": 1.0}  # cash delivery: no leverage
        super().__init__(config)
        self.allow_short: bool = bool(config.get("allow_short", False))
        self.price_limit = config.get("price_limit", 0.20)
        self.slippage_rate: float = config.get("slippage", 0.001)
        # Cost stack
        self.in_brokerage: float = config.get("in_brokerage", 0.0)
        self.in_stt: float = config.get("in_stt", 0.001)
        self.in_exchange_txn: float = config.get("in_exchange_txn", 0.0000297)
        self.in_sebi_fee: float = config.get("in_sebi_fee", 0.000001)
        self.in_stamp_duty: float = config.get("in_stamp_duty", 0.00015)
        self.in_gst: float = config.get("in_gst", 0.18)
        self.in_dp_charge: float = config.get("in_dp_charge", 0.0)

    def can_execute(self, symbol: str, direction: int, bar: pd.Series) -> bool:
        """India delivery execution rules.

        Args:
            symbol: NSE/BSE symbol (e.g. ``RELIANCE.NS``).
            direction: 1 (buy), -1 (short), 0 (sell/close).
            bar: Current bar (needs ``close`` + ``pre_close``/``pct_chg`` for
                circuit checks).

        Returns:
            True if the trade is allowed.
        """
        return india_can_execute(self, self, symbol, direction, bar)

    def round_size(self, raw_size: float, price: float) -> float:
        """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:
        """India delivery cost stack (see module docstring).

        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
        exchange_txn = notional * self.in_exchange_txn        # bilateral
        sebi_fee = notional * self.in_sebi_fee                # bilateral
        gst = (brokerage + exchange_txn + sebi_fee) * self.in_gst
        stt = notional * self.in_stt                          # bilateral (delivery)
        comm = brokerage + exchange_txn + sebi_fee + gst + stt
        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
        return comm

    def apply_slippage(self, price: float, direction: int) -> float:
        """India slippage (configurable)."""
        return price * (1 + direction * self.slippage_rate)


# ── Helpers ──


def _bar_date(bar: pd.Series):
    """Extract date from bar, handling various column names."""
    for col in ("trade_date", "date"):
        if col in bar.index:
            val = bar[col]
            if hasattr(val, "date"):
                return val.date()
            try:
                return pd.Timestamp(val).date()
            except Exception:
                pass
    if hasattr(bar, "name") and hasattr(bar.name, "date"):
        return bar.name.date()
    return None


def india_can_execute(state, rules, symbol: str, direction: int, bar: pd.Series) -> bool:
    """India delivery rules read against ``state`` with params from ``rules``.

    Composite runs pass the composite engine as ``state`` (shared positions
    and close panel) and the India sub-engine as ``rules`` (``allow_short``,
    ``price_limit``, slippage); a single-market run passes the same engine
    for both. Without this the composite evaluated T+1 against the
    sub-engine's always-empty positions and let same-day sells through
    (#1292).
    """
    # 1. Short selling: blocked unless explicitly modelling intraday (MIS).
    if direction == -1 and not rules.allow_short:
        return False

    # 2. T+1: can't sell shares bought today (delivery).
    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),
        position_direction=pos.direction if pos is not None else None,
    ):
        return False

    return True