[Blotter-commits] r1701 - pkg/quantstrat/demo

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Oct 27 17:50:02 CET 2015


Author: braverock
Date: 2015-10-27 17:50:02 +0100 (Tue, 27 Oct 2015)
New Revision: 1701

Modified:
   pkg/quantstrat/demo/macd.R
   pkg/quantstrat/demo/macdParameters.R
Log:
- update macd demo and macdParameters demo to use apply.paramset

Modified: pkg/quantstrat/demo/macd.R
===================================================================
--- pkg/quantstrat/demo/macd.R	2015-09-27 02:57:22 UTC (rev 1700)
+++ pkg/quantstrat/demo/macd.R	2015-10-27 16:50:02 UTC (rev 1701)
@@ -64,7 +64,7 @@
 
 #one indicator
 add.indicator(strat.st, name = "MACD", 
-			  arguments = list(x=quote(Cl(mktdata))),
+			  arguments = list(x=quote(Cl(mktdata)),nFast=fastMA, nSlow=slowMA),
 			  label='_' 
 )
 

Modified: pkg/quantstrat/demo/macdParameters.R
===================================================================
--- pkg/quantstrat/demo/macdParameters.R	2015-09-27 02:57:22 UTC (rev 1700)
+++ pkg/quantstrat/demo/macdParameters.R	2015-10-27 16:50:02 UTC (rev 1701)
@@ -1,9 +1,8 @@
-# Parameter demo for MACD demo
-# 
-# Author: Yu Chen
+# Parameter demo for MACD
 ###############################################################################
 
 require(foreach,quietly=TRUE)
+require(iterators)
 require(quantstrat)
 
 demo('macd',ask=FALSE)
@@ -17,49 +16,54 @@
 #please run macd demo before all these...
 
 #retrieve the strategy from the environment, since the 'macd' strategy uses store=TRUE
-stratMACD<-getStrategy('macd')
+strategy.st <- 'macd'
 
-paramStructure<-getParameterTable(stratMACD)
+### Set up Parameter Values
+.FastSMA = (1:20)
+.SlowSMA = (30:80)
+.nsamples = 10 #for random parameter sampling, less important if you're using doParallel or doMC
 
-rm(tPD2)
 
+### SMA paramset
 
-# Just provide leagal values and use random sampling.
-# Make nFast and nSlow over lap from 20 to 30 to test the constrains later.
-# First call don't need to input tPD2, the funciton will initial one and return it.
+add.distribution(strategy.st,
+                 paramset.label = 'SMA',
+                 component.type = 'indicator',
+                 component.label = '_', #this is the label given to the indicator in the strat
+                 variable = list(n = .FastSMA),
+                 label = 'nFAST'
+)
 
-tPD2<-setParameterDistribution(type='indicator',indexnum=1,distribution=list(nFast=(10:30)),label='nFast')
+add.distribution(strategy.st,
+                 paramset.label = 'SMA',
+                 component.type = 'indicator',
+                 component.label = '_', #this is the label given to the indicator in the strat
+                 variable = list(n = .SlowSMA),
+                 label = 'nSLOW'
+)
 
-# Initial one tPD2 and pass it also works as following two lines:
-## tPD2<-setParameterDistribution() 
-## tPD2<-setParameterDistribution(type='indicator',indexnum=1,distribution=list(nFast=(10:30)),label='nFast')
+add.distribution.constraint(strategy.st,
+                            paramset.label = 'SMA',
+                            distribution.label.1 = 'nFAST',
+                            distribution.label.2 = 'nSLOW',
+                            operator = '<',
+                            label = 'SMA'
+)
 
-tPD2<-setParameterDistribution(tPD2,component.type='indicator',indexnum=1,distribution=list(nSlow=(20:40)),label='nSlow')
-tPD2<-setParameterDistribution(tPD2,component.type='signal',indexnum=1,distribution=list(relationship=c('gt','gte')),label='sig1.gtgte')
 
-# Put constrains to the nFast and nSlow parameters.
-pConstraint2<-setParameterConstraint(constraintLabel='macdPC',paramList=c('nFast','nSlow'),relationship='lt')
+###
 
+results <- apply.paramset(strategy.st, 
+                          paramset.label='SMA', 
+                          portfolio.st=portfolio.st, 
+                          account.st=account.st, 
+                          nsamples=.nsamples, 
+                          verbose=TRUE)
 
+stats <- results$tradeStats
 
-#testPackList2<-applyParameter(strategy=stratMACD,portfolios=portfolio.st,parameterPool=tPD2,method='random',sampleSize=3,parameterConstrains=pConstraint2)
-#system.time(testPackList2<-applyParameter(strategy=stratMACD,portfolios=portfolio.st,parameterPool=tPD2,method='random',sampleSize=3,parameterConstrains=pConstraint2))
+print(stats)
 
-
-laststpar.rnd<-system.time(
-		testPackListPL<-applyParameter(strategy=stratMACD,portfolios=portfolio.st,parameterPool=tPD2,method='random',sampleSize=20,parameterConstraints=pConstraint2)
-)
-#laststpar.rnd
-
-# Please run either random mode or expand mode, since the latter run will overwrite the objects in .blotter.
-#laststpar.exp<-system.time(
-#		testPackListPL<-applyParameter(strategy=stratMACD,portfolios=portfolio.st,parameterPool=tPD2,method='random',sampleSize=20,parameterConstrains=pConstraint2)
-#)
-if(verbose >=1) print(laststpar.exp)
-
-#examine the stats from this parameter run:
-if(verbose >=1) print(testPackListPL$statsTable)
-
 ##### PLACE THIS BLOCK AHEAD OF DATE INITS IN DEMO SCRIPT ######
 # if(!exists('in_test') || !isTRUE(in_test)){
 #     initDate='2005-12-31' # ensure this is demo default



More information about the Blotter-commits mailing list