1 registered members (AndrewAMD),
1,248
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Linear Regression
#464427
02/12/17 17:03
02/12/17 17:03
|
Joined: Dec 2016
Posts: 13 Italy
Andrea66
OP
Newbie
|
OP
Newbie
Joined: Dec 2016
Posts: 13
Italy
|
Dear All, how is possible to print and calculate the linear regression as in the below picture ? I need also to print the straight line for a duration of several years ( lots of bars ). http://drive.google.com/open?id=0B7OAmRgfpHP-WkRjSk1uV3BVT2s
Last edited by Andrea66; 02/12/17 17:26.
|
|
|
Re: Linear Regression
[Re: Andrea66]
#464429
02/12/17 20:18
02/12/17 20:18
|
Joined: Aug 2016
Posts: 66
dr_panther
Junior Member
|
Junior Member
Joined: Aug 2016
Posts: 66
|
In general, Zorro is lighting fast, as long you don't do something stupid, it is fast enough Please check, if the following is, what you need, and search for "linear regression" in the manual, you will find some stuff there too.
function run(){
StartDate= 20060101;
BarPeriod = 1440;
LookBack = 1000 ;
vars Price = series(price());
vars linreg = series(LinearReg(Price, 1000));
plot("linreg", linreg[0],MAIN, BLUE);
}
|
|
|
Re: Linear Regression
[Re: dr_panther]
#464430
02/13/17 08:36
02/13/17 08:36
|
Joined: Dec 2016
Posts: 13 Italy
Andrea66
OP
Newbie
|
OP
Newbie
Joined: Dec 2016
Posts: 13
Italy
|
dr_panther, thanks for your reply. Unfortunately the result is not what I' m searching for. In the picture below the blue line is the result of the code you provided while the red line ( hand painted) is the regression straight line I need. Furthermore I would avoid to pass past data to the method calculation of the linear regression . For the linear regression you need only the present data for the calculation of the straight line. https://drive.google.com/open?id=0B7OAmRgfpHP-WkRjSk1uV3BVT2sThanks in advance, Andrea
Last edited by Andrea66; 02/13/17 17:11.
|
|
|
Re: Linear Regression
[Re: MatPed]
#464433
02/13/17 10:53
02/13/17 10:53
|
Joined: Dec 2016
Posts: 13 Italy
Andrea66
OP
Newbie
|
OP
Newbie
Joined: Dec 2016
Posts: 13
Italy
|
MatPed, yes you are right; I would plot the standard regression straight line which of course needs 2 values ( as per my picture above ). My final target would be also to plot the regression channels of the series. If you could provide also the r-squared value like in the picture below, would be great. https://drive.google.com/open?id=0B7OAmRgfpHP-YkVycXJIeXNRQlECiao
Last edited by Andrea66; 02/13/17 11:18.
|
|
|
Re: Linear Regression
[Re: Andrea66]
#464454
02/15/17 10:47
02/15/17 10:47
|
Joined: Dec 2016
Posts: 13 Italy
Andrea66
OP
Newbie
|
OP
Newbie
Joined: Dec 2016
Posts: 13
Italy
|
In MQL the code is something like this :
// variables double a,b,c, sumy=0.0, sumx=0.0, sumxy=0.0, sumx2=0.0, h=0.0,l=0.0; int x; // calculate linear regression for(int i=0; i<barsToCount; i++) { sumy+=Close[i]; sumxy+=Close[i]*i; sumx+=i; sumx2+=i*i; } c=sumx2*barsToCount-sumx*sumx; if(c==0.0) { Alert("Error in linear regression!"); return; } // Line equation b=(sumxy*barsToCount-sumx*sumy)/c; a=(sumy-sumx*b)/barsToCount; // Linear regression line in buffer for(x=0;x<barsToCount;x++) LR_line[x]=a+b*x;
but I'm not able to translate in Zorro code. "barsToCount" is the number of bars displayed in the graph.
May be also R has similar calculation but it's too difficult for me. Any help ?
Last edited by Andrea66; 02/15/17 11:08.
|
|
|
Re: Linear Regression
[Re: Andrea66]
#464455
02/15/17 11:47
02/15/17 11:47
|
Joined: Feb 2015
Posts: 652 Milano, Italy
MatPed
User
|
User
Joined: Feb 2015
Posts: 652
Milano, Italy
|
Well perfection is the worst enemy of production. so I guess that my proposal could be quite good Anyway, I remember little regarding Mql, but if you change "double" with "var", fill the value in 2 "vars" named Close and LR_line, your code should work 90%. You my encounter be the "vars Close" declaretion Close is a reserved name. Just use myClose or something like that. Try it and let me know. Ciao
|
|
|
Re: Linear Regression
[Re: MatPed]
#464460
02/15/17 15:32
02/15/17 15:32
|
Joined: Dec 2016
Posts: 13 Italy
Andrea66
OP
Newbie
|
OP
Newbie
Joined: Dec 2016
Posts: 13
Italy
|
I tried with the following code but without success ( it doesn'plot a straight line but a curve ):
#define N 50 // Number of bars function run() { StartDate= 20060101; EndDate = 20060601;
BarPeriod = 15;
// variables var a,b,c,sumy=0.0,sumx=0.0,sumxy=0.0,sumx2=0.0,h=0.0,l=0.0; int x,i; int barsToCount=50; vars myClose = series(priceClose()); for( i=0; i<N; i++) { sumy+=myClose[i]; sumxy+=myClose[i]*i; sumx+=i; sumx2+=i*i; } c=sumx2*N-sumx*sumx; // Line equation b=(sumxy*barsToCount-sumx*sumy)/c; a=(sumy-sumx*b)/N; var LR_line[N]; // Linear regression line in buffer for(x=0;x<N;x++) { LR_line[x]=a+b*x; plot("NEW_linreg", LR_line[x],MAIN, RED); } }
Setting LR_line as vars generate an error, thus I changed to var.
Last edited by Andrea66; 02/15/17 16:02.
|
|
|
|