query bloomberg api with python; tickers return nothing

4.3k Views Asked by At

I'm new to bloomberg terminals but I'm trying to pull data from bloomberg using the python API. The underlying c++ SDK seems to be working as I have pip installed the following python libraries:

blpapi
pdblp

I can connect to the terminal and run the example data that comes with the packages:

con = pdblp.BCon(debug=False, port=8194, timeout=5000)
con.start()
# print some data
con.bdh('SPY US Equity', ['PX_LAST', 'VOLUME'],'20150629', '20150630')

This returns the following:

ticker     SPY US Equity
field            PX_LAST       VOLUME
date
2015-06-29        205.42  202621332.0
2015-06-30        205.85  182925106.0

So everything seems to be working. The problem is if I want to try searching certain tickers it just returns an empty datafame:

con.bsrch('COH9')  #returns []
con.bsrch("COMDTY:COH9") 
con.bsrch('COH9 Comdty')
con.bsrch("COMDTY")
con.bsrch('CL1 Comdty')
con.bsrch('CO1 Comdty')

All of these return []. the 'bsrch' method should work because the following example provided in the readme works and fetches data:

con.bsrch("COMDTY:NGFLOW")

The issue is that each of these strings returns something in the bloomberg terminal yet returns nothing with this api. why? The docs say this is a search function?

I've tried other commands such as:

con.bdib('CL1 Comdty', start_datetime='20190127', end_datetime='20190128', event_type='BID', interval=1)

which also throws an error:

Traceback (most recent call last):
  File "bloomberg_api_test.py", line 56, in <module>
    bloomberg_api_test()
  File "bloomberg_api_test.py", line 38, in bloomberg_api_test
    print(con.bdib('CL1 Comdty', start_datetime='20190127', end_datetime='20190128', event_type='BID', interval=1))
  File "C:\Users\svc_tradingops\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pdblp\pdblp.py", line 681, in bdib
    data = pd.DataFrame(data).set_index('time').sort_index().loc[:, flds]
  File "C:\Users\svc_tradingops\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 4156, in set_index
    raise KeyError('{}'.format(missing))
KeyError: "['time']"

There doesn't seem to be much clear guidance in the docs about how to use these methods unless there is something I missed?

1

There are 1 best solutions below

3
On

Admittedly the bsrch is not a very well documented feature of the library. One of the issues around this is that bsrch uses makes an ExcelGetGridRequest via the underlying blpapi library and this service is not documented by Bloomberg. As of the last time I checked there is no information in the Services & schemas reference guide or the Core developer guide from Bloomberg about an ExcelGetGridRequest. One way to figure out if this is an issue with the underlying blpapi service or some handling via pdblp is to set debug=True, which will print the underlying response messages. e.g.

import pdblp
con = pdblp.BCon(debug=True).start()
con.bsrch("COMDTY:COH9")

The message below seems to indicate this is an invalid domain. As mentioned above there is not much documentation on what this means since the ExcelGetGridRequest service is not well documented but Bloomberg support may be able to shed more light on this service.

pdblp.pdblp:INFO:Sending Request:
ExcelGetGridRequest = {
    Domain = "COMDTY:COH9"
}

pdblp.pdblp:INFO:Event Type: 'RESPONSE'
pdblp.pdblp:INFO:Message Received:
GridResponse = {
    NumOfFields = 0
    NumOfRecords = 0
    ColumnTitles[] = {
    }
    DataRecords[] = {
    }
    ReachMax = false
    Error = "The domain entered: COMDTY:COH9 is not valid."
    SequenceNumber = 0
}

For bdib, as per the docstrings the start_datetime and end_datetime format should be YYYY-mm-ddTHH:MM:SS, but admittedly the error that is raised when this is not satisfied is a bit misleading. For example

con.bdib('CL1 Comdty', start_datetime='2019-01-28T10:00:00', 
         end_datetime='2019-01-28T10:05:00', event_type='BID', interval=1)

                      open   high    low  close  volume  numEvents
time                                                              
2019-01-28 10:00:00  52.62  52.67  52.62  52.66   10147        700
2019-01-28 10:01:00  52.66  52.69  52.64  52.69    9181        608
2019-01-28 10:02:00  52.69  52.70  52.68  52.69   12349        732
2019-01-28 10:03:00  52.69  52.71  52.68  52.70   11816        631
2019-01-28 10:04:00  52.70  52.70  52.67  52.69    8629        523