[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [EquisMetaStock Group] Subject: Linear Regression Slope code



PureBytes Links

Trading Reference Links

Hi Tom

> Roy, did you ever get the code worked out to duplicate the Linear Regression
> Slope function? (per below)
> If so, could you share it?

Yes, with a lot of assistance from Harry Ward. There is now another way to plot Linear Regression
Slope in the MetaStock Formula Language, other than by using the LinRegSlope() function or the Cum()
based code. As with your Linear Regression, lack of space imposes a limit on the number of periods
that can be used.

This is the suspect code that I started out with. It's OK for the first 500-1000 bars, but after
that the errors introduced by the large numbers generated internally make for inaccuracies, and I
found the inaccuracies to be unacceptable.

n:=Input("Time Periods",2,99,6);
(n*Sum(Cum(1)*C,n)-Sum(Cum(1),n)*Sum(C,n))/
(n*Sum(Pwr(Cum(1),2),n)-Pwr(Sum(Cum(1),n),2));

Along the way I discovered that the following adaptation is more accurate. The "b" variable should
be a constant, but it begins to wander as the number of bars increases (somewhere between 500 and
1500, and it's more pronounced with a lower periods value). By locking in the initial value of "b"
that particular failing is eliminated. There is still a problem of accuracy with the "a" variable as
the bar count climbs, but it's not quite as pronounced as with the "b" variable.

n:=Input("Time Periods",1,200,6);
a:=(n*Sum(Cum(1)*C,n)-Sum(Cum(1),n)*Sum(C,n));
b:=(n*Sum(Pwr(Cum(1),2),n)-Pwr(Sum(Cum(1),n),2));
b:=LastValue(ValueWhen(1,Cum(1)=n,b));
a/b;

Harry came up with a mathematical formula for LRS, and my interpretation of that in MetaStock is as
follows. I've only taken it up to 40 periods, which is where the 40 constant limitation kicks in.
There's still room for about 500 characters so 46 periods is probably the limit unless some further
economies can be made.

There are ways to limit the lead-in N/A period to only a few more than the number of periods
selected, but the extra code takes up more space and so the formula is restricted to a lower maximum
periods value. If you think it looks clumsy then I'd have to agree with you. However, it satisfies
my need for code that retains its accuracy when adapted to plot weekly LRS values on daily charts.
I'm still working on the weekly value version, where space is an even bigger problem because of the
timing issues involved, but it will be posted eventually.

Regards

Roy

  {Linear Regression Slope HW}
N:=Input("LRS Periods",2,40,6);
K:=Input("Price  O=1  H=2  L=3  C=4  MP=5",1,5,4);
K:=If(K=1,O,If(K=2,H,If(K=3,L,If(K=5,MP(),C))));
X:=ValueWhen(1,Cum(1)=N,Cum(Cum(1)))/N;
M:=(x-1)*K+(x-2)*Ref(K,-1)+
(n>2)*(x-3)*Ref(K,-2)+(n>3)*(x-4)*Ref(K,-3)+
(n>4)*(x-5)*Ref(K,-4)+(n>5)*(x-6)*Ref(K,-5)+
(n>6)*(x-7)*Ref(K,-6)+(n>7)*(x-8)*Ref(K,-7)+
(n>8)*(x-9)*Ref(K,-8)+(n>9)*(x-10)*Ref(K,-9)+
(n>10)*(x-11)*Ref(K,-10)+(n>11)*(x-12)*Ref(K,-11)+
(n>12)*(x-13)*Ref(K,-12)+(n>13)*(x-14)*Ref(K,-13)+
(n>14)*(x-15)*Ref(K,-14)+(n>15)*(x-16)*Ref(K,-15)+
(n>16)*(x-17)*Ref(K,-16)+(n>17)*(x-18)*Ref(K,-17)+
(n>18)*(x-19)*Ref(K,-18)+(n>19)*(x-20)*Ref(K,-19)+
(n>20)*(x-21)*Ref(K,-20)+(n>21)*(x-22)*Ref(K,-21)+
(n>22)*(x-23)*Ref(K,-22)+(n>23)*(x-24)*Ref(K,-23)+
(n>24)*(x-25)*Ref(K,-24)+(n>25)*(x-26)*Ref(K,-25)+
(n>26)*(x-27)*Ref(K,-26)+(n>27)*(x-28)*Ref(K,-27)+
(n>28)*(x-29)*Ref(K,-28)+(n>29)*(x-30)*Ref(K,-29)+
(n>30)*(x-31)*Ref(K,-30)+(n>31)*(x-32)*Ref(K,-31)+
(n>32)*(x-33)*Ref(K,-32)+(n>33)*(x-34)*Ref(K,-33)+
(n>34)*(x-35)*Ref(K,-34)+(n>35)*(x-36)*Ref(K,-35)+
(n>36)*(x-37)*Ref(K,-36)+(n>37)*(x-38)*Ref(K,-37)+
(n>38)*(x-38-1)*Ref(K,-38)+(n>(38+1))*(x-38-2)*Ref(K,-38-1);
D:=Pwr(1-x,2)+Pwr(2-x,2)+
(n>2)*Pwr(3-x,2)+(n>3)*Pwr(4-x,2)+
(n>4)*Pwr(5-x,2)+(n>5)*Pwr(6-x,2)+
(n>6)*Pwr(7-x,2)+(n>7)*Pwr(8-x,2)+
(n>8)*Pwr(9-x,2)+(n>9)*Pwr(10-x,2)+
(n>10)*Pwr(11-x,2)+(n>11)*Pwr(12-x,2)+
(n>12)*Pwr(13-x,2)+(n>13)*Pwr(14-x,2)+
(n>14)*Pwr(15-x,2)+(n>15)*Pwr(16-x,2)+
(n>16)*Pwr(17-x,2)+(n>17)*Pwr(18-x,2)+
(n>18)*Pwr(19-x,2)+(n>19)*Pwr(20-x,2)+
(n>20)*Pwr(21-x,2)+(n>21)*Pwr(22-x,2)+
(n>22)*Pwr(23-x,2)+(n>23)*Pwr(24-x,2)+
(n>24)*Pwr(25-x,2)+(n>25)*Pwr(26-x,2)+
(n>26)*Pwr(27-x,2)+(n>27)*Pwr(28-x,2)+
(n>28)*Pwr(29-x,2)+(n>29)*Pwr(30-x,2)+
(n>30)*Pwr(31-x,2)+(n>31)*Pwr(32-x,2)+
(n>32)*Pwr(33-x,2)+(n>33)*Pwr(34-x,2)+
(n>34)*Pwr(35-x,2)+(n>35)*Pwr(36-x,2)+
(n>36)*Pwr(37-x,2)+(n>37)*Pwr(38-x,2)+
(n>38)*Pwr((38+1)-x,2)+(n>(38+1))*Pwr(40-x,2);
M/D;




------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/BefplB/TM
--------------------------------------------------------------------~-> 

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/equismetastock/

<*> To unsubscribe from this group, send an email to:
    equismetastock-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/