Bloomberg Api for Python:响应中缺少部分结果

Bloomberg Api for Python: Parts of result missing in response

我正在使用 bloomberg api for python 来获取选项数据。首先,我得到了期权链的所有符号。然后我用它们来获得出价和要价。通过函数getOptionChain,有400多个选项,我查看了结果,没问题。然而,当我运行 getPX 函数时,我最终只得到了10 个结果。谁能帮我调查一下?提前致谢!

import blpapi
import pandas
import csv
options = blpapi.SessionOptions()
options.setServerHost('localhost')
options.setServerPort(8194)

SECURITY_DATA = blpapi.Name("securityData")
SECURITY = blpapi.Name("security")
FIELD_DATA = blpapi.Name("fieldData")
FIELD_ID = blpapi.Name("fieldId")
OPT_CHAIN = blpapi.Name("OPT_CHAIN")
SECURITY_DES = blpapi.Name("Security Description")


def getOptionChain (sec_list):
    session = blpapi.Session(options)
    session.start()
    session.openService('//blp/refdata')
    refDataService = session.getService("//blp/refdata")
    request = refDataService.createRequest("ReferenceDataRequest")
    for s in sec_list:
        request.append("securities",s)
    request.append("fields", "OPT_CHAIN")
    cid = session.sendRequest(request)
    try:
        # Process received events
        while(True):
            # We provide timeout to give the chance to Ctrl+C handling:
            ev = session.nextEvent(500)
            response = []
            for msg in ev:
                if cid in msg.correlationIds():
                    securityDataArray = msg.getElement(SECURITY_DATA)
                    for securityData in securityDataArray.values():
                        fieldData = securityData.getElement(FIELD_DATA)
                        for field in fieldData.elements():
                            for n in range(field.numValues()):
                                fld = field.getValueAsElement(n)
                                response.append (fld.getElement(SECURITY_DES).getValueAsString())
            # Response completely received, so we could exit
            if ev.eventType() == blpapi.Event.RESPONSE:
                break
    finally:
        # Stop the session
        session.stop()
    return response



def getPX (sec_list, fld_list):
    opt_chain_list = getOptionChain(sec_list)
    session = blpapi.Session(options)
    session.start()
    session.openService('//blp/refdata')
    refDataService = session.getService("//blp/refdata")
    request = refDataService.createRequest("ReferenceDataRequest")
    for s in opt_chain_list:
        request.append("securities",s)
    for f in fld_list:
        request.append("fields",f)
    cid = session.sendRequest(request)
    try:
        # Process received events
        while(True):
            # We provide timeout to give the chance to Ctrl+C handling:
            ev = session.nextEvent(500)
            response = {}
            for msg in ev:
                if cid in msg.correlationIds():
                    securityDataArray = msg.getElement(SECURITY_DATA)
                    for securityData in securityDataArray.values():
                        secName = securityData.getElementAsString(SECURITY)
                        fieldData = securityData.getElement(FIELD_DATA)
                        response[secName] = {}
                        for field in fieldData.elements():
                            response[secName][field.name()] = field.getValueAsFloat()
            # Response completely received, so we could exit
            if ev.eventType() == blpapi.Event.RESPONSE:
                break
    finally:
        # Stop the session
        session.stop()                   
    tempdict = {}
    for r in response:
        tempdict[r] = pandas.Series(response[r])
        data = pandas.DataFrame(tempdict)
    return data
                

sec = ["IBM US Equity"]
fld = ["PX_ASK","PX_BID"]
getPX(sec,fld)

看起来你把 "response = {}" 放在了错误的地方。 目前,您在循环的每次迭代中都在清除,因此每个进入的事件都会重新填充它。 如果将 "response = {}" 移动到 "While(True):" 之前,每次迭代都会附加到它而不是清除和重新填充。

第一个函数也是如此,但在这种情况下,批量数据会在单个事件中返回。如果您使用多种证券,您会看到相同的问题(单个 Bloomberg refdata(部分)响应包含最多 10 种证券的数据)。