使用公司提供的数据终端 如果您的公司有提供Bloomberg数据终端,则建议使用终端自带的Excel插件或接口,而不是使用Python API请求数据。因为终端的性能更高,且数据处理速度更快。这样可以实现快速获取并处理所需数据。
使用blpapi库 blpapi库是Bloomberg的Python交互库,具有更高的性能和更快的数据处理速度。使用blpapi库可以显著提高数据请求的速度。如下所示,使用blpapi库获取数据:
import blpapi
# 创建Bloomberg的Session
session = blpapi.Session()
session.start()
# 创建IntradayBar请求
request = session.createRequest("IntradayBarRequest")
request.set("security", "AAPL US Equity")
request.set("eventType", "TRADE")
request.set("interval", 1) # 间隔为1分钟
request.set("startDateTime", "2021-10-01T00:00:00.000Z")
request.set("endDateTime", "2021-10-10T23:59:59.999Z")
# 发送请求
session.sendRequest(request)
# 处理响应数据
while(True):
ev = session.nextEvent(500)
if ev.eventType() == blpapi.Event.RESPONSE or ev.eventType() == blpapi.Event.PARTIAL_RESPONSE:
bars = ev.getElement("barData").getElement("barTickData")
for i in range(bars.numValues()):
print("DateTime: ", bars.getValue(i).getElementAsDatetime("time").strftime("%Y-%m-%d %H:%M:%S"))
print("Open: ", bars.getValue(i).getElementAsFloat("open"))
print("High: ", bars.getValue(i).getElementAsFloat("high"))
print("Low: ", bars.getValue(i).getElementAsFloat("low"))
print("Close: ", bars.getValue(i).getElementAsFloat("close"))
print("Volume: ", bars.getValue(i