Search code examples
rbloombergrblpapi

Rblpapi for several securities with different start.date end.date


I recently started using Rblpapi. I think it is a lot easier to use than the Python counterparts. I have a Ddata frame with bond issuance dates and maturity dates, and I would like to extract all the daily yields between those two dates, for all the bonds in my sample. This would require to either specify start/end dates as a vector, or use sapply for each row in my data frame:

       cusip   issuance        mat
1: 000361AA3 10/24/1989 11/01/2001
2: 000361AB1 10/12/1993 10/15/2003
3: 00077DAB5 01/07/1994 01/12/1996
4: 00077DAF6 07/27/1994 08/01/2009
5: 00077TAA2 05/20/1993 05/15/2023

My understanding is that I can only specify start/end dates as a character. My first approach was to put a very early start.date and a very late end.date such that all the bond trades for all the bonds in the sample would be between these two dates:

require(data.table)
require(Rblpapi)

con <- blpConnect()

cusips <- c('00077DAB5 Corp', '00077DAF6 Corp', '44891AAF4 Corp' )
col <- "YLD_YTM_MID"
sdate <- as.Date("1985-01-01") #early date
edate <- as.Date("2017-04-01") #late date
periods <- c("periodicitySelection"="DAILY")

data <- bdh(sym, col, 
            start.date=sdate,end.date = edate, options=periods, con = con)

This however, gave me the following error:

Error: Choice sub-element not found for name 'securityData'.

I would assume the next best alternative is to use sapply


Solution

  • Here's the code I run that works:

    require(data.table)
    require(Rblpapi)
    
    con <- blpConnect()
    
    cusips <- c('00077DAB5 Corp', '00077DAF6 Corp', '44891AAF4 Corp' )
    col <- "YLD_YTM_MID"
    sdate <- as.Date("1985-01-01") #early date
    edate <- as.Date("2017-04-01") #late date
    periods <- c("periodicitySelection"="DAILY")
    
    data <- bdh(cusips, col, #changed sym to cusips
                start.date=sdate,end.date = edate, options=periods, con = con)
    str(data)
    
    List of 3
     $ 00077DAB5 Corp:'data.frame': 0 obs. of  2 variables:
      ..$ date       :Class 'Date'  int(0) 
      ..$ YLD_YTM_MID: num(0) 
     $ 00077DAF6 Corp:'data.frame': 247 obs. of  2 variables:
      ..$ date       : Date[1:247], format: "1997-11-18" "1997-11-19" "1997-11-20" ...
      ..$ YLD_YTM_MID: num [1:247] 6.77 6.74 6.77 6.74 6.77 ...
     $ 44891AAF4 Corp:'data.frame': 262 obs. of  2 variables:
      ..$ date       : Date[1:262], format: "2016-03-16" "2016-03-17" "2016-03-18" ...
      ..$ YLD_YTM_MID: num [1:262] 2.92 2.93 2.88 2.92 2.94 ...