pythonpython-3.9bloombergblpapi

Bloomberg API request timed out - Python


I am a Bloomberg Terminal user and can't establish a connection with the Python Bloomberg API.

I was able to install the blpapi package like described on the Bloomberg help page and tried to import the xbbg package to get some data.

https://www.bloomberg.com/professional/support/api-library/

https://pypi.org/project/xbbg/

When I run this:

import blpapi
from xbbg import blp

blp.bdp(tickers='NVDA US Equity', flds=['Security_Name', 'GICS_Sector_Name'])

the following error message appears:

12APR2022_15:07:37.756 33312:20836 ERROR blpapi_metadatamanagerimpl.cpp:247 blpapi.session.metadatamanager.{1} Resolve request timed out { RequestId=NULL }  
Traceback (most recent call last):
  File "C:\DevLab\MyMainEnv\lib\site-packages\IPython\core\interactiveshell.py", line 3361, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-16-83038899ddda>", line 5, in <cell line: 5>
    blp.bdp(tickers='NVDA US Equity', flds=['Security_Name', 'GICS_Sector_Name'])
  File "C:\DevLab\MyMainEnv\lib\site-packages\xbbg\blp.py", line 47, in bdp
    request = process.create_request(
  File "C:\DevLab\MyMainEnv\lib\site-packages\xbbg\core\process.py", line 47, in create_request
    req = srv.createRequest(request)
  File "C:\DevLab\MyMainEnv\lib\site-packages\blpapi\service.py", line 393, in createRequest
    _ExceptionUtil.raiseOnError(errCode)
  File "C:\DevLab\MyMainEnv\lib\site-packages\blpapi\exception.py", line 143, in raiseOnError
    _ExceptionUtil.raiseException(errorCode, description)
  File "C:\DevLab\MyMainEnv\lib\site-packages\blpapi\exception.py", line 135, in raiseException
    raise errorClass(description, errorCode)
blpapi.exception.InvalidArgumentException: Null service handle (0x00020002)
'blpapi' in sys.modules 
'xbbg' in sys.modules

gives me True

while

'blp' in sys.modules

gives me False

any ideas why this error occurs?


Solution

  • This is not an answer, but some diagnostic code to isolate whether the error is coming from Bloomberg (blpapi) or the xbbg wrapper that uses this API.

    It shows how to access Bloomberg data at the API level, without xbbg. If this code runs successfully then your Bloomberg connection is fine, and the problem is with xbbg, and vice-versa.

    import blpapi
    
    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost('localhost')
    sessionOptions.setServerPort(8194)
    
    session = blpapi.Session(sessionOptions)
    
    session.start()
    
    if session.openService('//blp/refdata'):
        svc = session.getService('//blp/refdata')
    
        req = svc.createRequest('ReferenceDataRequest')
    
        req.append('securities','NVDA US Equity')
        req.append('fields','SECURITY_NAME')
        req.append('fields','GICS_SECTOR_NAME')
    
        session.sendRequest(req)
    
        results = None
    
        while(True):
            ev = session.nextEvent()
    
            if ev.eventType() == blpapi.Event.RESPONSE:
                for msg in ev:
                    for elt in msg.asElement():
                        results = { e.getElementValue('security'): 
                                     { str(f.name()) : f.getValueAsString() 
                                         for f in e.getElement('fieldData') } 
                                   for e in elt }
                break
    
        print(results)
    

    The output should be:

    {'NVDA US Equity': {'SECURITY_NAME': 'NVIDIA Corp', 'GICS_SECTOR_NAME': 'Information Technology'}}
    

    This is essentially what xbbg is doing when you call bdp(), and tests whether the basic Bloomberg API is working.