I am trying to query specific Bloomberg tickers and write them into an excel. The code itself is pretty straightforward and I have gotten it to work for all tickers except "Move Index". When querying for the this particular ticker, I am getting the exception: raise ValueError(data) ValueError: []. I blocked out the particular query code with try-except to get more information about the error, but it's still returning the same exception with no further detail.
import os
from blp import blp
import pdblp
import blpapi
import datetime
import xlwings
import pandas as pd
import win32com.client
import pythoncom
import sys
import os
from PIL import ImageGrab
with open(os.path.join(os.getenv("TEMP"), "Bloomberg", "log", "bbcomm.log"),"r") as f:
try:
port = f.read().split("BLOOMBERG COMMUNICATION SERVER READY on Port: ")[-1].split("\n")[0]
except:
port = 8194
con = pdblp.BCon(debug = False, port = 8194, timeout = 10000000)
con.start()
today = datetime.datetime.today()
value_growth = con.bdh(['SVX Index','SGX Index'],'PX_Last',elms=[("periodicitySelection", "WEEKLY")], start_date="20220107",end_date= "20800101")
ten_year = con.bdh(['USGG10YR Index', 'USGG2YR Index'], 'PX_Last',elms=[("periodicitySelection", "WEEKLY")], start_date="20220107",end_date= "20800101" )
spx_drivers = con.bdh(['SPX Index','USGG2YR Index'],'PX_Last',elms=[("periodicitySelection", "WEEKLY")], start_date="20220107",end_date= "20800101" )
bloomberg = con.bdh(['GBTP10YR Index','GDBR10 Index','BICLB10Y Index','VIX Index','USGGT05Y Index','.EUCCBS3M G Index','CSI BARC Index','LP02OAS Index','V2X Index','SPX Index', 'SGX Index','S5INDU Index','RTY Index','CO1 Comdty','HG1 Comdty','USGGBE05 Index','XAU Curncy','XBTUSD Curncy'],'PX_Last', elms=[("periodicitySelection", "WEEKLY")], start_date="20220107",end_date= "20800101")
bloomberg1 = con.bdh('Move index', 'PX_Last',elms=[("periodicitySelection", "WEEKLY")], start_date="20220107",end_date= "20240101")
bloomberg_merged = pd.concat([bloomberg,bloomberg1], axis = 1)
with pd.ExcelWriter(r'FILE PATH', engine='openpyxl', if_sheet_exists='overlay', mode='a') as writer:
value_growth.to_excel(writer, sheet_name='Value_Growth', startcol=0, startrow= 581, header = False)
ten_year.to_excel(writer, sheet_name='10Y_drivers', startcol = 0, startrow = 216, header = False)
spx_drivers.to_excel(writer, sheet_name= 'SPX_Drivers', startcol = 0, startrow = 216, header = False)
bloomberg_merged.to_excel(writer, sheet_name='Bloomberg', startcol=0, startrow=579, header = False)
My questions are:
Short answer: change the ticker to MOVE Index
or move Index
.
Long answer: The Bloomberg API does not seem to accept mixed case tickers (but does accept mixed case fields).
Using the low-level blpapi
:
import blpapi
sessionOptions = blpapi.SessionOptions()
sessionOptions.setServerHost('localhost')
sessionOptions.setServerPort(8194)
session = blpapi.Session(sessionOptions)
session.start()
session.openService('//blp/refdata')
svc = session.getService('//blp/refdata')
request = svc.createRequest('ReferenceDataRequest')
request.append('securities','move Index')
request.append('fields','PX_Last')
session.sendRequest(request)
done = False
while not done:
event = session.nextEvent()
if event.eventType() == blpapi.event.Event.RESPONSE:
for msg in event:
print(msg)
done = True
else:
pass
Elicits this successful response from the API for move Index
(or for MOVE Index
)
CID: {[ valueType=AUTOGEN classId=0 value=6 ]}
RequestId: bfd8d841-7e52-41bd-b390-b626f4dff3bb
ReferenceDataResponse = {
securityData[] = {
securityData = {
security = "move Index"
eidData[] = {
}
fieldExceptions[] = {
}
sequenceNumber = 0
fieldData = {
PX_Last = 101.160000
}
}
}
}
Changing the ticker to Move Index
gives this:
CID: {[ valueType=AUTOGEN classId=0 value=7 ]}
RequestId: 71428563-0763-4f8f-bcb1-346e88682c91
ReferenceDataResponse = {
securityData[] = {
securityData = {
security = "Move Index"
eidData[] = {
}
securityError = {
source = "21932:rsfrdsvc2"
code = 43
category = "BAD_SEC"
message = "Unknown/Invalid Security [nid:21932]"
subcategory = "INVALID_SECURITY"
}
fieldExceptions[] = {
}
sequenceNumber = 0
fieldData = {
}
}
}
}
Interestingly, this does not happen with the Excel BDP() function, so I imagine the Excel addin is converting any tickers to all upper or all lower case.
As for the ValueError
, this is being generated by the pdblp
API wrapper when the response from the server contains a "securityError" entry in the securityData
section:
From pdblp.py
:
data = []
...
has_security_error = 'securityError' in d['securityData']
has_field_exception = len(d['securityData']['fieldExceptions']) > 0
if has_security_error or has_field_exception:
raise ValueError(data)
Arguably the exception data could be more informative and include information on the security and the API error!