[R] Continuasly Compunded Returns with quantmod-data

Dennis Murphy djmuser at gmail.com
Mon Nov 21 05:47:28 CET 2011


Hi:

Start by writing a small utility function to compute the CCR (not to
be confused with the rock band):

ccr <- function(x) 100 * (log(x[-1]) - log(x[-length(x)]))

# Small test:
x <- round(rnorm(10, 5, 0.5), 2)
ccr(x)      # Works on vectors...

# Load up the stock data:
library(quantmod)
getSymbols("GOOG",from="2011-11-01")
GOOG1<-GOOG[,1]

# Try it directly:
> ccr(GOOG[, 1])
           GOOG.Open
2011-11-02         0
2011-11-03         0
2011-11-04         0
2011-11-07         0
2011-11-08         0
2011-11-09         0
2011-11-10         0
2011-11-11         0
2011-11-14         0
2011-11-15         0
2011-11-16         0
2011-11-17         0

## Hmm, no go. Try this instead:

> apply(GOOG1, 2, ccr)
             GOOG.Open
2011-11-02  0.82403900
2011-11-03  0.35839274
2011-11-04  1.10123942
2011-11-07 -0.03033316
2011-11-08  2.60843853
2011-11-09 -0.78136988
2011-11-10  0.27598990
2011-11-11 -0.76704898
2011-11-14  1.10809039
2011-11-15  0.78637365
2011-11-16 -0.11756255
2011-11-17 -0.33220719
2011-11-18 -1.32834757

If you look at the structure of GOOG1 [str(GOOG1)], you'll see that it
is a one column matrix, so apply() is one way to go, especially if you
want to run the function on multiple columns of a matrix.

HTH,
Dennis

On Sun, Nov 20, 2011 at 3:52 PM, barb <mainzel89 at hotmail.com> wrote:
> Hey guys,
>
> i want to calculate the continuasly compounded returns for stock prices.
>
> Formula for CCR:
> R_t = ln(P_t/P_{t-1})*100
>
> With R:
>
> First i have to modify the vectors, so that they have the same length
> and we start at the second observation.
>
> log(GOOG1[-1]/GOOG1[1:length(GOOG1)-1])*100
>
> That does work with normal vectors.
>
> My Questions:
>
> 1) I want to use this for stock prices.
>
>
> so i use:
>
> library(quantmod)
> getSymbols("GOOG",from="2011-11-01")
> GOOG1<-GOOG[,1]
>
>
> If i use my formula i get only the value "1" for every observation :(
>
>
>
> Thanks for your time and help!
> I appreciate it
>
> Regards
> Tonio
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Continuasly-Compunded-Returns-with-quantmod-data-tp4090014p4090014.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list