help with quantstrat
my guess is that your myRSI and myCCI functions probably need to explicitly
define the colnames 'rsif', ...
myRSI=function(x,nrsi=8,m=20){
r=SMA(RSI(x,n=nrsi),n=m)
colnames(r) <- 'rsif'
return(r)
}
Check if the 'label=' argument is really being used to set column labels
in the "add.indicator" function definition ...
On Tue, Feb 3, 2015 at 11:43 AM, Olivier MARTIN <
Olivier.Martin at avignon.inra.fr> wrote:
Hi all,
I try to program a simple strategy to learn with the package quantstrat.
Hereafter is the program, and the message I obtained when I applyed the
strategy
Error in eval(expr, envir, enclos) : objet 'rsif' introuvable
Erreur dans `colnames<-`(`*tmp*`, value = seq(ncol(tmp_val))) :
attempt to set 'colnames' on an object with less than two dimensions
Could you explain me what is wrong ?
Regards,
Olivier.
#-------------------------
currency("EUR")
Sys.setenv(TZ="UTC")
getSymbols('ACA.PA')
stock("ACA.PA",currency="EUR")
# system settings
initDate <- '2007-01-01'
endDate <- '2014-12-30'
initEq <- 1
#strategy
mystrat <- "gg"
rm.strat(mystrat) # remove strategy
initPortf(mystrat,'ACA.PA', initDate=initDate)
initAcct(mystrat,portfolios=mystrat, initDate=initDate, initEq=initEq)
# initialize orders container
initOrders(portfolio=mystrat,initDate=initDate)
# instantiate a new strategy object
strategy(mystrat,store=TRUE)
myRSI=function(x,nrsi=8,m=20){
r=SMA(RSI(x,n=nrsi),n=m)
return(r)
}
myCCI=function(x,ncci=8,m=20){
r=SMA(CCI(x,n=ncci),n=m)
return(r)
}
#indicatros
mystrat=add.indicator(strategy =mystrat, name = "myRSI",arguments = list(x
=quote(Cl(mktdata)),nrsi=8 ,m=20), label="rsif")
mystrat=add.indicator(strategy =mystrat, name = "myRSI",arguments = list(x
=quote(Cl(mktdata)),nrsi=14,m=20), label="rsis")
mystrat=add.indicator(strategy =mystrat, name = "myCCI",arguments = list(x
=quote(Cl(mktdata)),ncci=8 ,m=20), label="ccif")
mystrat=add.indicator(strategy =mystrat, name = "myCCI",arguments = list(x
=quote(Cl(mktdata)),ncci=14,m=20), label="ccis")
#signals
mystrat=add.signal(mystrat,name="sigFormula",arguments =
list(columns=c("rsif","rsis","ccif","ccis"),formula="rsif>rsis &
ccif>ccis",cross=TRUE), label="up")
mystrat=add.signal(mystrat,name="sigFormula",arguments =
list(columns=c("rsif","rsis","ccif","ccis"),formula="rsif<rsis &
ccif<ccis",cross=TRUE), label="dn")
mystrat=add.signal(mystrat,name="sigFormula",arguments =
list(columns=c("rsif","rsis","ccif","ccis"),formula="!(rsif>rsis &
ccif>ccis ) | !(rsif<rsis & ccif<ccis)",cross=TRUE), label="ex")
# position rules
mystrat=add.rule(mystrat, name="ruleSignal",arguments = list(sigcol="up",
sigval=TRUE, orderqty=1,ordertype='market', orderside='long'), type='enter')
mystrat=add.rule(mystrat, name="ruleSignal",arguments = list(sigcol="ex",
sigval=TRUE, orderqty='all',ordertype='market', orderside='long'),
type='exit')
mystrat=add.rule(mystrat, name="ruleSignal",arguments = list(sigcol="dn",
sigval=TRUE, orderqty=1,ordertype='market', orderside='short'),
type='enter')
mystrat=add.rule(mystrat, name="ruleSignal",arguments = list(sigcol="ex",
sigval=TRUE, orderqty='all',ordertype='market', orderside='short'),
type='exit')
## apply strategy
applyStrategy(strategy=mystrat , portfolios=mystrat)
This is the message I obtain :
Error in eval(expr, envir, enclos) : objet 'rsif' introuvable
Erreur dans `colnames<-`(`*tmp*`, value = seq(ncol(tmp_val))) :
attempt to set 'colnames' on an object with less than two dimensions
_______________________________________________ R-SIG-Finance at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.