Bloomberg API 请求超时 - Python

Bloomberg API request timed out - Python

我是 Bloomberg Terminal 用户,无法与 Python Bloomberg API.

建立连接

我能够按照彭博帮助页面上的描述安装 blpapi 包,并尝试导入 xbbg 包以获取一些数据。

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

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

当我运行这个:

import blpapi
from xbbg import blp

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

出现以下错误消息:

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

给我 True

'blp' in sys.modules

给我 False

知道为什么会出现此错误吗?

这不是答案,而是一些诊断代码,用于隔离错误是来自 Bloomberg (blpapi) 还是来自使用此 API 的 xbbg 包装器。

它展示了如何在 API 级别访问 Bloomberg 数据,而无需 xbbg。如果此代码成功运行,则您的 Bloomberg 连接正常,问题出在 xbbg 和 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)

输出应该是:

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

这基本上就是当您调用 bdp() 时 xbbg 正在做的事情,并测试基本的 Bloomberg API 是否正常工作。