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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Oct 11 21:57:02 CEST 2012


Author: opentrades
Date: 2012-10-11 21:57:02 +0200 (Thu, 11 Oct 2012)
New Revision: 1217

Modified:
   pkg/quantstrat/R/parameters.R
   pkg/quantstrat/demo/bbandParameters.R
   pkg/quantstrat/demo/bbands.R
   pkg/quantstrat/demo/luxor.3.Parameters.R
   pkg/quantstrat/demo/luxor.4.Timespans.R
   pkg/quantstrat/man/paramConstraint.Rd
   pkg/quantstrat/man/setParameterDistribution.Rd
Log:
- setParameterDistribution() now uses component label instead of index numbers
- fixed up demos to use new syntax



Modified: pkg/quantstrat/R/parameters.R
===================================================================
--- pkg/quantstrat/R/parameters.R	2012-10-09 23:58:08 UTC (rev 1216)
+++ pkg/quantstrat/R/parameters.R	2012-10-11 19:57:02 UTC (rev 1217)
@@ -91,13 +91,50 @@
 #
 ###############################################################################
 
+label2index <- function(strategy, type, label)
+{
+    switch(type,
+        indicator = {
+            specs <- strategy$indicators
+        },
+        signal = {
+            specs <- strategy$signals
+        },
+        enter = {
+            specs <- strategy$rules$enter
+        },
+        exit = {
+            specs <- strategy$rules$exit
+        },
+        order = {
+            specs <- strategy$rules$order
+        },
+        chain = {
+            specs <- strategy$rules$chain
+        },
+        {
+            stop(paste(type, ': no such type in strategy', strategy))
+        }
+    )
 
+    i = 1
+    for(spec in specs)
+    {
+        if(spec$label == label)
+            return(i)
+        i <- i + 1
+    }
+    stop(paste(label, ': no such label with type', type, 'in strategy', strategy))
+
+    print(paste('====== index ==', i))
+}
+
 # Functions for parameter generating and testing.
 # 
 # Author: Yu Chen
 ###############################################################################
 
-#retreave the needed parameters and existing values after add*
+#retrieve the needed parameters and existing values after add*
 
 #' Extract the parameter structure from a strategy object.
 #' 
@@ -215,7 +252,6 @@
 #' 
 #' Each call to the function will set/update the distribution of ONE parameter in the 'parameter distribution object' that is associated with a specific strategy.  
 #' 
-#' Upon calling the function, the user must know the parameter structure of the strategy, function getParameterTable can be used to layout the parameter structure of a certain strategy.
 #' Parameter distribution object for one strategy usually won't work for another strategy, because different strategies has different parameter structure.
 #' Type of the parameter and the sequence in that type is needed to specify the exact parameter in THAT STRATEGY.
 #' 
@@ -225,17 +261,18 @@
 #' @examples 
 #' \dontrun{
 #' #(For complete demo see parameterTestMACD.R)
-#' tPD2<-setParameterDistribution(tPD2,'indicator',indexnum=1,
+#' tPD2<-setParameterDistribution(tPD2,strat,'indicator',component='FastSMA',
 #'      distribution=list(nFast=(10:30)),label='nFast')
-#' tPD2<-setParameterDistribution(tPD2,'indicator',indexnum=1,
+#' tPD2<-setParameterDistribution(tPD2,strat,'indicator',component='SlowSMA',
 #'      distribution=list(nSlow=(20:40)),label='nSlow')
-#' tPD2<-setParameterDistribution(tPD2,'signal',indexnum=1,
+#' tPD2<-setParameterDistribution(tPD2,strat,'signal',component='go.long',
 #'      distribution=list(relationship=c('gt','gte')),label='sig1.gtgte')
 #' }
 #' 
-#' @param paramDist The object that store the parameter list, if this parameter is missing, or object does not exist, the function will return a new object.
+#' @param paramDist The object in which the parameter list is stored; if this parameter is missing, or object does not exist, the function will return a new object.
+#' @param strategy The strategy object
 #' @param type A character string that specifies the type of the parameter, it takes the value in one of 'indicator', 'signal', 'enter', 'exit', 'order', 'chain'.
-#' @param indexnum Tells the sequence within the type, (for example: type = 'signal', indexnum =2 tells the function to update the 2nd signal in the strategy)  
+#' @param component The label of the strategy component (indicator/signal/rule) that contains the parameter
 #' @param distribution Distribution of the parameter, can be any function that returns a vector of value. See detail. (A numerical example: 1:10 or sample(1:20,6)
 #' @param weight The weight of each value in the distribution, if missing, the default value of all equal weights will be taken.
 #' @param label A string label to apply to the parameter distribution
@@ -243,20 +280,33 @@
 #' @return The returned object is a structure contains the distribution of parameters, if the input argument 'paramDist' is provided, the function update the input paramDist object and return the updated one. When specify the distribution of several parameters, usually the first returned object is passed to the next several call of the function as input argument 'paramDist'. See example. 
 #' @author Yu Chen
 #' @export
-setParameterDistribution<-function(paramDist=NULL,type=NULL,indexnum=0,distribution=NULL,weight,label,psindex=NULL) #All is needed, set to illegal values
+setParameterDistribution<-function(paramDist=NULL, strategy, type, component, distribution=NULL, weight, label, psindex=NULL) #All is needed,  set to illegal values
 {
+    missing.msg <- ': missing in call to setParameterDistribution'
+
+    if(!hasArg(strategy))
+        stop(paste('strategy', missing.msg))
+    if(!hasArg(type))
+        stop(paste('type', missing.msg))
+    if(!hasArg(component))
+        stop(paste('component', missing.msg))
+    if(!hasArg(distribution))
+        stop(paste('distribution', missing.msg))
+
+    if(!type %in% c("indicator","signal","enter","exit","order","chain"))
+        stop("Type must be a string in: indicator, signal, enter, exit, order", "chain")
+
+    if(!is.list(distribution) || length(distribution) != 1)
+        stop("distribution must be passed as a named list of length 1")
+
     if(!hasArg(paramDist) || !exists(as.character(substitute(paramDist))))
     {
         paramDist<-list()
         print('Object for parameter distribution initialized...')
     }
-    
-    if(!is.list(distribution) || length(distribution) != 1)
-        stop("distribution must be passed as a named list of length 1")
 
-    if(!type %in% c("indicator","signal","enter","exit","order","chain"))
-        stop("Type must be a string in: indicator, signal, enter, exit, order","chain")
-    
+    indexnum <- label2index(strategy, type, component)
+
     tmp_paramDist<-list()
     tmp_paramDist$type<-type
     tmp_paramDist$indexnum<-indexnum
@@ -270,17 +320,16 @@
     {
         tmp_paramDist$label<-label
     }
-    
-    
+
     if(!hasArg(weight)) weight<-sample(1/length(distribution[[1]]),length(distribution[[1]]),replace=TRUE)
-    
+
     tmp_paramDist$weight<-weight
-    
+
     if(!hasArg(psindex) || (hasArg(psindex) && is.null(psindex)))
         psindex = length(paramDist)+1
 
     #class(tmp_paramDist)<-'parameter_distribution'
-        
+
     #TODO put an check to see if the type/indexnum exist already.
     paramDist[[psindex]]<-tmp_paramDist
 
@@ -752,18 +801,22 @@
 #' @export
 setParameterConstraint<-function(paramConstraintObj=list(),constraintLabel,paramList,relationship)
 {
-    if(!hasArg(paramConstraintObj)||!exists(as.character(substitute(paramConstraintObj))))
+    if(!hasArg(paramConstraintObj) || !exists(as.character(substitute(paramConstraintObj))))
     {
         paramConstraintObj<-list()
         print('Parameter constraint object initialized...')     
     }
     else
     {
-        if (!is.list(paramConstraintObj)|length(paramConstraintObj)!=1)
+        if(!is.list(paramConstraintObj) || length(paramConstraintObj) != 1)
             stop("Parameter constrain object must be passed as a named list of length 1")
     }
     
-    if (missing(constraintLabel)) {constraintLabel<-paste("parameterConstraint",length(paramConstraintObj)+1)}
+    if(missing(constraintLabel))
+    {
+        constraintLabel<-paste("parameterConstraint",length(paramConstraintObj)+1)
+    }
+
     tmp_PC<-list()
     tmp_PC$constraintLabel<-constraintLabel
     #names(paramList)<-"label"

Modified: pkg/quantstrat/demo/bbandParameters.R
===================================================================
--- pkg/quantstrat/demo/bbandParameters.R	2012-10-09 23:58:08 UTC (rev 1216)
+++ pkg/quantstrat/demo/bbandParameters.R	2012-10-11 19:57:02 UTC (rev 1217)
@@ -16,7 +16,8 @@
 # or this will run in single threaded mode
 
 #please run bbands demo before all these...
-paramStructure<-getParameterTable(stratBBands)
+#paramStructure<-getParameterTable(stratBBands)
+#print(paramStructure)
 
 #tPD<-setParameterDistribution() need no more for initial object. 
 
@@ -38,12 +39,14 @@
 #undebug(applyParameter)
 
 
-# Just provide leagal values and use random sampling.
-tPD<-setParameterDistribution(tPD,'indicator',indexnum=1,distribution=list(sd=(1:3)),weight=c(.25,.25,.5),label='sd')
-tPD<-setParameterDistribution(tPD,'signal',indexnum=2,distribution=list(relationship=c("lt","lte")),label='rel')
-#tPD<-setParameterDistribution(tPD,'signal',indexnum=2,distribution=list(relationship=c("lte")))
-tPD<-setParameterDistribution(tPD,'indicator',indexnum=1,distribution=list(n=20:30),label='n')
+tPD <- NULL
 
+# Just provide legal values and use random sampling.
+tPD<-setParameterDistribution(tPD, strategy=stratBBands, type='indicator', component='BBands', distribution=list(sd=(1:3)), weight=c(.25, .25, .5), label='sd')
+tPD<-setParameterDistribution(tPD, strategy=stratBBands, type='signal', component='Cl.lt.LowerBand', distribution=list(relationship=c("lt", "lte")), label='rel')
+#tPD<-setParameterDistribution(tPD,strategy=stratBBands,'signal',indexnum=2,distribution=list(relationship=c("lte")))
+tPD<-setParameterDistribution(tPD, strategy=stratBBands, type='indicator', component='BBands', distribution=list(n=20:30), label='n')
+
 #pConstr<-setParameterConstraint()
 pConstraint<-setParameterConstraint(constraintLabel='PC1',paramList=c('sd','n'),relationship='gt')
 
@@ -62,3 +65,4 @@
 #
 ###############################################################################
 
+print(testPackList$statsTable)

Modified: pkg/quantstrat/demo/bbands.R
===================================================================
--- pkg/quantstrat/demo/bbands.R	2012-10-09 23:58:08 UTC (rev 1216)
+++ pkg/quantstrat/demo/bbands.R	2012-10-11 19:57:02 UTC (rev 1217)
@@ -27,7 +27,7 @@
 stratBBands <- strategy("bbands")
 
 #one indicator
-stratBBands <- add.indicator(strategy = stratBBands, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'))
+stratBBands <- add.indicator(strategy = stratBBands, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'), label='BBands')
 
 
 #add signals:
@@ -77,3 +77,4 @@
 # $Id$
 #
 ###############################################################################
+

Modified: pkg/quantstrat/demo/luxor.3.Parameters.R
===================================================================
--- pkg/quantstrat/demo/luxor.3.Parameters.R	2012-10-09 23:58:08 UTC (rev 1216)
+++ pkg/quantstrat/demo/luxor.3.Parameters.R	2012-10-11 19:57:02 UTC (rev 1217)
@@ -14,17 +14,18 @@
 
 method='expand'
 #method='random'
-#.sampleSize=20
+#.sampleSize=8
 
 #.fastRange=(1:20)
 #.slowRange=(21:80)
 
-.fastRange=(1:5)
+.fastRange=(41:45)
 .slowRange=(41:45)
 
 ###############################################################################
 
 require(foreach,quietly=TRUE)
+require(doMC)
 # example parallel initialization for doParallel. this or doMC, or doRedis are 
 # most probably preferable to doSMP
 #require(doParallel)
@@ -38,11 +39,12 @@
 
 s<-getStrategy('luxor')
 
-parameterTable<-getParameterTable(s)
+#parameterTable<-getParameterTable(s)
 #parameterTable
+#stop()
 
-tPD2<-setParameterDistribution(type = 'indicator', indexnum = 1, distribution = list(nFast = .fastRange), label = 'nFast')
-tPD2<-setParameterDistribution(tPD2, type = 'indicator', indexnum = 2, distribution = list(nSlow = .slowRange), label = 'nSlow')
+tPD2<-setParameterDistribution(strategy=s, type = 'indicator', component='nFast', distribution = list(nFast = .fastRange), label = 'nFast')
+tPD2<-setParameterDistribution(tPD2, strategy=s, type = 'indicator', component='nSlow', distribution = list(nSlow = .slowRange), label = 'nSlow')
 
 pConstraint2<-setParameterConstraint(constraintLabel='luxorPC',paramList=c('nFast','nSlow'),relationship='lt')
 

Modified: pkg/quantstrat/demo/luxor.4.Timespans.R
===================================================================
--- pkg/quantstrat/demo/luxor.4.Timespans.R	2012-10-09 23:58:08 UTC (rev 1216)
+++ pkg/quantstrat/demo/luxor.4.Timespans.R	2012-10-11 19:57:02 UTC (rev 1217)
@@ -30,7 +30,7 @@
 
 ###############################################################################
 
-#require(doMC)
+require(doMC)
 require(foreach,quietly=TRUE)
 # example parallel initialization for doParallel. this or doMC, or doRedis are 
 # most probably preferable to doSMP
@@ -45,7 +45,7 @@
 
 parameterTable<-getParameterTable(s)
 
-tPD2<-setParameterDistribution(type = 'enter', indexnum = 1, distribution = list(timespan = .timespans), label = 'Timespan')
+tPD2<-setParameterDistribution(strategy=s, type = 'enter', component='EnterLONG', distribution = list(timespan = .timespans), label = 'Timespan')
 
 registerDoMC(cores=2)
 

Modified: pkg/quantstrat/man/paramConstraint.Rd
===================================================================
--- pkg/quantstrat/man/paramConstraint.Rd	2012-10-09 23:58:08 UTC (rev 1216)
+++ pkg/quantstrat/man/paramConstraint.Rd	2012-10-11 19:57:02 UTC (rev 1217)
@@ -3,7 +3,7 @@
 \title{Internal function used in applyParameter function for process constraints on relationship between two parameter values. Basicly is the same as sigComparison function in signal.R written by Brian, with miner change.}
 \usage{
   paramConstraint(label, data = mktdata, columns,
-    relationship = c("gt", "lt", "eq", "gte", "lte"))
+    relationship = c("gt", "lt", "eq", "gte", "lte", "op"))
 }
 \arguments{
   \item{label}{text label to apply to the constraint}

Modified: pkg/quantstrat/man/setParameterDistribution.Rd
===================================================================
--- pkg/quantstrat/man/setParameterDistribution.Rd	2012-10-09 23:58:08 UTC (rev 1216)
+++ pkg/quantstrat/man/setParameterDistribution.Rd	2012-10-11 19:57:02 UTC (rev 1217)
@@ -2,22 +2,23 @@
 \alias{setParameterDistribution}
 \title{Function used to create an object that contains the distribution of parameters to be generated from, before testing parameters of a strategy.}
 \usage{
-  setParameterDistribution(paramDist = NULL, type = NULL,
-    indexnum = 0, distribution = NULL, weight, label,
+  setParameterDistribution(paramDist = NULL, strategy,
+    type, component, distribution = NULL, weight, label,
     psindex = NULL)
 }
 \arguments{
-  \item{paramDist}{The object that store the parameter
-  list, if this parameter is missing, or object does not
-  exist, the function will return a new object.}
+  \item{paramDist}{The object in which the parameter list
+  is stored; if this parameter is missing, or object does
+  not exist, the function will return a new object.}
 
+  \item{strategy}{The strategy object}
+
   \item{type}{A character string that specifies the type of
   the parameter, it takes the value in one of 'indicator',
   'signal', 'enter', 'exit', 'order', 'chain'.}
 
-  \item{indexnum}{Tells the sequence within the type, (for
-  example: type = 'signal', indexnum =2 tells the function
-  to update the 2nd signal in the strategy)}
+  \item{component}{The label of the strategy component
+  (indicator/signal/rule) that contains the parameter}
 
   \item{distribution}{Distribution of the parameter, can be
   any function that returns a vector of value. See detail.
@@ -50,30 +51,27 @@
   strategy.
 }
 \details{
-  When call the function, the user must know the parameter
-  strcture of the strategy, function getParameterTable can
-  be used to layout the parameter structure of a certain
-  strategy. Parameter distribution object for one strategy
-  usually won't work for another strategy, because
-  different strategies has different parameter structure.
-  Type of the parameter and the sequence in that type is
-  needed to specify the exact parameter in THAT STRATEGY.
+  Parameter distribution object for one strategy usually
+  won't work for another strategy, because different
+  strategies has different parameter structure. Type of the
+  parameter and the sequence in that type is needed to
+  specify the exact parameter in THAT STRATEGY.
 
   The parameter 'distribution' is a list contains vector of
   values NAMED WITH THE NAMES OF THE PARAMETERS, the values
   can be any type (integer, characters, etc) but must match
-  with the leagal value of that parameter. For example:
+  with the legal value of that parameter. For example:
   distribution=list(nFast=(10:30)) or
   distribution=list(relationship=c('gt','gte'))
 }
 \examples{
 \dontrun{
 #(For complete demo see parameterTestMACD.R)
-tPD2<-setParameterDistribution(tPD2,'indicator',indexnum=1,
+tPD2<-setParameterDistribution(tPD2,strat,'indicator',component='FastSMA',
      distribution=list(nFast=(10:30)),label='nFast')
-tPD2<-setParameterDistribution(tPD2,'indicator',indexnum=1,
+tPD2<-setParameterDistribution(tPD2,strat,'indicator',component='SlowSMA',
      distribution=list(nSlow=(20:40)),label='nSlow')
-tPD2<-setParameterDistribution(tPD2,'signal',indexnum=1,
+tPD2<-setParameterDistribution(tPD2,strat,'signal',component='go.long',
      distribution=list(relationship=c('gt','gte')),label='sig1.gtgte')
 }
 }



More information about the Blotter-commits mailing list