Type : Function, Name : NLR
InPuts: Length(NumericSimple), price(NumericSeries),
DesiredBar(NumericSimple),oNLRPrice(NumericRef),oStdDev(NumericRef);
vars: AvgX(0),AvgY(0),N(0),XM(0),YM(0),XM2(0),SXX(0),SXY(0),SYY(0),
SSYY(0),SXX2(0),SX2X2(0),SYX2(0),ACoeff(0),BCoeff(0),CCoeff(0),
X(0),Y(0), MaxLength(100),s(0),TX(0),TY(0),RV(0),ERR(0);
array: YValue[100](0),XValue[100](0);
if BarNumber = 1 then
begin
N = length;
{ be sure array size is protected }
if N > MaxLength then
N = MaxLength;
if N < 2 then
N = 2;
end;
{ fill arrays }
for value1 = 1 to N
begin
XValue[value1] = value1-1;
YValue[value1] = Price[value1-1];
end;
{ calc averages of x,y pairs }
AvgX = 0; AvgY = 0;
for value1 = 1 to N
begin
AvgX = AvgX + XValue[value1];
AvgY = AvgY + YValue[value1];
end;
if N <> 0 then
begin
AvgX = AvgX / N;
AvgY = AvgY / N;
end;
{ do regression and calc 3 coefficients }
SXX = 0; SXY = 0;SYY = 0;SXX2 = 0;SX2X2 = 0;SYX2 = 0;
for value1 = 1 to N
begin
XM = XValue[value1] — AvgX;
YM = YValue[value1] — AvgY;
XM2 = XValue[value1]*XValue[value1] — AvgX*AvgX;
SXX = SXX + XM*XM;
SXY = SXY + XM*YM;
SYY = SYY + YM*YM;
SXX2 = SXX2 + XM*XM2;
SX2X2 = SX2X2 + XM2*XM2;
SYX2 = SYX2 + YM*XM2;
end;
value99 = SXX*SX2X2 — SXX2*SXX2;
if value99 <> 0 then
begin
BCoeff = ( SXY*SX2X2 — SYX2*SXX2 ) / value99 ;
CCoeff = ( SXX*SYX2 — SXX2*SXY ) / value99;
end;
ACoeff = AvgY — BCoeff*AvgX — CCoeff*AvgX*AvgX;
{calc estimated price for desired bar
DesiredBar: use + for past and — for future
eg., for next bar in future:
DesiredBar = -1 }
oNLRPrice = ACoeff + BCoeff*DesiredBar+
CCoeff*DesiredBar*DesiredBar;
{calc std dev}
S = 0; AvgY = 0;
for value1 = 1 to N
begin
TY = YValue[value1];
TX = XValue[value1];
RV = ACoeff + BCoeff*XValue[value1]+
CCoeff*XValue[value1]*XValue[value1];
ERR = TY — RV;
AvgY = AvgY + TY;
S = S + ERR*ERR;
end;
if N-1 <> 0 then
if S /(N-1) > 0 then
S = SquareRoot(S /(N-1));
oStdDev = s;
NLR = 1;
Type : Indicator, Name : NLR1 Indicator
InPuts:
Length(20), price(close);
vars: S(0),oNLRPrice(0);
value1 = NLR(length, price, 0, oNLRPrice, S);
plot1(oNLRPrice, «NLR»);