Rblp / Rblpapi

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

Filling holidays historical prices with the last available price #379

Closed OussCHE closed 1 year ago

OussCHE commented 1 year ago

When pulling historical data (bdh),I am trying to replace the N/A when I have holidays (or unavailable values) with the last available quote.

For example,

library(Rblpapi)

blpConnect()
K <-bdh('OAH3 Comdty',
        fields = 'PX_LAST',
        start.date = '20221226',
        end.date = '20221226',
        include.non.trading.days = TRUE)

The values are NA,I try to get the last available value, so I added the following options parameter

library(Rblpapi)

blpConnect()
K <-bdh('OAH3 Comdty',
        fields = 'PX_LAST',
        start.date = '20221226',
        end.date = '20221226',
        include.non.trading.days = TRUE,
        options = c(Days = "A",Fill = "P"))

Error in bdh_Impl(con, securities, fields, start.date, end.date, options,  : 
  Sub-element '(null)' does not exist.

It should be known that using EXCEL the previous solution works " BDH("OAH3 COMDTY"; "PX_LAST"; "26/12/2022"; "26/12/2022"; "days"; "A"; "FILL"; "P") "

I also tried the following solutions but they also didn't work.

johnlaing commented 1 year ago

This is a case where the Excel and C++ APIs differ. It's documented in the dev guide but hard to find if you're not sure what to look for.

Try this:

bdh('OAH3 Comdty', fields = 'PX_LAST', start.date = '20221226', end.date = '20221226',
options = c(nonTradingDayFillOption="ALL_CALENDAR_DAYS", nonTradingDayFillMethod="PREVIOUS_VALUE"))
OussCHE commented 1 year ago

Thank you @johnlaing , It actuallty worked for a lot of products, but for some , it didn't work e.g:

blpConnect()
K <-bdh('/isin/AU2CLT240335', fields = 'PX_LAST', start.date = '20221226', end.date = '20221226',
        options = c(nonTradingDayFillOption="ALL_CALENDAR_DAYS", nonTradingDayFillMethod="PREVIOUS_VALUE"))

I still receive NA. I guess some Aussies have weird holidays :)

johnlaing commented 1 year ago

That one works ok for me too. Possibly it is something with your settings. Does it work in Excel? This might be a question for the helpdesk.

OussCHE commented 1 year ago

I am actually getting plain wrong value, when I ask for Last Price ,I am getting the Yield.I will ask the helpdesk about the reason.