Calculating trailing returns

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Calculating trailing returns

Am Gut
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.

Kind Regards,

AG

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating trailing returns

Michael Weylandt

> On Oct 26, 2015, at 9:21, Am Gut <[hidden email]> 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]]
>
> _______________________________________________
> [hidden email] 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.

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating trailing returns

Am Gut
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 <
[hidden email]> wrote:

>
> > On Oct 26, 2015, at 9:21, Am Gut <[hidden email]> 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]]
> >
> > _______________________________________________
> > [hidden email] 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]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating trailing returns

Michael Weylandt
On Mon, Oct 26, 2015 at 9:53 AM, Am Gut <[hidden email]> 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.

library(PerformanceAnalytics)
data(managers)
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


Michael


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

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Calculating trailing returns

Am Gut
Thanks Michael! This has solved my problem!

On Mon, Oct 26, 2015 at 12:01 PM, Michael Weylandt <
[hidden email]> wrote:

> On Mon, Oct 26, 2015 at 9:53 AM, Am Gut <[hidden email]> 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.
>
> library(PerformanceAnalytics)
> data(managers)
> 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
>
>
> Michael
>
>
>> ##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]]

_______________________________________________
[hidden email] 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.