You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于xbbg从Bloomberg提取非结算日遇空DataFrame问题求助

获取Bloomberg指数非结算日返回空DataFrame问题排查

我需要提前获取指定时间段内特定指数的非交易日,无需通过blp.bdh查询价格事后验证。针对country_currency_code字典中的每个指数,我关联对应国家代码,调用Bloomberg的CALENDAR_NON_SETTLEMENT_DATES字段获取非结算日,但尝试不同标的与国家代码后均返回空DataFrame,相关代码如下:

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

核心问题及修复方案

1. 国家/日历代码错误

Bloomberg的SETTLEMENT_CALENDAR_CODE有标准两位字母代码,你使用的部分代码不符合规范:

  • RUONIA Index对应的RUB应使用RU而非R$
  • TTHORON Index对应的TWD应使用TW而非T+
  • SRFXON3 Index对应的CHF应使用CH而非SZ
  • SIBCSORA Index对应的SGD应使用SG而非SI
  • MUTKCALM Index对应的JPY应使用JP而非JN

修正后的字典:

country_currency_code = {
    'SOFRRATE Index': ('USD Curncy', 'US'),
    'BISTTREF Index': ('TRY Curncy', 'TU'), 
    'MUTKCALM Index': ('JPY Curncy','JP'),
    'RUONIA Index': ('RUB Curncy','RU'),
    'SIBCSORA Index': ('SGD Curncy','SG'),
    'SONIO/N Index': ('GBP Curncy','GB'),
    'SRFXON3 Index': ('CHF Curncy','CH'),
    'TTHORON Index': ('TWD Curncy','TW')
}

2. 参数传递格式问题

xbbg的blp.bds方法更推荐使用关键字参数而非字符串列表传递参数,避免解析错误:

result = blp.bds(
    index,
    'CALENDAR_NON_SETTLEMENT_DATES',
    SETTLEMENT_CALENDAR_CODE=country_code,
    CALENDAR_START_DATE=start_date,
    CALENDAR_END_DATE=end_date
)

3. 标的与日历匹配问题

部分指数可能自带结算日历,无需额外指定SETTLEMENT_CALENDAR_CODE。可以尝试移除该参数,直接查询指数自身的非结算日:

result = blp.bds(
    index,
    'CALENDAR_NON_SETTLEMENT_DATES',
    CALENDAR_START_DATE=start_date,
    CALENDAR_END_DATE=end_date
)

4. 日期范围测试

先缩小日期范围(比如仅查询当月)验证是否能返回数据,排除大范围无数据的情况:

start_date = datetime(year, month, 1).strftime('%Y%m%d')
end_date = datetime(year, month, 28).strftime('%Y%m%d')

内容的提问来源于stack exchange,提问作者Jstne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 14:14:52