HKUDS/Vibe-Trading · error · ValueError

ts_argmax window must be >= 1, got {n}

Error message

ts_argmax window must be >= 1, got {n}

What it means

ts_argmax returns the 0-based index of the max within each rolling window (bottleneck-accelerated with a distance-from-end correction). It requires window n >= 1; both bn.move_argmax and pandas rolling would otherwise fail or produce nonsense.

Source

Thrown at agent/src/factors/base.py:229

    return float(np.argmax(arr_filled))


def _argmin_last(arr: np.ndarray) -> float:
    if np.isnan(arr).all():
        return np.nan
    arr_filled = np.where(np.isnan(arr), np.inf, arr)
    return float(np.argmin(arr_filled))


def ts_argmax(df: pd.DataFrame, n: int) -> pd.DataFrame:
    """Rolling argmax (0-based index into the window), warmup → NaN.

    Uses ``bottleneck.move_argmax`` when available (~350x faster).
    Correction: ``bn.move_argmax`` returns distance from window end,
    so we convert via ``(n - 1) - bn_result`` to get 0-based index from start.
    """
    if n < 1:
        raise ValueError(f"ts_argmax window must be >= 1, got {n}")
    if HAS_BOTTLENECK:
        arr = df.to_numpy(dtype=np.float64)
        raw = bn.move_argmax(arr, window=n, min_count=n, axis=0)
        corrected = (n - 1) - raw
        return pd.DataFrame(corrected, index=df.index, columns=df.columns)
    return df.rolling(window=n, min_periods=n).apply(_argmax_last, raw=True)


def ts_argmin(df: pd.DataFrame, n: int) -> pd.DataFrame:
    """Rolling argmin (0-based index into the window), warmup → NaN.

    Uses ``bottleneck.move_argmin`` when available (~350x faster).
    Correction: ``bn.move_argmin`` returns distance from window end,
    so we convert via ``(n - 1) - bn_result`` to get 0-based index from start.
    """
    if n < 1:
        raise ValueError(f"ts_argmin window must be >= 1, got {n}")
    if HAS_BOTTLENECK:

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Pass n >= 1
  2. Map 'current value only' semantics to n=1, not 0
  3. Sanitize window lists in bench configs

Example fix

// before
ts_argmax(df, 0)
// after
ts_argmax(df, 1)
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(n, int) or n < 1: raise ValueError(f'window must be int >= 1, got {n!r}')

Type guard

def is_valid_window(n: object) -> bool:
    return isinstance(n, int) and not isinstance(n, bool) and n >= 1

Prevention

When it happens

Trigger: Calling ts_argmax(df, 0), ts_argmax(df, -2), or benchmarking (bench_operators) with a window list containing 0.

Common situations: Benchmark scripts iterating windows like [0, 5, 10], user-supplied factor parameters, or off-by-one when converting from a 'lookback' convention where 0 means 'today only' (should be 1 here).

Related errors


AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28). Data as JSON: /api/errors/367e2d97e8338bff. Report an issue: GitHub.