"""
================================================================================
Strategy **D086** — ``s086``
================================================================================
Normalized position above 0.66 + VIX<23 + VRP.

**Exit / structure:** hold **6** sessions, trade kind ``sg``, params ``(36, -0.24, 0.17)``.
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": "D086", "theme": "range_position", "title": 'Upper third of 55d range short strangle'}

HOLD_SESSIONS = 6
TRADE_KIND = "sg"
TRADE_PARAMS = (36, -0.24, 0.17)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    hi = row.get("roll_high_55")
    lo = row.get("roll_low_55")
    if hi is None or lo is None or pd.isna(hi) or pd.isna(lo):
        return False
    hi_f, lo_f = float(hi), float(lo)
    if hi_f <= lo_f:
        return False
    u = (float(row["close"]) - lo_f) / (hi_f - lo_f)
    if u <= 0.66:
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 23.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, 36)
    if iv is None or iv <= rv:
        return False
    if ctx.chain_contracts(i) < 100 or not ch.contracts:
        return False
    _ = spy
    return True
