"""Example: Mean-reversion alpha signal.

Detects short-term overextension from a rolling mean and bets on
a pullback. Returns negative expected_return when price is above
the mean (expecting reversion down), positive when below.
"""

from __future__ import annotations

from vanta.tracker import TrackerBase
from vanta.indicators import SMA


class MeanReversionTracker(TrackerBase):
    """Predicts pullback after short-term overextension."""

    def __init__(self, lookback: int = 10):
        super().__init__()
        self.sma = SMA(period=lookback)
        self._last_close: float | None = None

    def tick(self, data: dict) -> None:
        super().tick(data)
        for candle in data.get("candles_1m", []):
            close = candle.get("close")
            if close is not None:
                self.sma.update(float(close))
                self._last_close = float(close)

    def predict(self, symbol: str, horizon_seconds: int, step_seconds: int) -> dict:
        if not self.sma.ready or self._last_close is None:
            return {"expected_return": 0.0}

        deviation = (self._last_close - self.sma.value) / max(abs(self.sma.value), 1e-9)

        # Negative signal: if price is above mean, expect it to come down
        return {"expected_return": -0.7 * deviation}
