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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Mar 4 22:01:36 CET 2012


Author: gsee
Date: 2012-03-04 22:01:36 +0100 (Sun, 04 Mar 2012)
New Revision: 963

Modified:
   pkg/FinancialInstrument/R/buildHierarchy.R
   pkg/FinancialInstrument/R/expires.R
   pkg/FinancialInstrument/R/update_instruments.yahoo.R
   pkg/FinancialInstrument/man/buildHierarchy.Rd
   pkg/FinancialInstrument/man/expires.Rd
Log:
 - buildHierarchy improvements:  removed "levels" arg (but still back-compatible);
                                                 can accept single vector or multiple arguments.
 - better matching syntax in update_instruments.TTR
 - minor update to expires docs

Modified: pkg/FinancialInstrument/R/buildHierarchy.R
===================================================================
--- pkg/FinancialInstrument/R/buildHierarchy.R	2012-03-04 20:09:15 UTC (rev 962)
+++ pkg/FinancialInstrument/R/buildHierarchy.R	2012-03-04 21:01:36 UTC (rev 963)
@@ -15,16 +15,11 @@
 
 #' Construct a hierarchy of instruments useful for aggregation
 #'
-#' All 'currency' instruments must be defined before instruments of other types 
-#' may be defined.
-#'
-#' In \dots you may pass any other arbitrary instrument fields that will be used 
-#' to create 'custom' fields. (This is not yet implemented)
-#'
+#' Construct a hierarchy of instruments useful for aggregation
+#' 
 #' @param primary_ids A character vector of \code{instrument} primary_ids to be 
 #' included in the hierarchy list
-#' @param levels A character vector of instrument attributes in top-down order
-#' @param ... not in use
+#' @param ... character names of instrument attributes in top-down order. 
 #' @author Peter Carl, Alexis Petit, Garrett See
 #' @return Constructs a data.frame that contains the list of assets in the first 
 #' column and the category or factor for grouping at each level in the following 
@@ -32,7 +27,19 @@
 #' @seealso \code{\link{instrument.table}}
 # TODO add a link to PortfolioAnalytics attribution functions, when they exist
 #' @export
-buildHierarchy <- function(primary_ids, levels, ...) {
+#' @examples
+#' \dontrun{
+#' # rm_instruments(keep.currencies=FALSE)
+#' ## Define some stocks
+#' update_instruments.TTR(c("XOM", "IBM", "CVX", "WMT", "GE"), exchange="NYSE")
+#' 
+#' buildHierarchy(ls_instruments(), "type")
+#' buildHierarchy(ls_stocks(), c("Name", "Sector"))
+#' buildHierarchy(ls_stocks(), "Industry", "MarketCap")
+#' }
+buildHierarchy <- function(primary_ids, ...) {
+    levels <- unlist(list(...))
+    if (!is.null(levels)) stopifnot(is.character(levels))
     out <- data.frame(primary_ids, stringsAsFactors=FALSE)
     ilist <- lapply(primary_ids, getInstrument)
     for (level in levels) {

Modified: pkg/FinancialInstrument/R/expires.R
===================================================================
--- pkg/FinancialInstrument/R/expires.R	2012-03-04 20:09:15 UTC (rev 962)
+++ pkg/FinancialInstrument/R/expires.R	2012-03-04 21:01:36 UTC (rev 963)
@@ -1,7 +1,7 @@
 #' extract the correct expires value from an \code{instrument}
 #'
-#' Currently, there are methods for \code{instrument}, \code{spread}, and
-#'  \code{character}
+#' Currently, there are methods for \code{instrument}, \code{spread},
+#'  \code{character}, and \code{xts}
 #'
 #' Will return either the last expiration date before a given \code{Date}, or 
 #' the first expiration date after a given \code{Date} 

Modified: pkg/FinancialInstrument/R/update_instruments.yahoo.R
===================================================================
--- pkg/FinancialInstrument/R/update_instruments.yahoo.R	2012-03-04 20:09:15 UTC (rev 962)
+++ pkg/FinancialInstrument/R/update_instruments.yahoo.R	2012-03-04 21:01:36 UTC (rev 963)
@@ -126,10 +126,10 @@
 update_instruments.TTR <- function(Symbols = c("stocks", "all"), exchange=c("AMEX","NASDAQ","NYSE")) {
     if (!suppressWarnings(is.currency.name("USD"))) currency("USD")
     df <- stockSymbols(exchange=exchange)    
-    if (!is.null(Symbols) && !(any(Symbols == c("stocks","all")))) {
-        cols <- try( match(Symbols,df$Symbol) )
-        if (!inherits(cols, 'try-error')) {
-            df <- df[cols,]
+    if (!is.null(Symbols) && !(any(c("stocks", "all") %in% Symbols))) {
+        rows <- try( match(Symbols,df$Symbol) )
+        if (!inherits(rows, 'try-error')) {
+            df <- df[rows,]
         } else {
             warning(paste(paste(Symbols,collapse=","), "not found among those listed on", paste(exchange,collapse=", ")))
             return(invisible(NULL))        

Modified: pkg/FinancialInstrument/man/buildHierarchy.Rd
===================================================================
--- pkg/FinancialInstrument/man/buildHierarchy.Rd	2012-03-04 20:09:15 UTC (rev 962)
+++ pkg/FinancialInstrument/man/buildHierarchy.Rd	2012-03-04 21:01:36 UTC (rev 963)
@@ -2,17 +2,15 @@
 \alias{buildHierarchy}
 \title{Construct a hierarchy of instruments useful for aggregation}
 \usage{
-  buildHierarchy(primary_ids, levels, ...)
+  buildHierarchy(primary_ids, ...)
 }
 \arguments{
   \item{primary_ids}{A character vector of
   \code{instrument} primary_ids to be included in the
   hierarchy list}
 
-  \item{levels}{A character vector of instrument attributes
-  in top-down order}
-
-  \item{...}{not in use}
+  \item{...}{character names of instrument attributes in
+  top-down order.}
 }
 \value{
   Constructs a data.frame that contains the list of assets
@@ -20,14 +18,20 @@
   grouping at each level in the following columns
 }
 \description{
-  All 'currency' instruments must be defined before
-  instruments of other types may be defined.
+  Construct a hierarchy of instruments useful for
+  aggregation
 }
-\details{
-  In \dots you may pass any other arbitrary instrument
-  fields that will be used to create 'custom' fields. (This
-  is not yet implemented)
+\examples{
+\dontrun{
+# rm_instruments(keep.currencies=FALSE)
+## Define some stocks
+update_instruments.TTR(c("XOM", "IBM", "CVX", "WMT", "GE"), exchange="NYSE")
+
+buildHierarchy(ls_instruments(), "type")
+buildHierarchy(ls_stocks(), c("Name", "Sector"))
+buildHierarchy(ls_stocks(), "Industry", "MarketCap")
 }
+}
 \author{
   Peter Carl, Alexis Petit, Garrett See
 }

Modified: pkg/FinancialInstrument/man/expires.Rd
===================================================================
--- pkg/FinancialInstrument/man/expires.Rd	2012-03-04 20:09:15 UTC (rev 962)
+++ pkg/FinancialInstrument/man/expires.Rd	2012-03-04 21:01:36 UTC (rev 963)
@@ -14,7 +14,7 @@
 }
 \description{
   Currently, there are methods for \code{instrument},
-  \code{spread}, and \code{character}
+  \code{spread}, \code{character}, and \code{xts}
 }
 \details{
   Will return either the last expiration date before a



More information about the Blotter-commits mailing list