"""
================================================================================
Strategy **D046** — ``s046``  (Tiny 20d drift — short strangle)
================================================================================
**Idea:** ``abs(ret_20) < 0.012`` means almost no net drift over 20 sessions; sell **42** DTE
strangle when **not** OPEX week to reduce pinning ambiguity.
**Entry:** drift band, ``NOT is_op_exp_week``, ``VIX in [13, 19]``, ``contracts>=100``.
**Exit:** Short strangle 42 DTE **-0.21 / 0.14**, hold **6**.
================================================================================
"""

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": "D046", "theme": "return_shock", "title": "Tiny 20d drift + non-OPEX short strangle"}

HOLD_SESSIONS = 6
TRADE_KIND = "sg"
TRADE_PARAMS = (42, -0.21, 0.14)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    r20 = row.get("ret_20")
    if r20 is None or pd.isna(r20) or abs(float(r20)) >= 0.012:
        return False
    fl = row.get("is_op_exp_week")
    if fl is None or pd.isna(fl) or bool(fl):
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx < 13.0 or vx > 19.0:
        return False
    if ctx.chain_contracts(i) < 100 or not ch.contracts:
        return False
    _ = spy
    return True
