[Blotter-commits] r1399 - in pkg/quantstrat: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Feb 21 10:57:58 CET 2013


Author: opentrades
Date: 2013-02-21 10:57:58 +0100 (Thu, 21 Feb 2013)
New Revision: 1399

Modified:
   pkg/quantstrat/R/tradeGraphs.R
   pkg/quantstrat/man/tradeGraphs.Rd
Log:
- rewrite of tradeGraphs.R by Chinmay Patil
- renamed fixed.params to params.filter and implemented as regular expression by Chinmay Patil



Modified: pkg/quantstrat/R/tradeGraphs.R
===================================================================
--- pkg/quantstrat/R/tradeGraphs.R	2013-02-19 20:40:06 UTC (rev 1398)
+++ pkg/quantstrat/R/tradeGraphs.R	2013-02-21 09:57:58 UTC (rev 1399)
@@ -2,7 +2,7 @@
 #'
 #' @param stats a data frame generated by tradeStats()
 #' @param free.params a vector of length 2, containing the column names for the data to use on the x and z axes
-#' @param fixed.params - not yet implemented
+#' @param params.filter - a regular expression to reduce dimensions by filtering on certain columns
 #' @param statistics a vector containing the column names to produce graphs for
 #' @param title an optional title to be printed above each graph
 #' @return invisible -- called for side-effect
@@ -11,70 +11,51 @@
 #' tradeGraphs (
 #'      stats = stats,
 #'      free.params = c("Param.indicator.1.nFast", "Param.indicator.2.nSlow"),
-#'      fixed.params = NULL,
+#'	params.filter = "Param.indicator.2.nSlow < 40 & Param.indicator.1.nFast > 5"
 #'      statistics = c("Net.Trading.PL", "maxDrawdown", "Avg.Trade.PL", "Num.Trades")
 #'      title = 'Luxor'
 #' )
 #' }
+#' @author Jan Humme, rewritten by Chinmay Patil
 #' @export
 
-tradeGraphs <- function(stats, free.params, fixed.params = NULL, statistics, title = NULL)
+tradeGraphs <- function(stats, free.params, params.filter = NULL, statistics, title = NULL)
 {
-        # TODO: implement fixed.params
-        # TODO: fix axes to use non scientific notation
-        # TODO: fix use of full rainbow for small fractions (eg. Profit.Factor, now only uses red)
+    # TODO: fix axes to use non scientific notation
+    # TODO: fix use of full rainbow for small fractions (eg. Profit.Factor, now only uses red)
 
-	if(!require(rgl, quietly=TRUE))	stop('The "rgl" package is required to use this function')
+    if(!require(reshape2, quietly=TRUE))    stop('The "reshape2" package is required to use this function')
+   
+    if(missing(stats))      stop('stats undefined')
+   
+    if(missing(free.params))        stop('free.params undefined')
+    if(length(free.params) != 2)    stop('free.params must be a vector of length 2')
+       
+    if(missing(statistics))         stop('must specify at least one statistics column to draw graph')
+	   
+	   
+    var1 <- free.params[1]
+    var2 <- free.params[2]
+	       
+	       
+    for(statistic in statistics) {
+			       
+        var3 <- statistic
+	           
+        if (length(params.filter) == 0 ) {
+            data <- stats[,c(var1, var2, var3)]
+        } else {
+            data <- subset(stats, eval(parse(text=params.filter)), select =  c(var1, var2, var3))
+        }
+        x <- recast(data, as.formula(paste0(var1, " ~ ", var2)) , id.var=c(var1,var2), measure.var=c(var3))$labels[[1]][,1]
+        y <-  recast(data, as.formula(paste0(var1, " ~ ", var2)) , id.var=c(var1,var2), measure.var=c(var3))$labels[[2]][,1]
+        z <-  recast(data, as.formula(paste0(var1, " ~ ", var2)) , id.var=c(var1,var2), measure.var=c(var3))$data
 
-        if(missing(stats))      stop('stats undefined')
+        col <- heat.colors(length(z))[rank(z)]
 
-        if(missing(free.params))        stop('free.params undefined')
-        if(length(free.params) != 2)    stop('free.params must be a vector of length 2')
-
-        if(missing(statistics))         stop('must specify at least one statistics column to draw graph')
-
-        # derive sizes from free parameters
-
-        range.free.param.1 = range(stats[[free.params[1]]])
-        min.free.param.1 = range.free.param.1[1]
-        max.free.param.1 = range.free.param.1[2]
-
-        range.free.param.2 = range(stats[[free.params[2]]])
-        min.free.param.2 = range.free.param.2[1]
-        max.free.param.2 = range.free.param.2[2]
-
-        nrows = range.free.param.1[2] - range.free.param.1[1] + 1
-        ncols = range.free.param.2[2] - range.free.param.2[1] + 1
-
-        # draw graph for each statistic
-
-        for(statistic in statistics)
-        {
-                # fill data matrix
-
-                graph.data <- matrix(NA, nrows, ncols)
-
-                graph.data[cbind(
-				stats[[free.params[1]]] - min.free.param.1 + 1,
-				stats[[free.params[2]]] - min.free.param.2 + 1)] <- stats[[statistic]]
-
-                # set up color vector
-
-                range.statistic = range(stats[[statistic]])
-                span.statistic = range.statistic[2] - range.statistic[1] + 1
-
-                colors <- rainbow(span.statistic)[graph.data - range.statistic[1] + 1]
-
-                # draw graph
-
-                rgl.open()
-                rgl.surface(x=min.free.param.1:max.free.param.1, z=min.free.param.2:max.free.param.2, y=graph.data, color=colors)
-                rgl.planes(a=1, alpha=0.7)
-
-                aspect3d(1,1,1)
-                axes3d()
-                title3d(title, NULL, free.params[1], statistic, free.params[2])
-        }
+        open3d()
+        persp3d(x,y,z, color=col, xlab=var1, ylab=var2, zlab=var3, main = title)
+    }
 }
 
 ###############################################################################

Modified: pkg/quantstrat/man/tradeGraphs.Rd
===================================================================
--- pkg/quantstrat/man/tradeGraphs.Rd	2013-02-19 20:40:06 UTC (rev 1398)
+++ pkg/quantstrat/man/tradeGraphs.Rd	2013-02-21 09:57:58 UTC (rev 1399)
@@ -2,7 +2,7 @@
 \alias{tradeGraphs}
 \title{Draw 3D graphs from tradeStats results using rgl}
 \usage{
-  tradeGraphs(stats, free.params, fixed.params = NULL,
+  tradeGraphs(stats, free.params, params.filter = NULL,
     statistics, title = NULL)
 }
 \arguments{
@@ -11,7 +11,8 @@
   \item{free.params}{a vector of length 2, containing the
   column names for the data to use on the x and z axes}
 
-  \item{fixed.params}{- not yet implemented}
+  \item{params.filter}{- a regular expression to reduce
+  dimensions by filtering on certain columns}
 
   \item{statistics}{a vector containing the column names to
   produce graphs for}
@@ -30,10 +31,13 @@
 tradeGraphs (
      stats = stats,
      free.params = c("Param.indicator.1.nFast", "Param.indicator.2.nSlow"),
-     fixed.params = NULL,
+	params.filter = "Param.indicator.2.nSlow < 40 & Param.indicator.1.nFast > 5"
      statistics = c("Net.Trading.PL", "maxDrawdown", "Avg.Trade.PL", "Num.Trades")
      title = 'Luxor'
 )
 }
 }
+\author{
+  Jan Humme, rewritten by Chinmay Patil
+}
 



More information about the Blotter-commits mailing list