Skip to content

quantstrat - problems adding multiple indicators

3 messages · Brian G. Peterson, Mark Knecht

#
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:
+                    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")
#
This has been discussed on this list multiple times.

TTR is taking the column namees of your market data and appending the 
indicator.

so:

Cl(mktdata)

will return multiple columns after the first indicator has been applied.

you most likely want

quote(Cl(mktdata)[,1])

to get only the first column.

Brian
On 09/10/2014 12:00 PM, Mark Knecht wrote:
#
On Wed, Sep 10, 2014 at 10:06 AM, Brian G. Peterson <brian at braverock.com> wrote:
Ah, indeed, that does work, although Googling for 'quantstrat' and
'quote(Cl(mktdata)[,1])' doesn't turn up much.

None the less, I now see that Cl() is finding all names with 'close'
anywhere in the name which indeed isn't want I thought it was doing
and the first one is the one I want:
SPY.Close SPY.Close.SMA.50.ind3 SPY.Close.SMA.200.ind4
2008-12-31     82.08                    NA                     NA
2009-01-02     84.56                    NA                     NA
2009-01-05     84.46                    NA                     NA
2009-01-06     85.02                    NA                     NA
2009-01-07     82.47                    NA                     NA
2009-01-08     82.81                    NA                     NA

Thanks for the quick (and reasonably friendly!) ;-) response.

Cheers,
Mark