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 for current close price on the chart while calculating right. So without using zero on "i" how can I do it?
//---- check for possible errors
//---- the last counted bar will be recounted
//---- main loop
for(int i=0; i<limit; 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;
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.
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