"""Cibles événementielles, isolées de tout calcul de feature.""" from datetime import date, timedelta from statistics import median def event_target(states, prediction_time, horizon): start = date.fromisoformat(prediction_time) - timedelta(days=1) window = [states.get((start + timedelta(days=i)).isoformat()) for i in range(horizon + 1)] if any(p is None for p in window): return None # Panel identique sur TOUTE la fenêtre, pas d'entrée/sortie au jour de l'extrême. shared = set(window[0]["prices"]) for p in window[1:]: shared.intersection_update(p["prices"]) if len(shared) < 3: return None deltas = [median((p["prices"][s] - window[0]["prices"][s]) * 100 for s in shared) for p in window[1:]] events = {} for direction, sign in (("up", 1), ("down", -1)): moves = [sign * d for d in deltas] for threshold in (2, 3, 5): hits = [i + 1 for i, d in enumerate(moves) if d >= threshold - 1e-8] events[f"{direction}_{threshold}"] = dict( hit=bool(hits), delay=hits[0] if hits else None, amplitude=max(moves), terminal=sign * deltas[-1], ) return dict( events=events, available_at=window[-1]["available_at"], panel=len(shared), future_min_delta=min(deltas), future_max_delta=max(deltas), )