HKUDS/Vibe-Trading · error · ValueError

window must be >= 1, got {window}

Error message

window must be >= 1, got {window}

What it means

find_peaks_valleys uses a centered window of size 2*window+1 for local-extrema detection; window < 1 makes the comparison meaningless (and would break slicing), so it raises immediately. Note: too-short series (n < 2*window+1) do NOT raise — they return empty peak/valley lists.

Source

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

from src.tools.path_utils import safe_run_dir


# ---------------------------------------------------------------------------
# Pattern detection functions
# ---------------------------------------------------------------------------

def find_peaks_valleys(close: pd.Series, window: int = 5) -> dict:
    """Detect peaks and valleys in a price series.

    Args:
        close: Closing price series.
        window: Half-window size; effective window is 2*window+1.

    Returns:
        Dict with keys "peaks" and "valleys", each a list of integer indices.
    """
    if window < 1:
        raise ValueError(f"window must be >= 1, got {window}")
    n = len(close)
    if n < 2 * window + 1:
        return {"peaks": [], "valleys": []}

    values = close.values.astype(float)
    peaks, valleys = [], []

    for i in range(window, n - window):
        seg = values[i - window : i + window + 1]
        if np.isnan(values[i]):
            continue
        seg = seg[~np.isnan(seg)]
        if len(seg) == 0:
            continue
        if values[i] == np.max(seg):
            peaks.append(i)
        if values[i] == np.min(seg):
            valleys.append(i)

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Use window >= 1 (typical 3–5)
  2. Clamp: window = max(1, int(user_value))
  3. Validate pattern_tool params before dispatch

Example fix

# before
find_peaks_valleys(close, window=0)
# after
find_peaks_valleys(close, window=max(1, window))
Defensive patterns

Strategy: validation

Validate before calling

window = int(window)
if window < 1:
    raise ArgumentError("window must be >= 1")
# short series return empty results, not errors

Type guard

def valid_peak_window(w: object) -> bool:
    return isinstance(w, int) and w >= 1

Try / catch

try:
    pivots = find_peaks_valleys(close, window=window)
except ValueError as e:
    if "window must be >= 1" in str(e):
        pivots = find_peaks_valleys(close, window=max(1, window))

Prevention

When it happens

Trigger: Calling find_peaks_valleys (or pattern tools support_resistance/head_and_shoulders/double_top_bottom/triangle/broadening that forward a window) with window=0 or a negative value.

Common situations: Defaulting window to 0 in config, or computing window from a tunable that can be zero.

Related errors


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