# Linear Regression Forecast formula or any Indicator? - page 2

507

I tried to simplify the code with this following, but it shows zero divide error. I know its because of the i = 0. But there is no other option too. I need Close[0] for current close price on the chart while calculating right. So without using zero on "i" how can I do it?

```int start()

{

int limit;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- the last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- main loop
for(int i=0; i<limit; i++)
{
//-------------slope
double sumy=0,
sumx=0,
sumxy=0,
sumx2=0;

sumy+=Close[i];
sumxy+=Close[i]*i;
sumx+=i;
sumx2+=i*i;

SLOPE = (RegLin_Period*sumxy - sumx*sumy) / (RegLin_Period*sumx2 - sumx*sumx) ;

INTER = (sumy - (SLOPE * sumx)/RegLin_Period );
R = INTER + (SLOPE * i);
Data[i] = R;
}

return(0);

}```

Thanks

16764

You're not summing anything when you zero them inside the loop. You're not computing a slope/Intercept when you have only one point (inside the loop.)

Compare that to your original functions

507

WHRoeder:

You're not summing anything when you zero them inside the loop. You're not computing a slope/Intercept when you have only one point (inside the loop.)

Compare that to your original functions

Ok, then getting back to my original functions. I corrected it as you recommended earlier. But why graphs are like that.  There is a huge variation from each data. I understand it must be something to do with the theory itself.

Well in the example below, For recent close price, x = 1 not zero, like I declared at first double sumx = 0; I tried declaring sumx = 1; but that's not helping.

This is the Excel calculation on EURUSD data, but on excel graphs are showing right.

507

I figure out what was the problem, i was calling function inside a function while calculating Intercept. But I solve the issue. Now its plotting right. But its plotting same way like the indicator I posted at the the first.

With Linear Regression Forecast indicator its still have such difference. :X