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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun May 5 00:39:40 CEST 2013


Author: opentrades
Date: 2013-05-05 00:39:39 +0200 (Sun, 05 May 2013)
New Revision: 1452

Modified:
   pkg/quantstrat/R/orders.R
   pkg/quantstrat/R/rules.R
Log:
- introduced enable.rule() for easy rule enabling / disabling
- fixed timestamp in orderbook to print 00:00:00 when midnight



Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R	2013-04-29 11:48:30 UTC (rev 1451)
+++ pkg/quantstrat/R/orders.R	2013-05-04 22:39:39 UTC (rev 1452)
@@ -411,7 +411,7 @@
                          orderset=orderset,
                          oldstatus="open", 
                          newstatus="replaced", 
-                         statustimestamp=timestamp)
+                         statustimestamp=format(timestamp, "%Y-%m-%d %H:%M:%S"))
         }
         # get order book
         if(!is.null(orders))

Modified: pkg/quantstrat/R/rules.R
===================================================================
--- pkg/quantstrat/R/rules.R	2013-04-29 11:48:30 UTC (rev 1451)
+++ pkg/quantstrat/R/rules.R	2013-05-04 22:39:39 UTC (rev 1452)
@@ -127,6 +127,36 @@
     strategy$name
 }
 
+#' enable a rule in the strategy
+#'
+#' function to make it easy to enable (or disable) a specific rule in a strategy
+#'
+#' @param strategy an object of type 'strategy' which contains the rule
+#' @param type one of "risk","order","rebalance","exit","enter","chain"
+#' @param label the label for the rule; grep will be used to match, so multiple rules may be enabled (disabled) as a result
+#' @param enabled TRUE/FALSE whether the rule is enabled for use in applying the strategy, default TRUE
+#' @param store TRUE/FALSE whether to store the updated strategy in the .strategy environment, or return it.  default FALSE
+#' @seealso \code{\link{add.rule}} \code{\link{applyStrategy}} 
+#' @export
+
+enable.rule <- function(strategy, type=c(NULL,"risk","order","rebalance","exit","enter","chain"), label, enabled=TRUE, store=FALSE)
+{
+    if (!is.strategy(strategy)) {
+        strategy<-try(getStrategy(strategy))
+        if(inherits(strategy,"try-error"))
+            stop ("You must supply an object or the name of an object of type 'strategy'.")
+        store=TRUE
+    } 
+
+    for(i in 1:length(strategy$rules[[type]]))
+        if(grepl(label, strategy$rules[[type]][[i]]$label))
+            strategy$rules[[type]][[i]]$enabled <- enabled
+
+    if (store) assign(strategy$name,strategy,envir=as.environment(.strategy))
+    else return(strategy)
+    strategy$name
+}
+
 #' apply the rules in the strategy to arbitrary market data 
 #' 
 #' In typical usage, this function will be called via \code{\link{applyStrategy}}.  



More information about the Blotter-commits mailing list