Download MetaTrader 5

Problem with Summation of the 5 last Closes

To add comments, please log in or register
Tom
34
Tom  

I want to code a linear regression of the 5 last Bars and i need the summation of the closes of the last 5 bars so i wrote this.

int i;
double sum_y = 0;
int sum_x = 0;

   for(i=1; i<=5; i++)
   {
   sum_y += Close[i];
   sum_x += i;
   }

So for example now sum_x should be always 5 but already after a few seconds there is 1000!! and with sum_y there is the same problem too.
whroeder1
15574
whroeder1  
  1. Sum of 1+2+3+4+5 will never equal 5.
  2. Are those variables global? Not local?
Tom
34
Tom  

I just want sum_y  = Close[1]+Close[2]+Close[3]+Close[4]+Close[5]

Ye you are right with sum_x. I dont need to calculate that for linear regression.

Variables are global.

whroeder1
15574
whroeder1  
insub: Variables are global.
Then they are not being reset to zero.
Vasyl Nosal
18950
Vasyl Nosal  
sum_x += 1;
Tom
34
Tom  
WHRoeder:
Then they are not being reset to zero.
I changed it and it works now!! thx WHRoeder.
To add comments, please log in or register