Search code examples
bloomberg

xbbg intraday bar requests raises error for fixed income


I am using this great package xbbg, essentially a wrapper around blpapi doing all the nasty connection issues for you and retrieving data in the form of pandas dfs.

In order to get intraday data, you have to first add the instrument in a .yml file. The examples were only with Index, Comdty, Equity and Curncy data, but I need fixed income data. I have added Corp in the assets.yml file in xbbg (running v0.7), wiriting the following:

Corp:
  - tickers: [IT0005425233]
    exch: MIL

However, I still get the error "Cannot fine exchange info".

MIL is correctly adde in the exchange.yml file:

MIL:
  tz: Europe/Rome
  allday: [800, 2200]

I also tried inserting the des ( BTPS 1.7 09/01/51) instead of the ISIN in the .yml file, the issue is still there. Anybody here who had the same problem? If yes, how did you solve it?

I know there's a workaround where you supply the ref= kwarg but if you are trying to get data for different asset classes and different exchanges it gets messy, I'd like to get to the root of the issue from the .yml file.


Solution

  • As with most things, there is a short answer and a long answer.

    The short answer is that the xbbg.bdib() function does not handle bonds currently (either with the Corp or Govt yellow keys). It only recognizes Equity, Comdty, Curncy and Index. The OP has done everything right, in terms of setting up assets.yml and exch.yml, but the code in const.py in the xbbg package has a hard-coded list of yellow keys and the ticker/exchange logic for each.

    The long answer is to insert your own handling of Govt (or other yellow keys):

    Add the MIL and HEL exchanges in exch.yml (as the OP is doing):

    MIL:
      tz: Europe/Rome
      allday: [800, 2200]
    
    HEL:
      tz: Europe/Helsinki
      allday: [730, 1300]
    

    And in the assets.yml add the following section for Govt:

    Govt:
      - isins: [IT]
        exch: MIL
      - isins: [FI]
        exch: HEL
    

    Then in the calling code, create a myBdib() function, which forwards the call to the bdib function after determining the 'exchange' for the Govt yellow key:

    from xbbg import blp,const
    from datetime import datetime
    import pandas as pd
    
    const.ASSET_INFO['Govt'] = ['isins']
    
    def myBdib(ticker: str, dt, session='allday', typ='TRADE', **kwargs) -> pd.DataFrame:
        yellowKey = ticker.split()[-1]
    
        if yellowKey == 'Govt':
            entries = const.asset_config(yellowKey).set_index('isins')
            kwargs['ref']=entries['exch'][ticker[0:2]]
    
        return blp.bdib(ticker,dt,session,typ,**kwargs)
    
    df = myBdib('IT0005425233 Govt',datetime(2023,1,26))
    print(df)
    
    df = myBdib('FI4000415153 Govt',datetime(2023,1,26))
    print(df)
    
    df = myBdib('EUR Curncy',datetime(2023,1,26))
    print(df)
    

    The const.py code in xbbg handles all the asset/exchange lookup. The line const.ASSET_INFO['Govt'] = ['isins'] adds an entry to the default processing to tell the asset parser to look for the isins keyword for Govt. Here I am just using the first two characters of the ISIN to pick up the exchange, but you could use any approach. For example you could change the keyword isins to tickers in assets.yml and the code, and match on ticker (eg BTPS).

    NB. There is a certain amount of caching going on. If you are having issues, try deleting all the files under the ..\xbbg\markets\cached folder and running again.