stock-analysis-plugin 0.2.10 → 0.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -11
- package/package.json +11 -10
- package/pi/index.ts +22 -2
- package/skills/stock-analysis/SKILL.md +4 -1
- package/tools/alert_rules.py +63 -14
- package/tools/name_resolver.py +16 -14
- package/tools/requirements.txt +2 -2
- package/tools/search_intel.py +2 -2
- package/tools/signal_tracker.py +6 -5
- package/tools/stock_data.py +255 -24
- package/tools/tiaa_disclosures.py +263 -0
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ A 股 / 港股 / 美股 / 日股 / 韩股 / 台股综合分析、多因子选股
|
|
|
32
32
|
|
|
33
33
|
## 功能概览
|
|
34
34
|
|
|
35
|
-
- **
|
|
35
|
+
- **72 个工具** — 行情数据、技术分析、K 线形态、资金流向、估值历史、财报日历、股东/ETF/股息、新闻舆情、风险筛查、市场状态等
|
|
36
36
|
- **21 个 Skills** — 综合分析、全市场选股、策略回测 + 18 个策略、复盘与研究方法论(缠论、波浪、龙头、情绪周期等)
|
|
37
37
|
- **策略回测引擎** — YAML DSL 定义策略,参数化条件组合,自动诊断,按需进行有限轮参数优化
|
|
38
38
|
- **多数据源 Failover** — 11 个数据源自动容灾切换(akshare / tushare / efinance / 腾讯行情 / 新浪行情 / pytdx / baostock / yfinance / finnhub / longbridge / alphavantage),A 股链路带粘性优选(最近成功的数据源下次优先尝试)
|
|
@@ -70,7 +70,7 @@ npm install @weaxs/stock-analysis-plugin
|
|
|
70
70
|
|
|
71
71
|
Pi 启动后自动加载,输入 `/skills` 确认。详见 [Pi Agent 接入指南](docs/pi-integration.md)([English](docs/pi-integration.en.md))。
|
|
72
72
|
|
|
73
|
-
新版 Pi(已核验 1.
|
|
73
|
+
新版 Pi(已核验 1.5.2)提供原生 `tool_search` 时,插件自动启用按需工具发现,72 项能力保留注册,仅命中工具加载完整定义。缺少原生发现接口的旧版 Pi 或 SDK 宿主保持全量暴露。可直接用中文关键词或工具名检索,例如 `实时行情`、`get_quote`、`get_financials`;Pi 1.5 之前的版本只能匹配英文关键词。
|
|
74
74
|
|
|
75
75
|
### Hermes Agent Plugin
|
|
76
76
|
|
|
@@ -95,7 +95,7 @@ register(ctx)
|
|
|
95
95
|
openclaw plugins install clawhub:@weaxs/openclaw-stock-analysis
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
-
OpenClaw Gateway 启动后自动加载并注册
|
|
98
|
+
OpenClaw Gateway 启动后自动加载并注册 72 个 tool。安装时 postinstall 会自动建 `.venv` 并装好 Python 依赖(前提:本机有 `python3 >= 3.10`)。
|
|
99
99
|
|
|
100
100
|
详见 [OpenClaw 接入指南](docs/openclaw-integration.md),或 plugin 自身说明 [`openclaw/README.md`](openclaw/README.md)。
|
|
101
101
|
|
|
@@ -106,7 +106,9 @@ dsh plugin --profile <你的profile> add @weaxs/dsh-stock-analysis
|
|
|
106
106
|
dsh --profile <你的profile>
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
-
作为 dsh bundle 安装:`dsh plugin add` 会把 `cordis.patch.yml` 叠加进 profile 组合,注册
|
|
109
|
+
作为 dsh bundle 安装:`dsh plugin add` 会把 `cordis.patch.yml` 叠加进 profile 组合,注册 72 个 tool + 21 个 skill。postinstall 自动建 `.venv`(pnpm 需在 profile 的 `pnpm-workspace.yaml` 里给本包配 `allowBuilds`,否则回退系统 `python3`)。
|
|
110
|
+
|
|
111
|
+
支持 dsh 前端的宿主还会在侧栏提供「股票仪表盘」:可排序、按市场筛选的自选股行情表(今日区间、成交额、30 日走势),顶部六市场指数带与 A 股涨跌分布,下方各市场指数、A 股涨跌统计和行业涨跌榜。插件在后台常驻预热并缓存行情,页面只读内存快照,打开即显示;交易时段有页面打开时约 30 秒更新一次。数据仍复用共享 Python CLI,不新增模型工具。
|
|
110
112
|
|
|
111
113
|
新版 dsh 的原生函数调用模式默认通过 `stock_tool_search` 按 Agent 搜索、加载所需工具,支持中文关键词或工具名;旧接口、搜索入口不可用时保留全量暴露。可设置 `toolDiscovery: false` 关闭,配置与 PTC 模式边界见下方接入文档。
|
|
112
114
|
|
|
@@ -253,7 +255,8 @@ Agent 会自动调用行情 → 技术面 → 基本面 → 资金面 → 消息
|
|
|
253
255
|
| `get_etf_details` | A 股 ETF 净值折溢价、规模变动、披露持仓、可选基准成分与跟踪误差 |
|
|
254
256
|
| `get_dividends` | A/HK 历史分红/除息日历/已实现 TTM 股息与连续年数;A/HK 报告期/年度排行 |
|
|
255
257
|
| `get_kline` | K 线数据(OHLCV),支持日/周/月线;A 股股票/ETF 前复权,每条记录 `adjusted=qfq`,指数 `adjusted=none` |
|
|
256
|
-
| `get_quote` | 实时行情快照(非交易时段返回最近交易日收盘价并以 as_of/stale 标注;`pe_basis` 区分 dynamic/trailing/unknown;ETF 的 premium_discount_rate
|
|
258
|
+
| `get_quote` | 实时行情快照(非交易时段返回最近交易日收盘价并以 as_of/stale 标注;`pe_basis` 区分 dynamic/trailing/unknown;ETF 的 premium_discount_rate 为正=溢价、负=折价);场外基金用 `get_fund_quote`,TIAAxxxxxx 返回投顾组合识别、`advisor_org` 与无统一净值提示;长钱/海外长钱/稳钱另有有知有行 `official_disclosures`(`partial=true`,非实时完整持仓);其他组合搜索可用时为 `search_partial`,否则 `unsupported_provider` |
|
|
259
|
+
| `get_fund_quote` | 6 位场外公募基金净值;`nav` 是 `nav_date` 对应的已披露日终净值而非实时价,`accumulated_nav` 为累计净值、`change_pct` 为日涨跌幅;`estimated_nav` 可能为 `null`,QDII 净值可能滞后 1-2 交易日 |
|
|
257
260
|
| `get_capital_flow` | 资金流向(仅 A 股) |
|
|
258
261
|
| `get_news` | 个股相关新闻 |
|
|
259
262
|
| `get_financials` | 财务指标 |
|
|
@@ -500,7 +503,7 @@ python tools/value_data.py dividends --mode ranking --market HK --report-date 20
|
|
|
500
503
|
python tools/value_data.py earnings_calendar --symbol 0700.HK
|
|
501
504
|
```
|
|
502
505
|
|
|
503
|
-
|
|
506
|
+
财报、基本信息、股票代码/名称映射及估值/股东/ETF/分红等慢变数据缓存到 `~/.stock-analysis/cache`(可通过 `STOCK_CACHE_DIR` 指定可写目录,适用于沙箱/只读用户目录),TTL 一天;板块成分/映射一周。行情报价不缓存。缓存写入失败时直接返回已获取数据;缓存文件原子替换,过期/损坏重拉,错误和显式部分失败不缓存;不以过期数据伪装最新数据。原有临时目录的粘性路由和反向成分缓存保留。 同键缓存缺失与 SEC ZIP 下载通过跨进程文件锁协调,等待最多 5 秒;普通缓存锁不可用或超时后仍可直接抓取,持久写入/ZIP 下载则返回错误,避免无界等待。有效缓存命中不要求目录可写。
|
|
504
507
|
|
|
505
508
|
估值使用可得数据的正值样本分位(`<=当前值`),返回 10/25/50/75/90 分位 Band 与实际起止日期;亏损 PE 分位为 null。当前 AKShare 已无 issue 中提到的 `stock_a_indicator_lg`,个股采用百度 PE-TTM/PB 历史,指数采用乐咕乐股;中证全指等通过中证官方历史接口取得多年滚动 PE,近期总股本 PE1 仍单独保留;PB 缺失时可配置 `LIXINGER_TOKEN` 补充理杏仁总市值加权(mcw)历史,返回每个指标的来源与口径,不拼接不同方法的同一指标。未配置或无权限时 PB 保持未知。A 股 `annual=true, periods=10` 请求十年年报;HK 按源实际可得年数返回,不补造缺失比率。
|
|
506
509
|
|
|
@@ -569,20 +572,23 @@ GitHub Actions 的 `e2e-host` 使用 Pi、Hermes、OpenClaw、dsh 的真实运
|
|
|
569
572
|
```
|
|
570
573
|
stock-analysis/
|
|
571
574
|
├── pi/ # Pi Agent Extension
|
|
572
|
-
│ └── index.ts # 注册
|
|
575
|
+
│ └── index.ts # 注册 72 个工具 + 21 个 skill
|
|
573
576
|
├── hermes/ # Hermes Agent Plugin
|
|
574
577
|
│ ├── plugin.yaml # 插件清单
|
|
575
578
|
│ ├── __init__.py # register(ctx) 入口
|
|
576
579
|
│ ├── schemas.py # 工具 JSON Schema 定义
|
|
577
|
-
│ └── tools.py #
|
|
580
|
+
│ └── tools.py # 72 个 handler → subprocess 调 CLI
|
|
578
581
|
├── openclaw/ # OpenClaw Plugin
|
|
579
582
|
│ ├── openclaw.plugin.json # manifest(contracts.tools)
|
|
580
583
|
│ ├── package.json # 含 openclaw 块(pluginApi/SDK 版本)
|
|
581
|
-
│ └── index.ts # definePluginEntry + registerTool ×
|
|
584
|
+
│ └── index.ts # definePluginEntry + registerTool ×72
|
|
582
585
|
├── dsh/ # DeepSeek Harness (dsh) Plugin
|
|
583
586
|
│ ├── cordis.patch.yml # bundle patch(insert 插件入口)
|
|
584
|
-
│ ├── package.json #
|
|
585
|
-
│
|
|
587
|
+
│ ├── package.json # dsh.bundle + dsh.client manifest
|
|
588
|
+
│ ├── index.ts # cordis apply(ctx) + defineTool ×72
|
|
589
|
+
│ ├── dashboard.ts # 后台预热缓存 + 快照/自选股 HTTP API 与持久化
|
|
590
|
+
│ ├── client.ts # 侧栏入口 + 主面板 + 主题同步(编译为 dist/client.js)
|
|
591
|
+
│ └── web/ # 仪表盘页面(app.ts 编译为 dist/web/app.js)
|
|
586
592
|
│
|
|
587
593
|
├── tools/ # 共享 Python CLI 工具
|
|
588
594
|
│ ├── stock_data.py # 行情 / 资金流 / 新闻 / 财务 / 短线情绪
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stock-analysis-plugin",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.11",
|
|
4
4
|
"description": "Pi Agent extension for stock analysis, screening, and strategy backtesting across A/HK/US/JP/KR/TW markets",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./pi/index.ts",
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"scripts": {
|
|
49
49
|
"postinstall": "node scripts/setup-python.mjs",
|
|
50
50
|
"build:openclaw": "esbuild openclaw/index.ts --bundle --format=esm --platform=node --external:openclaw/* --outfile=openclaw/dist/index.js",
|
|
51
|
-
"build:dsh": "esbuild dsh/index.ts dsh/research-role.ts --bundle --format=esm --platform=node --external:@deepseek-ai/* --outdir=dsh/dist"
|
|
51
|
+
"build:dsh": "esbuild dsh/index.ts dsh/research-role.ts --bundle --format=esm --platform=node --external:@deepseek-ai/* --outdir=dsh/dist && esbuild dsh/client.ts dsh/web/app.ts dsh/web/index.html dsh/web/style.css --bundle --format=iife --platform=browser --target=es2020 --outbase=dsh --outdir=dsh/dist --loader:.html=copy --loader:.css=copy"
|
|
52
52
|
},
|
|
53
53
|
"keywords": [
|
|
54
54
|
"pi-agent",
|
|
@@ -78,17 +78,18 @@
|
|
|
78
78
|
},
|
|
79
79
|
"license": "MIT",
|
|
80
80
|
"devDependencies": {
|
|
81
|
-
"@deepseek-ai/cordis": "4.0
|
|
82
|
-
"@deepseek-ai/dsh-llm": "0.2.
|
|
83
|
-
"@deepseek-ai/dsh-skill": "0.2.
|
|
84
|
-
"@deepseek-ai/dsh-system-prompt": "0.2.
|
|
85
|
-
"@deepseek-ai/dsh-
|
|
81
|
+
"@deepseek-ai/cordis": "4.1.0",
|
|
82
|
+
"@deepseek-ai/dsh-llm": "0.2.2-rc.1",
|
|
83
|
+
"@deepseek-ai/dsh-skill": "0.2.2-rc.1",
|
|
84
|
+
"@deepseek-ai/dsh-system-prompt": "0.2.2-rc.1",
|
|
85
|
+
"@deepseek-ai/dsh-tool-executor-native": "0.2.2-rc.1",
|
|
86
|
+
"@deepseek-ai/dsh-tools": "0.2.2-rc.1",
|
|
86
87
|
"@types/node": "^24.19.2",
|
|
87
88
|
"esbuild": "^0.28.2",
|
|
88
89
|
"jiti": "^2.7.0",
|
|
89
|
-
"openai": "^7.
|
|
90
|
-
"tsx": "^4.
|
|
91
|
-
"typebox": "^1.3
|
|
90
|
+
"openai": "^7.47.0",
|
|
91
|
+
"tsx": "^4.23.15",
|
|
92
|
+
"typebox": "^1.4.3",
|
|
92
93
|
"typescript": "^5.5.0"
|
|
93
94
|
},
|
|
94
95
|
"overrides": {
|
package/pi/index.ts
CHANGED
|
@@ -41,7 +41,7 @@ export default (pi: ExtensionAPI) => {
|
|
|
41
41
|
return discoveryEnabled ? {
|
|
42
42
|
message: {
|
|
43
43
|
customType: "stock-analysis-discovery",
|
|
44
|
-
content: "Stock analysis tools are available through tool_search. Search
|
|
44
|
+
content: "Stock analysis tools are available through tool_search. Search with exact tool names (e.g. get_quote, get_financials, screen_stocks, run_backtest) or keywords; their descriptions are Chinese, so Chinese keywords match on Pi 1.5+, while older Pi matches only English/ASCII terms. Load the tools needed for the task, then call them normally.",
|
|
45
45
|
display: false,
|
|
46
46
|
},
|
|
47
47
|
} : undefined;
|
|
@@ -1304,7 +1304,7 @@ export default (pi: ExtensionAPI) => {
|
|
|
1304
1304
|
registerTool({
|
|
1305
1305
|
name: "get_quote",
|
|
1306
1306
|
description:
|
|
1307
|
-
"获取股票实时行情报价(现价、涨跌幅、量比等)。支持A股、港股、美股、日股、韩股、台股;非交易时段返回最近交易日收盘价并以 as_of/stale 标注;ETF 的 premium_discount_rate 为正=溢价、负=折价。返回 source、UTC fetched_at 和提供方 data_as_of(未知为null);as_of_basis
|
|
1307
|
+
"获取股票实时行情报价(现价、涨跌幅、量比等)。支持A股、港股、美股、日股、韩股、台股;非交易时段返回最近交易日收盘价并以 as_of/stale 标注;ETF 的 premium_discount_rate 为正=溢价、负=折价。返回 source、UTC fetched_at 和提供方 data_as_of(未知为null);as_of_basis区分提供方时间与交易日历推断,采集时间不等于行情时间。场外基金用 get_fund_quote;TIAAxxxxxx 返回投顾组合识别、advisor_org 与无统一净值提示;长钱/海外长钱/稳钱另有有知有行 official_disclosures(partial=true,非实时完整持仓);其他组合搜索可用时为 search_partial,否则 unsupported_provider。",
|
|
1308
1308
|
parameters: {
|
|
1309
1309
|
type: "object",
|
|
1310
1310
|
properties: {
|
|
@@ -1321,6 +1321,26 @@ export default (pi: ExtensionAPI) => {
|
|
|
1321
1321
|
},
|
|
1322
1322
|
});
|
|
1323
1323
|
|
|
1324
|
+
registerTool({
|
|
1325
|
+
name: "get_fund_quote",
|
|
1326
|
+
description:
|
|
1327
|
+
"获取场外公募基金净值(6位基金代码,如018147)。nav 是已披露日终净值,非实时价;返回 accumulated_nav(累计净值)、change_pct(日涨跌幅)、nav_date(净值日期)。estimated_nav 为估算净值,可能为 null,不能当作实时价;QDII 净值可能滞后 1-2 交易日。",
|
|
1328
|
+
parameters: {
|
|
1329
|
+
type: "object",
|
|
1330
|
+
properties: {
|
|
1331
|
+
symbol: {
|
|
1332
|
+
type: "string",
|
|
1333
|
+
description: "6位场外公募基金代码,如 018147",
|
|
1334
|
+
},
|
|
1335
|
+
},
|
|
1336
|
+
required: ["symbol"],
|
|
1337
|
+
},
|
|
1338
|
+
async execute(_id, { symbol }) {
|
|
1339
|
+
const result = await py("stock_data.py", ["fund_quote", symbol]);
|
|
1340
|
+
return asText(result.stdout);
|
|
1341
|
+
},
|
|
1342
|
+
});
|
|
1343
|
+
|
|
1324
1344
|
registerTool({
|
|
1325
1345
|
name: "get_capital_flow",
|
|
1326
1346
|
description:
|
|
@@ -29,6 +29,7 @@ description: 综合股票分析:联合技术、基本面、资金、消息和
|
|
|
29
29
|
|------|---------|
|
|
30
30
|
| 个股买卖时机 | `get_technical_analysis` → `get_market_capabilities` → 当前市场支持的资金/风险工具 → `detect_market_regime`。A股可加 `get_capital_flow`(summary) 和 `screen_risk`;其他市场不得用缺失的资金或风险数据。veto_buy=true 则不入场;大盘下跌时个股看多信号降级为轻仓试错或等待;HOLD/WAIT 给出可观察触发条件 |
|
|
31
31
|
| 个股状态速览 | `get_quote` + `get_technical_analysis` |
|
|
32
|
+
| 场外公募基金净值 | `get_fund_quote`(6 位基金代码,如 018147) |
|
|
32
33
|
| 异动归因 | `detect_anomaly` + `get_news` |
|
|
33
34
|
| 大盘择时 | `detect_market_regime`;A 股可补充 `get_market_stats` |
|
|
34
35
|
|
|
@@ -46,6 +47,8 @@ python3 scripts/gather.py <symbol>
|
|
|
46
47
|
|
|
47
48
|
解读实时行情前先调 `get_trading_phase` 确认该市场当前盘中阶段(盘前/早盘/午间休市/午盘/交易中/盘后/休市),避免把休市时的静态快照当作盘中信号解读。
|
|
48
49
|
|
|
50
|
+
场外公募基金净值查询使用 `get_fund_quote(symbol="018147")`,不要当作股票实时报价。`nav` 是 `nav_date` 对应的已披露日终净值而非实时价;`accumulated_nav` 为累计净值,`change_pct` 为日涨跌幅。`estimated_nav` 是估算值且可能为 `null`,缺失时不得编造或当作实时成交价;QDII 净值可能滞后 1-2 交易日,须披露实际 `nav_date`。`TIAAxxxxxx` 使用 `get_quote` 获取投顾组合识别与无统一净值提示,不推算组合净值。`advisor_code/advisor_org` 按 TIAA 前三位机构段与资本市场标准网基金投顾业务编码表进行 best-effort 映射,来源和抓取时间随结果返回;投顾机构不等于主理人,查不到时保留 null。`official_disclosures` 目前仅对确认属于有知有行的 `TIAA001001/TIAA001002/TIAA001004` 提供,其他 TIAA 组合不得套用该站文章;有搜索配置时可返回 `disclosure_status=search_partial` 和 `public_search_results`,其中每项均为 `verification=unverified_search_result`,只能作为待核实线索,否则为 `unsupported_provider`。官方披露含发布日期、摘要、正文摘录和 URL;`disclosures_partial=true` 时必须视为不完整历史披露,不能当作实时业绩或完整成分基金清单。示例账户收益不等于用户收益,引用基金代码时必须保留对应文章日期和链接。
|
|
51
|
+
|
|
49
52
|
港股 `get_quote` 自动按 yfinance → 腾讯 → finnhub → longbridge 容灾,腾讯无需 API key,使用 `r_hk` 实时报价通道(`hk` 通道延迟约 15 分钟)。腾讯港股成交量为股,成交额及市值为港元;PB、换手率、量比为 null 时按缺失处理。该备用源只覆盖报价,K 线仍走原链路。
|
|
50
53
|
|
|
51
54
|
### 第二步:综合研判
|
|
@@ -88,7 +91,7 @@ python3 scripts/gather.py <symbol>
|
|
|
88
91
|
- 行情:`get_quote.price`、`get_quote.data_as_of`。
|
|
89
92
|
- 财务:`get_financials.report_date`、`get_financials.roe`,以及历史 `periods` 中实际返回的 `report_date`、`roe`,路径含数字下标。
|
|
90
93
|
- 回测:`run_backtest.metrics.total_trades`、`run_backtest.metrics.win_rate`。
|
|
91
|
-
每项保留原始 value,并按第 2 项填写 source/as_of;正文中的数字及其他证据的 as_of
|
|
94
|
+
每项保留原始 value,并按第 2 项填写 source/as_of;正文中的数字及其他证据的 as_of 元数据不替代独立引用。漏项先补齐再提交。报价过期时保留原始价格与时间作为历史证据;结论与风险说明均以报价的 data_as_of 为行情覆盖截止点,明确该时点至分析日及之后的行情变动未知,等待新报价后再制定当前数值交易计划。
|
|
92
95
|
4. 调用 `render_stock_report`:快速问答用 `brief`,完整研判用 `full`。
|
|
93
96
|
5. 输出渲染后的 Markdown;只在用户要求机器可读数据时附加结构化 JSON。
|
|
94
97
|
|
package/tools/alert_rules.py
CHANGED
|
@@ -140,28 +140,77 @@ def check_rules(symbol: str, rules: list[dict]) -> dict:
|
|
|
140
140
|
needs_anom = any(r.get("type") == "anomaly" for r in rules)
|
|
141
141
|
needs_risk = any(r.get("type") in ("risk_veto", "risk_level_at_least") for r in rules)
|
|
142
142
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
jobs
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
143
|
+
needs_quote = (
|
|
144
|
+
needs_anom
|
|
145
|
+
or needs_risk
|
|
146
|
+
or any(r.get("type") in ("price_below", "price_above", "change_pct_above", "change_pct_below") for r in rules)
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
# Share one market snapshot while retaining 60-bar alerts and 120-bar risk analysis.
|
|
150
|
+
with ThreadPoolExecutor(max_workers=4) as pool:
|
|
151
|
+
jobs = {}
|
|
152
|
+
if needs_quote:
|
|
153
|
+
quote_job = pool.submit(_run_json, "stock_data.py", ["quote", symbol])
|
|
154
|
+
jobs["quote"] = quote_job
|
|
155
|
+
if needs_tech or needs_anom or needs_risk:
|
|
156
|
+
from technical import analyze_records
|
|
157
|
+
|
|
158
|
+
kline_job = pool.submit(
|
|
159
|
+
_run_json,
|
|
160
|
+
"stock_data.py",
|
|
161
|
+
["kline", symbol, "--period", "daily", "--count", "120" if needs_risk else "60"],
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
def analyze(count):
|
|
165
|
+
records = kline_job.result()
|
|
166
|
+
try:
|
|
167
|
+
return analyze_records(symbol, records[-count:] if isinstance(records, list) else records)
|
|
168
|
+
except Exception as exc:
|
|
169
|
+
return {"error": f"Technical analysis failed ({type(exc).__name__})"}
|
|
170
|
+
|
|
171
|
+
if needs_risk:
|
|
172
|
+
from risk_screening import screen_risk
|
|
173
|
+
|
|
174
|
+
technical_job = pool.submit(analyze, 120)
|
|
175
|
+
|
|
176
|
+
def collect_risk():
|
|
177
|
+
try:
|
|
178
|
+
return screen_risk(
|
|
179
|
+
symbol, inputs={"quote": quote_job, "technical": technical_job}, run=_run_json
|
|
180
|
+
)
|
|
181
|
+
except Exception as exc:
|
|
182
|
+
return {"error": f"Risk analysis failed ({type(exc).__name__})"}
|
|
183
|
+
|
|
184
|
+
jobs["risk"] = pool.submit(collect_risk)
|
|
185
|
+
if needs_tech:
|
|
186
|
+
jobs["tech"] = pool.submit(analyze, 60)
|
|
187
|
+
if needs_anom:
|
|
188
|
+
from anomaly_detect import detect_anomalies
|
|
189
|
+
|
|
190
|
+
def anomalies():
|
|
191
|
+
records = kline_job.result()
|
|
192
|
+
try:
|
|
193
|
+
return detect_anomalies(
|
|
194
|
+
symbol,
|
|
195
|
+
records=records[-60:] if isinstance(records, list) else records,
|
|
196
|
+
quote=quote_job.result(),
|
|
197
|
+
run=_run_json,
|
|
198
|
+
)
|
|
199
|
+
except Exception as exc:
|
|
200
|
+
return {"error": f"Anomaly analysis failed ({type(exc).__name__})"}
|
|
201
|
+
|
|
202
|
+
jobs["anom"] = pool.submit(anomalies)
|
|
203
|
+
fetched = {key: future.result() for key, future in jobs.items()}
|
|
155
204
|
fetched = {
|
|
156
205
|
key: value if isinstance(value, dict) and "error" not in value else None for key, value in fetched.items()
|
|
157
206
|
}
|
|
158
|
-
quote = fetched
|
|
207
|
+
quote = fetched.get("quote") or {}
|
|
159
208
|
tech = fetched.get("tech")
|
|
160
209
|
anom = fetched.get("anom")
|
|
161
210
|
risk = fetched.get("risk")
|
|
162
211
|
|
|
163
212
|
warnings = []
|
|
164
|
-
if not quote or quote.get("price") is None:
|
|
213
|
+
if needs_quote and (not quote or quote.get("price") is None):
|
|
165
214
|
warnings.append("quote fetch failed or missing price — price/change rules cannot fire")
|
|
166
215
|
if needs_tech and not tech:
|
|
167
216
|
warnings.append("technical analysis fetch failed — volume_ratio rules cannot fire")
|
package/tools/name_resolver.py
CHANGED
|
@@ -9,30 +9,32 @@ from pathlib import Path
|
|
|
9
9
|
|
|
10
10
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
11
11
|
|
|
12
|
+
from _cache import DAY, cached # noqa: E402
|
|
12
13
|
from _subproc import utf8_stdio # noqa: E402
|
|
13
14
|
from stock_data import _akshare_retry # noqa: E402
|
|
14
15
|
|
|
15
|
-
_STOCK_MAP_CACHE = None
|
|
16
|
-
|
|
17
16
|
|
|
18
17
|
def _load_stock_map() -> list[dict]:
|
|
19
|
-
|
|
20
|
-
if _STOCK_MAP_CACHE is not None:
|
|
21
|
-
return _STOCK_MAP_CACHE
|
|
22
|
-
try:
|
|
18
|
+
def fetch():
|
|
23
19
|
import akshare as ak
|
|
24
20
|
|
|
25
21
|
df = _akshare_retry(ak.stock_zh_a_spot_em)
|
|
26
22
|
records = []
|
|
27
|
-
for
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
23
|
+
for row in df.to_dict("records"):
|
|
24
|
+
code, name = str(row["代码"]), row["名称"]
|
|
25
|
+
if (
|
|
26
|
+
len(code) != 6
|
|
27
|
+
or not code.isascii()
|
|
28
|
+
or not code.isdigit()
|
|
29
|
+
or not isinstance(name, str)
|
|
30
|
+
or not name.strip()
|
|
31
|
+
):
|
|
32
|
+
raise ValueError("Stock name map contains invalid codes or names")
|
|
33
|
+
records.append({"code": code, "name": name})
|
|
35
34
|
return records
|
|
35
|
+
|
|
36
|
+
try:
|
|
37
|
+
return cached("stock-name-map:v1", DAY, fetch)
|
|
36
38
|
except Exception as e:
|
|
37
39
|
raise RuntimeError(f"Failed to load stock list: {e}") from e
|
|
38
40
|
|
package/tools/requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
akshare>=1.
|
|
1
|
+
akshare>=1.20.24; python_version >= "3.11"
|
|
2
2
|
akshare>=1.14.0; python_version < "3.11"
|
|
3
3
|
yfinance>=0.2.36
|
|
4
4
|
pandas>=2.0.0
|
|
@@ -7,7 +7,7 @@ pyyaml>=6.0
|
|
|
7
7
|
pypinyin>=0.50.0
|
|
8
8
|
requests>=2.33.0
|
|
9
9
|
exchange-calendars>=4.5.0
|
|
10
|
-
newspaper4k>=0.9.
|
|
10
|
+
newspaper4k>=0.9.6.1
|
|
11
11
|
efinance>=0.5.0
|
|
12
12
|
baostock>=0.8.8
|
|
13
13
|
pytdx>=1.72
|
package/tools/search_intel.py
CHANGED
|
@@ -228,13 +228,13 @@ def _searxng_search(query: str, max_results: int = 5) -> list[dict]:
|
|
|
228
228
|
return []
|
|
229
229
|
|
|
230
230
|
|
|
231
|
-
def search_news(query: str, max_results: int = 10) -> dict:
|
|
231
|
+
def search_news(query: str, max_results: int = 10, budget_seconds: int = 45) -> dict:
|
|
232
232
|
results = []
|
|
233
233
|
engines_tried = []
|
|
234
234
|
engines_available = []
|
|
235
235
|
engines_errors = {}
|
|
236
236
|
|
|
237
|
-
with _search_budget(
|
|
237
|
+
with _search_budget(budget_seconds) as state:
|
|
238
238
|
for name, fn in [
|
|
239
239
|
("tavily", _tavily_search),
|
|
240
240
|
("brave", _brave_search),
|
package/tools/signal_tracker.py
CHANGED
|
@@ -136,11 +136,12 @@ def _save_records(store: Path, records: list):
|
|
|
136
136
|
def _append_record(store: Path, record: dict):
|
|
137
137
|
"""Append one complete JSON line; caller holds the store lock."""
|
|
138
138
|
payload = (json.dumps(record, ensure_ascii=False, default=str, allow_nan=False) + "\n").encode("utf-8")
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
139
|
+
previous = store.read_bytes() if store.exists() else b""
|
|
140
|
+
# Publish only complete records; a failed append must not leave a corrupt tail.
|
|
141
|
+
with atomic_write(store, binary=True) as stream:
|
|
142
|
+
stream.write(previous)
|
|
143
|
+
if previous and not previous.endswith(b"\n"):
|
|
144
|
+
stream.write(b"\n")
|
|
144
145
|
stream.write(payload)
|
|
145
146
|
|
|
146
147
|
|
package/tools/stock_data.py
CHANGED
|
@@ -1286,12 +1286,128 @@ def _quote_stale_marker(market: str):
|
|
|
1286
1286
|
}
|
|
1287
1287
|
|
|
1288
1288
|
|
|
1289
|
+
_YOUZHIYOUXING_TIAA = {
|
|
1290
|
+
"TIAA001001": "长钱账户",
|
|
1291
|
+
"TIAA001002": "海外长钱",
|
|
1292
|
+
"TIAA001004": "稳钱账户",
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
def _quote_tiaa(symbol: str) -> dict:
|
|
1297
|
+
import requests
|
|
1298
|
+
|
|
1299
|
+
disclosure_name = _YOUZHIYOUXING_TIAA.get(symbol)
|
|
1300
|
+
result = {
|
|
1301
|
+
"symbol": symbol,
|
|
1302
|
+
"name": None,
|
|
1303
|
+
"asset_type": "investment_advisory_portfolio",
|
|
1304
|
+
"type_description": "雪球投顾组合,无统一单位净值",
|
|
1305
|
+
"manager": None,
|
|
1306
|
+
"advisor_code": f"A{symbol[4:7]}",
|
|
1307
|
+
"advisor_org": None,
|
|
1308
|
+
"advisor_mapping_basis": "tiaa_prefix_best_effort",
|
|
1309
|
+
"advisor_source_url": None,
|
|
1310
|
+
"advisor_retrieved_at": None,
|
|
1311
|
+
"nav": None,
|
|
1312
|
+
"example_account_performance": None,
|
|
1313
|
+
"quote_type": None,
|
|
1314
|
+
"exchange": None,
|
|
1315
|
+
"source": None,
|
|
1316
|
+
"source_url": f"https://xueqiu.com/S/{symbol}",
|
|
1317
|
+
"data_as_of": None,
|
|
1318
|
+
"fetched_at": datetime.now(timezone.utc).isoformat(),
|
|
1319
|
+
"official_disclosures": [],
|
|
1320
|
+
"public_search_results": [],
|
|
1321
|
+
"disclosure_provider": "youzhiyouxing" if disclosure_name else None,
|
|
1322
|
+
"disclosure_status": "unavailable" if disclosure_name else "unsupported_provider",
|
|
1323
|
+
"disclosures_partial": True,
|
|
1324
|
+
"disclosures_as_of": None,
|
|
1325
|
+
"note": (
|
|
1326
|
+
"投顾组合无统一净值;主理人及完整业绩/持仓需在对应投顾平台登录后查询;"
|
|
1327
|
+
"官方公开披露仅为部分历史信息,持有人收益随买入时点和调仓跟进不同"
|
|
1328
|
+
),
|
|
1329
|
+
}
|
|
1330
|
+
try:
|
|
1331
|
+
with requests.Session() as session:
|
|
1332
|
+
session.get(
|
|
1333
|
+
"https://xueqiu.com/",
|
|
1334
|
+
headers={"User-Agent": "Mozilla/5.0"},
|
|
1335
|
+
timeout=10,
|
|
1336
|
+
allow_redirects=True,
|
|
1337
|
+
).raise_for_status()
|
|
1338
|
+
response = session.get(
|
|
1339
|
+
"https://stock.xueqiu.com/v5/stock/quote.json",
|
|
1340
|
+
params={"symbol": symbol, "extend": "detail"},
|
|
1341
|
+
headers={"User-Agent": "Mozilla/5.0", "Referer": "https://xueqiu.com/"},
|
|
1342
|
+
timeout=10,
|
|
1343
|
+
)
|
|
1344
|
+
response.raise_for_status()
|
|
1345
|
+
payload = response.json()
|
|
1346
|
+
if payload.get("error_code") not in (None, 0):
|
|
1347
|
+
raise ValueError("xueqiu portfolio metadata unavailable")
|
|
1348
|
+
quote = (payload.get("data") or {}).get("quote") or {}
|
|
1349
|
+
if quote.get("symbol") != symbol:
|
|
1350
|
+
raise ValueError("xueqiu returned different portfolio metadata")
|
|
1351
|
+
result.update(
|
|
1352
|
+
name=quote.get("name"),
|
|
1353
|
+
quote_type=quote.get("type"),
|
|
1354
|
+
exchange=quote.get("exchange"),
|
|
1355
|
+
source="xueqiu",
|
|
1356
|
+
data_as_of=_unix_quote_time(quote.get("timestamp")),
|
|
1357
|
+
)
|
|
1358
|
+
except Exception:
|
|
1359
|
+
result["warnings"] = ["xueqiu portfolio name unavailable; type guidance remains available"]
|
|
1360
|
+
if disclosure_name and result["name"] in (None, disclosure_name):
|
|
1361
|
+
try:
|
|
1362
|
+
from tiaa_disclosures import public_disclosures
|
|
1363
|
+
|
|
1364
|
+
disclosure = public_disclosures(symbol, disclosure_name)
|
|
1365
|
+
result["official_disclosures"] = disclosure["items"]
|
|
1366
|
+
result["disclosures_as_of"] = disclosure["as_of"]
|
|
1367
|
+
if disclosure["items"]:
|
|
1368
|
+
result["disclosure_status"] = "partial_public"
|
|
1369
|
+
except Exception:
|
|
1370
|
+
result.setdefault("warnings", []).append(
|
|
1371
|
+
"official public disclosures unavailable; type guidance remains available"
|
|
1372
|
+
)
|
|
1373
|
+
elif disclosure_name and result["name"]:
|
|
1374
|
+
result["disclosure_status"] = "unsupported_provider"
|
|
1375
|
+
result.setdefault("warnings", []).append("portfolio name does not match the registered disclosure source")
|
|
1376
|
+
else:
|
|
1377
|
+
from tiaa_disclosures import search_public
|
|
1378
|
+
|
|
1379
|
+
result["public_search_results"] = search_public(symbol, result["name"])
|
|
1380
|
+
if result["public_search_results"]:
|
|
1381
|
+
result["disclosure_provider"] = "web_search"
|
|
1382
|
+
result["disclosure_status"] = "search_partial"
|
|
1383
|
+
try:
|
|
1384
|
+
from tiaa_disclosures import advisor_metadata
|
|
1385
|
+
|
|
1386
|
+
result.update(advisor_metadata(symbol))
|
|
1387
|
+
except Exception:
|
|
1388
|
+
result.setdefault("warnings", []).append("advisor organization metadata unavailable")
|
|
1389
|
+
return result
|
|
1390
|
+
|
|
1391
|
+
|
|
1289
1392
|
def cmd_quote(args):
|
|
1393
|
+
if re.fullmatch(r"TIAA\d{6}", args.symbol, re.IGNORECASE):
|
|
1394
|
+
return _quote_tiaa(args.symbol.upper())
|
|
1290
1395
|
market = detect_market(args.symbol)
|
|
1291
1396
|
try:
|
|
1292
1397
|
result = quote_a(args.symbol, evidence=True) if market == "A" else quote_yf(args.symbol, evidence=True)
|
|
1293
1398
|
except Exception as e:
|
|
1294
|
-
|
|
1399
|
+
result = {"error": str(e)}
|
|
1400
|
+
# Exchange quotes cannot see open-end funds. Stock-only prefixes (SH 60/68, ChiNext 30, BSE) keep the bare error.
|
|
1401
|
+
if (
|
|
1402
|
+
isinstance(result, dict)
|
|
1403
|
+
and "error" in result
|
|
1404
|
+
and re.fullmatch(r"\d{6}", args.symbol)
|
|
1405
|
+
and not re.match(r"(?:60|68|30|43|83|87|92)", args.symbol)
|
|
1406
|
+
):
|
|
1407
|
+
result["hint"] = (
|
|
1408
|
+
f"{args.symbol} 若为场外公募基金,请改用 get_fund_quote(stock_data.py fund_quote {args.symbol})"
|
|
1409
|
+
)
|
|
1410
|
+
return result
|
|
1295
1411
|
if isinstance(result, dict) and "error" not in result:
|
|
1296
1412
|
marker = _quote_stale_marker(market)
|
|
1297
1413
|
if marker:
|
|
@@ -1322,6 +1438,109 @@ def cmd_quote(args):
|
|
|
1322
1438
|
return result
|
|
1323
1439
|
|
|
1324
1440
|
|
|
1441
|
+
# --------------- open-end fund quote ---------------
|
|
1442
|
+
|
|
1443
|
+
|
|
1444
|
+
def _fund_js_value(text: str, name: str):
|
|
1445
|
+
match = re.search(rf"\b{re.escape(name)}\s*=\s*", text)
|
|
1446
|
+
if not match:
|
|
1447
|
+
raise ValueError(f"eastmoney fund data missing {name}")
|
|
1448
|
+
try:
|
|
1449
|
+
return json.JSONDecoder().raw_decode(text[match.end() :])[0]
|
|
1450
|
+
except json.JSONDecodeError as exc:
|
|
1451
|
+
raise ValueError(f"eastmoney fund data has invalid {name}") from exc
|
|
1452
|
+
|
|
1453
|
+
|
|
1454
|
+
def _fund_nav_date(stamp) -> str:
|
|
1455
|
+
return datetime.fromtimestamp(stamp / 1000, timezone.utc).astimezone(ZoneInfo("Asia/Shanghai")).date().isoformat()
|
|
1456
|
+
|
|
1457
|
+
|
|
1458
|
+
def _fund_estimate(symbol: str) -> dict:
|
|
1459
|
+
import akshare as ak
|
|
1460
|
+
|
|
1461
|
+
with socket_timeout(25):
|
|
1462
|
+
df = ak.fund_value_estimation_em()
|
|
1463
|
+
if df is None or df.empty or "基金代码" not in df.columns:
|
|
1464
|
+
return {}
|
|
1465
|
+
rows = df[df["基金代码"] == symbol]
|
|
1466
|
+
value_col = next((c for c in df.columns if c.endswith("-估算数据-估算值")), None)
|
|
1467
|
+
change_col = next((c for c in df.columns if c.endswith("-估算数据-估算增长率")), None)
|
|
1468
|
+
if rows.empty or not value_col:
|
|
1469
|
+
return {}
|
|
1470
|
+
row = rows.iloc[0]
|
|
1471
|
+
estimated_nav = _parse_float(row.get(value_col))
|
|
1472
|
+
if estimated_nav is None:
|
|
1473
|
+
return {}
|
|
1474
|
+
return {
|
|
1475
|
+
"estimated_nav": estimated_nav,
|
|
1476
|
+
"estimated_change_pct": _parse_float(str(row.get(change_col)).rstrip("%")) if change_col else None,
|
|
1477
|
+
"estimated_at": value_col.split("-估算数据-", 1)[0],
|
|
1478
|
+
"estimate_status": "available",
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
|
|
1482
|
+
def cmd_fund_quote(args):
|
|
1483
|
+
symbol = args.symbol
|
|
1484
|
+
if not re.fullmatch(r"\d{6}", symbol):
|
|
1485
|
+
return {"error": "open-end fund symbol must be exactly 6 digits"}
|
|
1486
|
+
try:
|
|
1487
|
+
import requests
|
|
1488
|
+
|
|
1489
|
+
response = requests.get(f"https://fund.eastmoney.com/pingzhongdata/{symbol}.js", timeout=15)
|
|
1490
|
+
response.raise_for_status()
|
|
1491
|
+
text = response.text
|
|
1492
|
+
if _fund_js_value(text, "fS_code") != symbol:
|
|
1493
|
+
raise ValueError(f"eastmoney returned a different fund for {symbol}")
|
|
1494
|
+
name = _fund_js_value(text, "fS_name")
|
|
1495
|
+
nav_rows = _fund_js_value(text, "Data_netWorthTrend")
|
|
1496
|
+
accumulated_rows = _fund_js_value(text, "Data_ACWorthTrend")
|
|
1497
|
+
if not nav_rows:
|
|
1498
|
+
raise ValueError(f"eastmoney returned no NAV for {symbol}")
|
|
1499
|
+
latest = nav_rows[-1]
|
|
1500
|
+
nav_date = _fund_nav_date(latest["x"])
|
|
1501
|
+
accumulated_by_date = {_fund_nav_date(row[0]): row[1] for row in accumulated_rows}
|
|
1502
|
+
today = datetime.now(ZoneInfo("Asia/Shanghai")).date().isoformat()
|
|
1503
|
+
estimate = {
|
|
1504
|
+
"estimated_nav": None,
|
|
1505
|
+
"estimated_change_pct": None,
|
|
1506
|
+
"estimated_at": None,
|
|
1507
|
+
"estimate_status": "unavailable",
|
|
1508
|
+
}
|
|
1509
|
+
warnings = []
|
|
1510
|
+
try:
|
|
1511
|
+
estimate.update(_fund_estimate(symbol))
|
|
1512
|
+
except Exception:
|
|
1513
|
+
warnings.append("estimated NAV provider request failed; published NAV remains available")
|
|
1514
|
+
result = _clean_row(
|
|
1515
|
+
{
|
|
1516
|
+
"symbol": symbol,
|
|
1517
|
+
"name": name,
|
|
1518
|
+
"asset_type": "open_end_fund",
|
|
1519
|
+
"nav": latest.get("y"),
|
|
1520
|
+
"accumulated_nav": accumulated_by_date.get(nav_date),
|
|
1521
|
+
"change_pct": latest.get("equityReturn"),
|
|
1522
|
+
"nav_date": nav_date,
|
|
1523
|
+
"data_as_of": nav_date,
|
|
1524
|
+
"as_of": nav_date,
|
|
1525
|
+
"as_of_basis": "provider",
|
|
1526
|
+
"stale": nav_date < today,
|
|
1527
|
+
"source": "eastmoney",
|
|
1528
|
+
"source_url": f"https://fund.eastmoney.com/{symbol}.html",
|
|
1529
|
+
"fetched_at": datetime.now(timezone.utc).isoformat(),
|
|
1530
|
+
"note": (
|
|
1531
|
+
"nav is published end-of-day NAV, not a real-time price; QDII disclosure may lag "
|
|
1532
|
+
"1-2 trading days; estimated_nav is separate and may be unavailable"
|
|
1533
|
+
),
|
|
1534
|
+
**estimate,
|
|
1535
|
+
}
|
|
1536
|
+
)
|
|
1537
|
+
if warnings:
|
|
1538
|
+
result["warnings"] = warnings
|
|
1539
|
+
return result
|
|
1540
|
+
except Exception as e:
|
|
1541
|
+
return {"error": str(e)}
|
|
1542
|
+
|
|
1543
|
+
|
|
1325
1544
|
# --------------- capital_flow ---------------
|
|
1326
1545
|
|
|
1327
1546
|
|
|
@@ -1872,7 +2091,8 @@ def snapshot_hk() -> list:
|
|
|
1872
2091
|
try:
|
|
1873
2092
|
import akshare as ak
|
|
1874
2093
|
|
|
1875
|
-
|
|
2094
|
+
with socket_timeout(25):
|
|
2095
|
+
df = _akshare_retry(ak.stock_hk_spot_em)
|
|
1876
2096
|
col_map = {
|
|
1877
2097
|
"代码": "symbol",
|
|
1878
2098
|
"名称": "name",
|
|
@@ -1900,7 +2120,8 @@ def snapshot_us() -> list:
|
|
|
1900
2120
|
try:
|
|
1901
2121
|
import akshare as ak
|
|
1902
2122
|
|
|
1903
|
-
|
|
2123
|
+
with socket_timeout(25):
|
|
2124
|
+
df = _akshare_retry(ak.stock_us_spot_em)
|
|
1904
2125
|
col_map = {
|
|
1905
2126
|
"代码": "symbol",
|
|
1906
2127
|
"名称": "name",
|
|
@@ -1996,33 +2217,39 @@ def cmd_market_indices(args):
|
|
|
1996
2217
|
)
|
|
1997
2218
|
return results
|
|
1998
2219
|
elif region in _REGION_INDEX_SYMBOLS:
|
|
2220
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
2221
|
+
|
|
1999
2222
|
import yfinance as yf
|
|
2000
2223
|
|
|
2001
2224
|
symbols = _REGION_INDEX_SYMBOLS[region]
|
|
2002
|
-
|
|
2003
|
-
|
|
2225
|
+
|
|
2226
|
+
def fetch_index(item):
|
|
2227
|
+
sym, name = item
|
|
2004
2228
|
try:
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2229
|
+
info = yf.Ticker(sym).info
|
|
2230
|
+
return _clean_row(
|
|
2231
|
+
{
|
|
2232
|
+
"code": sym,
|
|
2233
|
+
"name": name,
|
|
2234
|
+
"price": info.get("regularMarketPrice"),
|
|
2235
|
+
"change": info.get("regularMarketChange"),
|
|
2236
|
+
"change_pct": info.get("regularMarketChangePercent"),
|
|
2237
|
+
"volume": info.get("regularMarketVolume"),
|
|
2238
|
+
"high": info.get("regularMarketDayHigh"),
|
|
2239
|
+
"low": info.get("regularMarketDayLow"),
|
|
2240
|
+
"open": info.get("regularMarketOpen"),
|
|
2241
|
+
"prev_close": info.get("regularMarketPreviousClose"),
|
|
2242
|
+
"source": "yfinance",
|
|
2243
|
+
"data_as_of": _unix_quote_time(info.get("regularMarketTime")),
|
|
2244
|
+
"fetched_at": datetime.now(timezone.utc).isoformat(),
|
|
2245
|
+
}
|
|
2022
2246
|
)
|
|
2023
2247
|
except Exception:
|
|
2024
|
-
|
|
2025
|
-
|
|
2248
|
+
return {"code": sym, "name": name, "note": "data unavailable"}
|
|
2249
|
+
|
|
2250
|
+
# Preserve provider order while overlapping independent quote requests.
|
|
2251
|
+
with ThreadPoolExecutor(max_workers=min(4, len(symbols))) as pool:
|
|
2252
|
+
return list(pool.map(fetch_index, symbols.items()))
|
|
2026
2253
|
return {"error": f"Unknown region: {region}"}
|
|
2027
2254
|
except Exception as e:
|
|
2028
2255
|
return {"error": str(e)}
|
|
@@ -3382,6 +3609,9 @@ def main():
|
|
|
3382
3609
|
p_quote = sub.add_parser("quote")
|
|
3383
3610
|
p_quote.add_argument("symbol")
|
|
3384
3611
|
|
|
3612
|
+
p_fund_quote = sub.add_parser("fund_quote")
|
|
3613
|
+
p_fund_quote.add_argument("symbol")
|
|
3614
|
+
|
|
3385
3615
|
p_cf = sub.add_parser("capital_flow")
|
|
3386
3616
|
p_cf.add_argument("symbol", nargs="?", default="")
|
|
3387
3617
|
p_cf.add_argument("--mode", default="detail", choices=["detail", "summary", "sector_flow"])
|
|
@@ -3451,6 +3681,7 @@ def main():
|
|
|
3451
3681
|
dispatch = {
|
|
3452
3682
|
"kline": cmd_kline,
|
|
3453
3683
|
"quote": cmd_quote,
|
|
3684
|
+
"fund_quote": cmd_fund_quote,
|
|
3454
3685
|
"capital_flow": cmd_capital_flow,
|
|
3455
3686
|
"news": cmd_news,
|
|
3456
3687
|
"financials": cmd_financials,
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"""Public, login-free TIAA disclosures from the official YouZhiYouXing site."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
import sys
|
|
5
|
+
import threading
|
|
6
|
+
import time
|
|
7
|
+
import xml.etree.ElementTree as ET
|
|
8
|
+
from concurrent.futures import TimeoutError as FutureTimeoutError
|
|
9
|
+
from datetime import datetime, timezone
|
|
10
|
+
from html import unescape
|
|
11
|
+
from html.parser import HTMLParser
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from urllib.parse import urlparse
|
|
14
|
+
|
|
15
|
+
import requests
|
|
16
|
+
|
|
17
|
+
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
18
|
+
from _cache import cached # noqa: E402
|
|
19
|
+
|
|
20
|
+
_SITEMAP = "https://youzhiyouxing.cn/sitemap.xml"
|
|
21
|
+
_ADVISOR_URL = "https://www.csisc.cn/zbscbzw/c100264/list_jjbm.shtml"
|
|
22
|
+
_ARTICLE_URL = re.compile(r"https://youzhiyouxing\.cn/(?:n/)?lite-posts/(\d+)(?:[/?#].*)?")
|
|
23
|
+
_DISCLOSURE_TERMS = ("示例账户", "成分基金", "配置", "业绩", "收益", "调仓", "持仓")
|
|
24
|
+
_MAX_ARTICLES = 40
|
|
25
|
+
_DISCLOSURE_DEADLINE = 30
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class _DisclosureTimeout(Exception):
|
|
29
|
+
"""A bounded disclosure attempt failed and must not be cached as an empty result."""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _until(deadline, fn, *args):
|
|
33
|
+
"""Run a blocking provider call in a daemon thread and stop waiting at the deadline.
|
|
34
|
+
|
|
35
|
+
A provider that ignores its own timeout can then neither stretch the request nor keep
|
|
36
|
+
the CLI process alive after it has answered.
|
|
37
|
+
"""
|
|
38
|
+
box = {}
|
|
39
|
+
|
|
40
|
+
def run():
|
|
41
|
+
try:
|
|
42
|
+
box["value"] = fn(*args)
|
|
43
|
+
except BaseException as exc: # re-raised in the caller's thread
|
|
44
|
+
box["error"] = exc
|
|
45
|
+
|
|
46
|
+
worker = threading.Thread(target=run, daemon=True)
|
|
47
|
+
worker.start()
|
|
48
|
+
worker.join(max(0.0, deadline - time.monotonic()))
|
|
49
|
+
if worker.is_alive():
|
|
50
|
+
raise _DisclosureTimeout
|
|
51
|
+
if "error" in box:
|
|
52
|
+
raise box["error"]
|
|
53
|
+
return box.get("value")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class _ArticleParser(HTMLParser):
|
|
57
|
+
def __init__(self):
|
|
58
|
+
super().__init__(convert_charrefs=True)
|
|
59
|
+
self.meta = {}
|
|
60
|
+
self.parts = []
|
|
61
|
+
self.ignored = 0
|
|
62
|
+
|
|
63
|
+
def handle_starttag(self, tag, attrs):
|
|
64
|
+
if tag in ("script", "style"):
|
|
65
|
+
self.ignored += 1
|
|
66
|
+
if tag == "meta":
|
|
67
|
+
values = dict(attrs)
|
|
68
|
+
if values.get("property") in ("og:title", "og:description"):
|
|
69
|
+
self.meta[values["property"]] = values.get("content", "")
|
|
70
|
+
if tag in ("p", "div", "h1", "h2", "h3", "li", "br"):
|
|
71
|
+
self.parts.append("\n")
|
|
72
|
+
|
|
73
|
+
def handle_endtag(self, tag):
|
|
74
|
+
if tag in ("script", "style") and self.ignored:
|
|
75
|
+
self.ignored -= 1
|
|
76
|
+
if tag in ("p", "div", "h1", "h2", "h3", "li"):
|
|
77
|
+
self.parts.append("\n")
|
|
78
|
+
|
|
79
|
+
def handle_data(self, data):
|
|
80
|
+
if not self.ignored:
|
|
81
|
+
self.parts.append(data)
|
|
82
|
+
|
|
83
|
+
def text(self):
|
|
84
|
+
lines = []
|
|
85
|
+
for line in "".join(self.parts).splitlines():
|
|
86
|
+
clean = re.sub(r"\s+", " ", line).strip()
|
|
87
|
+
if clean:
|
|
88
|
+
lines.append(clean)
|
|
89
|
+
title = self.meta.get("og:title")
|
|
90
|
+
title_lines = [index for index, line in enumerate(lines) if line == title]
|
|
91
|
+
if title_lines:
|
|
92
|
+
lines = lines[title_lines[-1] + 1 :]
|
|
93
|
+
comments = next((index for index, line in enumerate(lines) if line.endswith("条回应")), len(lines))
|
|
94
|
+
return "\n".join(lines[:comments])
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _sitemap_urls():
|
|
98
|
+
response = requests.get(_SITEMAP, headers={"User-Agent": "Mozilla/5.0"}, timeout=(2, 8))
|
|
99
|
+
response.raise_for_status()
|
|
100
|
+
root = ET.fromstring(response.content)
|
|
101
|
+
namespace = {"s": "http://www.sitemaps.org/schemas/sitemap/0.9"}
|
|
102
|
+
entries = []
|
|
103
|
+
for node in root.findall("s:url", namespace):
|
|
104
|
+
location = node.findtext("s:loc", default="", namespaces=namespace)
|
|
105
|
+
match = _ARTICLE_URL.fullmatch(location)
|
|
106
|
+
if match:
|
|
107
|
+
entries.append((node.findtext("s:lastmod", default="", namespaces=namespace), match.group(1)))
|
|
108
|
+
return [
|
|
109
|
+
f"https://youzhiyouxing.cn/n/lite-posts/{article_id}" for _, article_id in sorted(entries, reverse=True)[:40]
|
|
110
|
+
]
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _search_urls(portfolio_name, budget_seconds=45):
|
|
114
|
+
try:
|
|
115
|
+
from search_intel import search_news
|
|
116
|
+
|
|
117
|
+
query = f"site:youzhiyouxing.cn/n/lite-posts/ {portfolio_name} 示例账户 成分基金"
|
|
118
|
+
result = search_news(query, max_results=10, budget_seconds=budget_seconds)
|
|
119
|
+
if any("time budget exhausted" in str(error).lower() for error in result.get("engines_errors", {}).values()):
|
|
120
|
+
raise _DisclosureTimeout
|
|
121
|
+
urls = []
|
|
122
|
+
for row in result.get("results", []):
|
|
123
|
+
match = _ARTICLE_URL.fullmatch(row.get("url", ""))
|
|
124
|
+
if match:
|
|
125
|
+
urls.append(f"https://youzhiyouxing.cn/n/lite-posts/{match.group(1)}")
|
|
126
|
+
return urls
|
|
127
|
+
except Exception:
|
|
128
|
+
return []
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _article(url, portfolio_name):
|
|
132
|
+
try:
|
|
133
|
+
response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"}, timeout=(1, 3))
|
|
134
|
+
response.raise_for_status()
|
|
135
|
+
parser = _ArticleParser()
|
|
136
|
+
parser.feed(response.text)
|
|
137
|
+
text = parser.text()
|
|
138
|
+
if portfolio_name not in text or not any(term in text for term in _DISCLOSURE_TERMS):
|
|
139
|
+
return None
|
|
140
|
+
date_match = re.search(r"(20\d{2})\s*年\s*(\d{1,2})\s*月\s*(\d{1,2})\s*日", response.text)
|
|
141
|
+
published_at = None
|
|
142
|
+
if date_match:
|
|
143
|
+
published_at = "-".join([date_match.group(1), *(part.zfill(2) for part in date_match.groups()[1:])])
|
|
144
|
+
return {
|
|
145
|
+
"title": parser.meta.get("og:title", ""),
|
|
146
|
+
"url": url,
|
|
147
|
+
"published_at": published_at,
|
|
148
|
+
"summary": parser.meta.get("og:description", ""),
|
|
149
|
+
"text": text[:3000],
|
|
150
|
+
"source": "youzhiyouxing",
|
|
151
|
+
"partial": True,
|
|
152
|
+
}
|
|
153
|
+
except Exception:
|
|
154
|
+
return None
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def advisor_metadata(symbol):
|
|
158
|
+
advisor_code = f"A{symbol[4:7]}"
|
|
159
|
+
|
|
160
|
+
def fetch():
|
|
161
|
+
response = requests.get(_ADVISOR_URL, headers={"User-Agent": "Mozilla/5.0"}, timeout=15)
|
|
162
|
+
response.raise_for_status()
|
|
163
|
+
text = response.content.decode("utf-8-sig")
|
|
164
|
+
rows = re.findall(
|
|
165
|
+
r"<tr\b[^>]*>\s*<td\b[^>]*>\s*\d+\s*</td>\s*"
|
|
166
|
+
r"<td\b[^>]*>\s*(.*?)\s*</td>\s*<td\b[^>]*>\s*(A\d{3})\s*</td>",
|
|
167
|
+
text,
|
|
168
|
+
re.S,
|
|
169
|
+
)
|
|
170
|
+
return {
|
|
171
|
+
"organizations": {code: unescape(re.sub(r"<[^>]+>", "", name)).strip() for name, code in rows},
|
|
172
|
+
"retrieved_at": datetime.now(timezone.utc).isoformat(),
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
data = cached("csisc-fund-advisor-organizations", 24 * 60 * 60, fetch)
|
|
176
|
+
return {
|
|
177
|
+
"advisor_code": advisor_code,
|
|
178
|
+
"advisor_org": data["organizations"].get(advisor_code),
|
|
179
|
+
"advisor_mapping_basis": "tiaa_prefix_best_effort",
|
|
180
|
+
"advisor_source_url": _ADVISOR_URL,
|
|
181
|
+
"advisor_retrieved_at": data["retrieved_at"],
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def public_disclosures(symbol, portfolio_name):
|
|
186
|
+
def fetch():
|
|
187
|
+
deadline = time.monotonic() + _DISCLOSURE_DEADLINE
|
|
188
|
+
urls = _sitemap_urls()
|
|
189
|
+
search_results = []
|
|
190
|
+
|
|
191
|
+
timeout_state = {"search": False, "articles": False}
|
|
192
|
+
if time.monotonic() < deadline:
|
|
193
|
+
try:
|
|
194
|
+
search_results = _until(
|
|
195
|
+
deadline, _search_urls, portfolio_name, max(1, int(deadline - time.monotonic()))
|
|
196
|
+
)
|
|
197
|
+
# concurrent.futures.TimeoutError only aliases the builtin from Python 3.11.
|
|
198
|
+
except (TimeoutError, FutureTimeoutError, _DisclosureTimeout):
|
|
199
|
+
timeout_state["search"] = True
|
|
200
|
+
if time.monotonic() >= deadline:
|
|
201
|
+
timeout_state["search"] = True
|
|
202
|
+
else:
|
|
203
|
+
timeout_state["search"] = True
|
|
204
|
+
urls = list(dict.fromkeys([*search_results, *urls]))[:_MAX_ARTICLES]
|
|
205
|
+
items = []
|
|
206
|
+
for url in urls:
|
|
207
|
+
if time.monotonic() >= deadline:
|
|
208
|
+
timeout_state["articles"] = True
|
|
209
|
+
break
|
|
210
|
+
try:
|
|
211
|
+
item = _until(deadline, _article, url, portfolio_name)
|
|
212
|
+
if item:
|
|
213
|
+
items.append(item)
|
|
214
|
+
except _DisclosureTimeout:
|
|
215
|
+
timeout_state["articles"] = True
|
|
216
|
+
break
|
|
217
|
+
except (TimeoutError, FutureTimeoutError):
|
|
218
|
+
timeout_state["articles"] = True
|
|
219
|
+
if time.monotonic() >= deadline and urls:
|
|
220
|
+
timeout_state["articles"] = True
|
|
221
|
+
items.sort(key=lambda item: item["published_at"] or "", reverse=True)
|
|
222
|
+
items = items[:3]
|
|
223
|
+
result = {
|
|
224
|
+
"items": items,
|
|
225
|
+
"as_of": max((item["published_at"] for item in items if item["published_at"]), default=None),
|
|
226
|
+
}
|
|
227
|
+
if any(timeout_state.values()):
|
|
228
|
+
if not items:
|
|
229
|
+
raise _DisclosureTimeout
|
|
230
|
+
result["error"] = "disclosure_timeout"
|
|
231
|
+
return result
|
|
232
|
+
|
|
233
|
+
return cached(f"tiaa-public-disclosures:{symbol}", 6 * 60 * 60, fetch)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def search_public(symbol, portfolio_name):
|
|
237
|
+
def fetch():
|
|
238
|
+
try:
|
|
239
|
+
from search_intel import search_news
|
|
240
|
+
|
|
241
|
+
label = portfolio_name or symbol
|
|
242
|
+
result = search_news(f'"{symbol}" "{label}" 示例账户 成分基金 业绩', max_results=3)
|
|
243
|
+
rows = []
|
|
244
|
+
for row in result.get("results", []):
|
|
245
|
+
parsed = urlparse(row.get("url", ""))
|
|
246
|
+
if parsed.scheme not in ("http", "https") or not parsed.netloc:
|
|
247
|
+
continue
|
|
248
|
+
rows.append(
|
|
249
|
+
{
|
|
250
|
+
"title": row.get("title", ""),
|
|
251
|
+
"url": row["url"],
|
|
252
|
+
"snippet": row.get("snippet", ""),
|
|
253
|
+
"source": row.get("source", ""),
|
|
254
|
+
"publisher": parsed.netloc,
|
|
255
|
+
"verification": "unverified_search_result",
|
|
256
|
+
"partial": True,
|
|
257
|
+
}
|
|
258
|
+
)
|
|
259
|
+
return rows
|
|
260
|
+
except Exception:
|
|
261
|
+
return []
|
|
262
|
+
|
|
263
|
+
return cached(f"tiaa-public-search:{symbol}", 6 * 60 * 60, fetch)
|