pythonbloombergblpapi

How to pull out the list of active German government bonds using xbbg?


I would like to pull out the list of ISINs for existing active german government bonds using the xbbg library but I couldn't find a way to do so.

The Bloomberg ticker used to access the universe in Bloomberg is DBR Govt. The bql query in excel would be the following:

=@BQL.Query("get(ID_ISIN) for(filter(bondsUniv('Active'),TICKER==DBR))";"cols=2;rows=49")

I tried a few things:

1)

from xbbg import blp
query = blp.bdib(ticker='DBR Govt', dt=date.today()).tail()

based on example for equities, but I get an error KeyError: 'Cannot find exchange info for DBR Govt'.

or, for instance,

2)

from xbbg import blp
query = blp.bdp(tickers='DBR Govt', flds=['ID_ISIN'])

but this produces an empty dataframe. Does anyone have any idea what should I do?

Thanks a lot in advance.


Solution

  • Currently there is no officially published api for BQL via Python.

    There are some workarounds in this SO question.

    However, in this instance you can retrieve a list of current Bunds via the //blp/instruments service in the DAPI ... with a bit of work. The information on this service is buried on page 49 in the Bloomberg API reference.

    The xbbg package does not support this explicitly, but you can use its excellent framework for any DAPI request. Unfortunately I have not found a way to filter the returned list upfront for active bonds: the DAPI function returns every Bund, whether matured or not. However you can do a second bdp query for the bond maturities and filter on that:

    from xbbg.core import conn,process
    from xbbg import blp
    from datetime import date
    
    def allGovts(ticker): #Return all govts with the given ticker, matured or not
        req = process.create_request(service='//blp/instruments',request='govtListRequest')
        req.set('ticker',ticker)
        req.set('partialMatch',False)
        req.set('maxResults',1000)
            
        def _process_instruments(msg): #Process the response
            for elt in msg.asElement().getElement('results').values():
                yield elt.getElementAsString('parseky')
        
        conn.send_request(request=req)
        return process.rec_events(func=_process_instruments)
    
    def liveGovts(ticker): #Just return 'live' bonds, ordered by maturity
        tdy = date.today()
        dfAll = blp.bdp([g for g in allGovts(ticker)],['id_isin','maturity'])
        return dfAll[ dfAll['maturity'] > tdy ].sort_values('maturity')
    
    print(liveGovts('DBR'))
    

    with the result:

                        id_isin    maturity
    EJ506625 Corp  DE0001102309  2023-02-15
    EJ677578 Corp  DE0001102317  2023-05-15
    EJ815896 Corp  DE0001102325  2023-08-15
    ...
    ZR097974 Corp  DE0001102481  2050-08-15
    BR246981 Corp  DE0001102572  2052-08-15
    BY899086 Corp  DE0001102614  2053-08-15
    

    Of course, you may want other additional properties of the bonds, so you can amend the bdp call to include those extra fields.