"""Pure time-series trend-following (TSMOM) signal. No I/O."""
from __future__ import annotations


def ts_trend_signal(closes: list[float], lookbacks: list[int]) -> float | None:
    """Multi-horizon trend direction in [-1, 1] from an ascending close series.
    For each lookback L (need len(closes) > L), compute sign(closes[-1]/closes[-1-L] - 1);
    return the MEAN of those signs (so all-up -> +1, all-down -> -1, mixed -> in between).
    None if no lookback has enough history or any needed close <= 0."""
    sigs: list[float] = []
    for L in lookbacks:
        if len(closes) <= L:
            continue
        c0, c1 = closes[-1 - L], closes[-1]
        if c0 <= 0 or c1 <= 0:
            return None
        sigs.append(1.0 if c1 > c0 else (-1.0 if c1 < c0 else 0.0))
    if not sigs:
        return None
    return sum(sigs) / len(sigs)
