[Blotter-commits] r228 - in pkg/quantstrat: . R demo man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Feb 5 17:58:29 CET 2010
Author: braverock
Date: 2010-02-05 17:58:29 +0100 (Fri, 05 Feb 2010)
New Revision: 228
Modified:
pkg/quantstrat/NAMESPACE
pkg/quantstrat/R/signals.R
pkg/quantstrat/demo/simplestrat.R
pkg/quantstrat/man/sigComparison.Rd
pkg/quantstrat/man/sigCrossover.Rd
pkg/quantstrat/man/sigThreshold.Rd
Log:
- implement sigCrossover
- add documentation
Modified: pkg/quantstrat/NAMESPACE
===================================================================
--- pkg/quantstrat/NAMESPACE 2010-02-04 17:14:17 UTC (rev 227)
+++ pkg/quantstrat/NAMESPACE 2010-02-05 16:58:29 UTC (rev 228)
@@ -4,6 +4,7 @@
export(add.signal)
export(applySignals)
export(sigComparison)
+export(sigCrossover)
export(sigPeak)
export(sigThreshold)
export(strategy)
Modified: pkg/quantstrat/R/signals.R
===================================================================
--- pkg/quantstrat/R/signals.R 2010-02-04 17:14:17 UTC (rev 227)
+++ pkg/quantstrat/R/signals.R 2010-02-05 16:58:29 UTC (rev 228)
@@ -102,11 +102,20 @@
}
-#' signal function for comparisons
+#' generate comparison signal
+#'
+#' Currently, this function compares two columns.
+#' Patches to compare an arbitrary number of columns would be gladly accepted.
+#'
+#' Comparison will be applied from the first to the second column in the \code{columns} vector.
+#'
#' @param label text label to apply to the output
#' @param data data to apply comparison to
#' @param columns named columns to apply comparison to
#' @param relationship one of c("gt","lt","eq","gte","lte") or reasonable alternatives
+#' @example
+#' getSymbols("IBM")
+#' sigComparison(label="Cl.gt.Op",data=IBM,columns=c("Close","Open"),"gt")
#' @export
sigComparison <- function(label,data, columns, relationship=c("gt","lt","eq","gte","lte")) {
relationship=relationship[1] #only use the first one
@@ -133,8 +142,25 @@
return(ret_sig)
}
+#' generate a crossover signal
+#'
+#' This will generate a corssover signal, which is a dimension-reduced version
+#' of a comparison signal \code{\link{sigComparison}}.
+#'
+#' It will return TRUE on the period in which there is a crossover in the
+#' direction specified by \code{relationship}, and NA otherwise.
+#'
+#' If you want all the information, use a comparison instead.
+#'
+#' @param label text label to apply to the output
+#' @param data data to apply crossover to
+#' @param columns named columns to apply crossover of the first against the second
+#' @param relationship one of c("gt","lt","eq","gte","lte") or reasonable alternatives
+#' @export
sigCrossover <- function(label,data, columns, relationship=c("gt","lt","eq","gte","lte")) {
- # TODO should call sigComparison and then do a diff so we only have the signal in the period it actually changes
+ ret_sig = ifelse(diff(sigComparison(label=label,data=data,columns=columns,relationship=relationship))==1,TRUE,NA)
+ colnames(ret_sig)<-label
+ return(ret_sig)
}
#' signal function for peak/valley signals
@@ -156,7 +182,12 @@
return(ret_sig)
}
-#' signal function for threshold signal
+#' generate a threshold signal
+#'
+#' Many strategies, including RSI or MACD styles, make trading decisions when an indicator
+#' is over or under a specific threshold.
+#' This function generates the appropriate signal based on such a threshold.
+#'
#' @param label text label to apply to the output
#' @param data data to apply comparison to
#' @param column named column to apply comparison to
Modified: pkg/quantstrat/demo/simplestrat.R
===================================================================
--- pkg/quantstrat/demo/simplestrat.R 2010-02-04 17:14:17 UTC (rev 227)
+++ pkg/quantstrat/demo/simplestrat.R 2010-02-05 16:58:29 UTC (rev 228)
@@ -11,6 +11,6 @@
cbind(IBM.mod,sigComparison(label="Adjusted.gt.SMA",data=IBM.mod,columns=c("Adjusted","SMA10"),">"))
#or, do it properly and add it to the strategy:
-s<- add.signal(s,name="sigComparison",arguments = list(data=quote(mktdata),columns=c(".Close","Open"),relationship="gt"),label="ClosegtOpen")
-s<- add.signal(s,name="sigComparison",arguments = list(data=quote(mktdata),columns=c(".Close","up"),relationship="gt"),label="ClosegtUpperBand")
+s<- add.signal(s,name="sigComparison",arguments = list(data=quote(mktdata),columns=c("Close","Open"),relationship="gt"),label="Cl.gt.Op")
+s<- add.signal(s,name="sigComparison",arguments = list(data=quote(mktdata),columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperdBand")
IBM.mod<-applySignals(s,mktdata=IBM.mod)
\ No newline at end of file
Modified: pkg/quantstrat/man/sigComparison.Rd
===================================================================
--- pkg/quantstrat/man/sigComparison.Rd 2010-02-04 17:14:17 UTC (rev 227)
+++ pkg/quantstrat/man/sigComparison.Rd 2010-02-05 16:58:29 UTC (rev 228)
@@ -1,8 +1,12 @@
\name{sigComparison}
\alias{sigComparison}
-\title{signal function for comparisons...}
+\title{generate comparison signal...}
\usage{sigComparison(label, data, columns, relationship=c("gt", "lt", "eq", "gte", "lte"))}
-\description{signal function for comparisons}
+\description{generate comparison signal}
+\details{Currently, this function compares two columns.
+Patches to compare an arbitrary number of columns would be gladly accepted.
+
+Comparison will be applied from the first to the second column in the \code{columns} vector.}
\arguments{\item{label}{text label to apply to the output}
\item{data}{data to apply comparison to}
\item{columns}{named columns to apply comparison to}
Modified: pkg/quantstrat/man/sigCrossover.Rd
===================================================================
--- pkg/quantstrat/man/sigCrossover.Rd 2010-02-04 17:14:17 UTC (rev 227)
+++ pkg/quantstrat/man/sigCrossover.Rd 2010-02-05 16:58:29 UTC (rev 228)
@@ -1,3 +1,16 @@
\name{sigCrossover}
\alias{sigCrossover}
-\title{sigCrossover}
+\title{generate a crossover signal...}
+\usage{sigCrossover(label, data, columns, relationship=c("gt", "lt", "eq", "gte", "lte"))}
+\description{generate a crossover signal}
+\details{This will generate a corssover signal, which is a dimension-reduced version
+of a comparison signal \code{\link{sigComparison}}.
+
+It will return TRUE on the period in which there is a crossover in the
+direction specified by \code{relationship}, and NA otherwise.
+
+If you want all the information, use a comparison instead.}
+\arguments{\item{label}{text label to apply to the output}
+\item{data}{data to apply crossover to}
+\item{columns}{named columns to apply crossover of the first against the second}
+\item{relationship}{one of c("gt","lt","eq","gte","lte") or reasonable alternatives}}
Modified: pkg/quantstrat/man/sigThreshold.Rd
===================================================================
--- pkg/quantstrat/man/sigThreshold.Rd 2010-02-04 17:14:17 UTC (rev 227)
+++ pkg/quantstrat/man/sigThreshold.Rd 2010-02-05 16:58:29 UTC (rev 228)
@@ -1,8 +1,11 @@
\name{sigThreshold}
\alias{sigThreshold}
-\title{signal function for threshold signal...}
+\title{generate a threshold signal...}
\usage{sigThreshold(label, data, column, threshold=0, relationship=c("gt", "lt", "eq", "gte", "lte"))}
-\description{signal function for threshold signal}
+\description{generate a threshold signal}
+\details{Many strategies, including RSI or MACD styles, make trading decisions when an indicator
+is over or under a specific threshold.
+This function generates the appropriate signal based on such a threshold.}
\arguments{\item{label}{text label to apply to the output}
\item{data}{data to apply comparison to}
\item{column}{named column to apply comparison to}
More information about the Blotter-commits
mailing list