"""
================================================================================
Strategy **D016** — ``s016``
================================================================================

Theme
-----
**IV term structure inversion in ATM IV (front > back) with long vol.**

Economic idea
-------------
If **shorter** ATM IV (10 DTE) exceeds **longer** ATM IV (40 DTE) by more than **3 vol
points** (``iv10 - iv40 > 0.03`` in annualized decimal units), the curve embeds near-term
event or crash anxiety. This sleeve buys **14 DTE** straddle to express **near** expiry
richness without using VIX futures.

Entry
-----
1. ``iv10`` and ``iv40`` exist.
2. ``iv10 - iv40 > 0.03``.
3. ``VIX < 30`` (avoid stacking with pure panic where spreads blow out).
4. ``n_contracts[i] >= 80``.

Exit
----
Long straddle 14 DTE, hold **4** sessions.
================================================================================
"""

from __future__ import annotations

import math

from RenTech.core.options_data_loader import OptionChain
from RenTech.strategy_stack.diverse_theta_strategies_v1.context import ResearchContext

META = {"sid": "D016", "theme": "iv_term", "title": "ATM IV inverted (10d>40d) long straddle"}

HOLD_SESSIONS = 4
TRADE_KIND = "sl"
TRADE_PARAMS = (14,)


def wants_entry(i: int, row, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    a = ctx.iv_atm_dte(i, 10)
    b = ctx.iv_atm_dte(i, 40)
    if a is None or b is None:
        return False
    if a - b <= 0.03:
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 30.0:
        return False
    if ctx.chain_contracts(i) < 80:
        return False
    if not ch.contracts:
        return False
    _ = spy, row
    return True
