【Bug Report】在 macOS Sonoma (Apple Silicon) + Python 3.12 环境下,`qlib.init()` 始终只能加载2只股票,即使数据文件完整无缺
系统环境:
- 操作系统: macOS Sonoma 14.5 (darwin 24.5.0, arm64)
- Python 版本: 3.12
- Qlib 版本: 0.9.6 (通过
pip install pyqlib在一个全新的虚拟环境中安装)
问题描述:
无论使用何种方式准备数据(包括使用官方dump_bin.py脚本或手动创建二进制文件),qlib.init(provider_uri=...) 在我的系统上初始化后,D.instruments() 始终返回一个看起来像是内部查询过滤器的字典 {'market': 'all', 'filter_pipe': []},其长度为2,而不是预期的包含超过5000只股票代码的列表。
复现步骤:
-
数据准备: 我们准备了一个符合Qlib格式的数据目录
/Users/user/qlib/qlib_data/cn_data,其结构如下:qlib_data/cn_data/ ├── calendars/ │ └── day.txt (包含 5047 个交易日) ├── features/ │ ├── sh600000/ │ │ ├── close.bin │ │ └── ... │ └── ... (包含 5683 个股票子目录) └── instruments/ └── all.txt (包含 5683 行,格式为 "CODE\tMARKET")我们确信数据文件的格式是正确的,因为我们是严格按照Qlib的二进制规范生成的。
-
验证脚本 (
verify.py): 在一个全新的Python 3.12虚拟环境中,仅安装pyqlib,然后运行以下脚本:import qlib from qlib.data import D QLIB_DATA_DIR = "/Users/user/qlib/qlib_data/cn_data" print("Verifying Qlib data loading...") try: qlib.init(provider_uri=QLIB_DATA_DIR) print("Qlib initialized successfully.") instruments = D.instruments() print(f"D.instruments() 返回对象的类型: {type(instruments)}") print(f"D.instruments() 返回对象的长度: {len(instruments)}") print(f"D.instruments() 返回对象的内容: {instruments}") except Exception as e: print(f"An error occurred: {e}") -
实际输出:
Verifying Qlib data loading... Qlib initialized successfully. D.instruments() 返回对象的类型: <class 'dict'> D.instruments() 返回对象的长度: 2 D.instruments() 返回对象的内容: {'market': 'all', 'filter_pipe': []}
我们已尝试但失败的解决方案:
- 删除
~/.qlib缓存目录。 - 使用不同的数据转换方法。
- 创建全新的Python虚拟环境并重装Qlib。
- 全局搜索并清理所有可能的Qlib缓存文件。
结论:
这个问题似乎不是由数据本身或常规的环境配置问题引起的。它强烈指向 Qlib 核心初始化逻辑与特定系统环境之间的深层兼容性问题。D.instruments() 异常地返回了一个字典而不是预期的列表,这可能是问题的关键所在。我们已经用尽了所有外部调试手段。希望能得到开发团队的帮助,以确定问题的根源。
quick see see the documentation, bro @ericforai
| install with pip | install from source | plot | |
|---|---|---|---|
| Python 3.8 | ✔️ | ✔️ | ✔️ |
| Python 3.9 | ✔️ | ✔️ | ✔️ |
| Python 3.10 | ✔️ | ✔️ | ✔️ |
| Python 3.11 | ✔️ | ✔️ | ✔️ |
| Python 3.12 | ✔️ | ✔️ | ✔️ |
D.Instruments就是创建instrument字典 啊。 要返回stock列表要用,D.list_instruments
我遇到了同样的问题,同问怎么解决呢
很复杂 自己还是不要去碰了 下载社区数据吧
Hi, @ericforai
Thanks for your interest in qlib. I saw your description, and it's not a bug.
If you want to load the stock pool information for a specific period of time, you can use the following method.
>> from qlib.data import D
>> res = D.list_instruments(D.instruments("all"), as_list=True)
>> print(res)
D.instruments() returns the configuration information of the stock pool, and the length of 2 is as expected. See the documentation for more information.