"""
================================================================================
Strategy **D034** — ``s034``  (VIX depressed vs its 20d average — short strangle)
================================================================================
**Idea:** ``VIX < 0.93 * vix_ma20`` suggests fear reading is **below** its recent mean;
sell OTM strangle with **wider** wings than typical low-VIX sleeves.
**Entry:** ``VIX < 0.93 * vix_ma20``, ``iv_atm(45) > rv21``, ``contracts>=110``.
**Exit:** Short strangle 45 DTE **-0.30 / 0.22**, hold **7**.
================================================================================
"""

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": "D034", "theme": "vix_local_extreme", "title": "VIX below 93% of its 20d mean + VRP strangle"}

HOLD_SESSIONS = 7
TRADE_KIND = "sg"
TRADE_PARAMS = (45, -0.30, 0.22)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    vx = ctx.vix(i)
    ma = row.get("vix_ma20")
    if ma is None or pd.isna(ma) or float(ma) <= 0:
        return False
    if not math.isfinite(vx) or vx >= 0.93 * float(ma):
        return False
    rv = float(row["rv21"]) if pd.notna(row.get("rv21")) else float("nan")
    if not math.isfinite(rv) or rv <= 0:
        return False
    iv = ctx.iv_atm_dte(i, 45)
    if iv is None or iv <= rv:
        return False
    if ctx.chain_contracts(i) < 110 or not ch.contracts:
        return False
    _ = spy
    return True
