DaysSince1900
- get number of days since January 1st, 1900

Date/Time
(AmiBroker 5.20)


SYNTAX DaysSince1900()
RETURNS ARRAY
FUNCTION The function returns the number of days that passed since January 1st, 1900, counting from 2. January 1, 1900 is serial number 2, and January 1, 2008 is serial number 39448.

Technically is equal to Windows OLEDATE and Excel's DATEVALUE function. As to why it starts counting from 2 (two) - it is to get the same values as Excel DATEVALUE. Excel's DATEVALUE function starts counting from one but it includes Feb 29, 1900 which did not exist and this adds extra one day for all dates starting from Mar 1st, 1900.

The function can be used for calculations that involve calendar days as opposed to trading days and replaces previously proposed AFL solution http://www.amibroker.com/kb/2007/03/15/calendar-day-index/

Now RefDays can be implemeted as follows (see example)

EXAMPLE
SetBarsRequired( 365, 0 );
function RefDays( Array, Days )
{
   td =
DaysSince1900();
   result =
Null;

  
if( Days < 0 )
   {
    
for( i = BarCount -1; i >= -Days; i = i - 1 )
     {
       backday = td[ i ] + Days;
// Days is negative
      
for( j = -Days/2; j < i; j++ )
       {
          
if( td[ i - j ] <= backday )
           {
             result[ i ] = Array[ i - j ];
            
break;
           }
       }
     }
   }
  
return result;
}

Plot( C, "C", colorRed );
Plot( Ref( C, -252 ), "Close 252 bars back", colorBlue );
Plot( RefDays( C, -365 ), "Close 365 days back", colorGreen );
SEE ALSO Date() function , DateNum() function , DateTime() function , DateTimeConvert() function

References:

The DaysSince1900 function is used in the following formulas in AFL on-line library:

More information:

See updated/extended version on-line.