1
votes

I am fairly new to R and my question is rather basic. I use the Rblpapi package to download data from directly Bloomberg into a variable x. This data is then listed as "20 observations of 2 variables" in my environment data. It consists of a date (ordered by time) on the left and monthly price data, as follows:

-----------------------------
       Date      PX_LAST
-----------------------------
    2014-06-30    55.3;
    2014-07-31    52.1;
    etc...

I now would like to use this data in quantmod but it seems I need xts data. How can I easily convert this sort of data into the xts format, so that I can further work with it? I always get the error:

Error in try.xts(x, error = "chartSeries requires an xtsible object"): chartSeries requires an xtsible object

1

1 Answers

2
votes

bdh() returns a data.frame:

R> ibm <- bdh("IBM US Equity", "PX_LAST", start.date=Sys.Date()-5)
R> ibm
        date PX_LAST
1 2016-12-01  159.82
2 2016-12-02  160.02
3 2016-12-05  159.84
4 2016-12-06  160.35
R> class(ibm)
[1] "data.frame"
R> 

It is very easy to create an xts from this:

R> ibmxts <- xts(ibm[, -1, drop=FALSE],  order.by=ibm[,1])
R> ibmxts
           PX_LAST
2016-12-01  159.82
2016-12-02  160.02
2016-12-05  159.84
2016-12-06  160.35
R> 

Edit: getTicks() and getBars() both let you specify the return type and give you xts (or another type); I just filed an issue to remind us to add this here too...