使用 Python 通过 Bloomberg 的新数据 API (COM v3) 实现异步数据?

发布于 2024-08-16 16:31:57 字数 1062 浏览 14 评论 0原文

有谁知道如何使用Python通过Bloomberg的新数据API(COM v3)获取异步数据?我在 wilmott.com 上找到了下面的代码,它运行得很好,但它适用于旧的 API 版本。

有谁知道新版本对应的代码吗?

from win32com.client import DispatchWithEvents
from pythoncom import PumpWaitingMessages, Empty, Missing
from time import time

class BBCommEvent:
    def OnData(self, Security, cookie, Fields, Data, Status):
        print 'OnData: ' + `Data`

    def OnStatus(self, Status, SubStatus, StatusDescription):
        print 'OnStatus'

class TestAsync:
    def __init__(self):
        clsid = '{F2303261-4969-11D1-B305-00805F815CBF}'
        progid = 'Bloomberg.Data.1'

        print 'connecting to BBComm'        
        blp = DispatchWithEvents(clsid, BBCommEvent)
        blp.AutoRelease = False
        blp.Subscribe('EUR Curncy', 1, 'LAST_PRICE', Results = Empty)
        blp.Flush()

        end_time = time() + 5

        while 1:
            PumpWaitingMessages()
            if end_time < time():
                print 'timed out'
                break

if __name__ == "__main__":
    ta = TestAsync()

Does anyone know how to get asynchronous data through Bloomberg's new data API (COM v3) with Python? I found this code below on wilmott.com and it works just fine, but it's for the old API version.

Does anyone know the corresponding code for the new version?

from win32com.client import DispatchWithEvents
from pythoncom import PumpWaitingMessages, Empty, Missing
from time import time

class BBCommEvent:
    def OnData(self, Security, cookie, Fields, Data, Status):
        print 'OnData: ' + `Data`

    def OnStatus(self, Status, SubStatus, StatusDescription):
        print 'OnStatus'

class TestAsync:
    def __init__(self):
        clsid = '{F2303261-4969-11D1-B305-00805F815CBF}'
        progid = 'Bloomberg.Data.1'

        print 'connecting to BBComm'        
        blp = DispatchWithEvents(clsid, BBCommEvent)
        blp.AutoRelease = False
        blp.Subscribe('EUR Curncy', 1, 'LAST_PRICE', Results = Empty)
        blp.Flush()

        end_time = time() + 5

        while 1:
            PumpWaitingMessages()
            if end_time < time():
                print 'timed out'
                break

if __name__ == "__main__":
    ta = TestAsync()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昇り龍 2024-08-23 16:31:57

我终于想通了。我做了相当多的combrowse.py 侦探工作,并与 BBG API 下载中的 JAVA、C、C++ 和 .NET 示例进行了比较。有趣的是,彭博社帮助台的人们对这些事情几乎一无所知,或者也许我只是在和错误的人交谈。

这是我的代码。

asynchronousHandler.py:constants.py

import win32com.client
from pythoncom import PumpWaitingMessages
from time import time, strftime
import constants

class EventHandler:
    def OnProcessEvent(self, result):
        event = win32com.client.gencache.EnsureDispatch(result) 
        if event.EventType == constants.SUBSCRIPTION_DATA:
            self.getData(event)
        elif event.EventType == constants.SUBSCRIPTION_STATUS:
            self.getStatus(event)
        else:
            self.getMisc(event)
    def getData(self, event):
        iterator = event.CreateMessageIterator()
        while iterator.Next():
            message = iterator.Message  
            dataString = ''
            for fieldIndex, field in enumerate(constants.fields):           
                if message.AsElement.HasElement(field):
                    element = message.GetElement(field)
                    if element.IsNull:
                        theValue = ''
                    else:
                        theValue = ', Value: ' + str(element.Value) 
                    dataString = dataString + ', (Type: ' + element.Name + theValue + ')'
            print strftime('%m/%d/%y %H:%M:%S') + ', MessageType: ' + message.MessageTypeAsString + ', CorrelationId: ' + str(message.CorrelationId) + dataString
    def getMisc(self, event):
        iterator = event.CreateMessageIterator()
        while iterator.Next():
            message = iterator.Message
            print strftime('%m/%d/%y %H:%M:%S') + ', MessageType: ' + message.MessageTypeAsString
    def getStatus(self, event):
        iterator = event.CreateMessageIterator()
        while iterator.Next():
            message = iterator.Message
            if message.AsElement.HasElement('reason'):
                element = message.AsElement.GetElement('reason')
                print strftime('%m/%d/%y %H:%M:%S') + ', MessageType: ' + message.MessageTypeAsString + ', CorrelationId: ' + str(message.CorrelationId) + ', Category: ' + element.GetElement('category').Value + ', Description: ' + element.GetElement('description').Value 
            if message.AsElement.HasElement('exceptions'):
                element = message.AsElement.GetElement('exceptions')
                exceptionString = ''
                for n in range(element.NumValues):
                    exceptionInfo = element.GetValue(n)
                    fieldId = exceptionInfo.GetElement('fieldId')
                    reason = exceptionInfo.GetElement('reason')
                    exceptionString = exceptionString + ', (Field: ' + fieldId.Value + ', Category: ' + reason.GetElement('category').Value + ', Description: ' + reason.GetElement('description').Value + ') ' 
                print strftime('%m/%d/%y %H:%M:%S') + ', MessageType: ' + message.MessageTypeAsString + ', CorrelationId: ' + str(message.CorrelationId) + exceptionString

class bloombergSource:
    def __init__(self):
        session = win32com.client.DispatchWithEvents('blpapicom.Session' , EventHandler)
        session.Start()
        started = session.OpenService('//blp/mktdata')
        subscriptions = session.CreateSubscriptionList()
        for tickerIndex, ticker in enumerate(constants.tickers):
            if len(constants.interval) > 0:
                subscriptions.AddEx(ticker, constants.fields, constants.interval, session.CreateCorrelationId(tickerIndex))
            else:
                subscriptions.Add(ticker, constants.fields, session.CreateCorrelationId(tickerIndex))   
        session.Subscribe(subscriptions)
        endTime = time() + 2
        while True:
            PumpWaitingMessages()
            if endTime < time():                
                break               

if __name__ == "__main__":
    aBloombergSource = bloombergSource()

ADMIN = 1
AUTHORIZATION_STATUS = 11
BLPSERVICE_STATUS = 9
PARTIAL_RESPONSE = 6
PUBLISHING_DATA = 13
REQUEST_STATUS = 4
RESOLUTION_STATUS = 12
RESPONSE = 5
SESSION_STATUS = 2
SUBSCRIPTION_DATA = 8
SUBSCRIPTION_STATUS = 3
TIMEOUT = 10
TOKEN_STATUS = 15
TOPIC_STATUS = 14
UNKNOWN = -1
fields = ['BID']
tickers = ['AUD Curncy']
interval = '' #'interval=5.0'

对于历史数据,我使用了这个简单的脚本:

import win32com.client

session = win32com.client.Dispatch('blpapicom.Session')
session.QueueEvents = True
session.Start()
started = session.OpenService('//blp/refdata')
dataService = session.GetService('//blp/refdata')
request = dataService.CreateRequest('HistoricalDataRequest')
request.GetElement('securities').AppendValue('5 HK Equity')
request.GetElement('fields').AppendValue('PX_LAST')
request.Set('periodicitySelection', 'DAILY')
request.Set('startDate', '20090119')
request.Set('endDate', '20090130')
cid = session.SendRequest(request)
ADMIN = 1
AUTHORIZATION_STATUS = 11
BLPSERVICE_STATUS = 9
PARTIAL_RESPONSE = 6
PUBLISHING_DATA = 13
REQUEST_STATUS = 4
RESOLUTION_STATUS = 12
RESPONSE = 5
SESSION_STATUS = 2
SUBSCRIPTION_DATA = 8
SUBSCRIPTION_STATUS = 3
TIMEOUT = 10
TOKEN_STATUS = 15
TOPIC_STATUS = 14
UNKNOWN = -1
stayHere = True
while stayHere:
    event = session.NextEvent();
    if event.EventType == PARTIAL_RESPONSE or event.EventType == RESPONSE:
        iterator = event.CreateMessageIterator()
        iterator.Next()
        message = iterator.Message
        securityData = message.GetElement('securityData')
        securityName = securityData.GetElement('security')
        fieldData = securityData.GetElement('fieldData')
        returnList = [[0 for col in range(fieldData.GetValue(row).NumValues+1)] for row in range(fieldData.NumValues)]
        for row in range(fieldData.NumValues):
            rowField = fieldData.GetValue(row)
            for col in range(rowField.NumValues+1):
                colField = rowField.GetElement(col)
                returnList[row][col] = colField.Value
        stayHere = False
        break
element = None
iterator = None
message = None
event = None
session = None
print returnList

I finally figured it out. I did a fair bit of combrowse.py detective work, and I compared with the JAVA, C, C++, and .NET examples in the BBG API download. Interestingly enough the Bloomberg Helpdesk people knew pretty much null when it came to these things, or perhaps I was just talking to the wrong person.

Here is my code.

asynchronousHandler.py:

import win32com.client
from pythoncom import PumpWaitingMessages
from time import time, strftime
import constants

class EventHandler:
    def OnProcessEvent(self, result):
        event = win32com.client.gencache.EnsureDispatch(result) 
        if event.EventType == constants.SUBSCRIPTION_DATA:
            self.getData(event)
        elif event.EventType == constants.SUBSCRIPTION_STATUS:
            self.getStatus(event)
        else:
            self.getMisc(event)
    def getData(self, event):
        iterator = event.CreateMessageIterator()
        while iterator.Next():
            message = iterator.Message  
            dataString = ''
            for fieldIndex, field in enumerate(constants.fields):           
                if message.AsElement.HasElement(field):
                    element = message.GetElement(field)
                    if element.IsNull:
                        theValue = ''
                    else:
                        theValue = ', Value: ' + str(element.Value) 
                    dataString = dataString + ', (Type: ' + element.Name + theValue + ')'
            print strftime('%m/%d/%y %H:%M:%S') + ', MessageType: ' + message.MessageTypeAsString + ', CorrelationId: ' + str(message.CorrelationId) + dataString
    def getMisc(self, event):
        iterator = event.CreateMessageIterator()
        while iterator.Next():
            message = iterator.Message
            print strftime('%m/%d/%y %H:%M:%S') + ', MessageType: ' + message.MessageTypeAsString
    def getStatus(self, event):
        iterator = event.CreateMessageIterator()
        while iterator.Next():
            message = iterator.Message
            if message.AsElement.HasElement('reason'):
                element = message.AsElement.GetElement('reason')
                print strftime('%m/%d/%y %H:%M:%S') + ', MessageType: ' + message.MessageTypeAsString + ', CorrelationId: ' + str(message.CorrelationId) + ', Category: ' + element.GetElement('category').Value + ', Description: ' + element.GetElement('description').Value 
            if message.AsElement.HasElement('exceptions'):
                element = message.AsElement.GetElement('exceptions')
                exceptionString = ''
                for n in range(element.NumValues):
                    exceptionInfo = element.GetValue(n)
                    fieldId = exceptionInfo.GetElement('fieldId')
                    reason = exceptionInfo.GetElement('reason')
                    exceptionString = exceptionString + ', (Field: ' + fieldId.Value + ', Category: ' + reason.GetElement('category').Value + ', Description: ' + reason.GetElement('description').Value + ') ' 
                print strftime('%m/%d/%y %H:%M:%S') + ', MessageType: ' + message.MessageTypeAsString + ', CorrelationId: ' + str(message.CorrelationId) + exceptionString

class bloombergSource:
    def __init__(self):
        session = win32com.client.DispatchWithEvents('blpapicom.Session' , EventHandler)
        session.Start()
        started = session.OpenService('//blp/mktdata')
        subscriptions = session.CreateSubscriptionList()
        for tickerIndex, ticker in enumerate(constants.tickers):
            if len(constants.interval) > 0:
                subscriptions.AddEx(ticker, constants.fields, constants.interval, session.CreateCorrelationId(tickerIndex))
            else:
                subscriptions.Add(ticker, constants.fields, session.CreateCorrelationId(tickerIndex))   
        session.Subscribe(subscriptions)
        endTime = time() + 2
        while True:
            PumpWaitingMessages()
            if endTime < time():                
                break               

if __name__ == "__main__":
    aBloombergSource = bloombergSource()

constants.py:

ADMIN = 1
AUTHORIZATION_STATUS = 11
BLPSERVICE_STATUS = 9
PARTIAL_RESPONSE = 6
PUBLISHING_DATA = 13
REQUEST_STATUS = 4
RESOLUTION_STATUS = 12
RESPONSE = 5
SESSION_STATUS = 2
SUBSCRIPTION_DATA = 8
SUBSCRIPTION_STATUS = 3
TIMEOUT = 10
TOKEN_STATUS = 15
TOPIC_STATUS = 14
UNKNOWN = -1
fields = ['BID']
tickers = ['AUD Curncy']
interval = '' #'interval=5.0'

For historical data I used this simple script:

import win32com.client

session = win32com.client.Dispatch('blpapicom.Session')
session.QueueEvents = True
session.Start()
started = session.OpenService('//blp/refdata')
dataService = session.GetService('//blp/refdata')
request = dataService.CreateRequest('HistoricalDataRequest')
request.GetElement('securities').AppendValue('5 HK Equity')
request.GetElement('fields').AppendValue('PX_LAST')
request.Set('periodicitySelection', 'DAILY')
request.Set('startDate', '20090119')
request.Set('endDate', '20090130')
cid = session.SendRequest(request)
ADMIN = 1
AUTHORIZATION_STATUS = 11
BLPSERVICE_STATUS = 9
PARTIAL_RESPONSE = 6
PUBLISHING_DATA = 13
REQUEST_STATUS = 4
RESOLUTION_STATUS = 12
RESPONSE = 5
SESSION_STATUS = 2
SUBSCRIPTION_DATA = 8
SUBSCRIPTION_STATUS = 3
TIMEOUT = 10
TOKEN_STATUS = 15
TOPIC_STATUS = 14
UNKNOWN = -1
stayHere = True
while stayHere:
    event = session.NextEvent();
    if event.EventType == PARTIAL_RESPONSE or event.EventType == RESPONSE:
        iterator = event.CreateMessageIterator()
        iterator.Next()
        message = iterator.Message
        securityData = message.GetElement('securityData')
        securityName = securityData.GetElement('security')
        fieldData = securityData.GetElement('fieldData')
        returnList = [[0 for col in range(fieldData.GetValue(row).NumValues+1)] for row in range(fieldData.NumValues)]
        for row in range(fieldData.NumValues):
            rowField = fieldData.GetValue(row)
            for col in range(rowField.NumValues+1):
                colField = rowField.GetElement(col)
                returnList[row][col] = colField.Value
        stayHere = False
        break
element = None
iterator = None
message = None
event = None
session = None
print returnList
安静被遗忘 2024-08-23 16:31:57

为了让它工作,你需要安装 Bloomberg Desktop v3 API SDK,我这样做了,重新启动了我的机器,似乎可以工作。没有重新启动它就崩溃了。

如果您使用 Com explorer,您将看到 Bloomberg 元素现在已存在

For it to work you need to install Bloomberg Desktop v3 API SDK, I did that, restarted my machine, seems to work. Without the restart it just crashed.

If you use Com explorer, you will see the bloomberg elements are now present

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文