Problem with DateNum() in function using a passed external array

I'm passing a foreign array to a function which loops through it and calculates each member's date number. All seems to work fine, except when the very last day is calculated. For that day, the calculated value increases from the prior day by 70 when I would have expected 1 (or perhaps 3). Any ideas as to what I am doing wrong? Thank you.

function Results(mode, array,ratio,startdate,startamt,ir  )
{
local cd,adi,dp,xxx; 
	
	for( i = 0; i < BarCount; i++ )
	{
		cd = DateNum();
		cd = cd[i];
		datenumfunctionvalue = DateNum(); //LAST I VALUE IS THE PROBLEM
	}	
}

sp = Foreign("$SPY","Close");
SandP = Results("SP",sp,resp,spdn,se,0);

Hi @RobertJay.

DateNum does not return a sequential sequential series of numbers. In your example, 1170731 represents 7/31/2017 and 1170801 is 8/1/2017.

The date coding is explained here:

https://www.amibroker.com/guide/afl/datenum.html

-Alan

1 Like

Bingo! That’s exactly what I had lost sight of. I will use dayssince1900() instead. Thanks a million Alan for your quick and spot-on response.