{_LinRegSlopeSFC - Linear Regression Slope Super Fast Calc
by Alex Matulich, Unicorn Research Corporation, last updated 4/24/2004
Derived from an algorithm developed by Bob Fulks and separately by
Mark A. Simms (Index Options Group, 2002).
Update 11/17/2002 (Bob Fulks): Re-initialize every 1000 bars.
This is a super-efficient version of the Fulks/Simms Linear
Regression Slope Fast Calc algorithm. Here, a loop gets executed
only once during initialization, rather than at every bar.
This function assumes that the Y-axis (where X=0) always coincides
with the current bar. Therefore, the Y-intercept is the same as
the value of the regression line at the current bar, and is given
by the formula:
YIntercept = Average(Price, Length) + slope * Length/2;
}
Inputs:
Price(NumericSeries), {values on which to calculate slope}
Length(NumericSimple), {lookback length}
YIntercept(NumericRef); {optional Y-intercept value returned here}
Vars: xLen(0), ix(0), m1(0), m2(0), sumP(0), sumIP(0);
{Re-initialize at first bar, every 1000 bars, or when Length changes}
if xLen <> Length or Mod(CurrentBar, 1000) = 1 then begin
xLen = Length;
m1 = 6 / (Length * (Length + 1));
m2 = 2 / (Length - 1);
sumP = 0; sumIP = 0;
for ix = 0 to length-1 begin
sumP = sumP + Price[ix];
sumIP = sumIP + ix * Price[ix];
end;
{Linear regression slope super fast calculation}
end else begin
sumIP = sumIP + sumP - Length * Price[Length];
sumP = sumP + Price - Price[Length];
end;
value1 = m1 * (sumP - m2 * sumIP);
YIntercept = sumP/Length + value1 * Length * 0.5;
_LinRegSlopeSFC = value1;