2

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?

user3062260
  • 1,584
  • 4
  • 25
  • 53

1 Answers1

2

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
mgilbert
  • 3,495
  • 4
  • 22
  • 39
  • Thanks, its helpful to see a working example of con.bdib. Would you happen to know how to get con.bdh to return all fields for a given ticker without specifically naming them? The docs don't seem to mention this either. For example I'm interested in the 'Inventory' field of 'CGIEEUST' but if I try con.bdh('CGIEEUST Index', ['INDEX'], '20190101', '20190205') another cryptic error comes up. I'm happy to post this as a separate question? – user3062260 Feb 04 '19 at 16:51
  • This is not how the underlying `HistoricalDataRequest` API exposed by bloomberg works, desired fields must be explicitly queried. I'm unsure if there is a programmatic way to query a ticker for all relevant fields as I have never come across this specific use case. I think your best bet would be to inquire directly with bloomberg help to first see if this is even supported on their end. – mgilbert Feb 04 '19 at 20:05
  • Sorry about the newbie question, thanks again for your help – user3062260 Feb 05 '19 at 10:03