Help With TimeSeriesForecast Function

 

Hi guys. I'm new.


I pulled some code out of an indicator I found floating around somewhere in order to include it in my EA, but I'm having trouble with it. It doesn't return the same value that the indicator does.


I think it has something to do with the way I'm normalizing the doubles and floating point precision, but I can't figure it out.


I've tried it with NormalizeDouble() and without. I've tried only normalizing some of them, and without. I'm stumped. Please help, or maybe just post a function for TSF that actually WORKS. :)


Help would be preferred though, that way I can learn what the heck I'm doing wrong.


Anyhow here's the code I'm using, and the original indicator it's stolen from is attached:

double TimeSeriesForecast(int TSFPeriod, int Offset) {
	double sy=0;
	double sx=0;
	double sxy=0;
	double sx2=0;
	for (int cnt=0 + Offset; cnt <= (TSFPeriod + Offset); cnt++)
	{
		sy+=Close[cnt];
		sx+=(cnt - Offset);
		sx2+=NormalizeDouble((cnt - Offset)*(cnt - Offset),4);
		sxy+=NormalizeDouble(Close[cnt]*(cnt - Offset),4);
	}
	double kb=NormalizeDouble((TSFPeriod*sxy-sx*sy)/(TSFPeriod*sx2-sx*sx),4);
	double ka=NormalizeDouble((sy-kb*sx)/TSFPeriod,4);
	double ival=NormalizeDouble(ka+kb,4);
	
 return(ival);
}
Files:
 

LOL! I figured it out, and I feel so stupid. I made it MUCH harder than I should have.


Anyhow, here's the fixed code for anyone who wants it:


double TimeSeriesForecast(int TSFPeriod, int Offset) {
	double sy=0;
	double sx=0;
	double sxy=0;
	double sx2=0;
	for (int cnt=1; cnt <= TSFPeriod; cnt++)
	{
		sy+=Close[(cnt-1)+Offset];
		sx+=cnt;
		sx2+=cnt*cnt;
		sxy+=Close[(cnt-1)+Offset]*cnt;
	}
	double kb=(TSFPeriod*sxy-sx*sy)/(TSFPeriod*sx2-sx*sx);
	double ka=(sy-kb*sx)/TSFPeriod;
	double ival=ka+kb;
	
	
 return(ival);
}