How to use XTS period.apply() using TTR indicator functions?
I can't seem to use TTR indicator functions direclty with period.apply() from XTS. Please help me figure out what I'm doing wrong.
> require(TTR)
> require(quantmod)
> require(xts)
> data(sample_matrix)
> period.apply(sample_matrix, endpoints(开发者_C百科sample_matrix,"weeks"), RSI)
Error in EMA(c(NA, 0.190714286249097, 0.190459011271606, 0, 0, 0, NA, :
Invalid 'n'
I also tried to as.xts(sample_matrix)
first but it doesn't help.
Based on the comments to your question, I think you want something like this:
> RSI(Cl(to.period(sample_matrix, "weeks", 2)),4)
[,1]
2007-01-07 NA
2007-01-21 NA
2007-02-04 NA
2007-02-18 NA
2007-03-04 58.42659
2007-03-18 40.25955
2007-04-01 27.12793
2007-04-15 50.26745
2007-04-29 38.97652
2007-05-13 22.03943
2007-05-27 28.75952
2007-06-10 22.21261
2007-06-24 21.58207
2007-06-30 41.69338
精彩评论