Bloomberg Api for Python:部分结果在响应中丢失

时间:2015-03-30 21:42:04

标签: python blpapi

我使用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)




1 个答案:

答案 0 :(得分:3)

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

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