🚀 AI 一键生成 joinquant 策略代码

JoinQuant 平台如何筛选 ETF、LOF 及分级基金代码?

joinquant | 发布时间: 2025/12/30
以下内容由 EasyQuant 生成。

问题描述

如何筛选出市场上所有的 ETF 和 LOF 基金?

解决方案

在聚宽(JoinQuant)平台中,可以使用 get_all_securities 函数来获取市场上所有的 ETF 和 LOF 基金。

方法说明

get_all_securities(types=[], date=None)

  • types: 一个列表,用于指定要获取的证券类型。
    • 获取 ETF 基金,需传入 'etf'
    • 获取 LOF 基金,需传入 'lof'
    • 同时获取两者,需传入 ['etf', 'lof']
  • date: 查询日期。如果不填,默认返回历史上所有存在过的标的(包括已退市的)。如果只想获取当前市场上正在交易的标的,建议传入当前日期。

代码示例

以下代码可以在聚宽的研究环境回测环境中运行:

# -*- coding: utf-8 -*-
from jqdata import *
import datetime

# 获取当前日期还在上市的所有 ETF 和 LOF 基金
# date=datetime.date.today() 确保只筛选出当前未退市的标的
funds_df = get_all_securities(types=['etf', 'lof'], date=datetime.date.today())

# 打印结果的前5行
print("基金列表前5行数据:")
print(funds_df.head())

# 获取所有筛选出的基金代码列表
fund_codes = list(funds_df.index)
print(f"\n共筛选出 {len(fund_codes)} 只 ETF 和 LOF 基金。")

# 如果需要分别查看 ETF 和 LOF 的数量
etf_df = funds_df[funds_df['type'] == 'etf']
lof_df = funds_df[funds_df['type'] == 'lof']
print(f"其中 ETF 数量: {len(etf_df)}")
print(f"其中 LOF 数量: {len(lof_df)}")

返回数据说明

get_all_securities 返回的是一个 pandas.DataFrame 对象,包含以下列:

  • display_name: 中文名称
  • name: 缩写简称
  • start_date: 上市日期
  • end_date: 退市日期(未退市则为 2200-01-01)
  • type: 类型(etf 或 lof)

Q&A

Q: 如何只获取 ETF 基金?
A: 将 types 参数设置为 ['etf'] 即可,例如 get_all_securities(types=['etf'], date=datetime.date.today())

Q: get_all_securities 函数能否获取场外基金?
A: 可以。通过设置 types 参数为 ['open_fund'] (开放式基金)、['bond_fund'] (债券基金)、['stock_fund'] (股票型基金) 等来获取场外基金信息。具体类型代码请参考 API 文档。

Q: 为什么我不加 date 参数获取到的数量比加了 date 的多?
A: 如果不加 date 参数,函数默认返回历史上所有该类型的标的,包含了已经退市的基金。加上 date 参数后,系统会筛选出在该日期当天处于上市状态的标的。