Search code examples
pythonapibloomberg

How to get data from Bloomberg Python API?


How can I get the special cash through Python? In BLQ it works with

=BQL("6592 JP Equity","dropna(CASH_DIVS(DIVIDEND_TYPE=SPECIAL))","dates=range(1y,0d)","showdates=y")

Solution

  • Currently the BQL interface in Excel is not available via Python directly. There are clunky workarounds (driving Excel from Python and running the function in a spreadsheet), but in this case the information is accessible via the 'old school' data API, using Bulk Data (in the same way as the =BDS(...) function in Excel).

    The first hurdle with the DAPI is determining the names of the fields that you want (as they will not necessarily match those that BQL uses), and the names of any overrides. Type 6592 JP Equity FLDS Go in a Bloomberg Terminal window to see all the available data fields, which can then be searched:

    enter image description here

    The field for Dividend History - Cash is DVD_HIST

    If you click on DVD_HIST, you get the full description of the field, and any overrides that can be applied. In this case the override needed is DVD_START_DT.

    Armed with this information, you can use whichever Python package you prefer to access the Bloomberg data. My preference is for xbbg, but other packages are available. You need to install the Bloomberg API first from here if you don't already have it.

    The Bloomberg DAPI doesn't offer an override for dividend_type, so you have to pull back all the rows and filter them yourself:

    from xbbg import blp
    from datetime import datetime,timedelta
    
    #Find start date 
    dt = datetime.today() - timedelta(days=365)
    
    #Get all cash dividends after DVD_START_DT in a DataFrame
    dfAll = blp.bds('6592 JP Equity','DVD_HIST',DVD_START_DT=dt.strftime('%Y%m%d'))
    
    #Filter DataFrame for rows with dividend_type=='Special Cash'
    dfSpecial = dfAll[dfAll['dividend_type'] == 'Special Cash']
    
    print(dfSpecial)
    

    which returns:

                   declared_date     ex_date record_date payable_date  dividend_amount dividend_frequency dividend_type
    6592 JP Equity    2021-02-12  2021-12-29  2021-12-31         None             43.0           Semi-Anl  Special Cash
    6592 JP Equity    2021-02-12  2021-06-29  2021-06-30   2021-09-13             42.0           Semi-Anl  Special Cash
    6592 JP Equity    2020-02-13  2020-12-29  2020-12-31   2021-03-31             53.0           Semi-Anl  Special Cash