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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jun 30 23:33:51 CEST 2012


Author: braverock
Date: 2012-06-30 23:33:50 +0200 (Sat, 30 Jun 2012)
New Revision: 1095

Modified:
   pkg/quantstrat/R/parameters.R
   pkg/quantstrat/demo/macdParameters.R
Log:
- make demo less verbose
- process verbose argument in applyParameters
- change to doParallel in comments
- show how to get stats table


Modified: pkg/quantstrat/R/parameters.R
===================================================================
--- pkg/quantstrat/R/parameters.R	2012-06-30 16:22:14 UTC (rev 1094)
+++ pkg/quantstrat/R/parameters.R	2012-06-30 21:33:50 UTC (rev 1095)
@@ -328,12 +328,12 @@
 #' @param parameterConstraints The object created by setParameterConstraint function that specifies the constraints between each parameters,
 #' @param method Takes string 'expand' or 'random', specify how to generate samples of parameters. 'expand' will do all possible combinations of the parameter sets, 
 #' @param sampleSize Used when method=='random', specify how many parameter sets to generate and run test of.
-#' 
+#' @param verbose if verbose TRUE or 1+, will print a lot of debug info, default FALSE
 #' @seealso \code{\link{setParameterDistribution}}, \code{\link{setParameterConstraint}}
 #' 
 #' @author Yu Chen
 #' @export
-applyParameter<-function(strategy,portfolios,parameterPool,parameterConstraints,method,sampleSize){
+applyParameter<-function(strategy,portfolios,parameterPool,parameterConstraints,method,sampleSize,verbose=FALSE){
 	#need to create combination of distribution values in each slot of the parameterPool
 	
 	initialPortf<-getPortfolio(portfolios)
@@ -435,8 +435,8 @@
 			
 			iparamTable<-unique(iparamTable)
 			
-#			print("nnnnnnnnnnnnnnnnnnnnnnn")
-#			print(nrow(iparamTable))
+#			if(verbose >=1) print("nnnnnnnnnnnnnnnnnnnnnnn")
+#			if(verbose >=1) print(nrow(iparamTable))
 			
 			if (nrow(iparamTable)<tsampleSize)
 			{
@@ -459,11 +459,11 @@
 	testPackList$paramLabel<-paramLabel
 	
 	strategyList<-list()
-	print("ParamTable generated")
+	if(verbose >=1) print("ParamTable generated")
 	
 	
 	psize=nrow(paramTable)
-	print(psize)
+	if(verbose >=1) print(psize)
 	
 	
 	
@@ -477,16 +477,13 @@
 	testPackListPRL<-foreach (i = 1:psize, .export=c('instruments',symbols,'getSymbols','blotter','tmp_strategy'),.verbose=TRUE) %dopar% 
 			
 			{
-#				library(blotter)
-#				require(FinancialInstrument)
-				require(quantstrat)
+				require(quantstrat, quietly=TRUE)
 				
 				testPack<-list()
 				
 				#Pass environments needed.
                 loadInstruments(instruments)
 				.getSymbols<-as.environment(getSymbols)
-#				CAN NOT BE HERE, OTHERWISE will have problem extract to outside .blooter.. #.blotter<-as.environment(blotter)
 				
 				#Unpack symbols to worker. change later.
 				#seems need to go through assign, rather than just .export the names...
@@ -599,7 +596,7 @@
 				try(rm(list = rmastr, pos = .blotter),silent=FALSE)
 				try(rm(list=paste("order_book",testPack$account.st,sep="."),pos=.strategy),silent=FALSE)
 				
-				print('Initial portf')
+				if(verbose >=1) print('Initial portf')
 				
 #				Decide not to remove the main obj from .blotter, incase of non-parallel run.
 #				try(rm(list=paste("order_book",portfolios,sep='.'),pos=.strategy),silent=TRUE)
@@ -612,11 +609,11 @@
 				try({initOrders(portfolio=testPack$portfolio.st,initDate=initDate)})
 				
 # Apply strategy ######################################################################################
-				print("Apply strategy...")
+				if(verbose >=1) print("Apply strategy...")
 				
 				try(rm("PLtmp_strategy",pos=.strategy),silent=TRUE)
 				
-				print(PLtmp_strategy$signals[[2]])
+				if(verbose >=1) print(PLtmp_strategy$signals[[2]])
 				
 				assign("PLtmp_strategy1",PLtmp_strategy,envir=as.environment(.strategy))
 				
@@ -655,10 +652,10 @@
 	
 	for (k in 1: nrow(paramTable)){
 		testPackListPRLStructure$statsTable<-rbind(testPackListPRLStructure$stats,cbind(testPackListPRL[[k]]$parameters,testPackListPRL[[k]]$stats))
-		print(names(testPackListPRL[[k]]$blotterl))
+		if(verbose >=1) print(names(testPackListPRL[[k]]$blotterl))
 		
 		for(nn in 1:length(testPackListPRL[[k]]$blotterl)){
-#			print(paste(names(testPackListPRL[[k]]$blotterl)[nn],'nnp',nn,sep='.'))
+#			if(verbose >=1) print(paste(names(testPackListPRL[[k]]$blotterl)[nn],'nnp',nn,sep='.'))
 			assign(names(testPackListPRL[[k]]$blotterl[nn]),testPackListPRL[[k]]$blotterl[[nn]],envir=as.environment(.blotter))
 		}
 		names(testPackListPRL)[k]<-testPackListPRL[[k]]$portfolio.st
@@ -696,7 +693,7 @@
 #' @param relationship one of c("gt","lt","eq","gte","lte","op") or reasonable alternatives
 paramConstraint <- function(label,data=mktdata, columns, relationship=c("gt","lt","eq","gte","lte")) {
 	relationship=relationship[1] #only use the first one
-#	print(columns)
+#	if(verbose >=1) print(columns)
 	if (length(columns)==2){
 		ret_sig=NULL
 		if (relationship=='op'){

Modified: pkg/quantstrat/demo/macdParameters.R
===================================================================
--- pkg/quantstrat/demo/macdParameters.R	2012-06-30 16:22:14 UTC (rev 1094)
+++ pkg/quantstrat/demo/macdParameters.R	2012-06-30 21:33:50 UTC (rev 1095)
@@ -8,13 +8,17 @@
 
 demo('macd',ask=FALSE)
 
-# example parallel initialization for doSMP, doParallel, doMC, or doRedis are 
+# example parallel initialization for doParallel. this or doMC, or doRedis are 
 # most probably preferable to doSMP
-#require(doSMP)
-#workers <- startWorkers(2)
-#registerDoSMP(workers)
+#require(doParallel)
+#registerDoParallel() # by default number of physical cores -1
 
+
 #please run macd demo before all these...
+
+#retrieve the strategy from the environment, since the 'macd' strategy uses store=TRUE
+stratMACD<-getStrategy('macd')
+
 paramStructure<-getParameterTable(stratMACD)
 
 rm(tPD2)
@@ -45,7 +49,7 @@
 laststpar.rnd<-system.time(
 		testPackListPL<-applyParameter(strategy=stratMACD,portfolios=portfolio.st,parameterPool=tPD2,method='random',sampleSize=20,parameterConstraints=pConstraint2)
 )
-laststpar.rnd
+#laststpar.rnd
 
 # Please run either random mode or expand mode, since the latter run will overwrite the objects in .blotter.
 #laststpar.exp<-system.time(



More information about the Blotter-commits mailing list