@DS_London, thanks for the prod to look at blp/instruments. I dug into the Bloomberg for Enterprise Open API Services & schemas reference guide (PDF), which is very hard to find. I tried to find it again, but no luck. On page 75 I found the government lookup request and tweaked some of my existing code to get it to work. I had created separate functions for some of this code, but I didn't bother with this test. I thought it might help me to see if there was some way to pull a list of securities from an SRCH, but not sure if I can pull that off.
EDIT: If you are a BBG user, you can find the PDF referenced above at WAPI -> API Developer's Guide -> Reference Guide: Services and Schemas.
from argparse import ArgumentParser
import blpapi
SESSION_STARTED = blpapi.Name("SessionStarted")
SESSION_STARTUP_FAILURE = blpapi.Name("SessionStartupFailure")
# Removed optparse.OptionParser because it was deprecated.
def parseCmdLine():
parser = ArgumentParser(description='Retrieve reference data.')
parser.add_argument('-a',
'--ip',
dest='host',
help='server name or IP (default: %(default)s)',
metavar='ipAddress',
default='localhost')
parser.add_argument('-p',
dest='port',
type=int,
help='server port (default: %(default)s)',
metavar='tcpPort',
default=8194)
args = parser.parse_args()
return args
args = parseCmdLine()
# Fill SessionOptions
sessionOptions = blpapi.SessionOptions()
sessionOptions.setServerHost(args.host)
sessionOptions.setServerPort(args.port)
# Create a Session
session = blpapi.Session(sessionOptions)
# Start a Session
session.start()
session.openService("//blp/instruments")
instrumentsDataService = session.getService("//blp/instruments")
request = instrumentsDataService.createRequest("govtListRequest")
# request.asElement().setElement('partialMatch', True)
request.asElement().setElement('query', 'DBR')
request.asElement().setElement('ticker', '')
request.asElement().setElement('maxResults', 10)
session.sendRequest(request)
try:
# Process received events
while(True):
# We provide timeout to give the chance to Ctrl+C handling:
ev = session.nextEvent(500)
# below msg.messageType == GovtListResponse
for msg in ev:
if msg.messageType() == "GovtListResponse":
if msg.hasElement("responseError"):
print(msg.toString())
if msg.hasElement("results"):
data = msg.getElement("results")
print(data)
# Response completly received, so we could exit
if ev.eventType() == blpapi.Event.RESPONSE:
break
finally:
# Stop the session
session.stop()
>>>results[] = {
results = {
parseky = "BT245031 Corp"
name = "Bundesrepublik Deutschland Bundesanleihe"
ticker = "DBR"
}
results = {
parseky = "BP980366 Corp"
name = "Bundesrepublik Deutschland Bundesanleihe"
ticker = "DBR"
}
results = {
parseky = "BR246981 Corp"
name = "Bundesrepublik Deutschland Bundesanleihe"
ticker = "DBR"
}
results = {
parseky = "BN261261 Corp"
name = "Bundesrepublik Deutschland Bundesanleihe"
ticker = "DBR"
}
results = {
parseky = "AW416188 Corp"
name = "Bundesrepublik Deutschland Bundesanleihe"
ticker = "DBR"
}
results = {
parseky = "ZR097974 Corp"
name = "Bundesrepublik Deutschland Bundesanleihe"
ticker = "DBR"
}
results = {
parseky = "AP115404 Corp"
name = "Bundesrepublik Deutschland Bundesanleihe"
ticker = "DBR"
}
results = {
parseky = "AQ584649 Corp"
name = "Bundesrepublik Deutschland Bundesanleihe"
ticker = "DBR"
}
results = {
parseky = "AL997549 Corp"
name = "Bundesrepublik Deutschland Bundesanleihe"
ticker = "DBR"
}
results = {
parseky = "ZP220656 Corp"
name = "Bundesrepublik Deutschland Bundesanleihe"
ticker = "DBR"
}
}