如何检查Bloomberg Python API订阅中的订阅状态是否不好



我正在编写一个程序,用于使用Python API的订阅方法进行Bloomberg数据源检查。我即将完成它,现在我正在努力解决诸如订阅失败之类的边缘情况。我想检查订阅是否失败。如果失败,我会将其写入名为BadSubscription.txt的文件中。

彭博API软件包中的一个示例程序SimpleSubcriptionExample.py只有一行订阅状态代码,所以它并没有给我一个清晰的想法。

try:
# Process received events
eventCount = 0
while(True):
# We provide timeout to give the chance to Ctrl+C handling:
event = session.nextEvent(15000)
for msg in event:
if event.eventType() == blpapi.Event.SUBSCRIPTION_STATUS or 
event.eventType() == blpapi.Event.SUBSCRIPTION_DATA:
print("%s - %s" % (msg.correlationIds()[0].value(), msg))
else:
print(msg)

上面的代码在订阅不存在的证券/股权失败时打印以下内容:

SubscriptionFailure = {
reason = {
errorCode = 2
description = "Invalid security, rcode = -11"
category = "BAD_SEC"
source = " [nid:3924]:bbdbm10"
}
}

当订阅成功时,它会打印:

SubscriptionStarted = {
exceptions[] = {
}
streamIds[] = {
"1"
}
receivedFrom = {
address = "localhost:8194"
}
reason = "Subscriber made a subscription"
}

我想做的是为我的程序编写一个if语句,以捕获SubscriptionFailure并将消息写入文件:

for msg in event:
if (event.eventType() == blpapi.Event.SUBSCRIPTION_STATUS 
and (**the condition to catch the error**)):
f = open("BadSubscription.txt", "a+")
f.write(msg)

我正在寻找一个条件,以便在if语句中使用。

我试着阅读下面的存储库,但它也没有解释太多。https://bloomberg.github.io/blpapi-docs/python/3.13/_autosummary/blpapi.Session.html?highlight=subscription%20status

我第一次尝试

msg.correlationIds()[0].value().find("SubscriptionFailure")!=-1

作为条件,但没有起作用。

感谢@assilias,我找到了解决方案。

for msg in event:
if (event.eventType() == blpapi.Event.SUBSCRIPTION_STATUS 
and msg.messageType() == "SubscriptionFailure"):
f = open("BadSubscription.txt", "a+")
s = ""
if msg.getElement("reason").getElement("errorCode").getValueAsInteger() !=12:
s = msg.toString()
f.write(s)

上面的代码将以下内容写入我的文件:

SubscriptionFailure = {
reason = {
errorCode = 2
description = "Invalid security, rcode = -11"
category = "BAD_SEC"
source = " [nid:235]:bbdbm10"
}
}

相关内容

  • 没有找到相关文章

最新更新