"""
================================================================================
Strategy **D039** — ``s039``  (45 DTE skew very steep — short RR)
================================================================================
**Idea:** Use skew key **(45, -0.15, 0.10)** with threshold **0.048** (distinct from D018).
**Entry:** skew diff > 0.048, ``SPY>SMA200``, ``VIX in [13, 24]``, ``contracts>=100``.
**Exit:** Short RR 45 DTE **-0.15 / 0.10**, hold **9**.
================================================================================
"""

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": "D039", "theme": "skew", "title": "Very steep 45d skew + bull MA filter short RR"}

HOLD_SESSIONS = 9
TRADE_KIND = "rr"
TRADE_PARAMS = (45, -0.15, 0.10)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    sk = ctx.skew_iv_diff(i, 45, -0.15, 0.10)
    if sk is None or sk <= 0.048:
        return False
    s200 = row.get("sma_200")
    if s200 is None or pd.isna(s200) or float(row["close"]) <= float(s200):
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx < 13.0 or vx > 24.0:
        return False
    if ctx.chain_contracts(i) < 100 or not ch.contracts:
        return False
    _ = spy
    return True
