嗨,我正在使用Bloomberg API(出于学习目的,与问题不太相关-仅作为上下文)。
该过程的一部分是创建“请求”,这些“请求”作为查询发送到响应的服务器。
每个查询都保持“活动”状态,直到对象被杀死。我正在使用一个函数来从每个请求中调用数据,因为这是我经常使用的代码,并且请求的形状会随着每次调用而发生很大变化。
def calldata(req): # doesnt currently work. Dont know why
session.sendRequest(req)
while True:
ev = session.nextEvent()
tmp = []
if ev.eventType() == blpapi.Event.RESPONSE or blpapi.Event.PARTIAL_RESPONSE:
for msg in ev:
if msg.hasElement('securityData'):
data = msg.getElement('securityData')
tmp.append(data)
break
del req
return(tmp)
例如,请求可能在哪里
# Data for clean multiple data ::= cleanref
r = svc.createRequest('ReferenceDataRequest')
r.append('securities','MSFT US Equity')
r.append('fields','bid')
cleanref = calldata(r)
# Dirty reference for multiple data - both bad security and bad field
# ::= errorref
req = svc.createRequest("ReferenceDataRequest")
req.append('securities','Rhubarb Curncy')
req.append('fields','PX_LAST')
req.append('securities','SGD Curncy')
req.append('fields','desc')
errorref= calldata(req)
# Historical data request ::= histdata
request = svc.createRequest("HistoricalDataRequest")
request.getElement("securities").appendValue("IBM US Equity")
request.getElement("securities").appendValue("MSFT US Equity")
request.getElement("fields").appendValue("PX_LAST")
request.getElement("fields").appendValue("OPEN")
request.set("periodicityAdjustment", "ACTUAL")
request.set("periodicitySelection", "MONTHLY")
request.set("startDate", "20060101")
request.set("endDate", "20061231")
request.set("maxDataPoints", 100)
histdata = calldata(request)
# erroneous historical data request ::= histerr
requ = svc.createRequest("HistoricalDataRequest")
requ.getElement("securities").appendValue("IBM US Equity")
requ.getElement("securities").appendValue("MSFT US Equity")
requ.getElement("fields").appendValue("PX_LAST")
requ.getElement("fields").appendValue("Desc")
requ.set("periodicityAdjustment", "ACTUAL")
requ.set("periodicitySelection", "MONTHLY")
requ.set("startDate", "20060101")
requ.set("endDate", "20061231")
requ.set("maxDataPoints", 100)
histerr = calldata(requ)
所以我需要杀死函数中的每个请求才能重用它,但我无法弄清楚。我怀疑这可能与此有关,locals()
但我们将不胜感激地得到任何帮助。