[R-SIG-Finance] Monthly Midpoint return

G See gsee000 at gmail.com
Wed Jun 17 16:37:23 CEST 2015

Try this

    data(sample_matrix) # load sample data first
    x <- as.xts(sample_matrix)

    do.call(rbind, lapply(split(x, "months"),
                          function(xx) xx[ceiling(nrow(xx)/2)]))

This code splits the object by months, then uses lapply() to loop over
each month and apply a function which selects the row that is in the
middle of the month.  (you could replace ceiling() with floor() or

Once you have the subsetted data, you can calculate returns however
you normally would (ROC(), Delt(), dailyReturn(), etc.)


On Wed, Jun 17, 2015 at 9:30 AM, Mark Knecht <markknecht at gmail.com> wrote:
> On Wed, Jun 17, 2015 at 7:23 AM, Phil Steel <steelsteel25 at outlook.com> wrote:
>> Hi Brian and everybody else,
>> its downloaded stock prices I want to transform to middle of month stock price.
>> endpoints give end of month doesn't it?if Im using endpoints I get a really strange result with a lot of NA:s My Data starts with the 15:th som my idea it transforming the data to.monthly somehow
>> My function is ("provided" by Ulrich):
>> monthly = function(x){  sym <- sub("\\..*$", "", names(x)[1])  Ad(to.monthly(x, indexAt = 'lastof', drop.time = TRUE, name = sym))}
> Possibly put the month in an array, determine the size of the array
> and then take the price/returns at the midpoint of the array? That way
> number of days in the month, holidays, etc. wouldn't matter much?
> - Mark
> _______________________________________________
> 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.

More information about the R-SIG-Finance mailing list