[R-SIG-Finance] XTS - endpoints omits price changes

James j at jtoll.com
Wed Jul 16 23:30:31 CEST 2008


Hi,


I've been learning to use the XTS package and have run into a  
problem.  If I calculate monthly log normal price relatives as such:

 > df<-yahooSeries("QQQQ", from = "2007-01-01", to = "2007-12-31",  
returnClass=c("data.frame"))
 > x<-as.xts(df)
 > names(df)<-c("Open","High","Low","Close","Volume")
 > period.apply(x$Close, INDEX=endpoints(x, 'months'), FUN=function 
(x) sum(diff(log(x))))
2007-01-31   2007-02-28   2007-03-30   2007-04-30   2007-05-31    
2007-06-29   2007-07-31   2007-08-31   2007-09-28
  0.019013286 -0.015344398  0.009231545  0.052943687  0.027803331   
0.003367007 -0.010464725  0.020048207  0.050668995
   2007-10-31   2007-11-30   2007-12-31
  0.056634772 -0.051098382  0.006660162

What happens is that the price change between the last day of the  
previous month and the first day of the current month is ignored for  
all 12 months.  This is a problem because I should, at least in my  
opinion, be able to add all twelve monthly changes to get the yearly  
change.  And that should be the same as:

 > period.apply(x$Close, INDEX=endpoints(x, 'years'), FUN=function(x)  
sum(diff(log(x))))
2007-12-31
  0.1693641

But it's not, because 12 daily returns have been left out.  Is there  
a way to change this behavior, so that any given month, or period,  
will include all the price changes?

Thanks,

James



More information about the R-SIG-Finance mailing list