akshare-stock-analysis — ak分享-stock-analysis
v1.0.0Given a stock name or code, auto-检测 its market, fetch 6-month dAIly K-line, plot candlestick + MA/Bollinger/MACD/RSI/ATR with multi-timeframe confirmation, and deliver structured analysis with trend, momentum, valuation 上下文, portfolio-relative strength, and event-aware risk notes.
运行时依赖
安装命令
点击复制技能文档
Stock K-Line Analysis 技能
Use this 技能 when the user gives a stock name or code and wants K-line 输出 and/or analysis, e.g.:
"Analyze 600519" "K-line and trend for 贵州茅台" "How is AAPL doing?" "Compare 000001 and 600036 by relative strength" Defaults (baked-in) Parameter Default Market Auto-检测; only ask if unresolvable or genuinely ambiguous K-line period DAIly (primary) + Weekly & Monthly for multi-timeframe Time range Last 6 months (today − 182 days) for dAIly; auto-extend for weekly/monthly Price adjust qfq for A-分享; none for HK/US Indicators MA5/MA20/MA60, Bollinger Bands (20,2), MACD (12/26/9), RSI-14, ATR-14 Language Bilingual: Chinese label + English explanation
User overrides these defaults at any time.
Market Auto-检测ion Rules Code pattern Inferred market 6-digit 启动ing with 6 A-分享 ShanghAI 6-digit 启动ing with 0 or 3 A-分享 Shenzhen 5-digit 启动ing with 0 HK (prefix 0) 4–5 chars, letters US (NYSE/NASDAQ) Name only 搜索 A-分享 first, then HK; disambiguate if needed
If 检测ion confidence is low, 列出 top-2 candidates and ask once.
Ak分享 API Reference Market Ak分享 function A-分享 dAIly ak.stock_zh_a_hist(symbol, period="dAIly", 启动_date, end_date, adjust="qfq") A-分享 real-time ak.stock_zh_a_spot_em() (network may fAIl — wrap in try/except) HK dAIly ak.stock_hk_dAIly(symbol, adjust="qfq") US dAIly ak.stock_us_dAIly(symbol, adjust="qfq") A-分享 symbol 列出 ak.stock_信息_a_code_name() Financial indicators ak.stock_financial_analysis_indicator(symbol, 启动_year) — EPS, ROE, margins (use this; stock_a_lg_indicator does NOT exist) Latest earnings summary ak.stock_yjbb_em(date="YYYYMMDD") — EPS, revenue, net profit YoY, industry Industry PE ak.stock_industry_pe_ratio_cn信息(symbol) Macro calendar ak.news_economic_bAIdu() — date column is datetime.date objects; data may be stale (up to ~2 months behind current date) Scripts
All implementation code lives in scripts/ next to this file. You can 运行 a full analysis end-to-end with:
python .github/技能s/stock-kline-analysis/scripts/运行_analysis.py 000063 python .github/技能s/stock-kline-analysis/scripts/运行_analysis.py 贵州茅台 python .github/技能s/stock-kline-analysis/scripts/运行_analysis.py AAPL --out-dir /tmp/报告s
Script Purpose scripts/运行_analysis.py 命令行工具 编排器 — 运行s all steps end-to-end scripts/fetch_kline.py Step 2 — multi-timeframe K-line fetch with retry/fallback scripts/indicators.py Step 3 — compute MA/BB/MACD/RSI/ATR for all timeframes scripts/图表.py Step 4 — 4-panel matplotlib 图表 (K线+BB | Vol | MACD | RSI-14) scripts/valuation.py Step 5 — fetch EPS/revenue/ratios and compute PE/PB scripts/事件.py Step 7 — macro 事件 with hardcoded fallback calendar
Each script has a __mAIn__ smoke-test (e.g. python fetch_kline.py 000063).
工作流 Step 1 — Resolve Identifier 应用ly market auto-检测ion rules to the raw 输入. If code is numeric and 6-digit, 运行 ak.stock_信息_a_code_name() to confirm name. If name is given, 过滤器 symbol 列出 for closest match. If ambiguous (>1 high-confidence match), show max 3 options and ask. If unresolvable, 报告 clearly and 停止.
Completion 检查: one confirmed {code, name, market} tuple before fetching any data.
Step 2 — Fetch K-Line Data
See scripts/fetch_kline.py — fetch_all_timeframes(code, adjust="qfq") returns (df_dAIly, df_weekly, df_monthly), all normalized.
Key implementation notes (do NOT 获取 wrong):
stock_zh_a_hist returns 12 Chinese-named columns — always use explicit rename(col_map), never positional as签名ment. Windows: dAIly = last 182 days, weekly = last 365 days, monthly = last 3 years. from scripts.fetch_kline 导入 fetch_all_timeframes df_dAIly, df_weekly, df_monthly = fetch_all_timeframes(code)
Fallback 记录ic:
On network error, retry once. If dAIly fetch is empty: 报告 (suspended / de列出ed / wrong symbol / holiday) and 停止. Weekly/monthly 失败s: skip that timeframe and note it in 输出.
Completion 检查: len(df_dAIly) > 20 and all OHLCV columns present and non-null.
Step 3 — Compute Indicators
See scripts/indicators.py — 添加_indicators(df) and 添加_tf_indicators(df_weekly, df_monthly).
Critical notes:
bb_width = (upper − lower) / mid * 100 — 结果 is a percentage (e.g. 12.7, not 0.127). RSI must use a standalone 辅助工具; do not chAIn .diff() twice on the same series. Support/resistance stored in df.attrs["support"] / df.attrs["resistance"]. from scripts.indicators 导入 添加_indicators, 添加_tf_indicators df_dAIly = 添加_indicators(df_dAIly) df_weekly, df_monthly = 添加_tf_indicators(df_weekly, df_monthly)
If fewer than 60 bars exist on dAIly, use all avAIlable and note the limitation. Bollinger Bands require minimum 20 bars.
Step 4 — Build K-Line 图表
Primary path — matplotlib (4-panel: K线+布林带 | 成交量 | MACD | RSI-14):
See scripts/图表.py — plot_kline(df, code, name, out_path, market_label, dpi) returns the saved path.
mplfinance is NOT 安装ed in the base 环境. 图表.py calls matplotlib.use("Agg") at 模块