Linear Regression
Ordinary Least Squares Trend Line
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 R² coefficient of determination, which measures how well the straight line explains the price movement (0 = no fit, 1 = perfect fit).
Formula
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
| Port | Direction | Type | Description |
|---|---|---|---|
| input | Input | (number | null)[] | Any numeric series — typically close prices. |
| values | Output | (number | null)[] | Predicted (fitted) values along the regression line. |
| timestamps | Output | number[] | Bar timestamps aligned with values. |
| slope | Output | number | OLS slope — price change per bar. Positive = uptrend. |
| intercept | Output | number | OLS intercept (predicted value at bar index 0). |
| r2 | Output | number | R² coefficient: 0 = no linear fit, 1 = perfect linear fit. |