# Linear regression indicator & slope formula

#### essemkay

##### Member
Hi all

What is the Metastock 10.1 Formula indicator for

Linear Regression Indicator &
Linear Regression Slope

The formula not provide in metastock.

#### rkkarnani

##### Well-Known Member
No idea for specifically what is there in MS 10.1 but the formula below would work in it :

***********************************************

MetaStock -> Tools -> Indicator Builder -> New
Copy and paste formula below.

======================
LinReg Indicator/Slope
======================
---8<---------------------------

{ Linear Regression Indicator/Slope MS formula }
{ Precision accumulation errors courtesy of
MetaStock's Cum() single-precision function }
{ For private use only }

{ User inputs }
pds:=Input("Linear Regression periods",
2,2520,21);
plot:=Input("Linear Regression: [1]Indicator, [2]Slope",1,2,1);
x:=Input("[1]Open, [2]High, [3]Low, [4]Close, [5]WgtCl, [6]P",1,6,4);
x:=If(x=1,O,If(x=2,H,If(x=3,L,If(x=5,WC(),If(x=6,P,C)))));

{ Lin Reg Slope }
counter:=Cum(1)-pds;
y:=pds*Sum(counter*x,pds)
-Sum(counter,pds)*Sum(x,pds);
z:=pds*Sum(Pwr(counter,2),pds)
-Pwr(Sum(counter,pds),2);
z:=
LastValue(ValueWhen(1,Cum(IsDefined(z))=1,z));
LRS:=y/z;

{ Lin Reg Indicator - LR trendline end point }
LRI:=LRS*pds-LRS*(pds+1)/2+Mov(x,pds,S);

{ Lin Reg trendline start point }
LRstart:=Ref(LRI-LRS*(pds-1),pds-1);

{ Plot }
If(plot=1,LRI,LRS)

---8<---------------------------

#### vishal.yagnik

##### New Member
No idea for specifically what is there in MS 10.1 but the formula below would work in it :

***********************************************

MetaStock -> Tools -> Indicator Builder -> New
Copy and paste formula below.

======================
LinReg Indicator/Slope
======================
---8<---------------------------

{ Linear Regression Indicator/Slope MS formula }
{ Precision accumulation errors courtesy of
MetaStock's Cum() single-precision function }
{ For private use only }

{ User inputs }
pds:=Input("Linear Regression periods",
2,2520,21);
plot:=Input("Linear Regression: [1]Indicator, [2]Slope",1,2,1);
x:=Input("[1]Open, [2]High, [3]Low, [4]Close, [5]WgtCl, [6]P",1,6,4);
x:=If(x=1,O,If(x=2,H,If(x=3,L,If(x=5,WC(),If(x=6,P,C)))));

{ Lin Reg Slope }
counter:=Cum(1)-pds;
y:=pds*Sum(counter*x,pds)
-Sum(counter,pds)*Sum(x,pds);
z:=pds*Sum(Pwr(counter,2),pds)
-Pwr(Sum(counter,pds),2);
z:=
LastValue(ValueWhen(1,Cum(IsDefined(z))=1,z));
LRS:=y/z;

{ Lin Reg Indicator - LR trendline end point }
LRI:=LRS*pds-LRS*(pds+1)/2+Mov(x,pds,S);

{ Lin Reg trendline start point }
LRstart:=Ref(LRI-LRS*(pds-1),pds-1);

{ Plot }
If(plot=1,LRI,LRS)

---8<---------------------------
Do you have it for Amibroker