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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Aug 29 14:27:44 CEST 2011


Author: braverock
Date: 2011-08-29 14:27:44 +0200 (Mon, 29 Aug 2011)
New Revision: 747

Modified:
   pkg/quantstrat/R/indicators.R
   pkg/quantstrat/R/signals.R
   pkg/quantstrat/man/add.indicator.Rd
   pkg/quantstrat/man/add.signal.Rd
Log:
- make label handling more sophisticated

Modified: pkg/quantstrat/R/indicators.R
===================================================================
--- pkg/quantstrat/R/indicators.R	2011-08-28 11:04:13 UTC (rev 746)
+++ pkg/quantstrat/R/indicators.R	2011-08-29 12:27:44 UTC (rev 747)
@@ -31,6 +31,12 @@
 #' sort it all out for you at apply-time.  
 #' We will endeavor to get an example of named parameters into the demo scripts.
 #'
+#' if \code{label} is not supplied,  NULL default will be converted to '<name>.ind'
+#' if the indicator function returns one named column, we use that, and ignore the label.  
+#' 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.
+#' 
 #' @param strategy an object of type 'strategy' to add the indicator to
 #' @param name name of the indicator, must correspond to an R function
 #' @param arguments default arguments to be passed to an indicator function when executed
@@ -40,8 +46,13 @@
 #' @param enabled TRUE/FALSE whether the indicator is enabled for use in applying the strategy, default TRUE
 #' @param indexnum if you are updating a specific indicator, the index number in the $indicators list to update
 #' @param store TRUE/FALSE whether to store the strategy in the .strategy environment, or return it.  default FALSE
+#' @seealso 
+#' \code{\link{quote}}
+#' \code{\link{applyIndicators}}
+#' \code{\link{add.signal}}
+#' \code{link{add.rule}}
+#'  
 #' @export
-#' @seealso \code{\link{quote}}
 add.indicator <- function(strategy, name, arguments, parameters=NULL, label=NULL, ..., enabled=TRUE, indexnum=NULL, store=FALSE) {
     if(!is.strategy(strategy)) stop("You must pass in a strategy object to manipulate")
     tmp_indicator<-list()
@@ -138,7 +149,16 @@
         .formals$... <- NULL
         
         tmp_val<-do.call(fun,.formals)
-        if(is.null(names(tmp_val)) & ncol(tmp_val)==1) names(tmp_val)<-indicator$label
+        if(is.null(colnames(tmp_val))) {
+            if (ncol(tmp_val)==1) { #no names, only one column
+                colnames(tmp_val)<-indicator$label 
+            } else { #no names, more than one column
+                colnames(tmp_val) <- paste(indicator$label,seq(1,ncol(tmp_val)),sep='.') 
+            }  
+        } else { #we have column names, so paste
+            if(ncol(tmp_val)>1) colnames(tmp_val) <- paste(indicator$label,colnames(tmp_val),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
             mktdata<-cbind(mktdata,tmp_val)

Modified: pkg/quantstrat/R/signals.R
===================================================================
--- pkg/quantstrat/R/signals.R	2011-08-28 11:04:13 UTC (rev 746)
+++ pkg/quantstrat/R/signals.R	2011-08-29 12:27:44 UTC (rev 747)
@@ -1,14 +1,38 @@
 
 #' add a signal to a strategy
+#' 
+#' This adds a signal definition to a strategy object. 
+#' 
+#' Signals denote times at which the strategy \emph{may} want to 
+#' take action.  Common signals types from the literature include 
+#' crossovers, thresholds, or other interactions between your \code{mktdata}
+#' and your indicators.
+#' 
+#' if \code{label} is not supplied,  NULL default will be converted to '<name>.sig'
+#' if the signal function returns one named column, we use that, and ignore the label.  
+#' If the signal function returns multiple columns, the label will be 
+#' \code{\link{paste}}'d to either the returned column names or the 
+#' respective column number.
+#' 
 #' @param strategy an object of type 'strategy' to add the signal to
 #' @param name name of the signal, must correspond to an R function
 #' @param arguments named list of default arguments to be passed to an signal function when executed
 #' @param parameters vector of strings naming parameters to be saved for apply-time definition,default NULL, only needed if you need special names to avoid argument collision
-#' @param label arbitrary text label for signal output, NULL default will be converted to '<name>.sig'
+#' @param label arbitrary text label for signal output, default NULL
 #' @param ... any other passthru parameters
 #' @param enabled TRUE/FALSE whether the signal is enabled for use in applying the strategy, default TRUE
 #' @param indexnum if you are updating a specific signal, the index number in the $signals list to update
 #' @param store TRUE/FALSE whether to store the strategy in the .strategy environment, or return it.  default FALSE
+#' @seealso 
+#' \code{\link{applySignals}}
+#' \code{\link{add.indicator}}
+#' \code{link{add.rule}}
+#' \code{\link{sigComparison}}
+#' \code{\link{sigCrossover}}
+#' \code{\link{sigFormula}}
+#' \code{\link{sigPeak}}
+#' \code{\link{sigThreshold}}
+#'  
 #' @export
 add.signal <- function(strategy, name, arguments, parameters=NULL, label=NULL, ..., enabled=TRUE, indexnum=NULL, store=FALSE) {
     if(!is.strategy(strategy)) stop("You must pass in a strategy object to manipulate")
@@ -102,7 +126,15 @@
         .formals$... <- NULL
         
         tmp_val<-do.call(fun,.formals)
-        if(is.null(names(tmp_val)) & ncol(tmp_val)==1) names(tmp_val)<-signal$label
+        if(is.null(colnames(tmp_val))) {
+            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)) {
             # the signal returned a time series, so we'll name it and cbind it
             mktdata<-cbind(mktdata,tmp_val)

Modified: pkg/quantstrat/man/add.indicator.Rd
===================================================================
--- pkg/quantstrat/man/add.indicator.Rd	2011-08-28 11:04:13 UTC (rev 746)
+++ pkg/quantstrat/man/add.indicator.Rd	2011-08-29 12:27:44 UTC (rev 747)
@@ -87,8 +87,17 @@
   sort it all out for you at apply-time. We will endeavor
   to get an example of named parameters into the demo
   scripts.
+
+  if \code{label} is not supplied, NULL default will be
+  converted to '<name>.ind' if the indicator function
+  returns one named column, we use that, and ignore the
+  label. 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.
 }
 \seealso{
-  \code{\link{quote}}
+  \code{\link{quote}} \code{\link{applyIndicators}}
+  \code{\link{add.signal}} \code{link{add.rule}}
 }
 

Modified: pkg/quantstrat/man/add.signal.Rd
===================================================================
--- pkg/quantstrat/man/add.signal.Rd	2011-08-28 11:04:13 UTC (rev 746)
+++ pkg/quantstrat/man/add.signal.Rd	2011-08-29 12:27:44 UTC (rev 747)
@@ -21,8 +21,8 @@
   needed if you need special names to avoid argument
   collision}
 
-  \item{label}{arbitrary text label for signal output, NULL
-  default will be converted to '<name>.sig'}
+  \item{label}{arbitrary text label for signal output,
+  default NULL}
 
   \item{...}{any other passthru parameters}
 
@@ -36,6 +36,26 @@
   the .strategy environment, or return it.  default FALSE}
 }
 \description{
-  add a signal to a strategy
+  This adds a signal definition to a strategy object.
 }
+\details{
+  Signals denote times at which the strategy \emph{may}
+  want to take action.  Common signals types from the
+  literature include crossovers, thresholds, or other
+  interactions between your \code{mktdata} and your
+  indicators.
 
+  if \code{label} is not supplied, NULL default will be
+  converted to '<name>.sig' if the signal function returns
+  one named column, we use that, and ignore the label. If
+  the signal function returns multiple columns, the label
+  will be \code{\link{paste}}'d to either the returned
+  column names or the respective column number.
+}
+\seealso{
+  \code{\link{applySignals}} \code{\link{add.indicator}}
+  \code{link{add.rule}} \code{\link{sigComparison}}
+  \code{\link{sigCrossover}} \code{\link{sigFormula}}
+  \code{\link{sigPeak}} \code{\link{sigThreshold}}
+}
+



More information about the Blotter-commits mailing list