{"id":59,"date":"2007-03-15T15:15:06","date_gmt":"2007-03-15T20:15:06","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/2007\/03\/15\/calendar-day-index\/"},"modified":"2018-09-24T20:27:15","modified_gmt":"2018-09-24T19:27:15","slug":"calendar-day-index","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2007\/03\/15\/calendar-day-index\/","title":{"rendered":"Calendar day index"},"content":{"rendered":"

Someone asked me recently how to count calendar days<\/strong> (not bars) that passed since first available quote. AmiBroker has a built-in function ( since version 5.20 ) that calculates the number of days that passed since January 1st, 1900. This function can be used to easily implement number of days since first available quote Here is a sample formula that shows how to do that. <\/p>SetBarsRequired<\/span>( <\/span>sbrAll<\/span>,<\/span>0<\/span>);

function <\/span>TotalDays<\/span>()
{
 <\/span>d <\/span>= <\/span>DaysSince1900<\/span>();

 return <\/span>d <\/span>- <\/span>d<\/span>[ <\/span>0 <\/span>];
}

<\/span>td <\/span>= <\/span>TotalDays<\/span>();

<\/span>Plot<\/span>( <\/span>td<\/span>, <\/span>"TotalDays"<\/span>, <\/span>colorRed <\/span>); <\/code>

This function can be used to create some new interesting functions, for example the function that returns the value of array X calendar <\/strong> days back. I call it RefDays() because the syntax is almost the same as Ref() but it just uses calendar DAYS instead of bars.
Note that the function for simplicity handles only PAST references (‘days’ parameter is negative).<\/p>
SetBarsRequired<\/span>(<\/span>sbrAll<\/span>,<\/span>0<\/span>);

function <\/span>TotalDays<\/span>()
{
 <\/span>d <\/span>= <\/span>DaysSince1900<\/span>();

 return <\/span>d <\/span>- <\/span>d<\/span>[ <\/span>0 <\/span>];
}

function <\/span>RefDays<\/span>( Array, <\/span>Days <\/span>)
{
  <\/span>td <\/span>= <\/span>TotalDays<\/span>();

  <\/span>result <\/span>= <\/span>Null<\/span>;

  if( <\/span>Days <\/span>< <\/span>0 <\/span>)
  {
     for( <\/span>i <\/span>= <\/span>BarCount <\/span>-<\/span>1<\/span>; <\/span>i <\/span>>= -<\/span>Days<\/span>; <\/span>i <\/span>= <\/span>i <\/span>- <\/span>1 <\/span>)
     {
       for( <\/span>j <\/span>= <\/span>0<\/span>; <\/span>j <\/span>< <\/span>i<\/span>; <\/span>j<\/span>++ )
       {
          if( <\/span>td<\/span>[ <\/span>i <\/span>- <\/span>j <\/span>] <= <\/span>td<\/span>[ <\/span>i <\/span>] + <\/span>Days <\/span>)
          {
             <\/span>result<\/span>[ <\/span>i <\/span>] = Array[ <\/span>i <\/span>- <\/span>j <\/span>]; 
             <\/span>j <\/span>= <\/span>i<\/span>
          }
       }
     }
  }
  return <\/span>result<\/span>;
}

<\/span>Plot<\/span>( <\/span>C<\/span>, <\/span>"C"<\/span>, <\/span>colorRed <\/span>);
<\/span>Plot<\/span>( <\/span>Ref<\/span>( <\/span>C<\/span>, -<\/span>252 <\/span>), <\/span>"Close 252 bars back"<\/span>, <\/span>colorBlue <\/span>);
<\/span>Plot<\/span>( <\/span>RefDays<\/span>( <\/span>C<\/span>, -<\/span>365 <\/span>), <\/span>"Close 365 days back"<\/span>, <\/span>colorGreen <\/span>)<\/code>

As above example shows referencing 365 calendar days back is not very different from referencing 252 trading EOD bars back, so for most applications it seems not worth the effort of using exact calendar day differences.<\/p>

In the last example I want to show how to calculate number of calendar days in a range selected using begin\/end markers (double click to mark begin and double click again to mark end).<\/p>td <\/span>= <\/span>DaysSince1900<\/span>();
<\/span>Title <\/span>= <\/span>"Number of days in selected range = " <\/span>+ ( <\/span>EndValue<\/span>(<\/span>td<\/span>) - <\/span>BeginValue<\/span>(<\/span>td<\/span>) )<\/code>","protected":false},"excerpt":{"rendered":"

Someone asked me recently how to count calendar days (not bars) that passed since first available quote. AmiBroker has a built-in function ( since version 5.20 ) that calculates the number of days that passed since January 1st, 1900. This function can be used to easily implement number of days since first available quote Here […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[53],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/59"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=59"}],"version-history":[{"count":2,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":1526,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/59\/revisions\/1526"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}