问题描述
解决方案
在聚宽(JoinQuant)平台中,查询一只股票所属的行业和概念板块主要使用 get_industry 和 get_concept 这两个 API 函数。
以下是具体的查询方法和代码示例:
1. 查询所属行业 (get_industry)
使用 get_industry(security, date=None) 函数可以查询股票所属的行业。该函数会返回多个标准的行业分类,包括申万(SW)、聚宽(JQ)和证监会(ZJW)行业分类。
- 参数:
security:标的代码(字符串或列表),如'000001.XSHE'。date:查询日期,默认为 None(研究中为当天,回测中为回测当前日期)。
- 返回值:
- 返回一个字典,Key 是股票代码,Value 是包含各行业分类信息的字典。
2. 查询所属概念板块 (get_concept)
使用 get_concept(security, date=None) 函数可以查询股票所属的概念板块。
- 参数:
security:标的代码(字符串或列表)。date:查询日期。
- 返回值:
- 返回一个字典,Key 是股票代码,Value 是包含概念列表的字典(Key 为
'jq_concept')。
- 返回一个字典,Key 是股票代码,Value 是包含概念列表的字典(Key 为
代码示例
你可以在聚宽的研究环境或回测/模拟交易中运行以下代码:
# -*- coding: utf-8 -*-
from jqdata import *
# 设定要查询的股票代码,例如:平安银行
stock_code = '000001.XSHE'
# --- 1. 查询行业 ---
# 获取行业数据
industry_data = get_industry(stock_code)
print("=== 股票 %s 所属行业信息 ===" % stock_code)
if stock_code in industry_data:
ind_info = industry_data[stock_code]
# 打印申万一级行业
if 'sw_l1' in ind_info:
print("申万一级行业: %s (%s)" % (ind_info['sw_l1']['industry_name'], ind_info['sw_l1']['industry_code']))
# 打印聚宽一级行业
if 'jq_l1' in ind_info:
print("聚宽一级行业: %s (%s)" % (ind_info['jq_l1']['industry_name'], ind_info['jq_l1']['industry_code']))
# 打印证监会行业
if 'zjw' in ind_info:
print("证监会行业: %s (%s)" % (ind_info['zjw']['industry_name'], ind_info['zjw']['industry_code']))
else:
print("未查询到行业信息")
print("-" * 30)
# --- 2. 查询概念板块 ---
# 获取概念数据
concept_data = get_concept(stock_code)
print("=== 股票 %s 所属概念板块 ===" % stock_code)
if stock_code in concept_data:
# 获取概念列表
concepts = concept_data[stock_code]['jq_concept']
for c in concepts:
print("概念代码: %s, 概念名称: %s" % (c['concept_code'], c['concept_name']))
else:
print("未查询到概念信息")
输出结果示例(参考)
运行上述代码后,输出结果大致如下:
=== 股票 000001.XSHE 所属行业信息 ===
申万一级行业: 银行I (801780)
聚宽一级行业: 金融指数 (HY007)
证监会行业: 货币金融服务 (J66)
------------------------------
=== 股票 000001.XSHE 所属概念板块 ===
概念代码: GN028, 概念名称: 智能电网
概念代码: GN167, 概念名称: 融资融券
... (其他概念)
Q&A
Q: get_industry 返回结果中的 sw_l1, sw_l2, sw_l3 分别代表什么?
A: 它们分别代表申万行业分类的一级、二级和三级行业。sw_l1 通常是大类行业(如银行、医药生物),sw_l2 和 sw_l3 则是更细分的子行业。
Q: 如果我想反过来,查询某个行业或概念板块里包含哪些股票,应该用什么函数?
A:
- 查询行业成分股:使用
get_industry_stocks(industry_code, date=None)。 - 查询概念成分股:使用
get_concept_stocks(concept_code, date=None)。
Q: 为什么查询到的概念板块有时候会变动?
A: 概念板块是动态调整的,随着市场热点和公司业务的变化,聚宽数据团队会维护和更新概念成分股。如果在回测中指定了 date 参数,API 会返回该日期当时的概念分类,避免未来函数。
Q: 如何获取所有的行业代码列表?
A: 可以使用 get_industries(name='sw_l1')(获取申万一级)或 get_industries(name='jq_l1')(获取聚宽一级)等来查看所有行业代码和名称。