HKUDS/Vibe-Trading · error · ValueError

window must be >= 2, got {window}

Error message

window must be >= 2, got {window}

What it means

trend_line_slope fits a polynomial per rolling window; a window of 1 gives a single point (no slope) and window=0 makes numpy.polyfit raise LinAlgError, so the guard requires window >= 2 and raises this ValueError instead.

Source

Thrown at agent/src/tools/pattern_tool.py:146

        centers.sort(reverse=True)
        return [c for _, c in centers[:n]]

    return {"support": cluster(valley_prices, num_levels), "resistance": cluster(peak_prices, num_levels)}


def trend_line_slope(close: pd.Series, window: int = 20) -> pd.Series:
    """Compute rolling linear-fit slope.

    Args:
        close: Closing price series.
        window: Fitting window size.

    Returns:
        Series of slope values; first window-1 entries are NaN.
    """
    # polyfit needs >=2 points; window=1 raises LinAlgError.
    if window < 2:
        raise ValueError(f"window must be >= 2, got {window}")
    n = len(close)
    slopes = np.full(n, np.nan)
    values = close.values.astype(float)
    x = np.arange(window, dtype=float)

    for i in range(window - 1, n):
        seg = values[i - window + 1 : i + 1]
        if np.any(np.isnan(seg)):
            continue
        slopes[i] = np.polyfit(x, seg, 1)[0]

    return pd.Series(slopes, index=close.index)


def head_and_shoulders(close: pd.Series, window: int = 10) -> pd.Series:
    """Detect head-and-shoulders top pattern.

    Args:

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Use window >= 2 (typical 5–20)
  2. Clamp: window = max(2, int(value))
  3. If you need per-point slopes, use window=2 as the floor

Example fix

# before
trend_line_slope(close, window=1)
# after
trend_line_slope(close, window=max(2, window))
Defensive patterns

Strategy: validation

Validate before calling

window = int(window)
if window < 2:
    raise ArgumentError("window must be >= 2")

Type guard

def valid_slope_window(w: object) -> bool:
    return isinstance(w, int) and w >= 2

Try / catch

try:
    slopes = trend_line_slope(close, window=window)
except ValueError as e:
    if "window must be >= 2" in str(e):
        slopes = trend_line_slope(close, window=max(2, window))

Prevention

When it happens

Trigger: Calling trend_line_slope (or _trend_slope_summary) with window=1 or 0. Window=2 works and yields finite slopes.

Common situations: Sharing one 'window' parameter across peak detection (min 1) and slope (min 2); auto-derived windows hitting 1 on short series.

Related errors


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