[Blotter-commits] r226 - in pkg/quantstrat: R demo
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Feb 4 17:29:07 CET 2010
Author: braverock
Date: 2010-02-04 17:29:07 +0100 (Thu, 04 Feb 2010)
New Revision: 226
Modified:
pkg/quantstrat/R/indicators.R
pkg/quantstrat/R/signals.R
pkg/quantstrat/demo/simplestrat.R
Log:
- clean up function name matching
- update demo script
Modified: pkg/quantstrat/R/indicators.R
===================================================================
--- pkg/quantstrat/R/indicators.R 2010-02-04 15:20:53 UTC (rev 225)
+++ pkg/quantstrat/R/indicators.R 2010-02-04 16:29:07 UTC (rev 226)
@@ -51,9 +51,14 @@
for (indicator in strategy$indicators){
#TODO check to see if they've already been calculated
- if(!is.function(get(indicator$name))) {
- message(paste("Skipping indicator",indicator$name,"because there is no function by that name to call"))
- next()
+
+ if(!is.function(get(indicator$name))){
+ if(!is.function(get(paste("sig",indicator$name,sep='.')))){
+ message(paste("Skipping indicator",indicator$name,"because there is no function by that name to call"))
+ next()
+ } else {
+ indicator$name<-paste("ind",indicator$name,sep='.')
+ }
}
# see 'S Programming p. 67 for this matching
Modified: pkg/quantstrat/R/signals.R
===================================================================
--- pkg/quantstrat/R/signals.R 2010-02-04 15:20:53 UTC (rev 225)
+++ pkg/quantstrat/R/signals.R 2010-02-04 16:29:07 UTC (rev 226)
@@ -54,14 +54,19 @@
for (signal in strategy$signals){
#TODO check to see if they've already been calculated
- if(!is.function(get(signal$name)) & !is.function(get(paste("sig",signal$name,sep='.')))) {
- message(paste("Skipping signal",signal$name,"because there is no function by that name to call"))
- next()
+
+ if(!is.function(get(signal$name))){
+ if(!is.function(get(paste("sig",signal$name,sep='.')))){
+ message(paste("Skipping signal",signal$name,"because there is no function by that name to call"))
+ next()
+ } else {
+ signal$name<-paste("sig",signal$name,sep='.')
+ }
}
# see 'S Programming p. 67 for this matching
- fun<-try(match.fun(signal$name),silent=TRUE)
- if(inherits(fun,,"try-error")) fun <- match.fun(paste(sig,signal$name,'.'))
+ fun<-match.fun(signal$name)
+
.formals <- formals(fun)
onames <- names(.formals)
@@ -126,7 +131,7 @@
return(ret_sig)
}
-sigCrossover <- sigComparison <- function(label,data, columns, relationship=c("gt","lt","eq","gte","lte")) {
+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
}
Modified: pkg/quantstrat/demo/simplestrat.R
===================================================================
--- pkg/quantstrat/demo/simplestrat.R 2010-02-04 15:20:53 UTC (rev 225)
+++ pkg/quantstrat/demo/simplestrat.R 2010-02-04 16:29:07 UTC (rev 226)
@@ -1,16 +1,16 @@
s <- strategy("simplestrat")
s <- add.indicator(strategy = s, name = "SMA", arguments = list(x = quote(Cl(mktdata)), n=10), label="SMA10")
+s <- add.indicator(strategy = s, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), sd = 1.8,maType=quote(SMA)))
-# this indicator fails on dots, so we'll not enable it for now
-#s <- add.indicator(strategy = s, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), sd = 1.8,maType=quote(SMA)))
-
getSymbols("IBM")
IBM.mod=applyIndicators(s,mktdata=IBM)
#manually apply a signal function for demonstration
-IBM.mod = cbind(IBM.mod,sigComparison(label="Close.gt.Open",data=IBM.mod,columns=c("Close","Open"),">"))
-IBM.mod = cbind(IBM.mod,sigComparison(label="Adjusted.gt.SMA",data=IBM.mod,columns=c("Adjusted","SMA10"),">"))
+cbind(IBM.mod,sigComparison(label="Close.gt.Open",data=IBM.mod,columns=c("Close","Open"),">"))
+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.indicator(s,name="sigComparison",arguments = list(data=quote(mktdata),columns=c("Close","Open"),relationship=">"))label="Close.gt.Open")
\ No newline at end of file
+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")
+IBM.mod<-applySignals(s,mktdata=IBM.mod)
\ No newline at end of file
More information about the Blotter-commits
mailing list