[Blotter-commits] r1702 - in pkg/quantstrat: . R demo
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Oct 27 21:22:28 CET 2015
Author: braverock
Date: 2015-10-27 21:22:27 +0100 (Tue, 27 Oct 2015)
New Revision: 1702
Modified:
pkg/quantstrat/DESCRIPTION
pkg/quantstrat/R/parameters.R
pkg/quantstrat/demo/bbandParameters.R
pkg/quantstrat/demo/bbands.R
pkg/quantstrat/demo/macd.R
pkg/quantstrat/demo/macdParameters.R
Log:
- update bbands and bbandsParameters demos to use apply.paramset
Modified: pkg/quantstrat/DESCRIPTION
===================================================================
--- pkg/quantstrat/DESCRIPTION 2015-10-27 16:50:02 UTC (rev 1701)
+++ pkg/quantstrat/DESCRIPTION 2015-10-27 20:22:27 UTC (rev 1702)
@@ -1,7 +1,7 @@
Package: quantstrat
Type: Package
Title: Quantitative Strategy Model Framework
-Version: 0.9.1687
+Version: 0.9.1702
Date: $Date$
Author: Peter Carl, Brian G. Peterson, Joshua Ulrich, Jan Humme
Depends:
Modified: pkg/quantstrat/R/parameters.R
===================================================================
--- pkg/quantstrat/R/parameters.R 2015-10-27 16:50:02 UTC (rev 1701)
+++ pkg/quantstrat/R/parameters.R 2015-10-27 20:22:27 UTC (rev 1702)
@@ -190,6 +190,8 @@
#' @author Yu Chen
setParameterDistribution<-function(paramDist=NULL, strategy, component.type, component.label, distribution=NULL, weight, label, psindex=NULL) #All is needed, set to illegal values
{
+ .Deprecated(new = 'add.distribution', msg = 'old parameter code deprecated, please use add.distribution instead')
+
missing.msg <- ': missing in call to setParameterDistribution'
if(!hasArg(strategy))
@@ -648,6 +650,8 @@
#' @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", "op"))
{
+
+
if(length(relationship) != 1)
stop('paramConstraint: length(relationship)!=1')
Modified: pkg/quantstrat/demo/bbandParameters.R
===================================================================
--- pkg/quantstrat/demo/bbandParameters.R 2015-10-27 16:50:02 UTC (rev 1701)
+++ pkg/quantstrat/demo/bbandParameters.R 2015-10-27 20:22:27 UTC (rev 1702)
@@ -1,75 +1,70 @@
-# Parameter example for BBands demo
-#
-# Author: Yu Chen
-###############################################################################
-
-require(foreach,quietly=TRUE)
-require(quantstrat)
-
-# example parallel initialization for doParallel. this or doMC, or doRedis are
-# most probably preferable to doSMP
-#require(doParallel)
-#registerDoParallel() # by default number of physical cores -1
-
-demo('bbands',ask=FALSE)
-#the user should load a parallel backend for foreach before running,
-# or this will run in single threaded mode
-
-#please run bbands demo before all these...
-#paramStructure<-getParameterTable(stratBBands)
-#print(paramStructure)
-
-#tPD<-setParameterDistribution() need no more for initial object.
-
-#Do expand test
-#tPD<-setParameterDistribution(tPD,'indicator',indexnum=1,distribution=list(sd=(1:3)))
-#tPD<-setParameterDistribution(tPD,'signal',indexnum=2,distribution=list(sd=sample(1:10, size=1, replace=FALSE)))
-#tPD<-setParameterDistribution(tPD,'signal',indexnum=3,distribution=list(n=sample(1:10, size=1, replace=FALSE)))
-#
-##update the 3rd slot by using psindex
-#tPD<-setParameterDistribution(tPD,'signal',indexnum=2,distribution=list(n=c(20,30)),psindex=3)
-#testPackList<-applyParameter(strategy=stratBBands,portfolios='bbands',parameterPool=tPD,method='expand')
-
-
-
-
-#tPD
-
-#debug(applyParameter)
-#undebug(applyParameter)
-
-
-tPD <- NULL
-
-# Just provide legal values and use random sampling.
-tPD<-setParameterDistribution(tPD, strategy=stratBBands, component.type='indicator', component.label='BBands', distribution=list(sd=(1:3)), weight=c(.25, .25, .5), label='sd')
-tPD<-setParameterDistribution(tPD, strategy=stratBBands, component.type='signal', component.label='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, component.type='indicator', component.label='BBands', distribution=list(n=20:30), label='n')
-
-#pConstr<-setParameterConstraint()
-pConstraint<-setParameterConstraint(constraintLabel='PC1',paramList=c('sd','n'),relationship='gt')
-
-#testPackList<-applyParameter(strategy=stratBBands,portfolios=portfolio.st,parameterPool=tPD,method='expand',parameterConstraints=pConstraint)
-testPackList<-applyParameter(strategy=stratBBands,portfolios=portfolio.st,parameterPool=tPD,method='random',sampleSize=8,parameterConstraints=pConstraint)
-
-###############################################################################
-# R (http://r-project.org/) Quantitative Strategy Model Framework
-#
-# Copyright (c) 2009-2012
-# Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and Joshua Ulrich
-#
-# This library is distributed under the terms of the GNU Public License (GPL)
-# for full details see the file COPYING
-#
-# $Id: bbands.R 1097 2012-07-01 00:30:39Z braverock $
-#
-###############################################################################
-
-print(testPackList$statsTable)
-
-##### PLACE THIS BLOCK AT END OF DEMO SCRIPT ###################
-# book = getOrderBook(port)
-# stats = tradeStats(port)
-# rets = PortfReturns(acct)
-################################################################
+# Parameter example for BBands demo
+###############################################################################
+
+require(foreach,quietly=TRUE)
+require(iterators)
+require(quantstrat)
+
+# example parallel initialization for doParallel. this or doMC, or doRedis are
+# most probably preferable to doSMP
+#require(doParallel)
+#registerDoParallel() # by default number of physical cores -1
+
+demo('bbands',ask=FALSE)
+strategy.st='bbands'
+
+### User Set up pf parameter ranges to test
+.nlist = 10:40
+.sdlist = 1:4
+
+# number of random samples of the parameter distribution to use for random run
+.nsamples = 10
+
+add.distribution(strategy.st,
+ paramset.label = 'BBparams',
+ component.type = 'indicator',
+ component.label = 'BBands', #this is the label given to the indicator in the strat
+ variable = list(n = .nlist),
+ label = 'nFAST'
+)
+
+add.distribution(strategy.st,
+ paramset.label = 'BBparams',
+ component.type = 'indicator',
+ component.label = 'BBands', #this is the label given to the indicator in the strat
+ variable = list(sd = .sdlist),
+ label = 'nSLOW'
+)
+
+
+results <- apply.paramset(strategy.st,
+ paramset.label='BBparams',
+ portfolio.st=portfolio.st,
+ account.st=account.st,
+ nsamples=.nsamples,
+ verbose=TRUE)
+
+stats <- results$tradeStats
+
+print(stats)
+
+
+
+##### PLACE THIS BLOCK AT END OF DEMO SCRIPT ###################
+# book = getOrderBook(port)
+# stats = tradeStats(port)
+# rets = PortfReturns(acct)
+################################################################
+
+###############################################################################
+# R (http://r-project.org/) Quantitative Strategy Model Framework
+#
+# Copyright (c) 2009-2012
+# Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and Joshua Ulrich
+#
+# This library is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: bbands.R 1097 2012-07-01 00:30:39Z braverock $
+#
+###############################################################################
Modified: pkg/quantstrat/demo/bbands.R
===================================================================
--- pkg/quantstrat/demo/bbands.R 2015-10-27 16:50:02 UTC (rev 1701)
+++ pkg/quantstrat/demo/bbands.R 2015-10-27 20:22:27 UTC (rev 1702)
@@ -35,31 +35,87 @@
initAcct(account.st,portfolios='bbands', initDate=initDate)
initOrders(portfolio=portfolio.st,initDate=initDate)
addPosLimit(portfolio.st, stock.str, initDate, 200, 2 ) #set max pos
-stratBBands <- strategy("bbands")
+# set up parameters
+maType='SMA'
+n = 20
+sdp = 2
+
+strat.st<-portfolio.st
+# define the strategy
+strategy(strat.st, store=TRUE)
+
#one indicator
-stratBBands <- add.indicator(strategy = stratBBands, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'), label='BBands')
+add.indicator(strategy = strat.st,
+ name = "BBands",
+ arguments = list(HLC = quote(HLC(mktdata)),
+ n=n,
+ maType=maType,
+ sd=sdp
+ ),
+ label='BBands')
#add signals:
-stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperBand")
-stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("Close","dn"),relationship="lt"),label="Cl.lt.LowerBand")
-stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("High","Low","mavg"),relationship="op"),label="Cross.Mid")
+add.signal(strategy = strat.st,
+ name="sigCrossover",
+ arguments = list(columns=c("Close","up"),
+ relationship="gt"),
+ label="Cl.gt.UpperBand")
+add.signal(strategy = strat.st,
+ name="sigCrossover",
+ arguments = list(columns=c("Close","dn"),
+ relationship="lt"),
+ label="Cl.lt.LowerBand")
+
+add.signal(strategy = strat.st,name="sigCrossover",
+ arguments = list(columns=c("High","Low","mavg"),
+ relationship="op"),
+ label="Cross.Mid")
+
# lets add some rules
-stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market', orderside=NULL, threshold=NULL,osFUN=osMaxPos),type='enter')
-stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market', orderside=NULL, threshold=NULL,osFUN=osMaxPos),type='enter')
-stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL,osFUN=osMaxPos),type='exit')
+add.rule(strategy = strat.st,name='ruleSignal',
+ arguments = list(sigcol="Cl.gt.UpperBand",
+ sigval=TRUE,
+ orderqty=-100,
+ ordertype='market',
+ orderside=NULL,
+ threshold=NULL,
+ osFUN=osMaxPos),
+ type='enter')
+add.rule(strategy = strat.st,name='ruleSignal',
+ arguments = list(sigcol="Cl.lt.LowerBand",
+ sigval=TRUE,
+ orderqty= 100,
+ ordertype='market',
+ orderside=NULL,
+ threshold=NULL,
+ osFUN=osMaxPos),
+ type='enter')
+
+add.rule(strategy = strat.st,name='ruleSignal',
+ arguments = list(sigcol="Cross.Mid",
+ sigval=TRUE,
+ orderqty= 'all',
+ ordertype='market',
+ orderside=NULL,
+ threshold=NULL,
+ osFUN=osMaxPos),
+ label='exitMid',
+ type='exit')
+
+
#alternately, to exit at the opposite band, the rules would be...
-#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Lo.gt.UpperBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
-#stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Hi.lt.LowerBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
+#add.rule(strategy = strat.st,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Lo.gt.UpperBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
+#add.rule(strategy = strat.st,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Hi.lt.LowerBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
#TODO add thresholds and stop-entry and stop-exit handling to test
getSymbols(stock.str,from=initDate,index.class=c('POSIXt','POSIXct'))
start_t<-Sys.time()
-out<-try(applyStrategy(strategy=stratBBands , portfolios='bbands',parameters=list(sd=SD,n=N)) )
+out<-try(applyStrategy(strategy='bbands' , portfolios='bbands',parameters=list(sd=SD,n=N)) )
# look at the order book
#getOrderBook('bbands')
Modified: pkg/quantstrat/demo/macd.R
===================================================================
--- pkg/quantstrat/demo/macd.R 2015-10-27 16:50:02 UTC (rev 1701)
+++ pkg/quantstrat/demo/macd.R 2015-10-27 20:22:27 UTC (rev 1702)
@@ -64,25 +64,27 @@
#one indicator
add.indicator(strat.st, name = "MACD",
- arguments = list(x=quote(Cl(mktdata)),nFast=fastMA, nSlow=slowMA),
- label='_'
+ arguments = list(x=quote(Cl(mktdata)),
+ nFast=fastMA,
+ nSlow=slowMA),
+ label='_'
)
#two signals
add.signal(strat.st,name="sigThreshold",
- arguments = list(column="signal._",
- relationship="gt",
- threshold=0,
- cross=TRUE),
- label="signal.gt.zero"
+ arguments = list(column="signal._",
+ relationship="gt",
+ threshold=0,
+ cross=TRUE),
+ label="signal.gt.zero"
)
add.signal(strat.st,name="sigThreshold",
- arguments = list(column="signal._",
- relationship="lt",
- threshold=0,
- cross=TRUE),
- label="signal.lt.zero"
+ arguments = list(column="signal._",
+ relationship="lt",
+ threshold=0,
+ cross=TRUE),
+ label="signal.lt.zero"
)
####
@@ -90,15 +92,15 @@
# entry
add.rule(strat.st,name='ruleSignal',
- arguments = list(sigcol="signal.gt.zero",
- sigval=TRUE,
- orderqty=100,
- ordertype='market',
- orderside='long',
- threshold=NULL),
- type='enter',
- label='enter',
- storefun=FALSE
+ arguments = list(sigcol="signal.gt.zero",
+ sigval=TRUE,
+ orderqty=100,
+ ordertype='market',
+ orderside='long',
+ threshold=NULL),
+ type='enter',
+ label='enter',
+ storefun=FALSE
)
#alternatives for risk stops:
@@ -109,21 +111,21 @@
# exit
add.rule(strat.st,name='ruleSignal',
- arguments = list(sigcol="signal.lt.zero",
- sigval=TRUE,
- orderqty='all',
- ordertype='market',
- orderside='long',
- threshold=NULL,
- orderset='exit2'),
+ arguments = list(sigcol="signal.lt.zero",
+ sigval=TRUE,
+ orderqty='all',
+ ordertype='market',
+ orderside='long',
+ threshold=NULL,
+ orderset='exit2'),
type='exit',
- label='exit'
+ label='exit'
)
#end rules
####
-getSymbols(stock.str,from=initDate)
+getSymbols(stock.str,from=initDate, to='2014-06-01')
start_t<-Sys.time()
out<-applyStrategy(strat.st , portfolios=portfolio.st,parameters=list(nFast=fastMA, nSlow=slowMA, nSig=signalMA,maType=maType),verbose=TRUE)
end_t<-Sys.time()
Modified: pkg/quantstrat/demo/macdParameters.R
===================================================================
--- pkg/quantstrat/demo/macdParameters.R 2015-10-27 16:50:02 UTC (rev 1701)
+++ pkg/quantstrat/demo/macdParameters.R 2015-10-27 20:22:27 UTC (rev 1702)
@@ -19,42 +19,42 @@
strategy.st <- 'macd'
### Set up Parameter Values
-.FastSMA = (1:20)
-.SlowSMA = (30:80)
+.FastMA = (1:20)
+.SlowMA = (30:80)
.nsamples = 10 #for random parameter sampling, less important if you're using doParallel or doMC
-### SMA paramset
+### MA paramset
add.distribution(strategy.st,
- paramset.label = 'SMA',
+ paramset.label = 'MA',
component.type = 'indicator',
component.label = '_', #this is the label given to the indicator in the strat
- variable = list(n = .FastSMA),
+ variable = list(n = .FastMA),
label = 'nFAST'
)
add.distribution(strategy.st,
- paramset.label = 'SMA',
+ paramset.label = 'MA',
component.type = 'indicator',
component.label = '_', #this is the label given to the indicator in the strat
- variable = list(n = .SlowSMA),
+ variable = list(n = .SlowMA),
label = 'nSLOW'
)
add.distribution.constraint(strategy.st,
- paramset.label = 'SMA',
+ paramset.label = 'MA',
distribution.label.1 = 'nFAST',
distribution.label.2 = 'nSLOW',
operator = '<',
- label = 'SMA'
+ label = 'MA'
)
###
results <- apply.paramset(strategy.st,
- paramset.label='SMA',
+ paramset.label='MA',
portfolio.st=portfolio.st,
account.st=account.st,
nsamples=.nsamples,
More information about the Blotter-commits
mailing list