[R-SIG-Finance] quantstrat - problems adding multiple indicators
Brian G. Peterson
brian at braverock.com
Wed Sep 10 19:06:40 CEST 2014
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:
> 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