# [R-SIG-Finance] Calculating trailing returns

Am Gut agquantr at gmail.com
Mon Oct 26 15:53:03 CET 2015

```Good Morning Michael,

I have simple return data, in a daily periodicity. I am essentially trying
to calculate the trailing returns for say 252 periods, assuming I am trying
to look at trailing 12 month returns. So I have been trying to use a
product function, but I am having trouble specifying a look back period. I
did use the apply.rolling function from the PerformanceAnalytics package,
but was unable to us the prod function with it- the function was simply
returning an average:

##compute 12-month (lookback variable) returnm
List = list()
for (i in 1:ncol(sector_xts))
{
List[[i]] = apply.rolling(sector_xts[,i], function(x) prod(1+x)-1, width
= 252)

}
lookback_returns = do.call(cbind,List)

Is there any way to set a look back or trailing period number on the
product function? If not is there any ways that you guys can suggest to
calculate trailing returns of larger periodicity from daily return data?

Kind Regards,

AG

On Mon, Oct 26, 2015 at 10:34 AM, Michael Weylandt <
michael.weylandt at gmail.com> wrote:

>
> > On Oct 26, 2015, at 9:21, Am Gut <agquantr at gmail.com> wrote:
> >
> > Dear R Users,
> >
> > I am trying to calculate trailing month and annual returns with daily
> data.
> > The data is already in returns, and not in price. I have been looking
> for a
> > handy function to calculate trailing returns where I can specify the look
> > back period (number of lags), but have been completely unsuccessful. Can
> > anyone please suggest a way that I can calculate trailing returns from my
> > dataset of many different return streams? Any help would be greatly
> > appreciated.
>
> Assuming you have geometric/log/continuously-compounded returns, aren't
> you just looking for a rolling sum? TTR and zoo both provide this.
>
> >
> > Kind Regards,
> >
> > AG
> >
> >    [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-SIG-Finance at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > -- Subscriber-posting only. If you want to post, subscribe first.
> > -- Also note that this is not the r-help list where general R questions
> should go.
>

[[alternative HTML version deleted]]

```