Back to Docs
Traderoid

Linear Regression

Ordinary Least Squares Trend Line

IndicatorTrendOverlay

Overview

Linear Regression fits an ordinary least-squares (OLS) line through the entire input series and plots the predicted value at every bar. Unlike a moving average, the line is not lagged — it is redrawn over the full visible window each time new data arrives.

In addition to the predicted values, the node outputs the slope (rate of change per bar), the intercept, and the coefficient of determination, which measures how well the straight line explains the price movement (0 = no fit, 1 = perfect fit).

Formula

slope = (n·ΣxY − ΣxΣY) / (n·Σx² − (Σx)²)
intercept = (ΣY − slope·Σx) / n
predicted[i] = slope · i + intercept
R² = 1 − SS_res / SS_tot

Where x = 0, 1, 2, …, n−1 (bar index within the window) and Y is the input price series. SS_res is the residual sum of squares and SS_tot is the total sum of squares about the mean.

Parameters

Linear Regression has no configurable parameters. The regression is computed over the entire visible window set by the chart's window range control.

Inputs & Outputs

PortDirectionTypeDescription
inputInput(number | null)[]Any numeric series — typically close prices.
valuesOutput(number | null)[]Predicted (fitted) values along the regression line.
timestampsOutputnumber[]Bar timestamps aligned with values.
slopeOutputnumberOLS slope — price change per bar. Positive = uptrend.
interceptOutputnumberOLS intercept (predicted value at bar index 0).
r2OutputnumberR² coefficient: 0 = no linear fit, 1 = perfect linear fit.