{"record":{"id":"f6fd100b98985844","repo":"TheAlgorithms/Python","slug":"window-size-must-be-0","errorCode":null,"errorMessage":"window_size must be > 0","messagePattern":"window_size must be > 0","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"financial/exponential_moving_average.py","lineNumber":43,"sourceCode":"                        of the exponential average (window_size > 0)\n    :return: Yields a sequence of exponential moving averages\n\n    Formula:\n\n    st = alpha * xt + (1 - alpha) * st_prev\n\n    Where,\n    st : Exponential moving average at timestamp t\n    xt : stock price in from the stock prices at timestamp t\n    st_prev : Exponential moving average at timestamp t-1\n    alpha : 2/(1 + window_size) - smoothing factor\n\n    Exponential moving average (EMA) is a rule of thumb technique for\n    smoothing time series data using an exponential window function.\n    \"\"\"\n\n    if window_size <= 0:\n        raise ValueError(\"window_size must be > 0\")\n\n    # Calculating smoothing factor\n    alpha = 2 / (1 + window_size)\n\n    # Exponential average at timestamp t\n    moving_average = 0.0\n\n    for i, stock_price in enumerate(stock_prices):\n        if i <= window_size:\n            # Assigning simple moving average till the window_size for the first time\n            # is reached\n            moving_average = (moving_average + stock_price) * 0.5 if i else stock_price\n        else:\n            # Calculating exponential moving average based on current timestamp data\n            # point and previous exponential average value\n            moving_average = (alpha * stock_price) + ((1 - alpha) * moving_average)\n        yield moving_average\n","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/financial/exponential_moving_average.py#L25-L61","documentation":"Raised by exponential_moving_average() in financial/exponential_moving_average.py when window_size <= 0. The window size determines the smoothing factor alpha = 2/(1 + window_size); a non-positive window makes alpha undefined or >= 2, which destroys the exponential smoothing recurrence, so it is rejected before any price is processed.","triggerScenarios":"exponential_moving_average([], 0), a window_size of -1, or a window computed from a parameter that defaulted to 0; passing a window larger than the data length is allowed (the loop simply never reaches steady state), only <= 0 raises.","commonSituations":"CLI flags where the window option was not provided and defaults to 0; dividing user input (e.g. span in days) by a scale factor that yields 0; confusing argument order and passing a price list length as the window.","solutions":["Pass a positive integer window, e.g. exponential_moving_average(prices, 12).","Default the window to a sane value (12 or 26 are common for EMA) instead of 0 in your own CLI/config.","Validate `window_size >= 1` at the option-parsing layer so users get an argparse-style error, not a traceback."],"exampleFix":"# before\nema = exponential_moving_average(stock_prices, 0)  # ValueError\n\n# after\nema = exponential_moving_average(stock_prices, 12)","handlingStrategy":"validation","validationCode":"if not isinstance(window_size, int) or window_size < 1:\n    raise InputError(\"window_size must be a positive integer (e.g. 12)\")","typeGuard":"def valid_window(w) -> bool:\n    return isinstance(w, int) and not isinstance(w, bool) and w >= 1","tryCatchPattern":"try:\n    ema = exponential_moving_average(stock_prices, window_size)\nexcept ValueError as exc:\n    ema = exponential_moving_average(stock_prices, 12)  # deliberate fallback default\n    logger.warning(\"%s; fell back to window 12\", exc)","preventionTips":["Default window options to 12/26 in CLIs, never 0.","Validate computed windows (products of config values) before use.","Pass positional args in the documented order (data, window)."],"tags":["finance","ema","moving-average","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}