"""
================================================================================
Strategy **D033** — ``s033``  (VIX at local maximum — long straddle)
================================================================================
**Idea:** When **VIX equals its own 20-session rolling maximum** (within float tolerance),
fear may be peaking locally.
**Entry:** ``vix_close >= vix_roll_max_20 - 1e-6``, ``VIX<36``, ``iv_atm(14)`` exists,
``contracts>=80``.
**Exit:** Long straddle 14 DTE, hold **3**.
================================================================================
"""

from __future__ import annotations

import math

import pandas as pd

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

META = {"sid": "D033", "theme": "vix_local_extreme", "title": "VIX at 20d rolling max long straddle"}

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


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    vx = ctx.vix(i)
    mx = row.get("vix_roll_max_20")
    if mx is None or pd.isna(mx):
        return False
    if not math.isfinite(vx) or vx >= 36.0:
        return False
    if float(vx) < float(mx) - 1e-6:
        return False
    if ctx.iv_atm_dte(i, 14) is None:
        return False
    if ctx.chain_contracts(i) < 80 or not ch.contracts:
        return False
    _ = spy
    return True
