"""Example: Trend-following alpha signal.

Uses fast/slow EMA crossover to detect momentum direction.
Positive signal when fast EMA is above slow (uptrend),
negative when below (downtrend).
"""

from __future__ import annotations

from vanta.tracker import TrackerBase
from vanta.indicators import EMA


class TrendFollowingTracker(TrackerBase):
    """Follows short-term momentum via EMA crossover."""

    def __init__(self, fast_period: int = 5, slow_period: int = 20):
        super().__init__()
        self.fast_ema = EMA(period=fast_period)
        self.slow_ema = EMA(period=slow_period)

    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:
                price = float(close)
                self.fast_ema.update(price)
                self.slow_ema.update(price)

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

        # Normalized difference between fast and slow EMA
        spread = (self.fast_ema.value - self.slow_ema.value) / max(abs(self.slow_ema.value), 1e-9)

        return {"expected_return": 0.5 * spread}
