Gamestudio Links
Zorro Links
Newest Posts
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
LPDIRECT3DCUBETEXTUR
E9

by Ayumi. 04/12/24 11:00
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 04/11/24 14:56
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, Quad), 509 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
11honza11, ccorrea, sakolin, rajesh7827, juergen_wue
19045 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Linear Regression #464427
02/12/17 17:03
02/12/17 17:03
Joined: Dec 2016
Posts: 13
Italy
Andrea66 Offline OP
Newbie
Andrea66  Offline 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: 61
D
dr_panther Offline
Junior Member
dr_panther  Offline
Junior Member
D

Joined: Aug 2016
Posts: 61
In general, Zorro is lighting fast, as long you don't do something stupid, it is fast enough laugh

Please check, if the following is, what you need, and search for "linear regression" in the manual, you will find some stuff there too.

Code:
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 Offline OP
Newbie
Andrea66  Offline 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-WkRjSk1uV3BVT2s

Thanks in advance, Andrea


Last edited by Andrea66; 02/13/17 17:11.
Re: Linear Regression [Re: Andrea66] #464431
02/13/17 10:37
02/13/17 10:37
Joined: Feb 2015
Posts: 652
Milano, Italy
M
MatPed Offline
User
MatPed  Offline
User
M

Joined: Feb 2015
Posts: 652
Milano, Italy
Andrea,
Is your question regarding the indicator linearreg that is returning only one Var, while you expect two values in order to be able to plot the line y= b+mx?
Am I correct?

Ciao

Re: Linear Regression [Re: MatPed] #464433
02/13/17 10:53
02/13/17 10:53
Joined: Dec 2016
Posts: 13
Italy
Andrea66 Offline OP
Newbie
Andrea66  Offline 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-YkVycXJIeXNRQlE

Ciao

Last edited by Andrea66; 02/13/17 11:18.
Re: Linear Regression [Re: Andrea66] #464434
02/13/17 11:37
02/13/17 11:37
Joined: Feb 2015
Posts: 652
Milano, Italy
M
MatPed Offline
User
MatPed  Offline
User
M

Joined: Feb 2015
Posts: 652
Milano, Italy
Ok,
I guess that linearreg is built not taking into consideration the time axis. Is more like the linear regression of all point as they appear at the same instant. I could be wrong and I would wait some input from people more expert than me, but I guess that you can find two points of the line you are looking for using two simple call:

var Point1 = LinearReg(Price, 500);
var Point2 = LinearReg(Price, 1000);
connecting point 1 and point 2 should be an approximation "your" line.

Try if it answer your point, if it does not double check the TA-Lib source code of the indicator in order to understand what the indicator really does or start from the math to code a new indicator that will suit your need.

If you are looking for a channel breakout strategy, I guess that dr_panther will be the proper approach, because you may be looking for a channel that will adapt to market conditions...

Ciao

Re: Linear Regression [Re: MatPed] #464439
02/13/17 16:43
02/13/17 16:43
Joined: Dec 2016
Posts: 13
Italy
Andrea66 Offline OP
Newbie
Andrea66  Offline OP
Newbie

Joined: Dec 2016
Posts: 13
Italy
MatPed,

the linear regression straight line needs to evaluate all the points of the curve; it can't be calculate as your proposal.

Re: Linear Regression [Re: Andrea66] #464454
02/15/17 10:47
02/15/17 10:47
Joined: Dec 2016
Posts: 13
Italy
Andrea66 Offline OP
Newbie
Andrea66  Offline 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
M
MatPed Offline
User
MatPed  Offline
User
M

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 laugh

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 Offline OP
Newbie
Andrea66  Offline 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.
Page 1 of 2 1 2

Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1