1

First a reproducible example:

library(quantstrat)

getSymbols("AAPL")

Test<-period.apply(AAPL,endpoints(AAPL,on="weeks",k=10),ROC)
TestDF<-as.data.frame(Test)

I want to get the ROC for a certain stock or whatever for x weeks. Or in other words, I want to compare several stocks and rank them with their 10-week ROC, 20 week ROC etc. Obviously the period apply works, however when I want to convert it to a data Frame and look at my data I always get this error:

Error in coredata.xts(x) : currently unsupported data type

Any idea whats wrong?

MichiZH
  • 5,587
  • 12
  • 41
  • 81

1 Answers1

3

period.apply requires a function that returns a single row. ROC does not return a single row. So define your own function to do that.

Test <- period.apply(AAPL, endpoints(AAPL,on="weeks",k=10),
  function(x) log(last(x)/coredata(first(x))))
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Thanks Joshua, almost what I'm looking for. I didn't state clearly what I need..basically I want to rank certain assets (I don't have HLOC data for them, only close data) according to their x-week ROC. But I want to rank them daily. So the function should actually return the same amount of rows as my original data, but the lookback period shifts of course every day..How would I achieve that in that apple example? – MichiZH Dec 09 '13 at 14:06
  • 2
    @MichiZH: then you're going about it backwards. 1) calculate a x-week ROC, then 2) call `apply.daily` on the x-week ROC with `function(x) rank(coredata(x))`. – Joshua Ulrich Dec 09 '13 at 14:16
  • ah ok getting closer thanks :-) My last problem is how to calculate an x-week ROC? I could use period n=50 for 10 week ROC but this isn't correct if there are Holidays in between or the very first data we have is only weekly anyway, there it's not correct either.. – MichiZH Dec 09 '13 at 14:19
  • 1
    @MichiZH: You have to handle that manually. E.g. what's the x-week ROC on the day that's x-weeks after a holiday? It's undefined. You probably want to make your data regular by something like `merge(x, .xts(,seq(start(x),end(x),by="1 day")), fill=na.locf)`. – Joshua Ulrich Dec 09 '13 at 14:39
  • Thx alot, you're right..it's too undefined. Ok problem solved :-) – MichiZH Dec 09 '13 at 14:48