Tutorial on using pdblp¶
This tutorial provides some simple use cases for pdblp
. To start with,
import the library and create a BCon()
object
In [1]: import pdblp
In [2]: con = pdblp.BCon(debug=True, port=8194, timeout=5000)
Make sure that you are logged in to a Bloomberg terminal, after which you should be able to to start a connection as follows
In [3]: con.start()
To get some historical data, we can call bdh()
In [4]: con.bdh('SPY US Equity', 'PX_LAST',
'20150629', '20150630')
DEBUG:root:Sending Request:
HistoricalDataRequest = {
securities[] = {
"SPY US Equity"
}
fields[] = {
"PX_LAST"
}
periodicityAdjustment = ACTUAL
periodicitySelection = DAILY
startDate = "20150629"
endDate = "20150630"
overrides[] = {
}
}
DEBUG:root:Message Received:
HistoricalDataResponse = {
securityData = {
security = "SPY US Equity"
eidData[] = {
}
sequenceNumber = 0
fieldExceptions[] = {
}
fieldData[] = {
fieldData = {
date = 2015-06-29
PX_LAST = 205.420000
}
fieldData = {
date = 2015-06-30
PX_LAST = 205.850000
}
}
}
}
Out[4]:
ticker SPY US Equity
2015-06-29 205.42
2015-06-30 205.85
Notice that when con.debug == True
that the Response and Request messages
are printed to stdout. This can be quite useful for debugging but gets
annoying for normal use, so let’s turn it off and get some more data. This time
we request two fields which returns a DataFrame with a MultiIndex by default.
In [5]: con.debug = False
In [6]: con.bdh('SPY US Equity', ['PX_LAST', 'VOLUME'],
...: '20150629', '20150630')
...:
Out[6]:
ticker SPY US Equity
field PX_LAST VOLUME
date
2015-06-29 205.42 202621332.0
2015-06-30 205.85 182925106.0
But can also return data in long format
In [7]: con.bdh('SPY US Equity', ['PX_LAST', 'VOLUME'],
...: '20150629', '20150630', longdata=True)
...:
Out[7]:
date ticker field value
0 2015-06-29 SPY US Equity PX_LAST 2.054200e+02
1 2015-06-29 SPY US Equity VOLUME 2.026213e+08
2 2015-06-30 SPY US Equity PX_LAST 2.058500e+02
3 2015-06-30 SPY US Equity VOLUME 1.829251e+08
You can also override different FLDS
’s, for example
In [8]: con.bdh('MPMIEZMA Index', 'PX_LAST',
...: '20150101', '20150830')
...:
Out[8]:
ticker MPMIEZMA Index
field PX_LAST
date
2015-06-30 52.5
2015-07-31 52.4
In [9]: con.bdh('MPMIEZMA Index', 'PX_LAST',
...: '20150101', '20150830',
...: ovrds=[('RELEASE_STAGE_OVERRIDE', 'P')])
...: