logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
ngterry  
#1 Posted : Thursday, May 5, 2011 8:21:02 AM(UTC)
ngterry

Rank: Newbie

Groups: Registered, Registered Users
Joined: 5/5/2011(UTC)
Posts: 4

Does someone has the code of Time Series that except the LENGTH parameter, there is a TARGET parameter for setting the point in the past or future to use for projecting a regression value. Use a positive integer for a previous point and a negative integer for a future point? e.g. The projected value 5 bars in the future of a linear regression line over the past 20 bars.
wabbit  
#2 Posted : Thursday, May 5, 2011 3:02:32 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Ref() wabbit [:D]
ngterry  
#3 Posted : Saturday, May 7, 2011 8:16:13 AM(UTC)
ngterry

Rank: Newbie

Groups: Registered, Registered Users
Joined: 5/5/2011(UTC)
Posts: 4

I am sorry. I still don't get it. I do not know the time management. How to translate "Summation of XY of length" into code, where X means the timeline or bar number, and Y means the price e.g. Close, and length e.g. 28 days? What is the syntax of the following in MetaStock? for Value1 = 0 to Length-1 begin SumY=SumY + ref(Close, neg(Value1)); end;
wabbit  
#4 Posted : Monday, May 9, 2011 1:20:33 AM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Obviously you're doing what I thought you were doing; have a look at the built-in functions for time series forecast TSF() and Linear Regression Indicator LinearReg().


wabbit [:D]

ngterry  
#5 Posted : Monday, May 9, 2011 7:51:17 AM(UTC)
ngterry

Rank: Newbie

Groups: Registered, Registered Users
Joined: 5/5/2011(UTC)
Posts: 4

However, you might not understand what I am asking. I want to have a look of TSF() or LinearReg(), but the code of TSF() or LinearReg().
wabbit  
#6 Posted : Monday, May 9, 2011 8:22:30 AM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
You mean you want the underlying code how MS computes TSF() and LinearReg()?

Ignoring single precision errors, here's my take on LinearReg(C,20):

Code:

x:=Cum(1);
y:=CLOSE;

n:=20;

sumX:=Sum(x,n);
sumY:=Sum(y,n);
sumXY:=Sum(x*y,n);
sumX2:=Sum(x*x,n);

b:=( (n*sumXY)-(sumX*sumY) ) / ( (n*sumX2)-(sumX*sumX) );
a:=( sumY - (b*sumX) ) / n;

{plot/return}
a + (b*x);


so then the TSF(C,20) is the same, except you plot a + b*(x + 1);



wabbit [:D]

mstt  
#7 Posted : Tuesday, May 10, 2011 2:35:47 AM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)
Time Series Foercast is just the sum of Linear Regression and Linear Regression Slope. It's also included as one of the Mov() funtion options. MFL code for LR, LRS and TSF has been circulating for a number of years but it's suspect because of the huge numbers generated by the Cum() functions it uses, and MetaStock does not handle large numbers well. Personally I believe that Cum() should not be needed, and in one instance I have managed to generate one of the internal calculations without it. However the MFL code version loses the plot as the number of bars moves beyond 2000-3000 (as I recall). Roy
jjstein  
#8 Posted : Tuesday, May 10, 2011 7:25:13 AM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
>>it's suspect because of the huge numbers generated by the Cum() functions it uses, and MetaStock does not handle large numbers well.

Roy,

Do you think that effects the calculation of items like Advance/Decline line?


mstt  
#9 Posted : Tuesday, May 10, 2011 6:07:24 PM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)
Hi Johnathan Anything is possible but I think it’s rather unlikely that simple accumulations using Cum() are going to return suspect numbers. Take a look at the numbers generated by some of the expression in the first formula below and you’ll see just how big those numbers can be. Try plotting the MFL-based Linear Regression indicators below on 40-50 years of DJIA daily data and check that against the plot of a standard LinearReg() function. Here’s a typical MFL Linear Regression formula. {Linear Regression (N)} N:=14; (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))* Cum(1)+(Mov(C,N,S)-Mov(Cum(1),N,S)* (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))); When you look at the huge numbers generated by some of the expressions, "Pwr(Sum(Cum(1),14),2)*Cum(1)" for example, you can see that it’s not just bars or prices that are being accumulated. The result of this expression grows exponentially into trillions or even quadrillions if it spans enough data. Here’s another version of Linear Regression, also built in MFL, and it follows much the same pattern as above although that’s not obvious at first glance. The only significant difference is that my formula generates one constant (the B variable) directly from N rather than subtracting one huge number from another slightly bigger (huger) number. {Linear Regression Indicator} N:=Input("LR Indicator Periods",1,99,14); M:=Cum(1); U:=Sum(M,N); X:=Sum(C,N); Y:=Sum(M*C,N); Z:=Sum(Pwr(M,2),N); A:=(N*Y-U*X); B:=N*N*(N+1)*(2*N+1)/6-Pwr(N*(N+1)/2,2); LRS:=A/B; LRS*M + X/N - LRS*U/N; Not being a mathematician I have no idea how Linear Regression is generated within MetaStock as a standard function. What I do know is that discrete code versions come up short, and what I suspect is that a linear regression line or plot should only have to use N periods of data to get a result, not the complete history. I’m confused as to the original question posed by ngterry, but if it’s any help to him here are my MFL versions of Linear Regression Slope and Time Series Forecast. There’s a good chance that none of the code will post correctly, but since the official method of posting code doesn’t seem to work for me I’ll just take my chances. {Linear Regression Slope} N:=Input("LR Indicator Periods",1,99,14); M:=Cum(1); U:=Sum(M,N); X:=Sum(C,N); Y:=Sum(M*C,N); Z:=Sum(Pwr(M,2),N); A:=(N*Y-U*X); B:=N*N*(N+1)*(2*N+1)/6-Pwr(N*(N+1)/2,2); A/B; {Time Series Forecast} N:=Input("LR Indicator Periods",1,99,14); M:=Cum(1); U:=Sum(M,N); X:=Sum(C,N); Y:=Sum(M*C,N); Z:=Sum(Pwr(M,2),N); A:=(N*Y-U*X); B:=N*N*(N+1)*(2*N+1)/6-Pwr(N*(N+1)/2,2); LRS:=A/B; LRS*M + X/N - LRS*U/N +A/B; Roy
wabbit  
#10 Posted : Wednesday, May 11, 2011 7:50:55 AM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
mstt wrote:
Not being a mathematician I have no idea how Linear Regression is generated within MetaStock as a standard function.
MS uses the standard least squares algorithm to compute the linear regression trendline and appears to use all the precision of double datatypes, which then lose precision when cast back into a float datatype; you can see this if you use the data in another application and then compare the results, or as I have done, written the function as a private library formula.

mstt wrote:
What I do know is that discrete code versions come up short, and what I suspect is that a linear regression line or plot should only have to use N periods of data to get a result, not the complete history.
MS doesn't use the complete history, just the N bars required, but then again, it needs to know where those N bars on the chart so it has to use Cum(1) for the 'X' values.

Astute readers will see the denominator in my code:
Code:

b:=( (n*sumXY)-(sumX*sumY) ) / ( (n*sumX2)-(sumX*sumX) );

is the 'B' variable in Roy's code:
Code:

{Linear Regression Indicator}

N:=Input("LR Indicator Periods",1,99,14);

M:=Cum(1);

U:=Sum(M,N);

X:=Sum(C,N);

Y:=Sum(M*C,N);

Z:=Sum(Pwr(M,2),N);

A:=(N*Y-U*X);


B:=N*N*(N+1)*(2*N+1)/6-Pwr(N*(N+1)/2,2); 


LRS:=A/B;

LRS*M + X/N - LRS*U/N;



I don't have my copy of Scientific Notepad with me, so had to find the images for the summation cases (at Wikipedia):
Code:

[img]http://upload.wikimedia.org/math/1/4/3/143ddcb078e447775b45cfc125912aa0.png[/img]
[img]http://upload.wikimedia.org/math/8/e/3/8e3cd67ad9c332f8e4988d37e70b34eb.png[/img]


so indeed MS may be less prone to precision errors, acknowledging:
Code:

 ( (N*sumX2)-(sumX*sumX) ) == N*N*(N+1)*(2*N+1)/6-Pwr(N*(N+1)/2,2);
where:
X:=Cum(1);
SumX:=Sum(X,N);

SumX2:=Sum(X*X,N);



but unfortunately, the precision errors do still creep in from the numerator which also contain large numbers! If you need to be absolutely precise, like always, do the computations externally to MS and either use the precise results outside MS or accept once, a small precision loss casting the result to a float datatype for return to MetaStock.

Really astute readers will also notice we can have some fun with the algorithm and gain a different appreciation of the Linear Regression Indicator with (combined Roy's code with mine):
Code:

x:=Cum(1);
y:=CLOSE;

n:=14;

sumX:=Sum(x,n);
sumY:=Sum(y,n);
sumXY:=Sum(x*y,n);

num:=N*SumXY-SumX*SumY;
den:=N*N*(N+1)*(2*N+1)/6-Pwr(N*(N+1)/2,2);

b:=num/den;

{plot/return}
mov(y,n,S) + b*(x - mov(x,n,S));

Notice the use of the SMA's...


wabbit [:D]



Users browsing this topic
Guest (Hidden)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.