Quantstrat - external parameters
On 08/25/2012 11:35 PM, Jaeyoung Ahn wrote:
First of all, I appreciate all the hard works the developers have poured for this package. Is there anyone who could help me with this? I have searched this question and got to the following answer provided by Braverock. http://r.789695.n4.nabble.com/Parameters-setting-for-multiple-indicators-with-the-same-argument-names-td3793443.html I tried to follow his recommendation but couldn't get it work. My codes are as follows. It appears that only the first set of parameters are repeated over and over. External parameters (shortMA and longMA) don't seem like to be updated at all.
At this point I'm going to recommend that you look at
?setParameterDistribution
?setParameterConstraint
?applyParameters
and demo files luxor.3.R and luxor.3.Parameters.R
Like your example below, these demos are for a modified 2-MA cross
strategy taken from the Tomasini book. The numbers are chapter numbers,
as in the book.
Lines 40-43 in luxor.3.Parameters.R set the distributions for each MA,
and then define the relationship that nFast be less than ('lt') nSlow.
If you want to run these demos yourself, you'll need to choose an
appropriate parallel backend for foreach. On Windows, I'd recommend
doParallel as the easiest place to start.
At the time of the prior thread, referenced above, there was no demo
code for this type of strategy. Right now, I think you're better off
starting with applyParameters than writing your own loop.
Te model you outline below should work, it is likely failing do to a
misplaced quote() somewhere, though the exact cause isn't obvious to me
without more study.
One problem that I see immediately is that you are deleting the
portfolio in each loop iteration, so you would only see the last results
anyway. It seems that it would make more sense to create a portfolio
for each loop index, i, if shortMA<longMA so that you can collect all
the results.
However, applyParameters does all this for you, so it seems that you'd
be better off studying these demos instead.
Regards,
- Brian
input_data <- read.csv('mktdata.csv')
EURUSD <- xts(input_data[,3:6], as.POSIXct(input_data[,'Date'], tz='PST',
format='%Y-%m-%d', drop.time=F))
names(EURUSD) <- c("OPEN","HIGH","LOW","CLOSE")
shortMA = 5
longMA = 10
initDate <- as.Date('2000-01-02') - 1
stock.str <- 'EURUSD'
strat.name <- 'MAX'
initEq=1000000
dummy <- initPortf(name=strat.name,symbols=stock.str, initDate=initDate)
dummy <- initAcct(name=strat.name,portfolios=strat.name,
initDate=initDate, initEq=initEq)
initOrders(portfolio=strat.name,initDate=initDate)
strat <- strategy(strat.name)
*#indicators:*
strat <- add.indicator(strategy = strat, name = "SMA", arguments =
list(x=quote(Cl(mktdata)), *n=quote(shortMA))*,label= "ma_sn" )
strat <- add.indicator(strategy = strat, name = "SMA", arguments =
list(x=quote(Cl(mktdata)), *n=quote(longMA))*,label= "ma_ln")
# signals:
strat <- add.signal(strategy = strat,name="sigCrossover",
arguments = list(columns=c("ma_sn","ma_ln"),
relationship="gte"),
label="ma_sn.gt.ma_ln")
strat <- add.signal(strategy = strat,name="sigCrossover",
arguments =
list(column=c("ma_sn","ma_ln"),relationship="lt"),
label="ma_sn.lt.ma_ln")
# rules:
strat <- add.rule(strategy = strat,name='ruleSignal',
arguments = list(sigcol="ma_sn.gt.ma_ln",sigval=TRUE,
orderqty=1000000,
ordertype='market',
orderside='long',pricemethod='market'),type='enter',path.dep=T)
strat <- add.rule(strategy = strat,name='ruleSignal',
arguments = list(sigcol="ma_sn.lt.ma_ln",sigval=TRUE,
orderqty=-1000000,
ordertype='market',
orderside='long',pricemethod='market'),type='exit',path.deop=T)
library(PerformanceAnalytics)
ShortM <- c(5,10,15) #,20)
LongM <- c(20, 40) #, 50,60,100)
parm.comb <- expand.grid(ShortM=ShortM,LongM=LongM)
num.opts <- nrow(parm.comb)
res.vec <- rep(NA,num.opts)
res.vec2 <- res.vec
for(i in 1:num.opts) {
# initialize portfolio and orders
try(rm(list=ls(pos=.blotter),pos=.blotter),silent=TRUE)
try(rm(list=ls(pos=.strategy),pos=.strategy),silent=TRUE)
dummy <- initPortf(name=strat.name,symbols=stock.str, initDate=initDate)
initOrders(portfolio=strat.name,initDate=initDate)
# apply strategy
ShortMA <- parm.comb[i,"ShortM"]
LongMA <- parm.comb[i,"LongM"]
if (ShortMA<LongMA) {
out<-try(applyStrategy(strategy=strat, portfolios=strat.name,
verbose=F, parameters=list(shortMA=ShortMA, longMA= LongMA)))
# calculate performance matric
dummy <- updatePortf(Portfolio=strat.name,
Dates=paste('::',as.Date('2012-08-17'),sep=""))
trading.pl <- getPortfolio(strat.name)$summary$Net.Trading.PL
rets <- trading.pl/initEq
omega <- as.numeric(Omega(rets))
res.vec[i] <- omega
}
}