1
2# This R package is free software; you can redistribute it and/or
3# modify it under the terms of the GNU Library General Public
4# License as published by the Free Software Foundation; either
5# version 2 of the License, or (at your option) any later version.
6#
7# This R package is distributed in the hope that it will be useful,
8# but WITHOUT ANY WARRANTY; without even the implied warranty of
9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10# GNU Library General Public License for more details.
11#
12# You should have received a copy of the GNU Library General
13# Public License along with this R package; if not, write to the
14# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15# MA  02111-1307  USA
16
17
18################################################################################
19# FUNCTION:                 DESCRIPTION:
20#  holidayNYSE               Returns holidays for full-day NYSE calendar
21################################################################################
22
23# ---------------------------------------------------------------------------- #
24# Roxygen Tags
25#' @export
26# ---------------------------------------------------------------------------- #
27holidayNYSE <-
28    function(year = getRmetricsOptions("currentYear"))
29{
30    # A function implemented by Diethelm Wuertz
31    # improved speed and handling of time zone by Yohan Chalabi
32
33    # Description:
34    #   Returns 'timeDate' object for full-day NYSE holidays
35
36    # Arguments:
37    #   year - an integer variable or vector for the year(s)
38    #       ISO-8601 formatted as "CCYY" where easter or
39    #       easter related feasts should be computed.
40
41    # Value:
42    #   Returns the holiday calendar for the NYSE formatted as
43    #   'timeDate' object.
44
45    # Details:
46    #   The "New York Stock Exchange" calendar starts from year 1885.
47    #   The rules are listed at the web site http://www.nyse.com.
48
49    # Example:
50    #   > holiday.NYSE(2004)
51    #   [1] "America/New_York"
52    #   [1] [2004-01-01] [2004-01-19] [2004-02-16] [2004-04-09]
53    #   [5] [2004-05-31] [2004-07-05] [2004-09-06] [2004-11-25]
54
55    # FUNCTION:
56
57    #  Settings:
58    holidays <- NULL
59
60    # Iterate years:
61    for (y in year ) {
62        if (y >= 1885)
63            holidays <- c(holidays, as.character(USNewYearsDay(y)))
64        if (y >= 1885)
65            holidays <- c(holidays, as.character(USIndependenceDay(y)))
66        if (y >= 1885)
67            holidays <- c(holidays, as.character(USThanksgivingDay(y)))
68        if (y >= 1885)
69            holidays <- c(holidays, as.character(USChristmasDay(y)))
70        if (y >= 1887)
71            holidays <- c(holidays, as.character(USLaborDay(y)))
72        if (y != 1898 & y != 1906 & y != 1907)
73            holidays <- c(holidays, as.character(USGoodFriday(y)))
74        if (y >= 1909 & y <= 1953)
75            holidays <- c(holidays, as.character(USColumbusDay(y)))
76        if (y >= 1998)
77            holidays <- c(holidays, as.character(USMLKingsBirthday(y)))
78        if (y >= 1896 & y <= 1953)
79            holidays <- c(holidays, as.character(USLincolnsBirthday(y)))
80        if (y <= 1970)
81            holidays <- c(holidays, as.character(USWashingtonsBirthday(y)))
82        if (y > 1970)
83            holidays <- c(holidays, as.character(USPresidentsDay(y)))
84        if (y == 1918 | y == 1921 | (y >= 1934 & y <= 1953))
85            holidays <- c(holidays, as.character(USVeteransDay(y)))
86        if (y <= 1968 | y == 1972 | y == 1976 | y == 1980)
87            holidays <- c(holidays, as.character(USElectionDay(y)))
88        if (y <= 1970)
89            holidays <- c(holidays, as.character(USDecorationMemorialDay(y)))
90        if (y >= 1971)
91            holidays <- c(holidays, as.character(USMemorialDay(y)))
92    }
93
94    # Sort and Convert to 'timeDate':
95    holidays <- sort(holidays)
96    ans <- timeDate(format(holidays), zone = "NewYork", FinCenter = "NewYork")
97
98    # Move Sunday Holidays to Monday:
99    posix1 <- as.POSIXlt(ans, tz = "GMT")
100    ans <- ans + as.integer(posix1$wday==0) * 24 * 3600
101
102    # After July 3, 1959, move Saturday holidays to Friday
103    # ... except if at the end of monthly/yearly accounting period
104    # this is the last business day of a month.
105    posix2 <- as.POSIXlt(as.POSIXct(ans, tz = "GMT") - 24 * 3600)
106    y <- posix2$year + 1900
107    m <- posix2$mon + 1
108    calendar <- timeCalendar(y = y+(m+1)%/%13,
109                             m = m+1-(m+1)%/%13*12, d = 1,
110                             zone = "GMT", FinCenter = "GMT")
111    lastday <- as.POSIXlt(calendar - 24*3600, tz = "GMT")$mday
112    lon <- .last.of.nday(year = y, month = m, lastday = lastday, nday = 5)
113    ExceptOnLastFriday <- timeDate(format(lon), zone = "NewYork",
114                                   FinCenter = "NewYork")
115    ans <- ans - as.integer(ans >= timeDate("1959-07-03",
116                            zone ="GMT", FinCenter = "GMT") &
117                            as.POSIXlt(ans, tz = "GMT")$wday == 6  &
118                            (ans - 24*3600) != ExceptOnLastFriday ) * 24 * 3600
119
120    # Remove Remaining Weekend Dates:
121    posix3 <- as.POSIXlt(ans, tz = "GMT")
122    ans <- ans[ !(posix3$wday == 0 | posix3$wday == 6)]
123
124    # Return Value:
125    ans
126}
127
128
129################################################################################
130
131