XTS - endpoints omits price changes
An alternate (simpler) approach using 'quantmod':
monthlyReturn(x[,'Close'],type='log')
monthly.returns Jan 2007 0.019013286 Feb 2007 -0.016934043 Mar 2007 0.004605120 Apr 2007 0.054321097 May 2007 0.031061724 Jun 2007 0.003999584 Jul 2007 -0.001471671 Aug 2007 0.027802621 Sep 2007 0.050668995 Oct 2007 0.068045785 Nov 2007 -0.069992826 Dec 2007 -0.001755584
sum(monthlyReturn(x[,'Close'],type='log'))
[1] 0.1693641
yearlyReturn(x[,'Close'],type='log')
yearly.returns 2007-12-31 0.1693641 What is being called by periodReturn internally:
Delt(Cl(to.monthly(x)),type='log')
Delt.1.log Jan 2007 NA Feb 2007 -0.016934043 Mar 2007 0.004605120 Apr 2007 0.054321097 May 2007 0.031061724 Jun 2007 0.003999584 Jul 2007 -0.001471671 Aug 2007 0.027802621 Sep 2007 0.050668995 Oct 2007 0.068045785 Nov 2007 -0.069992826 Dec 2007 -0.001755584 see ?periodReturn for more info on formats and options. Jeff
On Wed, Jul 16, 2008 at 4:30 PM, James <j at jtoll.com> wrote:
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
_______________________________________________ R-SIG-Finance at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. -- If you want to post, subscribe first.
Jeffrey Ryan jeffrey.ryan at insightalgo.com ia: insight algorithmics www.insightalgo.com