Question

I can convert a daily series to weekly as below:

library(quantmod)
getSymbols("SPY", from="2013-01-01", to=Sys.Date())
chartSeries(SPY)
datW <- to.weekly( SPY)

But the series create weekly bars with a Close on a Friday. How can I change this to create mid week bars so the Weekly Bars show the Close on a Wednesday?

Thank you for your help.

Was it helpful?

Solution

It doesn't look like there's any easy way to do this. If you look at the source for to.period it basically wraps around an external call to .toPeriod to which it passes endpoints(x, period, k):

    xx <- .Call("toPeriod", x, endpoints(x, period, k), has.Vo(x), 
        has.Vo(x, which = TRUE), has.Ad(x) && is.OHLC(x), 
        index_at, cnames, PACKAGE = "xts")

The endpoints function also works by external call. So if you weekly periods that end on Wednesdays, it looks like you are either going to have to code it up yourself, or else look for some different library.

As a starting point, it's pretty easy to find rows of your data that occur on a Wednesday, like this:

> wednesdayRows<-which(as.POSIXlt(index(SPY))$wday==3)
> head(SPY[wednesdayRows,])
           SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
2013-01-02   145.11   146.15  144.73    146.06  192059000       143.95
2013-01-09   145.87   146.32  145.64    145.92   90745600       143.81
2013-01-16   146.77   147.28  146.61    147.05  104849500       144.92
2013-01-23   149.13   149.50  148.86    149.37  104596100       147.21
2013-01-30   150.64   150.94  149.93    150.07  137447700       147.90
2013-02-06   150.52   151.26  150.41    151.16  138762800       148.97

So now you just need to aggregate the OHLC data properly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top