[Blotter-commits] r1415 - in pkg/quantstrat: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Mar 27 15:18:03 CET 2013


Author: braverock
Date: 2013-03-27 15:18:03 +0100 (Wed, 27 Mar 2013)
New Revision: 1415

Modified:
   pkg/quantstrat/R/indicators.R
   pkg/quantstrat/R/match.names.R
   pkg/quantstrat/R/signals.R
   pkg/quantstrat/man/add.indicator.Rd
   pkg/quantstrat/man/applyIndicators.Rd
   pkg/quantstrat/man/match.names.Rd
Log:
- update label handling to paste to the end of the column names, consistent with earlier behavior
- update match.names to check for general match first, and check for match at end second if multiple matches found
- update documentation

Modified: pkg/quantstrat/R/indicators.R
===================================================================
--- pkg/quantstrat/R/indicators.R	2013-03-26 19:22:56 UTC (rev 1414)
+++ pkg/quantstrat/R/indicators.R	2013-03-27 14:18:03 UTC (rev 1415)
@@ -35,7 +35,7 @@
 #' there already exists an indicator with that label in which case it will be appended
 #' with a number (i.e. '<name>.ind.2', '<name>.ind.3', etc.).
 #' If the indicator function returns multiple columns, the label will be 
-#' \code{\link{paste}}'d to either the returned column names or the 
+#' \code{\link{paste}}'d to the end of either the returned column names or the 
 #' respective column number when applying it to \code{mktdata}.
 #' 
 #' @param strategy an object (or the name of an object) type 'strategy' to add the indicator to
@@ -102,6 +102,25 @@
 }
 
 #' apply the indicators in the strategy to arbitrary market data
+#' 
+#' \code{applyIndicators} will take the \code{mktdata} object, 
+#' and will apply each indicator specified in the strategy definition to it.
+#' 
+#' If the indicator function returns an xts object or a vector of the same length 
+#' as mktdata, the columns created by the indicator function will be added to the 
+#' mktdata object via \code{\link{cbind}}. 
+#' 
+#' If the indicator function returns multiple columns, the label will be 
+#' \code{\link{paste}}'d to the end of either the returned column names or the 
+#' respective column number when applying it to \code{mktdata}.
+#' 
+#' If the indicator returns some more complexobject, it will be added to a list 
+#' in the $indicators slot inside the \code{\link{applyStrategy}} execution frame.
+#' If you want your indicators to return a more complex object, 
+#' (such as a model specification and output from a regression), be advised
+#' that your signal generator, and potentially your rule function, will need
+#' to understand, anticipate, and know how to manipulate the internal strategy frame.
+#'   
 #' @param strategy an object of type 'strategy' to add the indicator to
 #' @param mktdata an xts object containing market data.  depending on indicators, may need to be in OHLCV or BBO formats
 #' @param parameters named list of parameters to be applied during evaluation of the strategy
@@ -192,9 +211,12 @@
         .formals$... <- NULL
         
         tmp_val<-do.call(fun,.formals)
+		
+		#add label
         if(is.null(colnames(tmp_val)))
             colnames(tmp_val) <- seq(ncol(tmp_val))
-        colnames(tmp_val) <- paste(indicator$label,colnames(tmp_val),sep='.')
+        if(!identical(colnames(tmp_val),indicator$label)) 
+			colnames(tmp_val) <- paste(colnames(tmp_val),indicator$label,sep='.')
 
         if (nrow(mktdata)==nrow(tmp_val) | length(mktdata)==length(tmp_val)) {
             # the indicator returned a time series, so we'll name it and cbind it

Modified: pkg/quantstrat/R/match.names.R
===================================================================
--- pkg/quantstrat/R/match.names.R	2013-03-26 19:22:56 UTC (rev 1414)
+++ pkg/quantstrat/R/match.names.R	2013-03-27 14:18:03 UTC (rev 1415)
@@ -10,13 +10,23 @@
 #' This small utility exists to do the matching in a centralized location 
 #' so that more robust error handling and reporting can be conducted.
 #'    
+#' The process to be followed is that first, \code{\link{grep}} will
+#' be called without modification, assuming that a unique match has 
+#' been supplied by the user.  If this fails, a match will be attempted
+#' by appending '$' to the regex, searching for a match at the end of the 
+#' column name, as would be constructed by the \code{\link{paste}} in
+#' e.g. \code{\link{applyIndicators}}. 
+#'  
 #' @param data_names names for the data to be matched to
 #' @param match_names names to match
 #' @export
 match.names <- function(match_names,data_names) {
     loc<-NULL
     for (mname in match_names){
-        t<-grep(paste(mname,"$",sep=""),data_names)
+        t<-grep(mname,data_names)
+		if(length(t)>1){
+			t<-grep(paste(mname,"$",sep=""),data_names)
+		}
         if(is.null(loc)) loc<-t
         else loc <- c(loc,t)
     }

Modified: pkg/quantstrat/R/signals.R
===================================================================
--- pkg/quantstrat/R/signals.R	2013-03-26 19:22:56 UTC (rev 1414)
+++ pkg/quantstrat/R/signals.R	2013-03-27 14:18:03 UTC (rev 1415)
@@ -133,16 +133,14 @@
         .formals$... <- NULL
         
         tmp_val<-do.call(fun,.formals)
-        if(is.null(colnames(tmp_val)) || !is.null(signal$label)) {
-            if (ncol(tmp_val)==1) { #no names, only one column
-                colnames(tmp_val)<-signal$label 
-            } else { #no names, more than one column
-                colnames(tmp_val) <- paste(signal$label,seq(1,ncol(tmp_val)),sep='.') 
-            }  
-        } else { #we have column names, so paste
-            if(ncol(tmp_val)>1) colnames(tmp_val) <- paste(signal$label,colnames(tmp_val),sep='.')
-        }
-        if (nrow(mktdata)==nrow(tmp_val) | length(mktdata)==length(tmp_val)) {
+		
+		#add label
+		if(is.null(colnames(tmp_val)))
+			colnames(tmp_val) <- seq(ncol(tmp_val))
+		if(!identical(colnames(tmp_val),signal$label)) 
+			colnames(tmp_val) <- paste(colnames(tmp_val),signal$label,sep='.')
+		
+		if (nrow(mktdata)==nrow(tmp_val) | length(mktdata)==length(tmp_val)) {
             # the signal returned a time series, so we'll name it and cbind it
             mktdata<-cbind(mktdata,tmp_val)
         } else {

Modified: pkg/quantstrat/man/add.indicator.Rd
===================================================================
--- pkg/quantstrat/man/add.indicator.Rd	2013-03-26 19:22:56 UTC (rev 1414)
+++ pkg/quantstrat/man/add.indicator.Rd	2013-03-27 14:18:03 UTC (rev 1415)
@@ -101,8 +101,9 @@
   appended with a number (i.e. '<name>.ind.2',
   '<name>.ind.3', etc.). If the indicator function returns
   multiple columns, the label will be \code{\link{paste}}'d
-  to either the returned column names or the respective
-  column number when applying it to \code{mktdata}.
+  to the end of either the returned column names or the
+  respective column number when applying it to
+  \code{mktdata}.
 }
 \examples{
 \dontrun{

Modified: pkg/quantstrat/man/applyIndicators.Rd
===================================================================
--- pkg/quantstrat/man/applyIndicators.Rd	2013-03-26 19:22:56 UTC (rev 1414)
+++ pkg/quantstrat/man/applyIndicators.Rd	2013-03-27 14:18:03 UTC (rev 1415)
@@ -22,9 +22,30 @@
   \code{mktdata} with indicators colums added.
 }
 \description{
-  apply the indicators in the strategy to arbitrary market
-  data
+  \code{applyIndicators} will take the \code{mktdata}
+  object, and will apply each indicator specified in the
+  strategy definition to it.
 }
+\details{
+  If the indicator function returns an xts object or a
+  vector of the same length as mktdata, the columns created
+  by the indicator function will be added to the mktdata
+  object via \code{\link{cbind}}.
+
+  If the indicator function returns multiple columns, the
+  label will be \code{\link{paste}}'d to the end of either
+  the returned column names or the respective column number
+  when applying it to \code{mktdata}.
+
+  If the indicator returns some more complexobject, it will
+  be added to a list in the $indicators slot inside the
+  \code{\link{applyStrategy}} execution frame. If you want
+  your indicators to return a more complex object, (such as
+  a model specification and output from a regression), be
+  advised that your signal generator, and potentially your
+  rule function, will need to understand, anticipate, and
+  know how to manipulate the internal strategy frame.
+}
 \examples{
 \dontrun{
 strategy("example", store=TRUE)

Modified: pkg/quantstrat/man/match.names.Rd
===================================================================
--- pkg/quantstrat/man/match.names.Rd	2013-03-26 19:22:56 UTC (rev 1414)
+++ pkg/quantstrat/man/match.names.Rd	2013-03-27 14:18:03 UTC (rev 1415)
@@ -23,5 +23,14 @@
   This small utility exists to do the matching in a
   centralized location so that more robust error handling
   and reporting can be conducted.
+
+  The process to be followed is that first,
+  \code{\link{grep}} will be called without modification,
+  assuming that a unique match has been supplied by the
+  user.  If this fails, a match will be attempted by
+  appending '$' to the regex, searching for a match at the
+  end of the column name, as would be constructed by the
+  \code{\link{paste}} in e.g.
+  \code{\link{applyIndicators}}.
 }
 



More information about the Blotter-commits mailing list