pythondataframecalendarbloombergxbbg

Extracting Non-Settlement Days from Bloomberg Using xbbg and Calendar Overrides


I would like to find the non trading days on a certain period for a certain ticker. The thing is that I cannot wait to see if I can retrieve a price through blp.bdh because I need to find the non trading days before they happen. I need to find non trading days, for each index in the country_currency_code dictionary. so I use 'CALENDAR_NON_SETTLEMENT_DATES' linked to a country code because holidays depend on the country.

I tried with different tickers, different country codes, none of it worked. I only get empty dataframes.

import pandas as pd
import numpy as np
from xbbg import blp
from datetime import datetime, timedelta

country_currency_code = {
    'SOFRRATE Index': ('USD Curncy', 'US'),
    'BISTTREF Index': ('TRY Curncy', 'TU'), 
    'MUTKCALM Index': ('JPY Curncy','JN'),
    'RUONIA Index': ('RUB Cunrcy','R$'),
    'SIBCSORA Index': ('SGD Curncy','SI'),
    'SONIO/N Index': ('GBP Curncy','GB'),
    'SRFXON3 Index': ('CHF Curncy','SZ'),
    'TTHORON Index': ('TWD Curncy','T+')
}

def shift_month(year: int, month: int, offset: int):
    new_month = month + offset
    new_year = year + (new_month - 1) // 12
    new_month = ((new_month - 1) % 12) + 1
    return new_year, new_month

def get_off_days(year: int, month: int):
    start_year, start_month = shift_month(year, month, -1)
    end_year, end_month = shift_month(year, month, +1)

    start_date = datetime(start_year, start_month, 10).strftime('%Y%m%d')
    end_date = datetime(end_year, end_month, 15).strftime('%Y%m%d')

    all_off_days = {}

    for index, (_, country_code) in country_currency_code.items():
        try:
            result = blp.bds(
                index,
                'CALENDAR_NON_SETTLEMENT_DATES',
                [
                    f'SETTLEMENT_CALENDAR_CODE={country_code}',
                    f'CALENDAR_START_DATE={start_date}',
                    f'CALENDAR_END_DATE={end_date}'
                ]
            )
            off_days = result.get('calendar_non_settlement_dates', [])
            all_off_days[index] = off_days
        except Exception as e:
            print(f"Erreur pour {index} : {e}")
            all_off_days[index] = []

    return all_off_days

Solution

  • Here is the right way to ask Bloomberg:

    holidays = blp.bds(
    'USD Curncy',
    'CALENDAR_NON_SETTLEMENT_DATES',
    SETTLEMENT_CALENDAR_CODE='FD',
    CALENDAR_START_DATE='20250101',
    CALENDAR_END_DATE='20261231'
    )

    print(holidays)

    N.B. 'FD' is the calendar for the US.

    You will get a DataFrame, with the column 'holiday_date' and the different dates written in format yyyy-mm-dd