matthewgilbert / pdblp

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

how to retrieve the not adjust price data ? #56

Closed kite8 closed 5 years ago

kite8 commented 5 years ago

Hi , I have a question : how to retrieve the not adjust price data, I found the parameters that are 'CshAdjAbnormal', 'CapChg', 'CshAdjNormal'.

So I try like this:

con = pb.BCon(port=8194, timeout=5000) con.start()

fields = ['HIGH', 'OPEN', 'LOW', 'VOLUME', 'PX_LAST'] start = '20181101' end = '20181201'

df = con.bdh("MSFT US Equity",fields, start, end, ovrds=[('CshAdjAbnormal', False), ('CapChg', False), ('CshAdjNormal', False)])

But it's error:


InvalidArgumentException Traceback (most recent call last)

in () 8 ovrds=[('CshAdjAbnormal', False), 9 ('CapChg', False), ---> 10 ('CshAdjNormal', False)]) C:\ProgramData\Anaconda3\lib\site-packages\pdblp\pdblp.py in bdh(self, tickers, flds, start_date, end_date, elms, ovrds, longdata) 259 260 data = self._bdh_list(tickers, flds, start_date, end_date, --> 261 elms, ovrds) 262 263 df = pd.DataFrame(data, columns=["date", "ticker", "field", "value"]) C:\ProgramData\Anaconda3\lib\site-packages\pdblp\pdblp.py in _bdh_list(self, tickers, flds, start_date, end_date, elms, ovrds) 290 # Process received events 291 for msg in self._receive_events(): --> 292 has_security_error = (msg.getElement('securityData') 293 .hasElement('securityError')) 294 has_field_exception = (msg.getElement('securityData') C:\ProgramData\Anaconda3\lib\site-packages\blpapi\message.py in getElement(self, name) 171 def getElement(self, name): 172 """Equivalent to asElement().getElement(name).""" --> 173 return self.asElement().getElement(name) 174 175 def getElementAsBool(self, name): C:\ProgramData\Anaconda3\lib\site-packages\blpapi\element.py in getElement(self, nameOrIndex) 345 name[0], 346 name[1]) --> 347 _ExceptionUtil.raiseOnError(res[0]) 348 return Element(res[1], self._getDataHolder()) 349 self.__assertIsValid() C:\ProgramData\Anaconda3\lib\site-packages\blpapi\exception.py in raiseOnError(errorCode, description) 143 """ 144 if errorCode: --> 145 _ExceptionUtil.raiseException(errorCode, description) 146 147 __copyright__ = """ C:\ProgramData\Anaconda3\lib\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 InvalidArgumentException: Choice sub-element not found for name 'securityData'. (0x00020002)

what should I do? Thank you !

kite8 commented 5 years ago

e.g.

start = '20181101' end = '20190122' df = con.bdh("MSFT US Equity",fields, start, end)

the close price of the df at '20190122' is equal to the real price, but MSFT had ex-dividend at the 2018-11-14 and 2019-02-20, so the the close price of the df at '20181101' is not equal to the real price.

If I update the data everyday, this will be a problem.

So I need to get the not adjust price & the dividend information. how to get the data from pdblp?

Thanks!

matthewgilbert commented 5 years ago

This should be done using the elms parameter in bdh, e.g.

df = con.bdh("MSFT US Equity", "PX_LAST", "20180101", "20180110",
             elms=[('CshAdjAbnormal', False), ('CapChg', False), ('CshAdjNormal', False)])

But I do agree that better error handling could be provided for this particular case. For example the response from a bad override is of the form

pdblp.pdblp:INFO:Event Type: 'RESPONSE'
pdblp.pdblp:INFO:Message Received:
HistoricalDataResponse = {
    responseError = {
        source = "bbdbh2"
        code = 27
        category = "BAD_ARGS"
        message = "Invalid override field id specified [nid:193] "
        subcategory = "INVALID_OVERRIDE_FIELD"
    }
}

but this throws a KeyError since it expects the response to have a securityData field.

--> 307             has_security_error = 'securityError' in d['securityData']
    308             has_field_exception = len(d['securityData']['fieldExceptions']) > 0
    309             if has_security_error or has_field_exception:

KeyError: 'securityData'
kite8 commented 5 years ago

Thanks! I will try it tomorrow.

kite8 commented 5 years ago

Thank you very much!! It's worked!!

=============== I have a humble request. Can you add the 'bdp' & 'bds' request?

I found the dividend info should use bds, like this in excel: =BDS("AAPL US Equity","DVD_HIST_ALL","headers=y","cols=7;rows=67")

Because I don't have Bloomberg terminal, I wrote the code, and post mail to my friend, and he run the code and tell me the result, so debugging is very inconvenient.

Thanks!!

matthewgilbert commented 5 years ago

I'm confused what your follow up question is? Similar functionality to bdp and bds is available via ref and bulkref. I believe =BDS("AAPL US Equity","DVD_HIST_ALL","headers=y","cols=7;rows=67") should be possible using pdblp.BCon.bulkref. Please review the documentation and tutorial available here

kite8 commented 5 years ago

Thank you.! I will to research it.

renxida commented 5 years ago

I reached this page by searching for PDBLP key error securitydata

Turns out I specified the start date > end date.

I'm leaving this message for other dumbdumbs like me who stumble across this thread.