How do I store data from the Bloomberg API into a Pandas dataframe? How do I store data from the Bloomberg API into a Pandas dataframe? python python

How do I store data from the Bloomberg API into a Pandas dataframe?


I use tia (https://github.com/bpsmith/tia/blob/master/examples/datamgr.ipynb)

It already downloads data as a panda dataframe from bloomberg.You can download history for multiple tickers in one single call and even download some bloombergs reference data (Central Bank date meetings, holidays for a certain country, etc)

And you just install it with pip.This link is full of examples but to download historical data is as easy as:

import pandas as pdimport tia.bbg.datamgr as dmmgr = dm.BbgDataManager()sids = mgr['MSFT US EQUITY', 'IBM US EQUITY', 'CSCO US EQUITY']df = sids.get_historical('PX_LAST', '1/1/2014', '11/12/2014')

and df is a pandas dataframe.

Hope it helps


You can also use pdblp for this (Disclaimer: I'm the author). There is a tutorial showing similar functionality available here https://matthewgilbert.github.io/pdblp/tutorial.html, the functionality could be achieved using something like

import pdblpcon = pdblp.BCon()con.start()con.bdh(['IBM US Equity', 'MSFT US Equity'], ['PX_LAST', 'OPEN'],        '20061227', '20061231', elms=[("periodicityAdjustment", "ACTUAL")])


I've just published this which might help

http://github.com/alex314159/blpapiwrapper

It's basically not very intuitive to unpack the message, but this is what works for me, where strData is a list of bloomberg fields, for instance ['PX_LAST','PX_OPEN']:

fieldDataArray = msg.getElement('securityData').getElement('fieldData')size = fieldDataArray.numValues()fieldDataList = [fieldDataArray.getValueAsElement(i) for i in range(0,size)]outDates = [x.getElementAsDatetime('date') for x in fieldDataList]output = pandas.DataFrame(index=outDates,columns=strData)for strD in strData:    outData = [x.getElementAsFloat(strD) for x in fieldDataList]    output[strD] = outDataoutput.replace('#N/A History',pandas.np.nan,inplace=True)output.index = output.index.to_datetime()return output