[R-SIG-Finance] quantstrat - problems adding multiple indicators

Mark Knecht markknecht at gmail.com
Wed Sep 10 19:00:20 CEST 2014


Hi,
   This started out as me essentially rewriting Guy Yollin's
quantstrat3 demo over by hand as a learning exercise. That worked
fine. I then wanted to add an addition moving average filter (50 day
above 200 day) and figure out how to add indicators & write the
signals, etc., to make that work. However I've gotten stuck. It seems
I can add either the 50 day or the 200 day moving averages fine, but
if I try to add both I get the following message when I run the
attached code:

> out<-applyStrategy("Test2" ,
+                    portfolios="Port2",
+                    verbose=TRUE)
Error in `colnames<-`(`*tmp*`, value = c("SPY.Close.SMA.200",
"SPY.Close.SMA.50.SMA.F.SMA.200" :
  length of 'dimnames' [2] not equal to array extent

   Can someone point out what I'm doing wrong in adding both of these
indicators?

Thanks,
Mark


library(quantstrat)
StartData = "2000-01-01"
EndData   = format(Sys.time(), "%Y-%m-%d")
initDate = '2009-12-31'
startDate = '2010-01-01'  # start of data
endDate   =  '2013-07-31'   # end of data
symbols   = c("SPY")
Sys.setenv(TZ="UTC")       # set time zone
initEq <- 1e6
currency("USD")
stock(symbols, currency="USD",multiplier=1)

Filter.Fast = 50
Filter.Slow = 200

getSymbols(symbols, src='yahoo',
           #            index.class=c("POSIXt","POSIXct"),
           from=StartData, to=EndData, adjust=TRUE)

SPY = round(SPY, 2)

strategy("Test2", store=TRUE)

add.indicator("Test2",
              label='ind1',
              name = "MACD",
              arguments = list(x=quote(Cl(mktdata))
              )
)

add.indicator("Test2",
              label='SMA.F',
              name = 'SMA',
              arguments = list(x=quote(Cl(mktdata)),
                               n=Filter.Fast
              )
)

add.indicator("Test2",
              label='SMA.S',
              name = 'SMA',
              arguments = list(x=quote(Cl(mktdata)),
                               n=Filter.Slow
              )
)

rm.strat("Port2")
initPortf(name="Port2", symbols, initDate=initDate)
initAcct(name="Port2", portfolios="Port2",
         initDate=initDate, initEq=initEq)
initOrders(portfolio="Port2", initDate=initDate)

out<-applyStrategy("Test2" ,
                   portfolios="Port2",
                   verbose=TRUE)

updatePortf("Port2")
updateAcct("Port2")
updateEndEq("Port2")



More information about the R-SIG-Finance mailing list