← 返回博客列表

【Python量化实战 #10】Python按概念行业筛选股票:成分股与批量行情

2026年07月28日 11:18 · 麦蕊智数 MyData

按「概念/行业」找相关股票,是主题投资与板块轮动的基础能力。本文是「Python量化实战」第 10 篇:concept_treestocks_of_conceptconcepts_of_stock 的组合用法。

Playground:https://www.mairuiapi.com/playground
文档:https://www.mairuiapi.com/hsdata

环境准备

pip install mairui pandas

SDK:https://github.com/MaiRuiApi/mairui

概念树与成分股

import os
import pandas as pd
from mairui import Client

licence = os.environ.get("MAIRUI_LICENCE", "your_licence")

with Client(licence=licence) as api:
    tree = api.concept_tree()
    tree_df = pd.DataFrame(tree or [])
    print(tree_df.head())
    print(tree_df.columns.tolist())
# 从树中取一个概念/行业代码(字段名以实际为准)
# concept_code = tree_df.iloc[0]["code"]  # 示例

concept_code = "你的概念代码"

with Client(licence=licence) as api:
    members = api.stocks_of_concept(concept_code)

mem_df = pd.DataFrame(members or [])
print(len(mem_df), mem_df.head())

股票反查概念

stock_code = "000001.SZ"

with Client(licence=licence) as api:
    concepts = api.concepts_of_stock(stock_code)

print(pd.DataFrame(concepts or []).head())

进阶:成分股批量拉日线

codes = []
# 从 mem_df 提取代码列后:
# codes = mem_df["dm"].astype(str).tolist()[:20]

with Client(licence=licence) as api:
    if codes:
        batch = api.map(
            api.stock_history,
            codes,
            period="d",
            dividend="n",
            lt=30,
            max_workers=6,
        )
        print("pulled", sum(1 for x in batch if x))

避坑

  • 概念代码与股票代码不要混用。
  • 成分可能较多,批量时控制并发与日限额。
  • 板块涨跌归因需结合行情,单看名单不够。

本篇为进阶数据模块收尾;后续进入策略实战(双均线等)。欢迎使用 Playground 验证接口。

麦蕊智数 · Python量化实战 #10

QQ 客服 3826425416 咨询时请提供证书号或订单号,便于快速处理
咨询