[Blotter-commits] r274 - pkg/quantstrat/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Feb 26 19:46:27 CET 2010


Author: braverock
Date: 2010-02-26 19:46:27 +0100 (Fri, 26 Feb 2010)
New Revision: 274

Modified:
   pkg/quantstrat/R/rules.R
   pkg/quantstrat/R/signals.R
   pkg/quantstrat/R/traderules.R
Log:
- add 'op' side to sigComparison
- add 'all' orderqty to ruleSignal
- add 'exit' rule handling only if you have a position

Modified: pkg/quantstrat/R/rules.R
===================================================================
--- pkg/quantstrat/R/rules.R	2010-02-26 17:35:46 UTC (rev 273)
+++ pkg/quantstrat/R/rules.R	2010-02-26 18:46:27 UTC (rev 274)
@@ -208,6 +208,10 @@
                     },
                     rebalance =, exit = , enter = {
                         if(isTRUE(hold)) next()    
+                        if(type=='exit'){
+                            # must have a position for an exit rules to fire
+                            if (getPosQty(Portfolio=portfolio,Symbol=symbol,Date=timestamp)==0) next()
+                        }
                         if(length(strategy$rules[[type]])>=1) {
                             ruleProc(strategy$rules[[type]],timestamp=timestamp, path.dep=path.dep, mktdata=mktdata,portfolio=portfolio, symbol=symbol)
                         }      

Modified: pkg/quantstrat/R/signals.R
===================================================================
--- pkg/quantstrat/R/signals.R	2010-02-26 17:35:46 UTC (rev 273)
+++ pkg/quantstrat/R/signals.R	2010-02-26 18:46:27 UTC (rev 274)
@@ -114,10 +114,12 @@
 #' 
 #' Comparison will be applied from the first to the second column in the \code{columns} vector.
 #' 
+#' Relationship 'op' means 'opposite side.  Reasonable attempt will be made to match.
+#' 
 #' @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
+#' @param relationship one of c("gt","lt","eq","gte","lte","op") or reasonable alternatives
 #' @example 
 #' getSymbols("IBM")
 #' sigComparison(label="Cl.gt.Op",data=IBM,columns=c("Close","Open"),"gt")
@@ -126,19 +128,30 @@
     relationship=relationship[1] #only use the first one
     if (length(columns==2)){
         ret_sig=NULL
+        if (relationship=='op'){
+            switch(columns[1],
+                    Low =, 
+                    low =, 
+                    bid = { relationship = 'lt' },
+                    Hi  =,
+                    High=,
+                    high=,
+                    ask = {relationship = 'gt'}
+            )
+        }
         columns <- match.names(columns,colnames(data))
         switch(relationship,
-                '>'  =,
-                'gt' = {ret_sig = data[,columns[1]] > data[,columns[2]]},
-                '<'  =,
-                'lt' = {ret_sig = data[,columns[1]] < data[,columns[2]]},
-                'eq'     = {ret_sig = data[,columns[1]] == data[,columns[2]]}, #FIXME any way to specify '='?
+                '>'   =,
+                'gt'  = {ret_sig = data[,columns[1]] > data[,columns[2]]},
+                '<'   ,
+                'lt'  = {ret_sig = data[,columns[1]] < data[,columns[2]]},
+                'eq'  = {ret_sig = data[,columns[1]] == data[,columns[2]]}, #FIXME any way to specify '='?
                 'gte' =,
                 'gteq'=,
-                'ge'     = {ret_sig = data[,columns[1]] >= data[,columns[2]]}, #FIXME these fail with an 'unexpected =' error if you use '>=' 
+                'ge'  = {ret_sig = data[,columns[1]] >= data[,columns[2]]}, #FIXME these fail with an 'unexpected =' error if you use '>=' 
                 'lte' =,
                 'lteq'=,
-                'le' 	 = {ret_sig = data[,columns[1]] <= data[,columns[2]]}
+                'le'  = {ret_sig = data[,columns[1]] <= data[,columns[2]]}
         )
     } else {
         stop("comparison of more than two columns not supported yet, patches welcome")
@@ -163,7 +176,12 @@
 #' @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")) {
-    ret_sig = ifelse(diff(sigComparison(label=label,data=data,columns=columns,relationship=relationship))==1,TRUE,NA)
+    ret_sig = NA
+    lng<-length(columns)
+    for (i in 1:(lng-1)) {
+        if (!is.na(ret_sig)) break()
+        ret_sig = ifelse(diff(sigComparison(label=label,data=data,columns=columns[c(i,lng)],relationship=relationship))==1,TRUE,NA)
+    }
     colnames(ret_sig)<-label
     return(ret_sig)
 }

Modified: pkg/quantstrat/R/traderules.R
===================================================================
--- pkg/quantstrat/R/traderules.R	2010-02-26 17:35:46 UTC (rev 273)
+++ pkg/quantstrat/R/traderules.R	2010-02-26 18:46:27 UTC (rev 274)
@@ -32,7 +32,12 @@
     if(!is.function(osFUN)) osFUN<-match.fun(osFUN)
     if (!is.na(mktdata[timestamp][,sigcol]) & mktdata[timestamp][,sigcol] == sigval) {
         #TODO add fancy formals matching for osFUN
-         
+        if(orderqty=='all'){
+            orderqty=-1*getPosQty(Portfolio=portfolio,Symbol=symbol,Date=timestamp)    
+        } 
+        if(orderqty>0 & orderqty<1){
+            # TODO add proportional order?  or should that go in order sizing function?
+        } 
         orderqty <- osFUN(strategy=strategy, mktdata=mktdata, timestamp=timestamp, orderqty=orderqty, ordertype=ordertype, orderside=orderside, portfolio=portfolio, symbol=symbol)
         #calculate order price using pricemethod
         pricemethod<-pricemethod[1] #only use the first if not set by calling function



More information about the Blotter-commits mailing list