linreg

linreg(x, n, offset)
Parameters
  • x (series) – Input series

  • n (integer) – Number of bars to calculate the regression

  • offset (numeric) – Regression offset

Returns

series

Linear regression curve. A line that best fits the prices specified over a user-defined time period. It is calculated using the least squares method.

\[\begin{split}\hat{b} &= \frac{\sum_{i=0}^{n-1}x_i \sum_{i=0}^{n-1}i^2 - \sum_{i=0}^{n-1}i \sum_{i=0}^{n-1}i x_i}{n \sum_{i=0}^{n-1}i^2 - (\sum_{i=0}^{n-1}i)^2}\\ \hat{a} &= \frac{n \sum_{i=0}^{n-1}i x_i - \sum_{i=0}^{n-1}i \sum_{i=0}^{n-1}x_i}{n \sum_{i=0}^{n-1}i^2 - (\sum_{i=0}^{n-1}i)^2}\\ \mathrm{linreg} &= \hat{b} - \hat{a} \mathrm{offset}\end{split}\]