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

Python调用Bloomberg接口查询Move Index遇ValueError问题求助

问题:Python查询Bloomberg Move Index时抛出ValueError: []

我用Python查询Bloomberg代码并写入Excel,其他代码查询均正常,但查询Move Index时抛出异常:raise ValueError(data) ValueError: []。已添加try-except块,但仍无法获取更多错误细节。

代码示例

import os 
from blp import blp 
import pdblp 
import blpapi
import datetime 
import xlwings 
import pandas as pd
import win32com.client
import pythoncom
import sys
import os 
from PIL import ImageGrab

with open(os.path.join(os.getenv("TEMP"), "Bloomberg", "log", "bbcomm.log"),"r") as f: 
    try:
        port = f.read().split("BLOOMBERG COMMUNICATION SERVER READY on Port: ")[-1].split("\n")[0]
    except:
        port = 8194

con = pdblp.BCon(debug = False, port = 8194, timeout = 10000000)
con.start()
today = datetime.datetime.today()

value_growth = con.bdh(['SVX Index','SGX Index'],'PX_Last',elms=[("periodicitySelection", "WEEKLY")], start_date="20220107",end_date= "20800101")
ten_year = con.bdh(['USGG10YR Index', 'USGG2YR Index'], 'PX_Last',elms=[("periodicitySelection", "WEEKLY")], start_date="20220107",end_date= "20800101" )
spx_drivers = con.bdh(['SPX Index','USGG2YR Index'],'PX_Last',elms=[("periodicitySelection", "WEEKLY")], start_date="20220107",end_date= "20800101" )
bloomberg = con.bdh(['GBTP10YR Index','GDBR10 Index','BICLB10Y Index','VIX Index','USGGT05Y Index','.EUCCBS3M G Index','CSI BARC Index','LP02OAS Index','V2X Index','SPX Index', 'SGX Index','S5INDU Index','RTY Index','CO1 Comdty','HG1 Comdty','USGGBE05 Index','XAU Curncy','XBTUSD Curncy'],'PX_Last', elms=[("periodicitySelection", "WEEKLY")], start_date="20220107",end_date= "20800101")
bloomberg1 = con.bdh('Move index', 'PX_Last',elms=[("periodicitySelection", "WEEKLY")], start_date="20220107",end_date= "20240101")
bloomberg_merged = pd.concat([bloomberg,bloomberg1], axis = 1)
with pd.ExcelWriter(r'FILE PATH', engine='openpyxl', if_sheet_exists='overlay', mode='a') as writer:
   value_growth.to_excel(writer, sheet_name='Value_Growth', startcol=0, startrow= 581, header = False)
   ten_year.to_excel(writer, sheet_name='10Y_drivers', startcol = 0, startrow = 216, header = False)
   spx_drivers.to_excel(writer, sheet_name= 'SPX_Drivers', startcol = 0, startrow = 216, header = False)
   bloomberg_merged.to_excel(writer, sheet_name='Bloomberg', startcol=0, startrow=579, header = False)

疑问

  1. 是否有人成功通过Python查询Bloomberg的Move Index?
  2. 如何进一步排查该ValueError的具体原因?

解答

针对问题1

可以成功查询,核心问题是你代码里的Bloomberg代码格式错误:Bloomberg代码是大小写敏感的,正确的标识应该是MOVE Index(全大写的MOVE,后缀Index首字母大写),而你写的Move index(混合大小写+小写index)会被API识别为无效代码,导致返回空数组触发ValueError。修正代码标识后即可正常获取数据。

针对问题2

从以下几个方向逐步排查:

  • 验证代码权限与有效性:先在Bloomberg Terminal中手动搜索MOVE Index,确认代码存在且你的账户有权限访问该数据。
  • 开启pdblp调试日志:将con = pdblp.BCon(debug = False, ...)改为debug=True,这样会输出API的原始请求和响应细节,能直接看到返回空数组的原因(比如代码无效、权限不足、参数错误)。
  • 单独测试目标查询:把bloomberg1的查询代码单独拎出来运行,排除其他代码的干扰,同时简化参数(比如先去掉periodicitySelection,用默认日度数据测试)。
  • 修正端口配置:你代码中读取日志获取了端口,但最后硬编码用了8194,改成使用读取到的port变量,避免端口不匹配导致请求失败。
  • 使用原生blpapi调试:如果pdblp的封装过于上层,直接用blpapi写极简请求,捕获更底层的API错误信息,示例代码如下:
import blpapi

# 初始化会话
session_options = blpapi.SessionOptions()
session_options.setServerHost("localhost")
session_options.setServerPort(8194)

session = blpapi.Session(session_options)
if not session.start():
    print("会话启动失败")
    exit()

# 打开参考数据服务
if not session.openService("//blp/refdata"):
    print("无法打开//blp/refdata服务")
    exit()

ref_data_service = session.getService("//blp/refdata")
request = ref_data_service.createRequest("HistoricalDataRequest")

# 设置请求参数
request.getElement("securities").appendValue("MOVE Index")
request.getElement("fields").appendValue("PX_Last")
request.set("startDate", "20220107")
request.set("endDate", "20240101")
request.set("periodicitySelection", "WEEKLY")

# 发送请求并打印响应
print("发送请求:", request)
session.sendRequest(request)

while True:
    event = session.nextEvent()
    for msg in event:
        print("收到响应信息:", msg)
    if event.eventType() == blpapi.Event.RESPONSE:
        break

内容的提问来源于stack exchange,提问作者fiona van der steur

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 18:54:50