Hello,
I am trying to backtest a custom momentum strategy. I am following the
maCross.R provided in the quantstrat package. I am getting the below error.
Any suggestions?

> out<-try(applyStrategy(strategy= stratTEAM ,
+ portfolios=portfolio.st))
Error in `[.xts`(x, (INDEX[y] + 1):INDEX[y + 1]) :
  subscript out of bounds

Here is my code.

> ttz<-Sys.getenv('TZ')
>
> Sys.setenv(TZ='UTC')
>
> .blotter<-new.env()  # blotter and strategy containers
>
> .strategy<-new.env()
>
>
>> rm(list=ls())
>
> initDate='2011-12-31'
>
> start.date<-"2012-01-01"
>
> end.date<-"2012-01-31"
>
> drop.time=TRUE
>
> getSymbols("AAPL",from=start.date,to=end.date)
>
> reg.time <- sapply(timeBasedSeq(paste(start.date,end.date,sep="/")),
>
>                    function(x) {as.POSIXlt(x)$wday %in% c(1:5)} )
>
> AAPL<-merge.xts(AAPL,reg.time)
>
> na.locf(AAPL)
>
> week.beg=3 #0-Sunday, 1-Mon, 2-Tues, 3- Wed, 4 - Thu, 5- Fri, 6-Sat
>
> #indx <- c(0, which(as.POSIXlt(time(AAPL))$wday == week.beg))
>
> #mean(tail(period.apply(Cl(AAPL), INDEX=indx, FUN=last),5))
>
>
>> lt_mean<-function(x){
>
>   indx<-c(which(as.POSIXlt(time(AAPL[paste('/',time(x)-1,sep="")]))$wday
>> == week.beg))
>
>   #  print(tail(period.apply(Cl(AAPL), INDEX=indx, FUN=last),2))
>
>   mean.curr<-mean(tail(period.apply(Cl(AAPL), INDEX=indx, FUN=last),2))
>
>   (mean.curr)
>
> }
>
>
>> # clean up variables
>
> suppressWarnings(rm("order_book.team",pos=.strategy))
>
> suppressWarnings(rm("account.team","portfolio.team",pos=.blotter))
>
> suppressWarnings(rm("account.st","portfolio.st
>> ","stock.str","stratTEAM",'start_t','end_t'))
>
>
>> stock.str='AAPL' # what are we trying it on
>
> currency('USD')
>
> stock(stock.str,currency='USD',multiplier=1)
>
>
>> #------------------> PARAMETER SETUP <-----------------#
>
>
>>
>> D=.07
>
> C=.03
>
> n=4
>
> m=5
>
>
>> initEq=1000000  # Initial port level
>
> order.size=3000  # Default order size
>
> txn.fee=-order.size*0.01 # 1-way txn fee
>
> portfolio.st='team'
>
> account.st='team'
>
> do.short=TRUE
>
> initDate=start.date
>
>
>> #-----------------> Initialize QS objects <------------#
>
> initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
>
> initAcct(account.st,portfolios=portfolio.st,
>> initDate=initDate,initEq=initEq)
>
> initOrders(portfolio=portfolio.st,initDate=initDate)
>
>
>> #------------------> STRATEGY DEF <--------------------#
>
> stratTEAM<- strategy(portfolio.st)
>
>
>> #-----> INDICATORS <-------------#
>
>
>> stratTEAM <- add.indicator(strategy = stratTEAM,
>
>                               name = "lt_mean",
>
>                               arguments = list(x=quote(Cl(mktdata))),
>
>                               label= "ltavg" )
>
>
>> stratTEAM <- add.indicator(strategy = stratTEAM,
>
>                               name = "SMA",
>
>                               arguments = list(x=quote(Cl(mktdata)),
>
>                                                n=m),
>
>                               label= "stavg")
>
>
>> #-----> SIGNALS <---------------#
>
>
>> stratTEAM <-
>
>   add.signal(stratTEAM,
>
>              name="sigFormula",
>
>              arguments = list(columns=c("Close","ltavg"),
>
>                               formula = "AAPL.Close > ltavg + D ",
>
>                               label="trigger",
>
>                               cross=TRUE),
>
>              label="Cl.gt.ltavg.plus.D")
>
> stratTEAM <-
>
>   add.signal(stratTEAM,
>
>              name="sigFormula",
>
>              arguments = list(columns=c("Close","ltavg"),
>
>                               formula = "AAPL.Close < ltavg - D ",
>
>                               label="trigger",
>
>                               cross=TRUE),
>
>              label="Cl.lt.ltavg.minus.D")
>
> stratTEAM <-
>
>   add.signal(stratTEAM,
>
>              name="sigFormula",
>
>              arguments = list(columns=c("Close","ltavg"),
>
>                               formula = "AAPL.Close < ltavg - D ",
>
>                               label="trigger",
>
>                               cross=TRUE),
>
>              label="Cl.lt.ltavg.minus.D")
>
> stratTEAM <-
>
>   add.signal(stratTEAM,
>
>              name="sigFormula",
>
>              arguments = list(columns=c("Close","stavg"),
>
>                               formula = "AAPL.Close > stavg + C ",
>
>                               label="trigger",
>
>                               cross=TRUE),
>
>              label="Cl.gt.stavg.plus.D")
>
> stratTEAM <-
>
>   add.signal(stratTEAM,
>
>              name="sigFormula",
>
>              arguments = list(columns=c("Close","stavg"),
>
>                               formula = "AAPL.Close < stavg - C ",
>
>                               label="trigger",
>
>                               cross=TRUE),
>
>              label="Cl.lt.stavg.minus.C")
>
> #-----> RULES <-----------------#
>
> stratTEAM <- add.rule(strategy = stratTEAM,
>
>                          name='ruleSignal',
>
>                          arguments = list(sigcol="Cl.gt.ltavg.plus.D",
>
>                                           sigval=TRUE,
>
>                                           orderqty=order.size,
>
>                                           ordertype='market',
>
>                                           orderside='long',
>
>                                           TxnFees=txn.fee),
>
>                          type='enter')
>
>
>> stratTEAM <- add.rule(strategy = stratTEAM,
>
>                          name='ruleSignal',
>
>                          arguments = list(sigcol="Cl.lt.ltavg.minus.D",
>
>                                           sigval=TRUE,
>
>                                           orderqty='all',
>
>                                           ordertype='market',
>
>                                           orderside='long',
>
>                                           TxnFees=txn.fee),
>
>                          type='exit')
>
>
>>
>> out<-try(applyStrategy(strategy= stratTEAM ,
>
>                        portfolios=portfolio.st))
>
>

	[[alternative HTML version deleted]]

