[Blotter-commits] r854 - in pkg/quantstrat: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Nov 24 19:16:49 CET 2011
Author: gsee
Date: 2011-11-24 19:16:48 +0100 (Thu, 24 Nov 2011)
New Revision: 854
Modified:
pkg/quantstrat/DESCRIPTION
pkg/quantstrat/R/indicators.R
pkg/quantstrat/man/add.indicator.Rd
pkg/quantstrat/man/applyIndicators.Rd
Log:
- use 'label' as the name of the indicator list in $indicators
- if 'label' is passed to add.indicator and there already
exists an indicator by that name, it will be overwritten.
- if add.indicator is called with label=NULL, label will become
'<name>.ind'. If an indicator already exists by that name,
a unique name will be created by appending a number
- the 'indexnum' arg can still be used; it can either be the
position of the indicator in the list, or its 'label' name.
- Before applyIndicators calculates and cbinds indicators, it
will first check 'mktdata' to see if they already exist. If
they do, they will be replaced. Therefore, mktdata will be
the same if applyIndicators is called multiple times, or only once.
- version 0.6
Modified: pkg/quantstrat/DESCRIPTION
===================================================================
--- pkg/quantstrat/DESCRIPTION 2011-11-19 16:32:20 UTC (rev 853)
+++ pkg/quantstrat/DESCRIPTION 2011-11-24 18:16:48 UTC (rev 854)
@@ -1,7 +1,7 @@
Package: quantstrat
Type: Package
Title: Quantitative Strategy Model Framework
-Version: 0.5.8
+Version: 0.6
Date: $Date$
Author: Peter Carl, Dirk Eddelbuettel, Brian G. Peterson,
Jeffrey A. Ryan, Joshua Ulrich, Garrett See, Yu Chen
Modified: pkg/quantstrat/R/indicators.R
===================================================================
--- pkg/quantstrat/R/indicators.R 2011-11-19 16:32:20 UTC (rev 853)
+++ pkg/quantstrat/R/indicators.R 2011-11-24 18:16:48 UTC (rev 854)
@@ -1,4 +1,3 @@
-
#' add an indicator to a strategy
#'
#' Indicators are typically standard technical or statistical analysis outputs,
@@ -31,20 +30,22 @@
#' 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 \code{label} is not supplied, NULL default will be converted to '<name>.ind' unless
+#' 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
-#' respective column number.
+#' 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
-#' @param name name of the indicator, must correspond to an R function
+#' @param name name of the indicator function -- must correspond to an R function
#' @param arguments default arguments to be passed to an indicator 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 indicator output, NULL default will be converted to '<name>.ind'
+#' @param label arbitrary text label for indicator output. This will also be used as the
+#' name of the indicator list when it is stored. NULL default will be converted to '<name>.ind'
#' @param ... any other passthru parameters
#' @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 indexnum if you are updating a specific indicator, the \code{label} or 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
#' @return if \code{strategy} was the name of a strategy, the name. It it was a strategy, the updated strategy.
#' @seealso
@@ -52,7 +53,15 @@
#' \code{\link{applyIndicators}}
#' \code{\link{add.signal}}
#' \code{link{add.rule}}
-#'
+#' @examples
+#' \dontrun{
+#' strategy("example", store=TRUE)
+#' getSymbols("SPY", src='yahoo')
+#' add.indicator('example', 'SMA', arguments=list(x=quote(Ad(SPY)), n=20))
+#' str(getStrategy('example')$indicators)
+#' out <- applyIndicators('example', SPY)
+#' tail(out)
+#' }
#' @export
add.indicator <- function(strategy, name, arguments, parameters=NULL, label=NULL, ..., enabled=TRUE, indexnum=NULL, store=FALSE) {
if (!is.strategy(strategy)) {
@@ -63,15 +72,24 @@
}
tmp_indicator<-list()
tmp_indicator$name<-name
- if(is.null(label)) label = paste(name,"ind",sep='.')
+ # if we have a 'label', that will be the name of the indicator, if it already exists,
+ # it will be overwritten. If label is NULL the indicator name will be "<name>.ind"
+ # unless that already exists in which case we will append that with a number.
+ if(is.null(label)) {
+ label <- paste(name,"ind",sep='.')
+ gl <- grep(label, names(strategy$indicators))
+ if (!identical(integer(0), gl)) label <- paste(label, length(gl)+1, sep=".")
+ }
tmp_indicator$label<-label
tmp_indicator$enabled=enabled
if (!is.list(arguments)) stop("arguments must be passed as a named list")
tmp_indicator$arguments<-arguments
if(!is.null(parameters)) tmp_indicator$parameters = parameters
if(length(list(...))) tmp_indicator<-c(tmp_indicator,list(...))
-
- if(!hasArg(indexnum) | (hasArg(indexnum) & is.null(indexnum))) indexnum = length(strategy$indicators)+1
+
+ #if(!hasArg(indexnum) || (hasArg(indexnum) && is.null(indexnum))) indexnum = length(strategy$indicators)+1
+ indexnum <- if (!is.null(indexnum)) {indexnum} else label
+
tmp_indicator$call<-match.call()
class(tmp_indicator)<-'strat_indicator'
@@ -87,6 +105,16 @@
#' @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
#' @param ... any other passthru parameters
+#' @return \code{mktdata} with indicators colums added.
+#' @examples
+#' \dontrun{
+#' strategy("example", store=TRUE)
+#' getSymbols("SPY", src='yahoo')
+#' add.indicator('example', 'SMA', arguments=list(x=quote(Ad(SPY)), n=20))
+#' str(getStrategy('example')$indicators)
+#' out <- applyIndicators('example', SPY)
+#' tail(out)
+#' }
#' @export
applyIndicators <- function(strategy, mktdata, parameters=NULL, ...) {
#TODO add Date subsetting
@@ -106,10 +134,15 @@
#rm('...')
nargs=NULL
}
+
+ # First, delete any colums in mktdata that correspond to indicators we're about
+ # to (re)calculate and cbind.
+ omit <- unique(do.call(c, lapply(names(strategy$indicators), grep, colnames(mktdata))))
+ cidx <- 1:NCOL(mktdata)
+ keep <- cidx[!cidx %in% omit]
+ mktdata <- mktdata[, keep]
for (indicator in strategy$indicators){
- #TODO check to see if they've already been calculated
-
if(!is.function(get(indicator$name))){
if(!is.function(get(paste("sig",indicator$name,sep='.')))){
# now add arguments from parameters
Modified: pkg/quantstrat/man/add.indicator.Rd
===================================================================
--- pkg/quantstrat/man/add.indicator.Rd 2011-11-19 16:32:20 UTC (rev 853)
+++ pkg/quantstrat/man/add.indicator.Rd 2011-11-24 18:16:48 UTC (rev 854)
@@ -10,8 +10,8 @@
\item{strategy}{an object (or the name of an object) type
'strategy' to add the indicator to}
- \item{name}{name of the indicator, must correspond to an
- R function}
+ \item{name}{name of the indicator function -- must
+ correspond to an R function}
\item{arguments}{default arguments to be passed to an
indicator function when executed}
@@ -21,8 +21,10 @@
needed if you need special names to avoid argument
collision}
- \item{label}{arbitrary text label for indicator output,
- NULL default will be converted to '<name>.ind'}
+ \item{label}{arbitrary text label for indicator output.
+ This will also be used as the name of the indicator list
+ when it is stored. NULL default will be converted to
+ '<name>.ind'}
\item{...}{any other passthru parameters}
@@ -30,7 +32,8 @@
enabled for use in applying the strategy, default TRUE}
\item{indexnum}{if you are updating a specific indicator,
- the index number in the $indicators list to update}
+ the \code{label} or the index number in the $indicators
+ list to update.}
\item{store}{TRUE/FALSE whether to store the strategy in
the .strategy environment, or return it. default FALSE}
@@ -93,13 +96,24 @@
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.
+ converted to '<name>.ind' unless 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 respective
+ column number when applying it to \code{mktdata}.
}
+\examples{
+\dontrun{
+strategy("example", store=TRUE)
+getSymbols("SPY", src='yahoo')
+add.indicator('example', 'SMA', arguments=list(x=quote(Ad(SPY)), n=20))
+str(getStrategy('example')$indicators)
+out <- applyIndicators('example', SPY)
+tail(out)
+}
+}
\seealso{
\code{\link{quote}} \code{\link{applyIndicators}}
\code{\link{add.signal}} \code{link{add.rule}}
Modified: pkg/quantstrat/man/applyIndicators.Rd
===================================================================
--- pkg/quantstrat/man/applyIndicators.Rd 2011-11-19 16:32:20 UTC (rev 853)
+++ pkg/quantstrat/man/applyIndicators.Rd 2011-11-24 18:16:48 UTC (rev 854)
@@ -18,8 +18,21 @@
\item{...}{any other passthru parameters}
}
+\value{
+ \code{mktdata} with indicators colums added.
+}
\description{
apply the indicators in the strategy to arbitrary market
data
}
+\examples{
+\dontrun{
+strategy("example", store=TRUE)
+getSymbols("SPY", src='yahoo')
+add.indicator('example', 'SMA', arguments=list(x=quote(Ad(SPY)), n=20))
+str(getStrategy('example')$indicators)
+out <- applyIndicators('example', SPY)
+tail(out)
+}
+}
More information about the Blotter-commits
mailing list