Problem with Summation of the 5 last Closes

 

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.
 
  1. Sum of 1+2+3+4+5 will never equal 5.
  2. Are those variables global? Not local?
 

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.

 
insub: Variables are global.
Then they are not being reset to zero.
 
sum_x += 1;
 
WHRoeder:
Then they are not being reset to zero.
I changed it and it works now!! thx WHRoeder.
Reason: