简体   繁体   English

如何在 Bloomberg Python API 订阅中检查订阅状态是否错误?

[英]How to check if the subscription status is bad in Bloomberg Python API subscription?

I am writing a program for doing Bloomberg data-feed check using the subscription method of Python API.我正在编写一个程序,用于使用 Python API 的订阅方法进行彭博数据馈送检查。 I am close to finishing it and I am now trying to cover edge cases such as a failed subscription.我即将完成它,我现在正在尝试涵盖诸如订阅失败之类的边缘情况。 I want to check if a subscription has failed.我想检查订阅是否失败。 If it fails, I will write it into a file named BadSubscription.txt.如果失败,我会将其写入名为 BadSubscription.txt 的文件中。

One of he example programs that come with Bloomberg API package, SimpleSubcriptionExample.py, has just 1 line of code for Subscription Status so it doesn't give me a clear idea.彭博 API 包附带的示例程序之一 SimpleSubcriptionExample.py 只有 1 行代码用于订阅状态,所以它没有给我一个清晰的想法。

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)

The above code prints the following when a subscription fails for subscribing to a security/equity that doesn't exist:当订阅因订阅不存在的证券/股票而失败时,上述代码将打印以下内容:

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

And when a subscription is successful it prints:当订阅成功时,它会打印:

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

What I want to do is write an if statement for my program to catch the SubscriptionFailure and write the message to the file:我想要做的是为我的程序编写一个 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)

I am looking for a condition to use in my if statement.我正在寻找在我的 if 语句中使用的条件。

I tried reading the following repository but it doesn't explain much, too.我尝试阅读以下存储库,但它也没有解释太多。 https://bloomberg.github.io/blpapi-docs/python/3.13/_autosummary/blpapi.Session.html?highlight=subscription%20status https://bloomberg.github.io/blpapi-docs/python/3.13/_autosummary/blpapi.Session.html?highlight=subscription%20status

I first tried我第一次尝试

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

as the condition but that didn't work.作为条件,但这不起作用。

Thanks to @assylias I found the solution.感谢@assylias,我找到了解决方案。

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)

The above code writes the following to my file:上面的代码将以下内容写入我的文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM