"""Example: RSI contrarian signal.

Buys oversold conditions (RSI < 30) and sells overbought (RSI > 70).
Signal strength scales with how extreme the RSI reading is.
"""

from __future__ import annotations

from vanta.tracker import TrackerBase
from vanta.indicators import RSI


class RSITracker(TrackerBase):
    """Contrarian signal based on RSI extremes."""

    def __init__(self, period: int = 14, oversold: float = 30.0, overbought: float = 70.0):
        super().__init__()
        self.rsi = RSI(period=period)
        self.oversold = oversold
        self.overbought = overbought

    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.rsi.update(float(close))

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

        rsi = self.rsi.value

        if rsi < self.oversold:
            # Oversold — expect bounce. Stronger signal the more extreme.
            strength = (self.oversold - rsi) / self.oversold
            return {"expected_return": 0.005 * strength}

        if rsi > self.overbought:
            # Overbought — expect pullback.
            strength = (rsi - self.overbought) / (100.0 - self.overbought)
            return {"expected_return": -0.005 * strength}

        return {"expected_return": 0.0}
