"""
================================================================================
Strategy **D005** — ``s005``
================================================================================

Theme
-----
**Volatility expansion after compression (bandwidth percentile jump).**

Economic idea
-------------
This is intentionally **different** from D004: instead of selling in a squeeze, we
**buy** straddle convexity when the Bollinger width percentile (252 sessions) **rises
above 0.85** while VIX is still **below 20**. The story is “compression resolved upward in
volatility measure before VIX fully catches up” — a pure daily snapshot hypothesis.

Entry
-----
1. ``bb_width_pct_rank_252`` finite and ``> 0.85``.
2. ``VIX < 20``.
3. ``iv_atm(14)`` exists.
4. ``n_contracts[i] >= 80``.

Exit
----
Long ATM straddle ~14 DTE, hold **3** sessions.
================================================================================
"""

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": "D005", "theme": "vol_expansion", "title": "BB width pct rank spike + mild VIX long straddle"}

HOLD_SESSIONS = 3
TRADE_KIND = "sl"
TRADE_PARAMS = (14,)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    pr = row.get("bb_width_pct_rank_252")
    if pr is None or pd.isna(pr) or float(pr) <= 0.85:
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 20.0:
        return False
    if ctx.iv_atm_dte(i, 14) is None:
        return False
    if ctx.chain_contracts(i) < 80:
        return False
    if not ch.contracts:
        return False
    _ = spy
    return True
