[Blotter-commits] r1111 - in pkg/quantstrat: . R demo man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jul 31 21:03:41 CEST 2012


Author: opentrades
Date: 2012-07-31 21:03:41 +0200 (Tue, 31 Jul 2012)
New Revision: 1111

Added:
   pkg/quantstrat/R/tradeGraphs.R
   pkg/quantstrat/demo/tradeGraphs.R
   pkg/quantstrat/man/tradeGraphs.Rd
Removed:
   pkg/quantstrat/demo/luxorGraphs.R
Modified:
   pkg/quantstrat/DESCRIPTION
   pkg/quantstrat/NAMESPACE
   pkg/quantstrat/man/applyParameter.Rd
Log:
added tradeGraphs() function

Modified: pkg/quantstrat/DESCRIPTION
===================================================================
--- pkg/quantstrat/DESCRIPTION	2012-07-30 11:00:47 UTC (rev 1110)
+++ pkg/quantstrat/DESCRIPTION	2012-07-31 19:03:41 UTC (rev 1111)
@@ -9,7 +9,7 @@
     xts(>= 0.8-2),TTR(>= 0.2),blotter(>= 0.7.2),
     FinancialInstrument(>= 0.12.5)
 Suggests:
-    PerformanceAnalytics,PortfolioAnalytics
+    PerformanceAnalytics,PortfolioAnalytics,rgl
 Maintainer: Brian G. Peterson <brian at braverock.com>
 Description: Specify, build, and back-test quantitative
     financial trading and portfolio strategies
@@ -29,3 +29,4 @@
     'osFUNs.R'
     'ruleOrderProc.R'
     'ruleSignal.R'
+    'tradeGraphs.R'

Modified: pkg/quantstrat/NAMESPACE
===================================================================
--- pkg/quantstrat/NAMESPACE	2012-07-30 11:00:47 UTC (rev 1110)
+++ pkg/quantstrat/NAMESPACE	2012-07-31 19:03:41 UTC (rev 1111)
@@ -1,9 +1,9 @@
 export(add.indicator)
 export(add.init)
+export(add.rule)
+export(add.signal)
 export(addOrder)
 export(addPosLimit)
-export(add.rule)
-export(add.signal)
 export(applyIndicators)
 export(applyParameter)
 export(applyRules)
@@ -31,5 +31,6 @@
 export(sigPeak)
 export(sigThreshold)
 export(strategy)
+export(tradeGraphs)
 export(updateOrders)
 export(updateStrategy)

Added: pkg/quantstrat/R/tradeGraphs.R
===================================================================
--- pkg/quantstrat/R/tradeGraphs.R	                        (rev 0)
+++ pkg/quantstrat/R/tradeGraphs.R	2012-07-31 19:03:41 UTC (rev 1111)
@@ -0,0 +1,95 @@
+#' Draw 3D graphs from tradeStats results using rgl
+#'
+#' @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 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
+#' @examples
+#' \dontrun{
+#' tradeGraphs (
+#'      stats = stats,
+#'      free.params = c("Param.indicator.1.nFast", "Param.indicator.2.nSlow"),
+#'      fixed.params = NULL,
+#'      statistics = c("Net.Trading.PL", "maxDrawdown", "Avg.Trade.PL", "Num.Trades")
+#'      title = 'Luxor'
+#' )
+#' }
+#' @export
+
+tradeGraphs <- function(stats, free.params, fixed.params = 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)
+
+        require(rgl)
+
+        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')
+
+        # 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)
+
+                for(i in 1:nrow(stats[free.params[1]]))
+                {
+                        free.param.1 = stats[[free.params[1]]][i]
+                        free.param.2 = stats[[free.params[2]]][i]
+
+                        graph.data[free.param.1 - min.free.param.1 + 1, free.param.2 - min.free.param.2 + 1] = stats[[statistic]][i]
+                }
+
+                # 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])
+        }
+}
+
+###############################################################################
+# R (http://r-project.org/) Quantitative Strategy Model Framework
+#
+# Copyright (c) 2009-2011
+# Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, Garrett See, and Joshua Ulrich 
+#
+# This library is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: wrapup.R 849 2011-11-16 17:47:25Z gsee $
+#
+###############################################################################

Deleted: pkg/quantstrat/demo/luxorGraphs.R
===================================================================
--- pkg/quantstrat/demo/luxorGraphs.R	2012-07-30 11:00:47 UTC (rev 1110)
+++ pkg/quantstrat/demo/luxorGraphs.R	2012-07-31 19:03:41 UTC (rev 1111)
@@ -1,89 +0,0 @@
-#!/usr/bin/Rscript --vanilla
-
-require(rgl)
-
-load('../data/luxor.250.RData')
-
-# parameters
-
-range.Fast = range(stats$Param.indicator.1.nFast)
-min.Fast = range.Fast[1]
-max.Fast = range.Fast[2]
-
-range.Slow = range(stats$Param.indicator.2.nSlow)
-min.Slow = range.Slow[1]
-max.Slow = range.Slow[2]
-
-nrows = range.Fast[2] - range.Fast[1] + 1
-ncols = range.Slow[2] - range.Slow[1] + 1
-
-# results
-
-range.Net.Trading.PL = range(stats$Net.Trading.PL)
-min.Net.Trading.PL = range.Net.Trading.PL[1]
-max.Net.Trading.PL = range.Net.Trading.PL[2]
-length.Net.Trading.PL = range.Net.Trading.PL[2] - range.Net.Trading.PL[1] + 1
-
-range.Max.Drawdown = range(stats$maxDrawdown)
-min.Max.Drawdown = range.Max.Drawdown[1]
-max.Max.Drawdown = range.Max.Drawdown[2]
-length.Max.Drawdown = range.Max.Drawdown[2] - range.Max.Drawdown[1] + 1
-
-range.Avg.Trade.PL = range(stats$Avg.Trade.PL)
-min.Avg.Trade.PL = range.Avg.Trade.PL[1]
-max.Avg.Trade.PL = range.Avg.Trade.PL[2]
-length.Avg.Trade.PL = range.Avg.Trade.PL[2] - range.Avg.Trade.PL[1] + 1
-
-range.Num.Trades = range(stats$Num.Trades)
-min.Num.Trades = range.Num.Trades[1]
-max.Num.Trades = range.Num.Trades[2]
-length.Num.Trades = range.Num.Trades[2] - range.Num.Trades[1] + 1
-
-Net.Trading.PL = matrix(NA, nrows, ncols)
-Max.Drawdown = matrix(NA, nrows, ncols)
-Avg.Trade.PL = matrix(NA, nrows, ncols)
-Num.Trades = matrix(NA, nrows, ncols)
-
-for(i in 1:length(stats$Net.Trading.PL))
-{
-	fast = stats$Param.indicator.1.nFast[i]
-	slow = stats$Param.indicator.2.nSlow[i]
-
-	Net.Trading.PL	[fast-min.Fast+1, slow-min.Slow+1] = stats$Net.Trading.PL[[i]]
-	Max.Drawdown	[fast-min.Fast+1, slow-min.Slow+1] = stats$maxDrawdown[[i]]
-	Avg.Trade.PL	[fast-min.Fast+1, slow-min.Slow+1] = stats$Avg.Trade.PL[[i]]
-	Num.Trades	[fast-min.Fast+1, slow-min.Slow+1] = stats$Num.Trades[[i]]
-}
-
-# make graphs
-
-colors <- rainbow(length.Net.Trading.PL, start=0, end=6/6)[Net.Trading.PL - min.Net.Trading.PL + 1]
-rgl.open()
-rgl.surface(x=min.Fast:max.Fast, z=min.Slow:max.Slow, y=Net.Trading.PL, color=colors)
-rgl.planes(a=1, alpha=0.7)
-aspect3d(1,1,1)
-axes3d()
-title3d('luxor',NULL,'FastSMA','Net.Trading.PL','SlowSMA')
-
-colors <- rainbow(length.Max.Drawdown, start=0, end=4/6)[Max.Drawdown - min.Max.Drawdown + 1]
-rgl.open()
-rgl.surface(x=min.Fast:max.Fast, z=min.Slow:max.Slow, y=Max.Drawdown, color=colors)
-aspect3d(1,1,1)
-axes3d()
-title3d('luxor',NULL,'FastSMA','Max.Drawdown','SlowSMA')
-
-colors <- rainbow(length.Avg.Trade.PL, start=0, end=6/6)[Avg.Trade.PL - min.Avg.Trade.PL + 1]
-rgl.open()
-rgl.surface(x=min.Fast:max.Fast, z=min.Slow:max.Slow, y=Avg.Trade.PL, color=colors)
-rgl.planes(a=1, alpha=0.7)
-aspect3d(1,1,1)
-axes3d()
-title3d('luxor',NULL,'FastSMA','Avg.Trade.PL','SlowSMA')
-
-colors <- rainbow(length.Num.Trades, start=0, end=3/6)[Num.Trades - min.Num.Trades + 1]
-rgl.open()
-rgl.surface(x=min.Fast:max.Fast, z=min.Slow:max.Slow, y=Num.Trades, color=colors)
-aspect3d(1,1,1)
-axes3d()
-title3d('luxor',NULL,'FastSMA','Num.Trades','SlowSMA')
-

Copied: pkg/quantstrat/demo/tradeGraphs.R (from rev 1110, pkg/quantstrat/demo/luxorGraphs.R)
===================================================================
--- pkg/quantstrat/demo/tradeGraphs.R	                        (rev 0)
+++ pkg/quantstrat/demo/tradeGraphs.R	2012-07-31 19:03:41 UTC (rev 1111)
@@ -0,0 +1,91 @@
+#!/usr/bin/Rscript --vanilla
+
+require(rgl)
+
+load('../data/luxor.250.RData')
+
+# parameters
+
+range.Fast = range(stats$Param.indicator.1.nFast)
+min.Fast = range.Fast[1]
+max.Fast = range.Fast[2]
+
+range.Slow = range(stats$Param.indicator.2.nSlow)
+min.Slow = range.Slow[1]
+max.Slow = range.Slow[2]
+
+nrows = range.Fast[2] - range.Fast[1] + 1
+ncols = range.Slow[2] - range.Slow[1] + 1
+
+# results
+
+range.Net.Trading.PL = range(stats$Net.Trading.PL)
+min.Net.Trading.PL = range.Net.Trading.PL[1]
+max.Net.Trading.PL = range.Net.Trading.PL[2]
+length.Net.Trading.PL = range.Net.Trading.PL[2] - range.Net.Trading.PL[1] + 1
+
+range.Max.Drawdown = range(stats$maxDrawdown)
+min.Max.Drawdown = range.Max.Drawdown[1]
+max.Max.Drawdown = range.Max.Drawdown[2]
+length.Max.Drawdown = range.Max.Drawdown[2] - range.Max.Drawdown[1] + 1
+
+range.Avg.Trade.PL = range(stats$Avg.Trade.PL)
+min.Avg.Trade.PL = range.Avg.Trade.PL[1]
+max.Avg.Trade.PL = range.Avg.Trade.PL[2]
+length.Avg.Trade.PL = range.Avg.Trade.PL[2] - range.Avg.Trade.PL[1] + 1
+
+range.Num.Trades = range(stats$Num.Trades)
+min.Num.Trades = range.Num.Trades[1]
+max.Num.Trades = range.Num.Trades[2]
+length.Num.Trades = range.Num.Trades[2] - range.Num.Trades[1] + 1
+
+Net.Trading.PL = matrix(NA, nrows, ncols)
+Max.Drawdown = matrix(NA, nrows, ncols)
+Avg.Trade.PL = matrix(NA, nrows, ncols)
+Num.Trades = matrix(NA, nrows, ncols)
+
+for(i in 1:length(stats$Net.Trading.PL))
+{
+	fast = stats$Param.indicator.1.nFast[i]
+	slow = stats$Param.indicator.2.nSlow[i]
+
+	Net.Trading.PL	[fast-min.Fast+1, slow-min.Slow+1] = stats$Net.Trading.PL[[i]]
+	Max.Drawdown	[fast-min.Fast+1, slow-min.Slow+1] = stats$maxDrawdown[[i]]
+	Avg.Trade.PL	[fast-min.Fast+1, slow-min.Slow+1] = stats$Avg.Trade.PL[[i]]
+	Num.Trades	[fast-min.Fast+1, slow-min.Slow+1] = stats$Num.Trades[[i]]
+}
+
+# make graphs
+
+options(scipen=99)
+
+colors <- rainbow(length.Net.Trading.PL, start=0, end=6/6)[Net.Trading.PL - min.Net.Trading.PL + 1]
+rgl.open()
+rgl.surface(x=min.Fast:max.Fast, z=min.Slow:max.Slow, y=Net.Trading.PL, color=colors)
+rgl.planes(a=1, alpha=0.7)
+aspect3d(1,1,1)
+axes3d()
+title3d('luxor',NULL,'FastSMA','Net.Trading.PL','SlowSMA')
+
+colors <- rainbow(length.Max.Drawdown, start=0, end=4/6)[Max.Drawdown - min.Max.Drawdown + 1]
+rgl.open()
+rgl.surface(x=min.Fast:max.Fast, z=min.Slow:max.Slow, y=Max.Drawdown, color=colors)
+aspect3d(1,1,1)
+axes3d()
+title3d('luxor',NULL,'FastSMA','Max.Drawdown','SlowSMA')
+
+colors <- rainbow(length.Avg.Trade.PL, start=0, end=6/6)[Avg.Trade.PL - min.Avg.Trade.PL + 1]
+rgl.open()
+rgl.surface(x=min.Fast:max.Fast, z=min.Slow:max.Slow, y=Avg.Trade.PL, color=colors)
+rgl.planes(a=1, alpha=0.7)
+aspect3d(1,1,1)
+axes3d()
+title3d('luxor',NULL,'FastSMA','Avg.Trade.PL','SlowSMA')
+
+colors <- rainbow(length.Num.Trades, start=0, end=3/6)[Num.Trades - min.Num.Trades + 1]
+rgl.open()
+rgl.surface(x=min.Fast:max.Fast, z=min.Slow:max.Slow, y=Num.Trades, color=colors)
+aspect3d(1,1,1)
+axes3d()
+title3d('luxor',NULL,'FastSMA','Num.Trades','SlowSMA')
+

Modified: pkg/quantstrat/man/applyParameter.Rd
===================================================================
--- pkg/quantstrat/man/applyParameter.Rd	2012-07-30 11:00:47 UTC (rev 1110)
+++ pkg/quantstrat/man/applyParameter.Rd	2012-07-31 19:03:41 UTC (rev 1111)
@@ -3,7 +3,8 @@
 \title{Generate parameter sets for a specific strategy, test the strategy on each set of parameters, output result package.}
 \usage{
   applyParameter(strategy, portfolios, parameterPool,
-    parameterConstraints, method, sampleSize)
+    parameterConstraints, method, sampleSize,
+    verbose = FALSE, ...)
 }
 \arguments{
   \item{strategy}{The strategy to test paramters to.}
@@ -25,6 +26,11 @@
 
   \item{sampleSize}{Used when method=='random', specify how
   many parameter sets to generate and run test of.}
+
+  \item{verbose}{if verbose TRUE or 1+, will print a lot of
+  debug info, default FALSE}
+
+  \item{\dots}{any other passthru parameters}
 }
 \value{
   In the returned result pack as a list of object:

Added: pkg/quantstrat/man/tradeGraphs.Rd
===================================================================
--- pkg/quantstrat/man/tradeGraphs.Rd	                        (rev 0)
+++ pkg/quantstrat/man/tradeGraphs.Rd	2012-07-31 19:03:41 UTC (rev 1111)
@@ -0,0 +1,39 @@
+\name{tradeGraphs}
+\alias{tradeGraphs}
+\title{Draw 3D graphs from tradeStats results using rgl}
+\usage{
+  tradeGraphs(stats, free.params, fixed.params = NULL,
+    statistics, title = NULL)
+}
+\arguments{
+  \item{stats}{a data frame generated by tradeStats()}
+
+  \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{statistics}{a vector containing the column names to
+  produce graphs for}
+
+  \item{title}{an optional title to be printed above each
+  graph}
+}
+\value{
+  invisible -- called for side-effect
+}
+\description{
+  Draw 3D graphs from tradeStats results using rgl
+}
+\examples{
+\dontrun{
+tradeGraphs (
+     stats = stats,
+     free.params = c("Param.indicator.1.nFast", "Param.indicator.2.nSlow"),
+     fixed.params = NULL,
+     statistics = c("Net.Trading.PL", "maxDrawdown", "Avg.Trade.PL", "Num.Trades")
+     title = 'Luxor'
+)
+}
+}
+



More information about the Blotter-commits mailing list