matthewgilbert / pdblp

pandas wrapper for Bloomberg Open API
MIT License
240 stars 69 forks source link

ExcelGetGridRequest Response Element Inconsistent with Schema #54

Open matthewgilbert opened 5 years ago

matthewgilbert commented 5 years ago

Data returned from making a ExcelGetGridRequest seems inconsistent with schema. An element is a blpapi.DataType.CHOICE however element.getChoice() raises an error. This is illustrated below.

import blpapi

def get_messages(session):
    msgs = []
    while True:
        ev = session.nextEvent(2000)
        if ev.eventType() == blpapi.Event.TIMEOUT:
            break
        for msg in ev:
            msgs.append(msg)
    return msgs

session = blpapi.Session()
session.start()
session.openService('//blp/exrsvc')
exrService = session.getService('//blp/exrsvc')
request = exrService.createRequest('ExcelGetGridRequest')
request.set('Domain', "COMDTY:VESSEL")
session.sendRequest(request)

grid_msg = get_messages(session)[-1]

print("blpapi.DataType.CHOICE: %s" % blpapi.DataType.CHOICE)
print("Data Type: %s" % grid_msg.asElement().getElement('DataRecords').getValue(0).getElement('DataFields').datatype())
print(grid_msg.asElement().getElement('DataRecords').getValue(0).getElement('DataFields'))
blpapi.DataType.CHOICE: 16
Data Type: 16
DataFields[] = {
    DataFields = {
        StringValue = "IMO9843247 Index"
    }
    DataFields = {
        StringValue = "IHC HOLLAND CO01298"
    }
}
grid_msg.asElement().getElement('DataRecords').getValue(0).getElement('DataFields').getChoice()
---------------------------------------------------------------------------
UnknownErrorException                     Traceback (most recent call last)
<ipython-input-8-b7718a80ff5f> in <module>()
----> 1 grid_msg.asElement().getElement('DataRecords').getValue(0).getElement('DataFields').getChoice()

~/anaconda3/lib/python3.6/site-packages/blpapi/element.py in getChoice(self)
    392         self.__assertIsValid()
    393         res = internals.blpapi_Element_getChoice(self.__handle)
--> 394         _ExceptionUtil.raiseOnError(res[0])
    395         return Element(res[1], self._getDataHolder())
    396 

~/anaconda3/lib/python3.6/site-packages/blpapi/exception.py in raiseOnError(errorCode, description)
    143         """
    144         if errorCode:
--> 145             _ExceptionUtil.raiseException(errorCode, description)
    146 
    147 __copyright__ = """

~/anaconda3/lib/python3.6/site-packages/blpapi/exception.py in raiseException(errorCode, description)
    135                 description = "Unknown"
    136         errorClass = _ExceptionUtil.__getErrorClass(errorCode)
--> 137         raise errorClass(description, errorCode)
    138 
    139     @staticmethod

UnknownErrorException: Attempt to getChoice on non-choice element 'DataFields' (0x00000003)
matthewgilbert commented 5 years ago

The top level also appears inconsistent. Usually this is a blpapi.Datatype.CHOICE for other Response types however here it appears to be a sequence.

print("blpapi.DataType.SEQUENCE: %s" % blpapi.DataType.SEQUENCE)
print("Data Type: %s" % grid_msg.asElement().datatype())
blpapi.DataType.SEQUENCE: 15
Data Type: 15

For reference the message looks like

print(grid_msg.toString()[:400] + '\n...' + grid_msg.toString()[-400:])
GridResponse = {
    NumOfFields = 0
    NumOfRecords = 1413
    ColumnTitles[] = {
        "TICKER", "VESSEL NAME"
    }
    DataRecords[] = {
        DataRecords = {
            DataFields[] = {
                DataFields = {
                    StringValue = "IMO9843247 Index"
                }
                DataFields = {
                    StringValue = "IHC HOLLAND CO01298"

...       StringValue = "HUI JIAN"
                }
            }
        }
        DataRecords = {
            DataFields[] = {
                DataFields = {
                    StringValue = "IMO9872688 Index"
                }
                DataFields = {
                    StringValue = "NEW TIMES"
                }
            }
        }
    }
    ReachMax = true
    SequenceNumber = 34
}