{"id":111,"date":"2007-05-07T09:04:54","date_gmt":"2007-05-07T09:04:54","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/05\/07\/date-arithmetic\/"},"modified":"2008-01-05T10:16:15","modified_gmt":"2008-01-05T10:16:15","slug":"date-arithmetic","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/05\/07\/date-arithmetic\/","title":{"rendered":"Date Calculations (v2)"},"content":{"rendered":"

In Real-Time trading, you may need to perform date calculations that are referenced to your computer date instead of to the DateNumber of your data. Instead of struggling with years, dates, and months, it is much easier to use a linear data system like Rata Die<\/a>, which simply counts the number of days since December 31 of the year zero. To use the Rata Die method you only need two conversion functions to convert between the Rata Die to DateNumbers and vise versa. In this post the Rata Die system will be used to calculate NASDAQ non-trading days and to calculate the date of the previous trading day.<\/p>\n

The conversion functions listed below were posted on the AmiBroker User list<\/a> by Paul Ho (thanks Paul!) and will be used the perform the needed calculations in this post.<\/p>\n

NASDAQ non-trading days are copied from the NASDAQ<\/a> website, converted to DateNumbers, and entered into the code using a ParamStr() function so that they can be changed annually without digging in the code. Here is typical listing for 2007:<\/p>\n

NASDAQ Holiday Trading Schedule
\n2007 Dates – Unless noted, the following dates are holidays on which The NASDAQ Stock Market is closed:
\nJanuary 1 – New Year’s Day
\nJanuary 15 – Martin Luther King Jr.’s Birthday
\nFebruary 19 – Presidents’ Day
\nApril 6 – Good Friday
\nMay 28 – Memorial Day
\nJuly 4 – Independence Day
\nSeptember 3 – Labor Day
\nNovember 22 – Thanksgiving Day
\nDecember 25 – Christmas Day<\/p><\/blockquote>\n

To find the previous trading date, the code starts by using the previous Rata Die date, and if that isn’t a trading date, it decrements the Rata Die number until a trading day is found. Selected variables are output to the Chart Title so that you can vary the date using the ParamDate() and see haw the conversions work..<\/p>\n

function DateNumberToRataDie( DateNumber )
\n{
\nnum = DateNumber\/10000;
\nyyyy = int(num) + 1900;
\nnum = frac(num) * 100;
\nmm = int(num);
\ndd = frac(num)*100;
\nyyyy = yyyy + int((mm-14)\/12);
\nmm = IIf(mm < 3, mm+12, mm);
\nRataDieNum = round(dd + int((153*mm-457)\/5) + 365*yyyy + int(yyyy\/4) - int(yyyy\/100) + int(yyyy\/400) - 306);
\nreturn (RataDieNum);
\n}<\/code><\/p>\n

function RataDieToDateNumber(RataDieNum)
\n{
\nz = RataDieNum + 306;
\ng = z - 0.25;
\na = int(G\/36524.25);
\nb = a - int(A\/4);
\nyr = int((b + g)\/365.25);
\nCc = b + z - int(365.25 * yr);
\nmm = int((5 * Cc + 456)\/153);
\ndd = Cc - int((153*mm-457)\/5);
\nyr = IIf(mm > 12, yr + 1, yr);
\nmm = IIf(mm > 12, mm - 12, mm);
\ndn = (yr-1900)*10000+mm*100+dd;
\nreturn dn;
\n}<\/code><\/p>\n

function NoTradingDay( RataDieNum )
\n{
\nglobal DN, NasdaqNTDN;
\nDN = RataDieToDateNumber(RataDieNum );
\nDnStr = NumToStr(DN,1.0,False);
\nDW = RataDieNum%7;
\nreturn DW == 0 OR DW == 6 OR StrFind( NasdaqNTDN, DNStr);
\n}<\/code><\/p>\n

NasdaqNTDN = \"1070101,1070115,1070219,1070406,1070528,1070704,1070903,1071122,1071225\";
\nweekdays = \"Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday\";
\nDN1 = ParamDate(\"Date\",\"05\/07\/2007\");
\nRD1 = DateNumberToRataDie(DN1);
\nDW1 = RD1%7;
\nRD2 = RD1-1;
\nDN2 = RataDieToDateNumber(RD2);
\nDW2 = RD2%7;
\nwhile( NoTradingDay( RD2 ) )
\n{
\nRD2--;
\nDN2 = RataDieToDateNumber(RD2);
\nDW2 = RD2%7;
\n}
\nTitle = \"\\n\"+
\n\"CURRENT TRADING DAY:\\n\\n\"+
\n\" Date: \"+NumToStr(DateTimeConvert( 2, DN1),formatDateTime)+\" \"+StrExtract(Weekdays,DW1)+\"\\n\"+
\n\" Day Number: \"+NumToStr(DW1,1.0)+\"\\n\"+
\n\" Rata Die: \"+NumToStr(RD1,1.0,False)+\"\\n\"+
\nWriteIf(NoTradingDay( RD1 ),\"This is Not a Trading Day\",\"This is a Trading Day\")+\"\\n\\n\\n\"+
\n\"PREVIOUS TRADING DAY:\\n\\n\"+
\n\" Prev TDay: \"+NumToStr(DateTimeConvert( 2, DN2),formatDateTime)+\" \"+StrExtract(Weekdays,DW2)+\"\\n\"+
\n\" Day Number: \"+NumToStr(RD2%7,1.0)+\"\\n\"+
\n\" Rata Die: \"+NumToStr(RD2,1.0,False)+\"\\n\"+
\nWriteIf(NoTradingDay( RD2 ),\"This is Not a Trading Day\", \"This is a Trading Day\");<\/code><\/p>\n

Edited by Al Venosa<\/p>\n","protected":false},"excerpt":{"rendered":"

In Real-Time trading, you may need to perform date calculations that are referenced to your computer date instead of to the DateNumber of your data. Instead of struggling with years, dates, and months, it is much easier to use a linear data system like Rata Die, which simply counts the number of days since December […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[64],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/111"}],"collection":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/comments?post=111"}],"version-history":[{"count":0,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/111\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}