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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Dec 11 20:27:32 CET 2011


Author: gsee
Date: 2011-12-11 20:27:32 +0100 (Sun, 11 Dec 2011)
New Revision: 873

Modified:
   pkg/FinancialInstrument/R/format_id.R
   pkg/FinancialInstrument/man/sort_ids.Rd
Log:
sort_ids will check instrument envir to see if there is a more accurate expires than parse_id can give

Modified: pkg/FinancialInstrument/R/format_id.R
===================================================================
--- pkg/FinancialInstrument/R/format_id.R	2011-12-11 17:29:00 UTC (rev 872)
+++ pkg/FinancialInstrument/R/format_id.R	2011-12-11 19:27:32 UTC (rev 873)
@@ -215,20 +215,36 @@
 #' Primarily intended for use on the primary_ids of \code{\link{future_series}} instruments.
 #' This will sort ids by expiration.  All ids that do not contain month and year information 
 #' will be sorted alphabetically (separately) and appended to the end of the other sorted ids.
+#'
+#' If an instrument is defined, and has a date in its \sQuote{expires} field, that date will be
+#' used as the expiration date.  Otherwise, it is assumed that the contract expires
+#' on the first day of its expiration month.  This means that if some products are defined
+#' and other products that expire in the same month are not defined, the ones that are
+#' not defined will come first in the vector of sorted ids.
 #' @param ids character vector of ids
 #' @param ... arguments to pass through to \code{\link{parse_id}}
 #' @return sorted character vector of the same length as \code{ids}
 #' @author Garrett See
 #' @seealso \code{\link{parse_id}}
 #' @examples
+#' \dontrun{
 #' ids <- c("ES_U11",'GLD','SPY',"YM_Jun11",'DIA','VX_V10')
 #' sort_ids(ids)
+#' }
 #' @export
 sort_ids <- function(ids, ...) {
     if (is.null(ids)) return(NULL)
     f <- function(x, ...) {
+        tmpi <- getInstrument(x, silent=TRUE)
+        if (is.instrument(tmpi)) {
+            if (is.timeBased(suppressWarnings(try(as.Date(tmpi$expires), silent=TRUE)))) {
+                return(as.Date(tmpi$expires))
+            } else if (is.timeBased(suppressWarnings(try(as.Date(tmpi$expires, format='%Y%m%d'), silent=TRUE)))) {
+                return(as.Date(tmpi$expires, format='%Y%m%d'))
+            }
+        }
         pid <- parse_id(x, ...)
-        as.Date(paste(pid$year,pid$month,15,sep=''),format="%Y%b%d")
+        as.Date(paste(pid$year, pid$month, 1, sep=''), format="%Y%b%d")
     }
     out1 <- names(sort(sapply(ids,f, ...)))
     out2 <- sort(ids[!(ids %in% out1)])

Modified: pkg/FinancialInstrument/man/sort_ids.Rd
===================================================================
--- pkg/FinancialInstrument/man/sort_ids.Rd	2011-12-11 17:29:00 UTC (rev 872)
+++ pkg/FinancialInstrument/man/sort_ids.Rd	2011-12-11 19:27:32 UTC (rev 873)
@@ -21,10 +21,22 @@
   (separately) and appended to the end of the other sorted
   ids.
 }
+\details{
+  If an instrument is defined, and has a date in its
+  \sQuote{expires} field, that date will be used as the
+  expiration date.  Otherwise, it is assumed that the
+  contract expires on the first day of its expiration
+  month.  This means that if some products are defined and
+  other products that expire in the same month are not
+  defined, the ones that are not defined will come first in
+  the vector of sorted ids.
+}
 \examples{
+\dontrun{
 ids <- c("ES_U11",'GLD','SPY',"YM_Jun11",'DIA','VX_V10')
 sort_ids(ids)
 }
+}
 \author{
   Garrett See
 }



More information about the Blotter-commits mailing list