# [R-SIG-Finance] How to compute percentage change of xts ?

markleeds at verizon.net markleeds at verizon.net
Wed Jul 2 21:16:08 CEST 2008

```  hi: below is a function that takes a series of numbers ( assumed to be
prices ) and calculates the associated series of returns.
you have to either call it 4 times for open, high,low and close  or
modify it to deal with multiple series simultaneously. I imagine there
have to
be R  packages that do below in a much fancier way such as you're asking
about but below works in the simple case where you just have
a series of prices ( numeric vectors. not xts objects ).

#==============================================
# function calculates arithmetic cumulative returns given a vector of
prices

calcreturns <-
function(prices)
{
P1 <- prices[-length(prices)]
P2 <- prices[-1]
returns <- P2 / P1 - 1
c(0,returns)
}

#=========================================================================

On Wed, Jul 2, 2008 at  2:56 PM, pierre8r-list at yahoo.fr wrote:

> Helllo,
>
> Sorry, I repost my e-mail because I forgot the title.
>
> My script creates an xts object.
>
>
> First question:
> I would like to compute for each Open Close from this time-series, the
> percentage change.
> Is there a function that realizes this?
>
> Second question:
> I wish also to calculate the percentage change between two consecutive
> Close.
> Is there a function that realizes this?
>
> Thank you,
>
> Pierre8r
>
>
>
>       ____________________________________________________________
> ente http://mail.yahoo.fr
>
> _______________________________________________
> 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.

```