{"record":{"id":"36dd42f4b8274093","repo":"OpenBB-finance/OpenBB","slug":"calculation-asks-for-at-least-last-window-days-o","errorCode":null,"errorMessage":"Calculation asks for at least last {window} days of data","messagePattern":"Calculation asks for at least last (.+?) days of data","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"openbb_platform/extensions/technical/openbb_technical/helpers.py","lineNumber":510,"sourceCode":"    window: int\n        Length of look back period\n\n    Returns\n    -------\n    float:\n        R2 of fit to log data\n    float:\n        Coefficient of linear regression\n    Series:\n        Values for best fit line\n    \"\"\"\n    # pylint: disable=import-outside-toplevel\n    from numpy import arange, exp, log\n    from pandas import Series\n    from sklearn.linear_model import LinearRegression\n\n    if len(values) < window:\n        raise ValueError(f\"Calculation asks for at least last {window} days of data\")\n\n    values = values[-window:]\n\n    y = log(values)\n    X = arange(len(y)).reshape(-1, 1)  # pylint: disable=invalid-name\n\n    lr = LinearRegression()\n    lr.fit(X, y)\n\n    r2 = lr.score(X, y)\n    coef = lr.coef_[0]\n    annualized_coef = (exp(coef) ** 252) - 1\n\n    return r2, annualized_coef, Series(lr.predict(X))\n\n\ndef calculate_fib_levels(\n    data: \"DataFrame\",","sourceCodeStart":492,"sourceCodeEnd":528,"githubUrl":"https://github.com/OpenBB-finance/OpenBB/blob/3e071fcc2cd9f891cac6040ae60296dba76dab46/openbb_platform/extensions/technical/openbb_technical/helpers.py#L492-L528","documentation":"In the regression helper (openbb_quantitative-style log regression in openbb_technical/helpers.py), raises when len(values) < window: the trend/regression fit requires at least `window` most-recent observations and refuses to run on shorter series.","triggerScenarios":"Calling the regression/trendline helper (used by drawing/analysis endpoints) with a window larger than the series length, e.g. window=365 on 200 days of prices.","commonSituations":"Daily-bar assumptions applied to weekly/monthly series (a year of weekly bars is ~52, not 365); short history for new listings; window parameters copied from long-history charts.","solutions":["Reduce window to <= len(values).","Fetch a longer history before computing the regression.","Derive window from actual data length: window = min(window, len(values)).","Confirm the data frequency matches the window's 'days' semantics."],"exampleFix":"# before\nregression(values=prices[:100], window=252)  # raises\n\n# after\nregression(values=prices, window=min(252, len(prices)))","handlingStrategy":"validation","validationCode":"assert len(values) >= window, f\"need >= {window} points, have {len(values)}\"\nwindow = min(window, len(values))","typeGuard":"def window_covers_series(values, window: int) -> bool:\n    return len(values) >= window","tryCatchPattern":"try:\n    r2, coef, fit = regression(values, window=window)\nexcept ValueError as e:\n    if \"at least last\" in str(e):\n        window = len(values)\n        r2, coef, fit = regression(values, window=window)\n    else:\n        raise","preventionTips":["Match window 'days' to daily bars only","Fetch >= window bars before trend fitting","Clamp window to series length in exploratory code"],"tags":["regression","window","technical","insufficient-data"],"backgroundTag":null,"analyzedSha":"3e071fcc2cd9f891cac6040ae60296dba76dab46","analyzedAt":"2026-08-14T23:40:48.960Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}