"""
================================================================================
Strategy **D036** — ``s036``  (wide 55d channel — long straddle)
================================================================================
**Idea:** Wide **close-only** channels imply elevated movement recently; buy straddle when
ratio **> 0.09** and **VIX not** extreme.
**Entry:** ``(roll_high_55-roll_low_55)/close > 0.09``, ``VIX<27``, ``contracts>=80``.
**Exit:** Long straddle 21 DTE, hold **5**.
================================================================================
"""

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": "D036", "theme": "price_channel", "title": "Wide 55d close channel + non-extreme VIX long straddle"}

HOLD_SESSIONS = 5
TRADE_KIND = "sl"
TRADE_PARAMS = (21,)


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")
    c = float(row["close"])
    if hi is None or lo is None or pd.isna(hi) or pd.isna(lo) or c <= 0:
        return False
    ratio = (float(hi) - float(lo)) / c
    if ratio <= 0.09:
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 27.0:
        return False
    if ctx.chain_contracts(i) < 80 or not ch.contracts:
        return False
    _ = spy
    return True
