Rblp / Rblpapi

R package interfacing the Bloomberg API from https://www.bloomberglabs.com/api/
Other
166 stars 75 forks source link

Time stamp and periodicity #245

Open scfhath opened 6 years ago

scfhath commented 6 years ago

Hello,

I have been working with your excellent package, Rblpapi.

I have a couple of questions:

• I primarily work with economic data and find that for some series the dates don’t line up with the last calendar day of the month, to enable easy merging with other datasets. I have tried setting include.non.trading.days = TRUE but that does not seem to work. I have managed to fudge around this (see code below) but wondered whether there was something I could include in the bdh function, the equivalent of indexAt = ‘lastof’, that could make this easier?

• If setting a lower frequency of data, eg. Daily or weekly to quarterly, is there a way to pull the average of the quarter rather than automatically pulling the close price? I basically want to do the Excel equivalent of setting Period = CQ and Quote = A but can’t seem to make it work in R.

I essentially want to end up with an xts object, timestamped on the last calendar day of the quarter, with quarterly data (averaged over the quarter rather than the closing value)... Any direction would be greatly appreciated. Thank you!

library(Rblpapi) blpConnect()

Set time period

start_date <- as.Date("2006/10/01", "%Y/%m/%d") end_date <- as.Date("2017/06/30", "%Y/%m/%d") opt <- c("periodicitySelection"="QUARTERLY") securities <- c("....")

Import data from BBG, convert to xts

bbg <- bdh(securities, "PX_LAST", include.non.trading.days = TRUE, start.date = start_date, end.date = end_date, options = opt) bbg <- as.data.frame(bbg) rownames(bbg) <- bbg[,1] bbg <- bbg[, -grep("date", colnames(bbg))] colnames(bbg) <- c("tenyr", "baa", "somambs", "spx", "fdtr", "somabond", "tp") bbg <- as.xts(bbg) bbg <- to.monthly(bbg, indexAt = 'lastof', OHLC = FALSE)

joel23888 commented 6 years ago

@scfhath if you want to explore what options are available you can refer to the BLPAPI developer guide at https://data.bloomberglp.com/professional/sites/10/2017/03/BLPAPI-Core-Developer-Guide.pdf. In this case I think you may find what you are looking for around pp 92-93.