pythonpython-3.xbloombergblpapi

Error Accessing Data in Message Element


I have an issue trying to process a ReferenceDataRequest. Here is all the code I am using to fill in a session.

global options
options = parseCmdLine()
sessionOptions = blpapi.SessionOptions()                                   
sessionOptions.setServerHost(options.host)
sessionOptions.setServerPort(options.port)
session = blpapi.Session(sessionOptions)                            

if not session.start():                                                  
    print ("Failed to start session.")
    return False

if not session.openService("//blp/refdata"):                            
    print ("Failed to open //blp/refdata")
    return False

refDataService = session.getService("//blp/refdata")                   
request = refDataService.createRequest("ReferenceDataRequest")       
request.getElement("securities").appendValue("UX1 Index")             
request.getElement("fields").appendValue("FUT_DAYS_EXPIRE")
cid = session.sendRequest(request)                                    

while(True):                                                            
    ev = session.nextEvent(500)                                         
    for msg in ev:
        if cid in msg.correlationIds():
            # print(msg)
            data = msg.getElement("securityData").getElement("fieldData")

    if ev.eventType() == blpapi.Event.RESPONSE:
            break

print(data)
session.stop()

I am getting this error. blpapi.exception.UnknownErrorException: Attempt access name 'fieldData' on array element 'securityData' (0x00000003).

This is what the msg received looks like.

ReferenceDataResponse = {
    securityData[] = {
        securityData = {
            security = "UX1 Index"
            eidData[] = {
            }
            fieldExceptions[] = {
            }
            sequenceNumber = 0
            fieldData = {
                FUT_DAYS_EXPIRE = 27
            }
        }
    }
}

Is there anyway to fix this issue?


Solution

  • The solution is to just narrow down the msg elements and values. Took a lot of testing but this is the solution to my specific problem.

    refDataService = session.getService("//blp/refdata")                    
    request = refDataService.createRequest("ReferenceDataRequest")         
    request.getElement("securities").appendValue("UX1 Index")             
    request.getElement("fields").appendValue("FUT_DAYS_EXPIRE")
    cid = session.sendRequest(request)                                     
    while(True):                                                           
        ev = session.nextEvent(500)                                        
        for msg in ev:      
            if cid in msg.correlationIds():
                data = msg.getElement("securityData").getValue().getElement("fieldData").getElement("FUT_DAYS_EXPIRE").getValue()
    
    
        if ev.eventType() == blpapi.Event.RESPONSE:
                break
    
    print(data)
    session.stop()
    

    Looking at the provided examples in the DAPI blpapi directory helped me.