访问消息元素中的数据时出错
Error Accessing Data in Message Element
我在尝试处理 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
}
}
}
}
有办法解决这个问题吗?
解决方案是缩小 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 目录中提供的示例对我有所帮助。
你也可以
data = msg.getElement("securityData").getElement("securityData").getElement("fieldData").getElement("FUT_DAYS_EXPIRE").getValue()
.
我相信 getValue() 起作用是因为 securityData[] 数组中只有一个值。
我在尝试处理 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
}
}
}
}
有办法解决这个问题吗?
解决方案是缩小 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 目录中提供的示例对我有所帮助。
你也可以
data = msg.getElement("securityData").getElement("securityData").getElement("fieldData").getElement("FUT_DAYS_EXPIRE").getValue()
.
我相信 getValue() 起作用是因为 securityData[] 数组中只有一个值。