访问消息元素中的数据时出错

时间:2018-06-21 15:26:08

标签: python python-3.x bloomberg blpapi

我在尝试处理ReferenceDataRequest时遇到问题。这是我用来填写会话的所有代码。

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()

我收到此错误。 blpapi.exception.UnknownErrorException: Attempt access name 'fieldData' on array element 'securityData' (0x00000003)

这是收到msg的样子。

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

反正有解决此问题的方法吗?

2 个答案:

答案 0 :(得分:0)

解决方案是缩小msg元素和值的范围。经过大量测试,但这是解决我特定问题的方法。

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()

看看DAPI blpapi目录中提供的示例对我有帮助。

答案 1 :(得分:0)

您也可以这样做

data = msg.getElement("securityData").getElement("securityData").getElement("fieldData").getElement("FUT_DAYS_EXPIRE").getValue()

我相信getValue()之所以有用,是因为securityData []数组中只有一个值。