"""Example: Volume-weighted momentum signal.

Weights the momentum signal by relative volume. High-volume moves
get stronger signals (more conviction), low-volume moves are discounted.
"""

from __future__ import annotations

from vanta.tracker import TrackerBase
from vanta.indicators import EMA, VWAP, SMA


class VolumeProfileTracker(TrackerBase):
    """Momentum signal weighted by volume conviction."""

    def __init__(self, momentum_period: int = 10, volume_period: int = 20):
        super().__init__()
        self.price_ema = EMA(period=momentum_period)
        self.vwap = VWAP()
        self.vol_sma = SMA(period=volume_period)
        self._last_close: float | None = None
        self._last_volume: float | None = None

    def tick(self, data: dict) -> None:
        super().tick(data)
        for candle in data.get("candles_1m", []):
            close = float(candle.get("close", 0))
            volume = float(candle.get("volume", 0))

            self.price_ema.update(close)
            self.vwap.update(close, volume)
            self.vol_sma.update(volume)
            self._last_close = close
            self._last_volume = volume

    def predict(self, symbol: str, horizon_seconds: int, step_seconds: int) -> dict:
        if not all([self.price_ema.ready, self.vwap.ready,
                     self.vol_sma.ready, self._last_close, self._last_volume]):
            return {"expected_return": 0.0}

        # Momentum: price vs VWAP
        momentum = (self._last_close - self.vwap.value) / max(abs(self.vwap.value), 1e-9)

        # Volume conviction: current volume vs average
        avg_vol = self.vol_sma.value
        vol_ratio = self._last_volume / max(avg_vol, 1e-9)
        conviction = min(vol_ratio, 3.0) / 3.0  # cap at 3x average

        return {"expected_return": 0.3 * momentum * conviction}
