Linear Regression

Calculate the rolling Linear Regression of a price series: the end point of the least-squares line over the window, its slope, intercept and angle, and the Time Series Forecast one period ahead.

Over every window, a straight line is fitted through the prices by ordinary least squares. Its end point is a moving average that lags less than a simple average, as it follows the trend of the window rather than its middle (Chande and Kroll call it the linear regression indicator), the slope is the trend in price per period, and the Time Series Forecast extends the line by one period. A price above a rising regression line is read as a confirmed uptrend, and the slope turning negative as the trend of the window turning down.

The formula is as follows, with time \(x = 0\), …, window - 1 from the oldest price of the window to the latest:

\[\text{Slope} \;(b_{1}) = \frac{\operatorname{Sum}((x - \operatorname{Mean}(x)) \cdot (\text{Close} - \operatorname{Mean}(\text{Close})))}{\operatorname{Sum}((x - \operatorname{Mean}(x))^{2})}\] \[\text{Intercept} \;(b_{0}) = \operatorname{Mean}(\text{Close}) - b_{1} \cdot \operatorname{Mean}(x),\;\; \text{the line at the oldest period of the window}\] \[\text{Linear Regression} = b_{0} + b_{1} \cdot (\text{window} - 1),\;\; \text{the line at the latest period}\] \[\text{Time Series Forecast} = b_{0} + b_{1} \cdot \text{window},\;\; \text{the line one period ahead}\] \[\text{Angle} = \operatorname{arctan}(b_{1}) \;\text{in degrees}\]

Also known as: LINEARREG, linear regression indicator, end-point moving average, least squares moving average, LSMA, TSF, time series forecast, linear regression slope, intercept, angle.

Example · AAPL, MSFT · 2021 to 2025

Install the Finance Toolkit with pip install financetoolkit -U, then call get_linear_regression:

from financetoolkit import Toolkit

toolkit = Toolkit(
    tickers=["AAPL", "MSFT"],
    api_key="FINANCIAL_MODELING_PREP_KEY",
    start_date="2021-01-01",
    end_date="2025-12-31",
)

toolkit.econometrics.get_linear_regression().xs("AAPL", level=1, axis="columns")

Or, with the Finance Toolkit MCP set up, ask an AI assistant such as Claude or ChatGPT:

What were the 14-day linear regression value, slope, angle, intercept and time series forecast of Apple's share price over the last five trading days of 2025?

Which returns:

Using Your Own Data

get_linear_regression runs on any data, not only prices: ratios, economic indicators, yield curves or your own dataset. Pass a DataFrame, a Series or a dictionary of them to Econometrics as data, with the dates in the rows or the columns, and set changes to how its changes are calculated: "percentage", "log", "difference" for rates and ratios, or None when the data are changes already. For example, for the monthly unemployment rates of the United States and Germany:

from financetoolkit import Economics, Econometrics

economics = Economics(start_date="2000-01-01")

unemployment = economics.get_unemployment_rate(
    countries=["United States", "Germany"], period="monthly"
)

econometrics = Econometrics(data=unemployment, changes="difference")

econometrics.get_linear_regression()

Parameters

get_linear_regression accepts the following parameters.

ParameterTypeDescriptionExample
periodstr | NoneThe data frequency (daily, weekly, monthly, quarterly, or yearly). Defaults to None, which uses “daily”."weekly"
columnstrThe historical data column the statistic is calculated on: “Adj Close” for the prices (the series as given with Econometrics(data=...)), “Close” for the traded prices or “Return” for the returns (the changes of the series). Defaults to “Adj Close”."Return"
windowintThe number of periods the line is fitted over, at least 2. Defaults to 14.20
include_benchmarkboolWhether to include “Benchmark” among the assets. Defaults to False.True
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise).2
show_columnsstr | list[str] | NoneThe columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column.None, which keeps every column

References

Share