智能券商平台开发时板块、行业基础数据怎么获取?实操来了
在开发智能券商平台时必然会涉及到板块、行业这些基本数据业务上都会去做热门行业或市场总览等计算所以先要有行业板块的基础数据然后要获取到行业板块下的成分股最后才能根据个股数据进行计算。这个过程不难但还是有些复杂所以记录一下我是怎么获取行业板块基础数据的。下面直接贴具体的Python代码因为有详细的注释就不再另加描述了。同步行业字典表 code_industry实现见 stock_lab.equity.code_industry_sync。 本脚本有两种数据来源二选一 1) 默认 sourcescreeners - 从 OpenBB 拉美股/港股 screenerFinviz、FMP、可选 Yahoo合并去重得到 (sector, industry) 列表再写入 MySQL。 2) --from-code-infosourcecode_info - 不调用任何外部 screener API。 - 从本库表 code_info 中读取「已由 update_code_info_sector_industry 等脚本写入」的 sector环境变量常为 board 列与 industry 列做 SQL DISTINCT 后再按同一套规则 归一化、去重写入 code_industry。 - 适用你已用 profile 把各标的行业写进 code_info希望字典表与之一致、且避免 Yahoo 限流。 - 板块为占位符如 Unknown、空时归一化后与「无板块」一致若字典表里已有 industry 但 sector 仍是 Unknown可用默认 screener 同步加 **--refresh-unknown-sectors**用本次 Finviz/FMP 等合并结果按 industry 名回填真实板块。 --json将 run_sync 返回的统计字典以 JSON 打印到 stdout便于脚本解析不写 --json 则打印人类可读摘要。 --dry-run只计算条数不连接写库见 run_sync 内逻辑。 from __future__ import annotations import argparse import json import sys from pathlib import Path _ROOT Path(__file__).resolve().parents[2] if str(_ROOT) not in sys.path: sys.path.insert(0, str(_ROOT)) from stock_lab.equity.code_industry_sync import run_sync def main() - None: p argparse.ArgumentParser( description( 同步板块/行业字典到 MySQL code_industry。 默认同时拉取美股Finviz可选合并 FMP与港股优先 FMP默认不再自动请求 Yahoo 避免限流。需 Yahoo 时请传 --hk-yahoo-fallback。 按 (sector, industry) 去重合并后写入。 ), ) p.add_argument(--dry-run, actionstore_true, help只统计不写库) p.add_argument(--skip-us, actionstore_true, help不拉美股Finviz仅 screeners 模式有效) p.add_argument(--skip-hk, actionstore_true, help不拉港股仅 screeners 模式有效) p.add_argument(--us-limit, typeint, default10000) p.add_argument(--hk-limit, typeint, default10000) p.add_argument( --hk-yahoo-only, actionstore_true, help港股只用 Yahoo不先尝试 FMPFMP 需在 OpenBB 配置 fmp_api_key, ) p.add_argument( --hk-fmp-only, actionstore_true, help显式港股仅 FMP与默认行为相同与 --hk-yahoo-fallback 互斥, ) p.add_argument( --hk-yahoo-fallback, actionstore_true, helpFMP 无有效 sector/industry 时再请求 Yahoo易触发限流默认关闭, ) p.add_argument( --json, actionstore_true, help将本次同步统计以 JSON 输出到标准输出字段含 source、merged_unique_pairs、inserted 等, ) p.add_argument( --prune-stale, actionstore_true, help对本次拉取中未出现的 (sector, industry_name_en) 做软删deleted_at 若 limit 过小或 API 不完整可能误删。默认关闭。, ) p.add_argument( --require-both, actionstore_true, help未使用 --skip-us/--skip-hk 时若美股或港股任一侧条数为 0 则报错退出保证两侧都有数据再写库, ) p.add_argument( --from-code-info, actionstore_true, help从 MySQL code_info 已写入的 sector(board)industry 去重后写入 code_industry与 update_code_info_sector_industry 同一套列不调用 screener, ) p.add_argument( --refresh-unknown-sectors, actionstore_true, help仅 screener 模式同步前将库中 sector 为空或 Unknown 等占位的行按本次美/港 screener 合并结果按 industry 名回填板块, ) args p.parse_args() if args.from_code_info and (args.skip_us or args.skip_hk): p.error(--from-code-info 与 --skip-us/--skip-hk 互斥) if args.from_code_info and args.refresh_unknown_sectors: p.error(--refresh-unknown-sectors 仅适用于默认 screener 模式不能与 --from-code-info 同用) if args.hk_yahoo_only and args.hk_fmp_only: p.error(--hk-yahoo-only 与 --hk-fmp-only 不能同时使用) if args.hk_fmp_only and args.hk_yahoo_fallback: p.error(--hk-fmp-only 与 --hk-yahoo-fallback 不能同时使用) # 港股仅当用户显式 --hk-yahoo-fallback 且未 --hk-fmp-only 时在 FMP 无数据后再请求 Yahoo hk_yahoo_fallback args.hk_yahoo_fallback and not args.hk_fmp_only # --from-code-info走 code_info 聚合路径仅 MySQL DISTINCT 与库内一致的列配置 if args.from_code_info: summary run_sync( sourcecode_info, dry_runargs.dry_run, prune_staleargs.prune_stale, require_both_marketsargs.require_both, refresh_unknown_sectorsFalse, ) else: # 默认OpenBB screener 路径美/港可 skip港股 Yahoo 回退由 hk_yahoo_fallback 控制 summary run_sync( sourcescreeners, skip_usargs.skip_us, skip_hkargs.skip_hk, us_limitargs.us_limit, hk_limitargs.hk_limit, hk_prefer_fmpnot args.hk_yahoo_only, hk_yahoo_fallbackhk_yahoo_fallback, dry_runargs.dry_run, prune_staleargs.prune_stale, require_both_marketsargs.require_both, refresh_unknown_sectorsargs.refresh_unknown_sectors, ) # 以下警告仅针对 screener 模式code_info 模式若行业为空会在 run_sync 或 SQL 侧体现为 0 条 if not args.from_code_info and not args.skip_us and not args.skip_hk: if summary[us_pairs] 0: print( 警告: 美股本次未得到任何 sector/industry 对合并结果仅含港股若港股有数据。, filesys.stderr, ) if summary[hk_pairs] 0: print( 警告: 港股本次未得到任何 sector/industry 对合并结果仅含美股若美股有数据。 请在 .env 配置 FMP_API_KEY推荐若必须走 Yahoo 再加 --hk-yahoo-fallback易限流。, filesys.stderr, ) # --jsonstdout 输出机器可读统计否则输出简短键值列表 if args.json: print(json.dumps(summary, ensure_asciiFalse, indent2, defaultstr)) else: print(完成。) for k, v in summary.items(): print(f {k}: {v}) if __name__ __main__: main()代码可直接运行前提你的环境是好的同时安装了OpenBB并配置了对应的数据源。如果你第一次开发券商平台前面的坑还是有点多欢迎交流一起学习一起进步。