Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed bug in bizdays() #124 #127

Merged
merged 1 commit into from
Jul 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Version 6.2
* Fixed bug in accuracy() when non-integer seasonality used.
* Made auto.arima() robust to non-integer seasonality.
* Fixed bug in auto.arima where allowmean was ignored when stepwise=FALSE.
* Fixed bug in bizdays().

Version 6.1 (11 May 2015)
* Made auto.arima more robust
Expand Down
35 changes: 23 additions & 12 deletions R/calendar.R
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
bizdays <- function(x, FinCenter) {
bizdays <- function(x, FinCenter = c("New York", "London", "NERC", "Tokyo",
"Zurich")) {
# Return the number of trading days corresponding to the input ts
#
# Args:
# x: a ts object
# FinCenter: inherits "FinCenter" from "timeDate" package
# FinCenter: inherits holiday calendar from "timeDate" package
#
# Returns:
# A matrix contains the number of trading days
if (is.null(tsp(x))) {
stop("We cannot handle a time series without time attributes.")
}
if (missing(FinCenter)) {
FinCenter <- "GMT"
}
# Convert tsp to date
freq <- frequency(x)
years <- start(x)[1L]:end(x)[1L]
# Grab the holidays from years and financial center
FinCenter <- match.arg(FinCenter)
if (FinCenter == "New York") {
holidays <- timeDate::holidayNYSE(years)
} else if (FinCenter == "London") {
holidays <- timeDate::holidayLONDON(years)
} else if (FinCenter == "NERC") {
holidays <- timeDate::holidayNERC(years)
} else if (FinCenter == "Tokyo") {
holidays <- timeDate::holidayTSX(years)
} else if (FinCenter == "Zurich") {
holidays <- timeDate::holidayZURICH(years)
}
if (freq == 12L) { # monthly data
date <- zoo::as.Date(time(x))
start <- date[1L]
end <- seq(date[length(date)], length = 2L, by = "month")[2L] - 1L
days.len <- as.timeDate(seq(start, end, by = "days"), FinCenter = FinCenter)
days.len <- timeDate::timeSequence(from = start, to = end)
# Grab business days
biz <- days.len[isBizday(days.len,
holidays = unique(format(days.len, "%Y")))]
bizdays <- format(biz, format = "%Y %b")
biz <- days.len[timeDate::isBizday(days.len, holidays = holidays)]
bizdays <- format(biz, format = "%Y-%m")
} else if (freq == 4L) { # Quarterly data
date <- zoo::as.Date(time(x))
start <- date[1L]
end <- seq(date[length(date)], length = 2L, by = "3 month")[2L] - 1L
days.len <- as.timeDate(seq(start, end, by = "days"), FinCenter = FinCenter)
biz <- days.len[isBizday(days.len,
holidays = unique(format(days.len, "%Y")))]
days.len <- timeDate::timeSequence(from = start, to = end)
# Grab business days
biz <- days.len[timeDate::isBizday(days.len, holidays = holidays)]
bizdays <- format(as.yearqtr(biz), format = "%Y Qtr%q")

} # else if (freq == 52L) { # Weekly data
Expand Down
12 changes: 5 additions & 7 deletions man/bizdays.Rd
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
\name{bizdays}
\alias{bizdays}
\title{Number of trading days in each season}
\usage{bizdays(x, FinCenter)}
\usage{bizdays(x, FinCenter = c("New York", "London", "NERC", "Tokyo", "Zurich"))}

\arguments{
\item{x}{Monthly or quarterly time series}
\item{FinCenter}{A character with the the location of the financial center named as "continent/city".
This concept allows to handle data records collected in different time zones and mix them up to have
always the proper time stamps with respect to your personal financial center, or alternatively to the
GMT reference time. More details on \code{\link[timeDate]{finCenter}}.}
\item{FinCenter}{Major financial center.}
}
\description{Returns number of trading days in each month or quarter of the observed time period.}
\description{Returns number of trading days in each month or quarter of the observed time period in a major financial center.}

\details{Useful for trading days length adjustments. More on how to define "business days",
please refer to \code{\link[timeDate]{isBizday}}.}
Expand All @@ -19,6 +16,7 @@ please refer to \code{\link[timeDate]{isBizday}}.}

\author{Earo Wang}
\examples{
bizdays(wineind, FinCenter = "Sydney")
x <- ts(rnorm(30), start = c(2013, 2), frequency = 12)
bizdays(x, FinCenter = "New York")
}
\keyword{ts}