[Blotter-commits] r952 - in pkg/FinancialInstrument: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Mar 2 01:14:51 CET 2012
Author: gsee
Date: 2012-03-02 01:14:51 +0100 (Fri, 02 Mar 2012)
New Revision: 952
Modified:
pkg/FinancialInstrument/DESCRIPTION
pkg/FinancialInstrument/R/expires.R
pkg/FinancialInstrument/R/update_instruments.yahoo.R
pkg/FinancialInstrument/man/expires.Rd
pkg/FinancialInstrument/man/expires.character.Rd
pkg/FinancialInstrument/man/expires.instrument.Rd
pkg/FinancialInstrument/man/expires.spread.Rd
Log:
- expires functions return Date instead of chr
- update_instruments.instrument updates "defined.by" and "updated"
Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION 2012-02-29 16:56:34 UTC (rev 951)
+++ pkg/FinancialInstrument/DESCRIPTION 2012-03-02 00:14:51 UTC (rev 952)
@@ -11,7 +11,7 @@
meta-data and relationships. Provides support for
multi-asset class and multi-currency portfolios. Still
in heavy development.
-Version: 0.12.1
+Version: 0.12.2
URL: https://r-forge.r-project.org/projects/blotter/
Date: $Date$
Depends:
Modified: pkg/FinancialInstrument/R/expires.R
===================================================================
--- pkg/FinancialInstrument/R/expires.R 2012-02-29 16:56:34 UTC (rev 951)
+++ pkg/FinancialInstrument/R/expires.R 2012-03-02 00:14:51 UTC (rev 952)
@@ -1,15 +1,31 @@
#' extract the correct expires value from an \code{instrument}
#'
-#' Currently, there are methods for \code{instrument} and \code{character}
+#' Currently, there are methods for \code{instrument}, \code{spread}, and
+#' \code{character}
#'
-#' Will return either the last expiration date before a given Date, or the
-#' first expiration date after a given Date (if \code{expired==FALSE}).
+#' Will return either the last expiration date before a given \code{Date}, or
+#' the first expiration date after a given \code{Date}
+#' (if \code{expired==FALSE}).
+#'
+#' If an \code{\link{instrument}} contains a value for expires that does not
+#' include a day (e.g. "2012-03"), or if the expires value is estimated from
+#' a \code{future_series} primary_id, it will be assumed that the
+#' \code{instrument} expires on the first of the month (i.e. if the expires
+#' value of an instrument were "2012-03", or if there were no expires value
+#' but the suffix_id were "H12", the value returned would be "2012-03-01").
+#' Note that most non-energy future_series expire after the first of the month
+#' indicated by their suffix_id and most energy products expire in the month
+#' prior to their suffix_id month.
+#'
#' @param x instrument or name of instrument
-#' @param ... not in use
-#' @return character string representation of an expiration date
+#' @param ... arguments to be passed to methods
+#' @return an expiration \code{Date}
#' @author Garrett See
#' @seealso \code{\link{expires.instrument}}, \code{\link{expires.character}},
-#' \code{\link{getInstrument}}
+#' \code{\link{sort_ids}}
+#'
+#' \code{\link{getInstrument}} and \code{\link{buildHierarchy}} to see actual
+#' values stored in \code{instrument}
#' @examples
#' \dontrun{
#' instr <- instrument("FOO_U1", currency=currency("USD"), multiplier=1,
@@ -50,22 +66,46 @@
#' returned will be the last one before \code{Date}. If \code{expired} is
#' \code{FALSE} the first one after \code{Date} will be returned. Note that
#' if \code{expires} is a single value, \code{expired} will be ignored.
+#' @param silent silence warnings?
#' @method expires instrument
#' @S3method expires instrument
#' @author Garrett See
#' @keywords internal
-expires.instrument <- function(x, Date, expired=TRUE, ...) {
+expires.instrument <- function(x, Date, expired=TRUE, silent=FALSE, ...) {
if (is.instrument(x)) {
if (missing(Date)) Date <- Sys.Date()
if (!inherits(Date, "Date")) Date <- as.Date(Date)
xp <- x[["expires"]]
if (length(xp) == 0) return(NULL)
- dxp <- try(as.Date(xp), silent=TRUE) #Date(s) of expiration
- if (inherits(dxp, 'try-error')) return(paste(xp))
- if (length(dxp) == 1) return(paste(xp))
+ chars <- nchar(as.character(xp))
+ if (any(chars %in% c(0:5, 9)) || any(chars > 10)) {
+ warning(paste("The following values of 'expires' in instrument",
+ x$primary_id,
+ "could not be converted to Date and will be ignored:",
+ xp[chars %in% c(0:5, 9) | chars > 10]))
+ xp <- xp[chars %in% c(6:8, 10)]
+ }
+ if (any(chars < 8) && !isTRUE(silent)) {
+ warning(paste("only Year and Month found", "...",
+ "assuming expiration occurs on the 1st of the month"))
+ }
+ dxp <- do.call(c, lapply(xp, function(xx) {
+ if (inherits(xx, "Date")) {
+ xx
+ } else if (nchar(xx) == 10) {
+ as.Date(xx)
+ } else if (nchar(xx) == 8) {
+ as.Date(xx, format="%Y%m%d")
+ } else if (nchar(xx) == 7) {
+ as.Date(paste(xx, "01", sep="-"))
+ } else if (nchar(xx) == 6) {
+ as.Date(paste(xx, "01", sep=""), format="%Y%m%d")
+ }
+ }))
+ if (length(dxp) == 1) return(dxp)
if (isTRUE(expired)) {
- return(paste(last(dxp[dxp <= Date])))
- } else return(paste(first(dxp[dxp >= Date])))
+ return(last(dxp[dxp <= Date]))
+ } else return(first(dxp[dxp >= Date]))
} else NextMethod("expires")
}
@@ -86,21 +126,26 @@
#' \code{expires} is a vector. If \code{expired} is \code{TRUE} the date
#' returned will be the last one before \code{Date}. If \code{expired} is
#' \code{FALSE} the first one after \code{Date} will be returned.
+#' @param silent silence warnings?
#' @method expires character
#' @S3method expires character
#' @seealso \code{\link{expires.instrument}}
#' @author Garrett See
#' @keywords internal
-expires.character <- function(x, Date, expired=TRUE, ...) {
+expires.character <- function(x, Date, expired=TRUE, silent=FALSE, ...) {
xi <- getInstrument(x, silent=TRUE)
if (is.instrument(xi)) {
- expires.instrument(xi)
+ expires.instrument(xi, Date=Date, expired=expired, silent=silent,
+ ...=...)
} else {
- warning(paste(x, "is not defined. Inferring only Month and Year"))
+ if (!isTRUE(silent)) {
+ warning(paste(x, "is not defined ... assuming expiration occurs on",
+ "the 1st of the month implied by suffix_id"))
+ }
pid <- parse_id(x)
mth <- grep(pid$month, month.abb, ignore.case=TRUE)
mth <- sprintf("%02d", mth, sep="-")
- paste(pid$year, mth, sep="-")
+ as.Date(paste(pid$year, mth, "01", sep="-"))
}
}
@@ -114,7 +159,7 @@
#' @param Date Can be a Date or character string. When \code{expires} is a
#' vector, the retuned value will be one of the two values of \code{expires}
#' that are closest to \code{Date}. (which one will be determined by the value
- #' of \code{expired}).
+#' of \code{expired}).
#' @param expired TRUE/FALSE. This determines which date will be used when
#' \code{expires} is a vector. If \code{expired} is \code{TRUE} the date
#' returned will be the last one before \code{Date}. If \code{expired} is
@@ -124,18 +169,24 @@
#' @seealso \code{\link{expires.instrument}}
#' @author Garrett See
#' @keywords internal
-expires.spread <- function(x, Date, expired=TRUE, ...) {
+expires.spread <- function(x, Date, expired=TRUE, silent=FALSE, ...) {
if (inherits(x, "spread")) {
- if (!is.null(x$expires)) return(x$expires)
+ if (!is.null(x$expires)) {
+ return(expires.instrument(x, Date=Date, expired=expired,
+ silent=silent, ...=...))
+ }
members <- if (!is.null(x$memberlist$members)) {
x$memberlist$members
} else if (!is.null(x$members)) {
x$members
} else {
- warning(paste("Cannot determine members of x$primary_id"))
+ if (!isTRUE(silent)) {
+ warning(paste("Cannot determine members of x$primary_id"))
+ }
return(NextMethod("expires"))
}
- return(expires(sort_ids(members)[1]))
+ return(expires.character(sort_ids(members)[1]), Date=Date,
+ expired=expired, silent=silent, ...=...)
} else NextMethod("expires")
}
Modified: pkg/FinancialInstrument/R/update_instruments.yahoo.R
===================================================================
--- pkg/FinancialInstrument/R/update_instruments.yahoo.R 2012-02-29 16:56:34 UTC (rev 951)
+++ pkg/FinancialInstrument/R/update_instruments.yahoo.R 2012-03-02 00:14:51 UTC (rev 952)
@@ -265,6 +265,14 @@
si[[n]] <- r[[n]]
}
}
+ db <- si$defined.by
+ if (!is.null(db)) {
+ db <- unlist(strsplit(db,";"))
+ db <- rev(unique(c(r$primary_id, rev(db))))
+ db <- paste(db,collapse=";")
+ } else db <- r$primary_id
+ si$defined.by <- db
+ si$updated <- Sys.time()
si
})
if (isTRUE(assign_i)) {
Modified: pkg/FinancialInstrument/man/expires.Rd
===================================================================
--- pkg/FinancialInstrument/man/expires.Rd 2012-02-29 16:56:34 UTC (rev 951)
+++ pkg/FinancialInstrument/man/expires.Rd 2012-03-02 00:14:51 UTC (rev 952)
@@ -7,19 +7,32 @@
\arguments{
\item{x}{instrument or name of instrument}
- \item{...}{not in use}
+ \item{...}{arguments to be passed to methods}
}
\value{
- character string representation of an expiration date
+ an expiration \code{Date}
}
\description{
- Currently, there are methods for \code{instrument} and
- \code{character}
+ Currently, there are methods for \code{instrument},
+ \code{spread}, and \code{character}
}
\details{
Will return either the last expiration date before a
- given Date, or the first expiration date after a given
- Date (if \code{expired==FALSE}).
+ given \code{Date}, or the first expiration date after a
+ given \code{Date} (if \code{expired==FALSE}).
+
+ If an \code{\link{instrument}} contains a value for
+ expires that does not include a day (e.g. "2012-03"), or
+ if the expires value is estimated from a
+ \code{future_series} primary_id, it will be assumed that
+ the \code{instrument} expires on the first of the month
+ (i.e. if the expires value of an instrument were
+ "2012-03", or if there were no expires value but the
+ suffix_id were "H12", the value returned would be
+ "2012-03-01"). Note that most non-energy future_series
+ expire after the first of the month indicated by their
+ suffix_id and most energy products expire in the month
+ prior to their suffix_id month.
}
\examples{
\dontrun{
@@ -48,7 +61,10 @@
}
\seealso{
\code{\link{expires.instrument}},
- \code{\link{expires.character}},
- \code{\link{getInstrument}}
+ \code{\link{expires.character}}, \code{\link{sort_ids}}
+
+ \code{\link{getInstrument}} and
+ \code{\link{buildHierarchy}} to see actual values stored
+ in \code{instrument}
}
Modified: pkg/FinancialInstrument/man/expires.character.Rd
===================================================================
--- pkg/FinancialInstrument/man/expires.character.Rd 2012-02-29 16:56:34 UTC (rev 951)
+++ pkg/FinancialInstrument/man/expires.character.Rd 2012-03-02 00:14:51 UTC (rev 952)
@@ -3,7 +3,7 @@
\title{character expires extraction method}
\usage{
\method{expires}{character} (x, Date, expired = TRUE,
- ...)
+ silent = FALSE, ...)
}
\arguments{
\item{Date}{Can be a Date or character string. When
@@ -18,6 +18,8 @@
the last one before \code{Date}. If \code{expired} is
\code{FALSE} the first one after \code{Date} will be
returned.}
+
+ \item{silent}{silence warnings?}
}
\description{
if no \code{instrument} can be found by the id of
Modified: pkg/FinancialInstrument/man/expires.instrument.Rd
===================================================================
--- pkg/FinancialInstrument/man/expires.instrument.Rd 2012-02-29 16:56:34 UTC (rev 951)
+++ pkg/FinancialInstrument/man/expires.instrument.Rd 2012-03-02 00:14:51 UTC (rev 952)
@@ -3,7 +3,7 @@
\title{instrument expires extraction method}
\usage{
\method{expires}{instrument} (x, Date, expired = TRUE,
- ...)
+ silent = FALSE, ...)
}
\arguments{
\item{Date}{Can be a Date or character string. When
@@ -19,6 +19,8 @@
\code{FALSE} the first one after \code{Date} will be
returned. Note that if \code{expires} is a single value,
\code{expired} will be ignored.}
+
+ \item{silent}{silence warnings?}
}
\description{
Returns either the last expiration date before
Modified: pkg/FinancialInstrument/man/expires.spread.Rd
===================================================================
--- pkg/FinancialInstrument/man/expires.spread.Rd 2012-02-29 16:56:34 UTC (rev 951)
+++ pkg/FinancialInstrument/man/expires.spread.Rd 2012-03-02 00:14:51 UTC (rev 952)
@@ -3,7 +3,7 @@
\title{spread expires extraction method}
\usage{
\method{expires}{character} (x, Date, expired = TRUE,
- ...)
+ silent = FALSE, ...)
}
\arguments{
\item{Date}{Can be a Date or character string. When
More information about the Blotter-commits
mailing list