R and Rblpapi: flexible start dates

544 views Asked by At

I'm using Rblpapi's bdh formula to download Bloomberg time series data and trying to separate the date variable from the rest of the code in order to gain flexibility. I'm struggling, however, to get this working. My code looks as follows:

periods <- c("periodicitySelection"="MONTHLY") #set monthly periodicity
start <- c("start.date"=as.Date("1990-01-01")) #set start date
var1<-bdh("NAPMPMI Index","PX_LAST",start.date=start,options=periods) #download data var1

I get the error "Error in bdh_Impl(con, securities, fields, start.date, end.date, options, : not compatible with STRSXP"

What should my code look like in order to fix this?

Thanks & kind regards

1

There are 1 answers

2
Dirk is no longer here On BEST ANSWER

One of those things -- bdh() wants a simple Date variable, not a named list:

R> periods <- c("periodicitySelection"="MONTHLY")
R> bdh("NAPMPMI Index","PX_LAST",start.date=as.Date("2016-01-01"), options=periods)
         date PX_LAST
1  2016-01-31    48.2
2  2016-02-29    49.5
3  2016-03-31    51.8
4  2016-04-30    50.8
5  2016-05-31    51.3
6  2016-06-30    53.2
7  2016-07-31    52.6
8  2016-08-31    49.4
9  2016-09-30    51.5
10 2016-10-31    51.9
11 2016-11-30    53.2
R> 

Check the examples in the documentation, they all show this usage.

Edit: In case the above was not clear enough:

R> sym <- "NAPMPMI Index"
R> col <- "PX_LAST"
R> sdate <- as.Date("2016-01-01")
R> bdh(sym, col, start.date=sdate, options=periods)
         date PX_LAST
1  2016-01-31    48.2
2  2016-02-29    49.5
3  2016-03-31    51.8
4  2016-04-30    50.8
5  2016-05-31    51.3
6  2016-06-30    53.2
7  2016-07-31    52.6
8  2016-08-31    49.4
9  2016-09-30    51.5
10 2016-10-31    51.9
11 2016-11-30    53.2
R>