Array-Based Linear Regression Channels and Deviation Bands
Summary
This Pine library fits a straight line to paired arrays of x and y observations using ordinary least squares. It returns fitted values alongside the largest positive and negative residuals and the average absolute residual. A separate drawing routine uses those outputs to display the fitted line, extreme residual offsets, and bands at the average absolute deviation above and below the fit.
The example maintains arrays of recent price pivots and their bar indices, updating them when pivot highs or lows appear, then draws a channel through those points. This illustrates using regression on irregularly spaced turning points rather than a fixed sequence of bars. The document provides implementation details and an example, not trading rules or empirical performance evidence. The deviation measure is an average absolute residual rather than a conventional standard deviation, and the regression denominator can be zero if the x values provide no variation; users should account for such edge cases when adapting the method.
Key ideas
- Ordinary least squares fits a line to paired x and y arrays and returns fitted values.
- The function reports maximum positive and negative residuals and average absolute residual size.
- A drawing method plots the fitted line with residual-based offsets and average-deviation bands.
- The example applies regression to arrays of price pivot values and their bar indices.
- The library describes a calculation and visualization method, without testing a trading strategy.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.