[R-SIG-Finance] Calculating trailing returns

Michael Weylandt michael.weylandt at gmail.com
Mon Oct 26 17:01:11 CET 2015

On Mon, Oct 26, 2015 at 9:53 AM, Am Gut <agquantr at gmail.com> wrote:

> 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:

You're not passing the function argument correctly (you're passing
'trim=function(x) prod(1+x)-1'). Check the function signature.

M1 <- managers[, 1, drop=FALSE]

apply.rolling(M1, width=5, FUN=function(x) prod(1+x)-1)[5] ## cumulative
return of first 5 observations

## compare to manual calculation
prod(M1[1:5]+1) - 1


> ##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)

	[[alternative HTML version deleted]]

More information about the R-SIG-Finance mailing list