"""
================================================================================
Strategy **D062** — ``s062``
================================================================================
``close > roll_high_55`` and ``close > 1.01*sma_20`` + VIX<24 + VRP.

**Exit / structure:** hold **5** sessions, trade kind ``ss``, params ``(31,)``.
Signal logic is implemented only in this module (no imports from sibling strategies).
================================================================================
"""

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": "D062", "theme": "channel_break", "title": 'Close above 55d high and above SMA20 momentum'}

HOLD_SESSIONS = 5
TRADE_KIND = "ss"
TRADE_PARAMS = (31,)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    hi = row.get("roll_high_55")
    s20 = row.get("sma_20")
    if hi is None or s20 is None or pd.isna(hi) or pd.isna(s20):
        return False
    c = float(row["close"])
    if c <= float(hi) or c <= 1.01 * float(s20):
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 24.0:
        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, 31)
    if iv is None or iv <= rv:
        return False
    if ctx.chain_contracts(i) < 80 or not ch.contracts:
        return False
    _ = spy
    return True
