[Blotter-commits] r942 - in pkg/FinancialInstrument: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Feb 23 17:22:09 CET 2012


Author: gsee
Date: 2012-02-23 17:22:09 +0100 (Thu, 23 Feb 2012)
New Revision: 942

Added:
   pkg/FinancialInstrument/R/expires.R
   pkg/FinancialInstrument/man/expires.Rd
   pkg/FinancialInstrument/man/expires.character.Rd
   pkg/FinancialInstrument/man/expires.instrument.Rd
Modified:
   pkg/FinancialInstrument/DESCRIPTION
   pkg/FinancialInstrument/NAMESPACE
Log:
 - add generic expires function with methods for instrument and character
   (method documentation is not quite perfect yet)


Modified: pkg/FinancialInstrument/DESCRIPTION
===================================================================
--- pkg/FinancialInstrument/DESCRIPTION	2012-02-23 16:06:27 UTC (rev 941)
+++ pkg/FinancialInstrument/DESCRIPTION	2012-02-23 16:22:09 UTC (rev 942)
@@ -11,7 +11,7 @@
     meta-data and relationships. Provides support for
     multi-asset class and multi-currency portfolios. Still
     in heavy development.
-Version: 0.11.4
+Version: 0.12.0
 URL: https://r-forge.r-project.org/projects/blotter/
 Date: $Date$
 Depends:
@@ -47,3 +47,4 @@
     'saveSymbols.R'
     'Tick2Sec.R'
     'all.equal.instrument.R'
+    'expires.R'

Modified: pkg/FinancialInstrument/NAMESPACE
===================================================================
--- pkg/FinancialInstrument/NAMESPACE	2012-02-23 16:06:27 UTC (rev 941)
+++ pkg/FinancialInstrument/NAMESPACE	2012-02-23 16:22:09 UTC (rev 942)
@@ -11,6 +11,7 @@
 export(C2M)
 export(currency)
 export(exchange_rate)
+export(expires)
 export(fn_SpreadBuilder)
 export(format_id)
 export(formatSpreadPrice)
@@ -115,6 +116,8 @@
 export(volep)
 importFrom(zoo,as.Date)
 S3method(all.equal,instrument)
+S3method(expires,character)
+S3method(expires,instrument)
 S3method(print,id.list)
 S3method(print,instrument)
 S3method(print,suffix.list)

Added: pkg/FinancialInstrument/R/expires.R
===================================================================
--- pkg/FinancialInstrument/R/expires.R	                        (rev 0)
+++ pkg/FinancialInstrument/R/expires.R	2012-02-23 16:22:09 UTC (rev 942)
@@ -0,0 +1,105 @@
+#' extract the correct expires value from an \code{instrument}
+#'
+#' Currently, there are methods for \code{instrument} 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}).
+#' @param x instrument or name of instrument
+#' @param ... not in use
+#' @return character string representation of an expiration date
+#' @author Garrett See
+#' @seealso \code{\link{expires.instrument}}, \code{\link{expires.character}}, 
+#'   \code{\link{getInstrument}}
+#' @examples
+#' \dontrun{
+#' instr <- instrument("FOO_U1", currency=currency("USD"), multiplier=1,
+#'                     expires=c("2001-09-01", "2011-09-01", "2021-09-01"), 
+#'                     assign_i=FALSE)
+#' #Last value of expires that's not after Sys.Date
+#' expires(instr) 
+#' # First value of expires that hasn't already passed.
+#' expires(instr, expired=FALSE)
+#' # last value that's not after 2011-01-01
+#' expires(instr, Date="2011-01-01") 
+#' # first value that's not before 2011-01-01
+#' expires(instr, Date="2011-01-01", expired=FALSE) 
+#'
+#' ## expires.character
+#' expires("FOO_U1") # warning that FOO_U1 is not defined
+#' instrument("FOO_U1", currency=currency("USD"), multiplier=1,
+#'            expires=c("2001-09-01", "2011-09-01", "2021-09-01"), 
+#'            assign_i=TRUE)
+#' expires("FOO_U1")
+#' }
+#' @export
+expires <- function(x, ...) {
+    UseMethod("expires")
+}
+
+
+#' instrument expires extraction method
+#' 
+#' Returns either the last expiration date before \code{Date}, or the 
+#' first expiration date after \code{Date} (if \code{expired==FALSE}).
+#' @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})
+#' @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 
+#'   \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.
+#' @method expires instrument
+#' @S3method expires instrument
+#' @author Garrett See
+#' @keywords internal
+expires.instrument <- function(x, Date, expired=TRUE, ...) {
+    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))
+        if (isTRUE(expired)) {
+            return(paste(last(dxp[dxp <= Date])))
+        } else return(paste(first(dxp[dxp >= Date])))
+    } else NextMethod("expires")
+}
+
+
+#' character expires extraction method
+#' 
+#' if no \code{instrument} can be found by the id of \code{x}, or if the 
+#' \code{instrument} does not have an \code{expires} attribute, an attempt
+#' will be made to infer the year and month of expiration using \code{parse_id}
+#' in which case the returned value will be a string of the format 
+#' \dQuote{YYYY-MM}.  Presently, \code{Date} and \code{expired} will be ignored 
+#' if \code{x} is not the name of an instrument
+#' @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}).  
+#' @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 
+#'   \code{FALSE} the first one after \code{Date} will be returned.
+#' @method expires character
+#' @S3method expires character
+#' @seealso \code{\link{expires.instrument}}
+#' @author Garrett See
+#' @keywords internal
+expires.character <- function(x, Date, expired=TRUE, ...) {
+    xi <- getInstrument(x, silent=TRUE)
+    if (is.instrument(xi)) {
+        expires.instrument(xi)
+    } else {
+        warning(paste(x, "is not defined. Inferring only Month and Year"))
+        pid <- parse_id(x)
+        mth <- grep(pid$month, month.abb, ignore.case=TRUE)
+        mth <- sprintf("%02d", mth, sep="-")
+        paste(pid$year, mth, sep="-")
+    }
+}

Added: pkg/FinancialInstrument/man/expires.Rd
===================================================================
--- pkg/FinancialInstrument/man/expires.Rd	                        (rev 0)
+++ pkg/FinancialInstrument/man/expires.Rd	2012-02-23 16:22:09 UTC (rev 942)
@@ -0,0 +1,54 @@
+\name{expires}
+\alias{expires}
+\title{extract the correct expires value from an \code{instrument}}
+\usage{
+  expires(x, ...)
+}
+\arguments{
+  \item{x}{instrument or name of instrument}
+
+  \item{...}{not in use}
+}
+\value{
+  character string representation of an expiration date
+}
+\description{
+  Currently, there are methods for \code{instrument} 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}).
+}
+\examples{
+\dontrun{
+instr <- instrument("FOO_U1", currency=currency("USD"), multiplier=1,
+                    expires=c("2001-09-01", "2011-09-01", "2021-09-01"),
+                    assign_i=FALSE)
+#Last value of expires that's not after Sys.Date
+expires(instr)
+# First value of expires that hasn't already passed.
+expires(instr, expired=FALSE)
+# last value that's not after 2011-01-01
+expires(instr, Date="2011-01-01")
+# first value that's not before 2011-01-01
+expires(instr, Date="2011-01-01", expired=FALSE)
+
+## expires.character
+expires("FOO_U1") # warning that FOO_U1 is not defined
+instrument("FOO_U1", currency=currency("USD"), multiplier=1,
+           expires=c("2001-09-01", "2011-09-01", "2021-09-01"),
+           assign_i=TRUE)
+expires("FOO_U1")
+}
+}
+\author{
+  Garrett See
+}
+\seealso{
+  \code{\link{expires.instrument}},
+  \code{\link{expires.character}},
+  \code{\link{getInstrument}}
+}
+

Added: pkg/FinancialInstrument/man/expires.character.Rd
===================================================================
--- pkg/FinancialInstrument/man/expires.character.Rd	                        (rev 0)
+++ pkg/FinancialInstrument/man/expires.character.Rd	2012-02-23 16:22:09 UTC (rev 942)
@@ -0,0 +1,39 @@
+\name{expires.character}
+\alias{expires.character}
+\title{character expires extraction method}
+\usage{
+  \method{expires}{character} (x, Date, expired = TRUE,
+    ...)
+}
+\arguments{
+  \item{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}).}
+
+  \item{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
+  \code{FALSE} the first one after \code{Date} will be
+  returned.}
+}
+\description{
+  if no \code{instrument} can be found by the id of
+  \code{x}, or if the \code{instrument} does not have an
+  \code{expires} attribute, an attempt will be made to
+  infer the year and month of expiration using
+  \code{parse_id} in which case the returned value will be
+  a string of the format \dQuote{YYYY-MM}.  Presently,
+  \code{Date} and \code{expired} will be ignored if
+  \code{x} is not the name of an instrument
+}
+\author{
+  Garrett See
+}
+\seealso{
+  \code{\link{expires.instrument}}
+}
+\keyword{internal}
+

Added: pkg/FinancialInstrument/man/expires.instrument.Rd
===================================================================
--- pkg/FinancialInstrument/man/expires.instrument.Rd	                        (rev 0)
+++ pkg/FinancialInstrument/man/expires.instrument.Rd	2012-02-23 16:22:09 UTC (rev 942)
@@ -0,0 +1,32 @@
+\name{expires.instrument}
+\alias{expires.instrument}
+\title{instrument expires extraction method}
+\usage{
+  \method{expires}{instrument} (x, Date, expired = TRUE,
+    ...)
+}
+\arguments{
+  \item{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})}
+
+  \item{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
+  \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.}
+}
+\description{
+  Returns either the last expiration date before
+  \code{Date}, or the first expiration date after
+  \code{Date} (if \code{expired==FALSE}).
+}
+\author{
+  Garrett See
+}
+\keyword{internal}
+



More information about the Blotter-commits mailing list