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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Oct 30 23:29:47 CET 2013


Author: bodanker
Date: 2013-10-30 23:29:47 +0100 (Wed, 30 Oct 2013)
New Revision: 1558

Modified:
   pkg/quantstrat/R/ruleOrderProc.R
   pkg/quantstrat/R/rules.R
Log:
- calculate periodicity(mktdata) in applyRules and pass to ruleOrderProc
  to avoid calling periodicity for every order
- correct environment passed to do.call in ruleProc
- add data=mktdata to sigThreshold calls


Modified: pkg/quantstrat/R/ruleOrderProc.R
===================================================================
--- pkg/quantstrat/R/ruleOrderProc.R	2013-10-30 16:47:23 UTC (rev 1557)
+++ pkg/quantstrat/R/ruleOrderProc.R	2013-10-30 22:29:47 UTC (rev 1558)
@@ -125,7 +125,11 @@
       if(hasArg(allowMagicalThinking)) allowMagicalThinking=match.call(expand.dots=TRUE)$allowMagicalThinking
       else allowMagicalThinking = FALSE
       
-      freq = periodicity(mktdata)
+      # Get cached frequency, if it's available
+      if(hasArg(periodicity))
+          freq <- eval(match.call(expand.dots=TRUE)$periodicity, envir=parent.frame())
+      else
+          freq <- periodicity(mktdata)
       #switch on frequency
       switch(orderType,
              market = {

Modified: pkg/quantstrat/R/rules.R
===================================================================
--- pkg/quantstrat/R/rules.R	2013-10-30 16:47:23 UTC (rev 1557)
+++ pkg/quantstrat/R/rules.R	2013-10-30 22:29:47 UTC (rev 1558)
@@ -409,7 +409,7 @@
                         }    
                         if (is.na(col)) stop("no price discernable for stoplimit in applyRules")                            
                     } 
-                    cross<-sigThreshold(label='tmpstop',column=col,threshold=tmpprice,relationship=relationship)
+                    cross<-sigThreshold(label='tmpstop',data=mktdata,column=col,threshold=tmpprice,relationship=relationship)
                     cross <- cross[timespan][-1]  # don't look for crosses on curIndex
                     if(any(cross)){
                         # find first index that would cross after this index
@@ -458,7 +458,7 @@
                         if (is.na(col)) stop("no price discernable for limit in applyRules")
                     }
                     # use sigThreshold
-                    cross<-sigThreshold(label='tmplimit',column=col,threshold=tmpprice,relationship=relationship)
+                    cross<-sigThreshold(label='tmplimit',data=mktdata,column=col,threshold=tmpprice,relationship=relationship)
                     cross <- cross[timespan][-1]  # don't look for crosses on curIndex
                     if(any(cross)){
                         # find first index that would cross after this index
@@ -561,10 +561,9 @@
         
     hold=FALSE
     holdtill=first(time(Dates))-1 # TODO FIXME make holdtill default more robust?
-    
     mktinstr<-getInstrument(symbol)
-    
     curIndex<-1
+    freq <- periodicity(mktdata)  # run once and pass to ruleOrderProc
     
     while(curIndex){
         timestamp=Dates[curIndex]    
@@ -607,7 +606,7 @@
                             else
                                 timestamp=NULL
 
-                            closed.orders <- ruleOrderProc(portfolio=portfolio, symbol=symbol, mktdata=mktdata, timestamp=timestamp, ...)
+                            closed.orders <- ruleOrderProc(portfolio=portfolio, symbol=symbol, mktdata=mktdata, timestamp=timestamp, periodicity=freq, ...)
                         }
                     },
                     chain = {
@@ -702,7 +701,7 @@
         if(!is.null(rule$arguments$prefer)) .formals$prefer = rule$arguments$prefer
         
         # evaluate rule in applyRules' environment
-        tmp_val <- do.call(rule$name, .formals, envir=parent.frame(2))
+        tmp_val <- do.call(rule$name, .formals, envir=parent.frame(1))
                 
 #            print(paste('tmp_val ==', tmp_val))
     } #end rules loop



More information about the Blotter-commits mailing list