ashareapi-pi 0.0.0-stage → 0.2.5
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/LICENSE +21 -0
- package/README.en.md +130 -0
- package/README.md +129 -2
- package/package.json +30 -4
- package/skills/ashareapi/SKILL.md +244 -0
- package/skills/ashareapi/references/endpoints.md +127 -0
- package/skills/ashareapi/references/errors.md +113 -0
- package/skills/ashareapi/references/fields.md +96 -0
- package/skills/ashareapi/references/sdk.md +204 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ashareapi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.en.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<a href="https://ashareapi.com/en/"><img src="https://ashareapi.com/icon-512.png" width="88" height="88" alt="ashareapi"></a>
|
|
4
|
+
|
|
5
|
+
# ashareapi — Official Pi Package for the A-Share Data API
|
|
6
|
+
|
|
7
|
+
Let **Pi** ([pi.dev](https://pi.dev)) query China A-share data directly: quotes / candles / **5-level order book** / financials / money flow / dragon-tiger list / sectors / convertible bonds / factor screening / macro — **32 endpoints, and 5 of them need no key**.
|
|
8
|
+
|
|
9
|
+
One command to install — afterwards Pi knows **which endpoint to call, how to fill the parameters, what units the fields use, and what to do on errors**.
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+

|
|
13
|
+
[](https://www.npmjs.com/package/ashareapi-pi)
|
|
14
|
+

|
|
15
|
+
|
|
16
|
+
**Runs on Pi (pi.dev)**:
|
|
17
|
+
|
|
18
|
+

|
|
19
|
+
|
|
20
|
+
[中文](README.md) · **English**
|
|
21
|
+
|
|
22
|
+
[Website](https://ashareapi.com/en/) · [Docs](https://ashareapi.com/en/docs/) · [Endpoint list](https://ashareapi.com/en/endpoints/) · [MCP](https://ashareapi.com/en/mcp/) · [Agent Skill](https://ashareapi.com/en/skill/)
|
|
23
|
+
|
|
24
|
+
[Source](https://github.com/ashareapi/ashareapi-skill) · [Issues](https://github.com/ashareapi/ashareapi-skill/issues) · [Changelog](https://ashareapi.com/en/changelog/)
|
|
25
|
+
|
|
26
|
+
</div>
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## What is this
|
|
31
|
+
|
|
32
|
+
An **A-share data Pi package**: once installed, asking Pi "what is 600667 trading at?" makes it call the API and answer with **real market data** instead of guessing.
|
|
33
|
+
|
|
34
|
+
Inside is an **Agent Skill** (an open standard) — Pi and Claude Code / Codex use **the same skill specification**, so this manual is portable across them. It covers the parameters and field semantics of 32 endpoints, unit conventions (`volume` is in lots / `amount` in yuan / ratios are percentages), date semantics, and common errors plus rate-limit handling.
|
|
35
|
+
|
|
36
|
+
Under the hood it talks to the REST API at [ashareapi.com](https://ashareapi.com/en/) (`https://api.ashareapi.com/v1/...`) — Pi can just `curl` it; the official SDKs or MCP are equally fine.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Why use it
|
|
41
|
+
|
|
42
|
+
- **One command to install**: `pi install npm:ashareapi-pi`, then `/reload` — no JSON to hand-edit, no script to run
|
|
43
|
+
- **Free endpoints are genuinely key-free**: `/v1/quote` `/v1/kline` `/v1/hot` `/v1/changedist` `/v1/market-overview` work right after installation
|
|
44
|
+
- **Automatic source failover**: 70 data sources back each other up on the backend; if one has a problem it switches to the next, and **your quota is not charged**
|
|
45
|
+
- **Fixed conventions, no post-processing**: K-lines are always **forward-adjusted (qfq)** (there is no `adjust` parameter), so there is no double-adjustment
|
|
46
|
+
- **No data ≠ failure**: "the market genuinely has no data" (e.g. a suspended stock) is returned separately from "the fetch failed" — the agent will not mistake a suspension for an outage
|
|
47
|
+
- **Written for AI**: it states *when to use which endpoint / how to fill the parameters / what units the fields use / what to do on errors*, so the agent does not call it wrong
|
|
48
|
+
- **Nothing for you to maintain**: the content is generated by a script from the Agent Skill (single source of truth), and a Pi upgrade needs no change here
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Install
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pi install npm:ashareapi-pi
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Then `/reload` (or restart Pi). To try it once without touching your config:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pi -e npm:ashareapi-pi
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Then just ask:
|
|
65
|
+
|
|
66
|
+
| You ask | Endpoint used |
|
|
67
|
+
|---|---|
|
|
68
|
+
| What is 600667 trading at? | `/v1/quote` |
|
|
69
|
+
| Pull the last 60 daily candles for 600667 | `/v1/kline` |
|
|
70
|
+
| How is the market doing today? | `/v1/market-overview` |
|
|
71
|
+
| What are people watching right now? | `/v1/hot` |
|
|
72
|
+
| What is today's advance/decline distribution? | `/v1/changedist` |
|
|
73
|
+
|
|
74
|
+
Uninstall:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pi remove npm:ashareapi-pi
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## API key setup
|
|
83
|
+
|
|
84
|
+
The 5 endpoints above work with **no configuration at all**.
|
|
85
|
+
|
|
86
|
+
To use the rest (order book / financials / money flow / dragon-tiger list / sectors / screening / macro …) you need a key: [get one here](https://ashareapi.com/en/pricing/) (from ¥9.9).
|
|
87
|
+
|
|
88
|
+
Put the key in the `ASHAREAPI_KEY` environment variable, or just tell Pi — the manual spells out how to send it:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
curl -H "Authorization: Bearer ct-your-key" "https://api.ashareapi.com/v1/quote?code=sh600667"
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## What you can query
|
|
97
|
+
|
|
98
|
+
**Free (no key)**
|
|
99
|
+
|
|
100
|
+
Live quotes · candles · attention list · market breadth · market overview
|
|
101
|
+
|
|
102
|
+
**Requires a key**
|
|
103
|
+
|
|
104
|
+
Order book · full-field profile · financial statements · money flow · technical indicators · shareholders · events · dragon-tiger list · factor screening · sectors · sector valuation · macro · convertible bonds · ETFs · IPOs · dividends · research digest · symbol search · usage
|
|
105
|
+
|
|
106
|
+
Field meanings, parameters, and response samples are in the [docs](https://ashareapi.com/en/docs/).
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## Related
|
|
111
|
+
|
|
112
|
+
**Official SDKs**: Python `pip install ashareapi` · Node.js / TypeScript `npm install ashareapi`
|
|
113
|
+
|
|
114
|
+
**MCP server**: `https://api.ashareapi.com/mcp` ([configuration guide](https://ashareapi.com/en/mcp/)) — Pi has **MCP built in**, so a single URL is enough; Claude Code / Codex / Cursor and 15+ other clients work the same way
|
|
115
|
+
|
|
116
|
+
**Agent Skill** (an endpoint manual for AI agents): <https://ashareapi.com/en/skill/>
|
|
117
|
+
|
|
118
|
+
**This package's content** is generated by a script from the Agent Skill (single source of truth: <https://github.com/ashareapi/ashareapi-skill>), never maintained by hand.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Version
|
|
123
|
+
|
|
124
|
+
See [npm](https://www.npmjs.com/package/ashareapi-pi) for the latest version. The content comes from the Agent Skill and **the version tracks the skill**; see the [changelog](https://ashareapi.com/en/changelog/).
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT
|
package/README.md
CHANGED
|
@@ -1,3 +1,130 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<a href="https://ashareapi.com"><img src="https://ashareapi.com/icon-512.png" width="88" height="88" alt="ashareapi"></a>
|
|
4
|
+
|
|
5
|
+
# ashareapi — A股数据 API 官方 Pi 包
|
|
6
|
+
|
|
7
|
+
让 **Pi**([pi.dev](https://pi.dev))直接查 A 股数据:行情 / K线 / **五档盘口** / 财务 / 资金 / 龙虎榜 / 板块 / 可转债 / 因子选股 / 宏观 —— **32 个端点,5 个免费无需 Key**。
|
|
8
|
+
|
|
9
|
+
一条命令装好 —— 装完 Pi 就知道**该调哪个端点、参数怎么填、字段什么单位、错了怎么处理**。
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+

|
|
13
|
+
[](https://www.npmjs.com/package/ashareapi-pi)
|
|
14
|
+

|
|
15
|
+
|
|
16
|
+
**支持 Pi(pi.dev)**:
|
|
17
|
+
|
|
18
|
+

|
|
19
|
+
|
|
20
|
+
**中文** · [English](README.en.md)
|
|
21
|
+
|
|
22
|
+
[官网](https://ashareapi.com) · [文档](https://ashareapi.com/docs/) · [端点清单](https://ashareapi.com/endpoints/) · [MCP 接入](https://ashareapi.com/mcp) · [Agent Skill](https://ashareapi.com/skill)
|
|
23
|
+
|
|
24
|
+
[GitHub 源码](https://github.com/ashareapi/ashareapi-skill) · [问题反馈 Issues](https://github.com/ashareapi/ashareapi-skill/issues) · [更新日志](https://ashareapi.com/changelog)
|
|
25
|
+
|
|
26
|
+
</div>
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 这是什么
|
|
31
|
+
|
|
32
|
+
一个 **A股数据 Pi 包**:装上之后,你在 Pi 里问「600667 现在多少钱」,它会自己去调接口拿**真实数据**,而不是编一个数给你。
|
|
33
|
+
|
|
34
|
+
里面是一份 **Agent Skill**(开放标准)—— Pi 与 Claude Code / Codex 用**同一套 skill 规范**,所以这份说明书在它们之间通用。内容 = 32 个端点的参数与字段语义、单位换算(`volume` 是手 / `amount` 是元 / 比率为百分数)、数据日期语义、常见错误与限流处理。
|
|
35
|
+
|
|
36
|
+
底层接的是 [ashareapi.com](https://ashareapi.com) 的 REST API(`https://api.ashareapi.com/v1/...`)—— Pi 直接 `curl` 调即可;也可以换成官方 SDK 或 MCP。
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## 为什么用它
|
|
41
|
+
|
|
42
|
+
- **一条命令装好**:`pi install npm:ashareapi-pi`,装完 `/reload` 即用,不用配 JSON、不用写脚本
|
|
43
|
+
- **免费端点真的免 Key**:`/v1/quote` `/v1/kline` `/v1/hot` `/v1/changedist` `/v1/market-overview` 这 5 个装上就能调
|
|
44
|
+
- **多源自动切换**:后端 70 个数据源互为备份,某个源出问题会自动换下一个,且**不扣调用次数**
|
|
45
|
+
- **口径固定,不用二次处理**:K 线固定**前复权**(没有 `adjust` 参数),不会二次复权
|
|
46
|
+
- **无数据 ≠ 失败**:停牌这类「市场真没数据」和「取数失败」分开返回 —— Agent 不会把停牌误判成故障
|
|
47
|
+
- **说明书写给 AI 看**:讲清「什么时候用哪个端点 / 参数怎么填 / 字段什么单位 / 错了怎么处理」,Agent 不会调错
|
|
48
|
+
- **不用你维护**:内容由脚本从 Agent Skill 生成(单一事实源),Pi 升级也不用跟着改
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## 安装
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pi install npm:ashareapi-pi
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
装完 `/reload`(或重开 Pi)即可。想先试一次、不写进配置:
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pi -e npm:ashareapi-pi
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
然后在对话里直接问:
|
|
65
|
+
|
|
66
|
+
| 你可以这样问 | 会用到的端点 |
|
|
67
|
+
|---|---|
|
|
68
|
+
| 600667 现在多少钱? | `/v1/quote` |
|
|
69
|
+
| 帮我拉一下 600667 最近 60 天的日 K | `/v1/kline` |
|
|
70
|
+
| 今天市场什么情况? | `/v1/market-overview` |
|
|
71
|
+
| 现在大家都在看什么股票? | `/v1/hot` |
|
|
72
|
+
| 今天涨跌家数怎么分布? | `/v1/changedist` |
|
|
73
|
+
|
|
74
|
+
卸载:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
pi remove npm:ashareapi-pi
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## 配置 API Key
|
|
83
|
+
|
|
84
|
+
上面 5 个端点**不用配任何东西**就能用。
|
|
85
|
+
|
|
86
|
+
要用其余端点(盘口 / 财务 / 资金 / 龙虎榜 / 板块 / 选股 / 宏观…),需要一个 Key:[获取 Key](https://ashareapi.com/pricing)(体验版 ¥9.9 起)。
|
|
87
|
+
|
|
88
|
+
把 Key 放进环境变量 `ASHAREAPI_KEY`,或者直接告诉 Pi —— 说明书里写明了怎么带上:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
curl -H "Authorization: Bearer ct-你的Key" "https://api.ashareapi.com/v1/quote?code=sh600667"
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## 能查什么
|
|
97
|
+
|
|
98
|
+
**免费(无需 Key)**
|
|
99
|
+
|
|
100
|
+
实时行情 · K线 · 热搜榜 · 涨跌分布 · 市场总览
|
|
101
|
+
|
|
102
|
+
**需 Key**
|
|
103
|
+
|
|
104
|
+
五档盘口 · 全字段画像 · 财务三表 · 资金流 · 技术指标 · 股东 · 事件 · 龙虎榜 · 因子选股 · 板块 · 板块估值 · 宏观 · 可转债 · ETF · 新股 · 分红 · 脱水研报 · 代码搜索 · 用量
|
|
105
|
+
|
|
106
|
+
字段含义、参数与返回样例见[文档](https://ashareapi.com/docs/)。
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## 相关
|
|
111
|
+
|
|
112
|
+
**官方 SDK**:Python `pip install ashareapi` · Node.js / TypeScript `npm install ashareapi`
|
|
113
|
+
|
|
114
|
+
**MCP 服务器**:`https://api.ashareapi.com/mcp`([配置说明](https://ashareapi.com/mcp))—— Pi **内置 MCP**,填一条 URL 就能接;Claude Code / Codex / Cursor 等 19 家客户端同样
|
|
115
|
+
|
|
116
|
+
**Agent Skill**(给 AI 读的接口说明书):<https://ashareapi.com/skill>
|
|
117
|
+
|
|
118
|
+
**本包内容**由脚本从 Agent Skill 生成(单一事实源:<https://github.com/ashareapi/ashareapi-skill>),不手工维护。
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## 版本
|
|
123
|
+
|
|
124
|
+
最新版本见 [npm](https://www.npmjs.com/package/ashareapi-pi)。本包内容来自 Agent Skill,**版本号与 skill 一致**;变更见 [ashareapi.com/changelog](https://ashareapi.com/changelog)。
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ashareapi-pi",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.5",
|
|
4
|
+
"description": "A股数据 API 官方 Pi 包 —— 装上即可查 A 股行情 / K线 / 财务 / 资金 / 龙虎榜(32 个端点说明书,开放标准 Agent Skills)",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi-package",
|
|
7
|
+
"pi",
|
|
8
|
+
"agent-skills",
|
|
9
|
+
"ashareapi",
|
|
10
|
+
"a-share",
|
|
11
|
+
"stock-data"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"homepage": "https://ashareapi.com",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/ashareapi/ashareapi-skill.git"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/ashareapi/ashareapi-skill/issues"
|
|
21
|
+
},
|
|
22
|
+
"pi": {
|
|
23
|
+
"skills": [
|
|
24
|
+
"./skills"
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"skills",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
]
|
|
32
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ashareapi
|
|
3
|
+
description: 用 ashareapi 获取 A 股数据:行情 / K线 / 财务三表 / 资金流 / 龙虎榜 / 板块 / 可转债 / 因子选股 / 宏观 / 产业链。当用户要查 A 股现价、K线走势、财报(营收净利毛利率)、主力资金、龙虎榜(机构/游资)、涨停、板块轮动、可转债条款(强赎/双低)、ETF、新股打新、指数估值、产业链上下游,或要写调用 A 股数据的代码、接 REST API / 官方 SDK(Python: pip install ashareapi · Node.js: npm install ashareapi)时使用。含 32 个端点的参数与字段语义、单位换算(volume 是手 / amount 是元 / 比率为百分数)、数据日期语义、常见错误与限流处理(匿名 5 次/分,解一次 PoW 挑战提到 60 次/分)。
|
|
4
|
+
license: MIT
|
|
5
|
+
metadata:
|
|
6
|
+
version: "0.2.5"
|
|
7
|
+
updated: "2026-10-02"
|
|
8
|
+
homepage: "https://ashareapi.com"
|
|
9
|
+
docs: "https://ashareapi.com/docs"
|
|
10
|
+
endpoints: "https://ashareapi.com/endpoints"
|
|
11
|
+
changelog: "https://ashareapi.com/changelog"
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
# ashareapi — A 股数据 API
|
|
15
|
+
|
|
16
|
+
**32 个 HTTP 端点**(`https://api.ashareapi.com/v1/...`),覆盖行情 / K线 / **五档盘口** / 财务 / 资金 / 龙虎榜 / 板块 / 转债 / 选股 / 宏观 / 产业链。
|
|
17
|
+
**5 个端点无需 Key**(装上就能调),其余需要 Key(¥9.9 起)。另有**官方 SDK**:Python `pip install ashareapi` · Node.js / TypeScript `npm install ashareapi`。
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 一、先判断:这个任务要不要 Key
|
|
22
|
+
|
|
23
|
+
| 任务 | 免费端点够不够 |
|
|
24
|
+
|---|---|
|
|
25
|
+
| 现价 / K线 / 热搜 / 市场总览 / 涨跌分布 | ✅ **够,无需 Key**(先试这个)|
|
|
26
|
+
| 财报 / 资金流 / 龙虎榜 / 板块 / 转债 / 选股 / 宏观 … | ❌ 需要 Key(`Authorization: Bearer <key>`)|
|
|
27
|
+
|
|
28
|
+
**免费 5 个**:`/v1/quote`(行情)· `/v1/kline`(K线)· `/v1/hot`(热搜)· `/v1/market-overview`(大盘画像)· `/v1/changedist`(涨跌分布)
|
|
29
|
+
**工具端点**(也免 Key):`/v1/health`(健康检查)· `/v1/challenge`(PoW 提额)
|
|
30
|
+
|
|
31
|
+
## 二、免费端点:直接调(无需任何鉴权)
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
# 行情:现价 / 开高低 / 成交量 / 换手率
|
|
35
|
+
curl "https://api.ashareapi.com/v1/quote?code=sh600667"
|
|
36
|
+
|
|
37
|
+
# K线(日/周/月)
|
|
38
|
+
curl "https://api.ashareapi.com/v1/kline?code=sh600667&period=day&count=5"
|
|
39
|
+
|
|
40
|
+
# 热搜榜
|
|
41
|
+
curl "https://api.ashareapi.com/v1/hot?limit=10"
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Python:
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
import requests
|
|
48
|
+
|
|
49
|
+
r = requests.get("https://api.ashareapi.com/v1/quote",
|
|
50
|
+
params={"code": "sh600667"}, timeout=10)
|
|
51
|
+
body = r.json()
|
|
52
|
+
if not body.get("ok"):
|
|
53
|
+
raise RuntimeError(body) # 上游取数失败(已自动换源,且不扣次数)
|
|
54
|
+
bar = body["data"][0] # 最新一根(当日实时)
|
|
55
|
+
print(bar["date"], bar["last"], bar["turnover"])
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## 三、带 Key 调用
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
curl -H "Authorization: Bearer ct-你的Key" \
|
|
62
|
+
"https://api.ashareapi.com/v1/fund?code=sh600667"
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Key 从 https://ashareapi.com/pricing 获取。**401 = 没带 Key 调了付费端点**。
|
|
66
|
+
|
|
67
|
+
## 四、按任务找端点(决策树)
|
|
68
|
+
|
|
69
|
+
| 用户想要 | 用这个端点 | 备注 |
|
|
70
|
+
|---|---|---|
|
|
71
|
+
| 现价 / 开高低 / 换手 | `/v1/quote` | 免费 |
|
|
72
|
+
| **封单多少 / 买盘卖盘 / 盘口 / 挂单** | **`/v1/orderbook`** | **五档盘口**(买五卖五·秒级快照·**盘中才有意义**·量单位=手)|
|
|
73
|
+
| **估值 / PE / PB / 市值 / 股本 / 涨停价** | **`/v1/snapshot`** | **全字段画像**(**仅A股**·付费·**要多项时比分别调更省次数**)|
|
|
74
|
+
| 走势 / 历史 K线 | `/v1/kline` | 免费;**仅日/周/月,无分钟级**;价格口径固定**前复权**(除权除息日不跳空)——**不要再自己复权**(会二次复权)|
|
|
75
|
+
| 什么股票热门 | `/v1/hot` | 免费 |
|
|
76
|
+
| 大盘怎么样 / 风格轮动 / 估值分位 | `/v1/market-overview` | 免费;`type` 选 summary/trade/interval/technical/margin/valuation/rotation |
|
|
77
|
+
| 涨跌家数 / 涨停家数 / 市场广度 | `/v1/changedist` | 免费;**当期口径**(别用 market-overview?type=updown,那是 T-1)|
|
|
78
|
+
| 财报 / 营收 / 净利润 / 毛利率 | `/v1/finance` | 三表 |
|
|
79
|
+
| 主力资金 / 资金流 / 龙虎榜 / 大宗 / 两融 | **`/v1/fund`** | **一个接口拿全交易面**(优先用)|
|
|
80
|
+
| 龙虎榜分榜(机构 / 游资 / 席位)| `/v1/lhb` | `type=institution/hotmoney/activeseat` |
|
|
81
|
+
| 技术面 / MACD / KDJ / RSI / BOLL | `/v1/technical` | |
|
|
82
|
+
| 谁在持有 / 股东户数 / 机构持仓 | `/v1/shareholder` | |
|
|
83
|
+
| 筹码 / 套牢盘 / 成本分布 | `/v1/chip` | |
|
|
84
|
+
| 板块涨幅榜 / 轮动 | `/v1/sector` | |
|
|
85
|
+
| 某板块贵不贵 / 估值分位 | `/v1/sector-valuation` | `code=pt01801780` 形式 |
|
|
86
|
+
| 可转债 / 强赎 / 双低 / 溢价 | `/v1/bond` | `code=sh113052` 形式 |
|
|
87
|
+
| ETF | `/v1/etf` | `code=sh510300` |
|
|
88
|
+
| 新股 / 打新 | `/v1/ipo` | |
|
|
89
|
+
| 分红送转 | `/v1/dividend` | |
|
|
90
|
+
| 个股事件(42 类)/ 解禁 / 回购 | `/v1/events` | |
|
|
91
|
+
| 个股事件日历(某日有什么事件)| `/v1/calendar` | **不是宏观日历**(宏观用 `/v1/macro`)|
|
|
92
|
+
| 研报 / 机构观点 | `/v1/dehydrated` | `mode=list/detail` |
|
|
93
|
+
| 宏观 / LPR / CPI / GDP | `/v1/macro` | `region=cn/us/...` |
|
|
94
|
+
| 产业链 / 上下游 / 某公司链上位置 | `/v1/industry-chain` | `mode=list/graph/stock` |
|
|
95
|
+
| 筛选股票 / 低估高 ROE | `/v1/screen` | `expr` 多因子交集 或 `preset`(22 个预设)|
|
|
96
|
+
| 只知道名字,要代码 | `/v1/search` | **先搜代码再查数据** |
|
|
97
|
+
| 公司是做什么的 | `/v1/profile` | |
|
|
98
|
+
| 融资融券 | `/v1/margin-trade` | `code` 必填(支持批量)|
|
|
99
|
+
| 大宗交易 | `/v1/block-trade` | |
|
|
100
|
+
| 我的用量 / 额度 | `/v1/usage` | |
|
|
101
|
+
|
|
102
|
+
**完整参数与返回字段** → 读 `references/endpoints.md`。
|
|
103
|
+
|
|
104
|
+
## 五、四个必知语义(最常踩的坑)
|
|
105
|
+
|
|
106
|
+
1. **单位不统一,先看清**
|
|
107
|
+
- `volume` 是**手**(×100 = 股)· `amount` 是**元**(不是万元)
|
|
108
|
+
- 比率字段是**百分数**:`instBuyRate: 20` 表示 **20%**(不是 0.2)
|
|
109
|
+
- 金额有时以**字符串**返回(`"135160431.2"`)→ **先 `float()` 再算**,否则 `+` 会拼字符串
|
|
110
|
+
|
|
111
|
+
2. **数据日期语义**:休市日**不会**变成"今天"。看返回里的 `date` 字段判断数据属于哪个交易日(`quote` 的最新一根就是当日实时)。
|
|
112
|
+
|
|
113
|
+
3. **返回结构:`data` 是主载荷,形状有 4 种**(统一信封 `{ok, endpoint, tier, elapsed_ms, source, data}`)
|
|
114
|
+
- `data` 可能是:**对象数组**(多数端点)/ **表列表**(`finance`)/ **多段结构**(`shareholder`·`calendar`)/ **Markdown 文本**(`bond`·`sector` 等 9 个)
|
|
115
|
+
- ⚠️ **`structured` / `tables` 是【条件字段】—— 不是每个端点都有**:**只有 `data` 是 Markdown 时才附**
|
|
116
|
+
⇒ 代码里**一律写 `body.get("structured")`**;写 `body["structured"]` 在 `quote`/`kline`/`finance` 上会 **KeyError**
|
|
117
|
+
- ⚠️ `/v1/health` · `/v1/challenge` · `/v1/usage` 信封**没有 `data`**
|
|
118
|
+
- 四种形状怎么判别 + 通吃写法 → `references/endpoints.md` §返回形态
|
|
119
|
+
|
|
120
|
+
4. **`ok:false` 不等于"我们写错了"**:那是**上游取数失败**(我们已自动换源,**不扣调用次数**)→ 重试一次通常就好。**空结果**(如当天没大宗交易)与"失败"是两件事。
|
|
121
|
+
|
|
122
|
+
## 六、错误与限流
|
|
123
|
+
|
|
124
|
+
| 现象 | 含义 | 怎么办 |
|
|
125
|
+
|---|---|---|
|
|
126
|
+
| **401** | 没带 Key 调了付费端点 | 拿 Key,或改用 5 个免费端点 |
|
|
127
|
+
| **429** | 超出档位频率(匿名 5 次/分)| 降频;或**解一次 PoW 挑战提到 60 次/分**(见下);或升级档位 |
|
|
128
|
+
| **`ok:false`** | 上游取数失败(已自动换源)| **不扣次数**,重试一次 |
|
|
129
|
+
| **200 但 data 为空** | 当前确实没有这类数据(如当天无大宗交易)| 换条件 / 稍后再试,**不是故障** |
|
|
130
|
+
| **5xx** | 重试耗尽 | 稍后再试 |
|
|
131
|
+
|
|
132
|
+
**匿名提额(PoW)**:
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
# 1) 拿挑战
|
|
136
|
+
curl "https://api.ashareapi.com/v1/challenge"
|
|
137
|
+
# → {challenge, difficulty, expires_in, how_to}
|
|
138
|
+
# 2) 算 nonce(sha256(challenge.nonce) 前 difficulty 位为 0),然后:
|
|
139
|
+
curl -H "X-PoW: <challenge>.<nonce>" "https://api.ashareapi.com/v1/quote?code=sh600667"
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
详细错误语义 → 读 `references/errors.md`。
|
|
143
|
+
|
|
144
|
+
## 七、要写代码?用官方 SDK(Python / Node.js)
|
|
145
|
+
|
|
146
|
+
**Python**:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
pip install ashareapi # 基础(返回 list[dict])
|
|
150
|
+
pip install "ashareapi[pandas]" # 加 DataFrame 支持(推荐)
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
from ashareapi import AShareAPI
|
|
155
|
+
|
|
156
|
+
cli = AShareAPI() # 免费端点无需 Key
|
|
157
|
+
df = cli.quote("sh600667") # → DataFrame
|
|
158
|
+
print(df[["date", "last", "turnover"]])
|
|
159
|
+
|
|
160
|
+
cli = AShareAPI("ct-你的Key") # 付费端点
|
|
161
|
+
print(cli.fund("sh600667")) # 资金流 + 龙虎榜 + 大宗 + 两融
|
|
162
|
+
print(cli.screen(preset="low_pe", limit=10))
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
**Node.js / TypeScript**:
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
npm install ashareapi # 零运行时依赖(原生 fetch,需 Node ≥ 18)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
import { AShareAPI } from "ashareapi";
|
|
173
|
+
|
|
174
|
+
const cli = new AShareAPI(); // 免费端点无需 Key
|
|
175
|
+
const bars = await cli.quote("sh600667"); // → 对象数组
|
|
176
|
+
console.log(bars[0].last);
|
|
177
|
+
|
|
178
|
+
const paid = new AShareAPI({ apiKey: "ct-你的Key" });
|
|
179
|
+
console.log(await paid.screen("", "low_pe", 10, "ROETTM"));
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
> 两个 SDK **同 32 个方法 / 同 5 类异常 / 同重试策略**;差异只是语言惯例:
|
|
183
|
+
> Python 用 `snake_case` 且返回 DataFrame,Node 用 `camelCase`(也认 `snake_case` 别名)且**全部返回 Promise**。
|
|
184
|
+
|
|
185
|
+
**代码格式随便写**:`sh600667` / `600667.SH` / `600667` 都认(自动归一化)。
|
|
186
|
+
**32 个端点 = 32 个方法**(`/v1/margin-trade` → Python `margin_trade()` / Node `marginTrade()`)。
|
|
187
|
+
|
|
188
|
+
完整用法与 5 类异常 → 读 `references/sdk.md`。
|
|
189
|
+
|
|
190
|
+
## 八、详细参考(按需读取,不要一次全读)
|
|
191
|
+
|
|
192
|
+
| 文件 | 什么时候读 |
|
|
193
|
+
|---|---|
|
|
194
|
+
| [references/endpoints.md](references/endpoints.md) | 要确认某端点的**完整参数 / 返回字段** |
|
|
195
|
+
| [references/fields.md](references/fields.md) | 要**算数**(单位换算、字段含义、字符串数字)|
|
|
196
|
+
| [references/errors.md](references/errors.md) | 遇到 401/429/ok:false/空结果,或要处理限流 |
|
|
197
|
+
| [references/sdk.md](references/sdk.md) | 用户要**写 Python / Node.js 代码**或问 SDK |
|
|
198
|
+
|
|
199
|
+
---
|
|
200
|
+
|
|
201
|
+
**边界(诚实)**:
|
|
202
|
+
- K 线**只提供日/周/月**,**没有分钟级**
|
|
203
|
+
- 新闻/公告**全文**不在 API 范围(有 `dehydrated` 研报摘要、`events` 事件标签、`calendar` 事件日历)
|
|
204
|
+
- 数据仅供研究参考,**不构成投资建议** —— 本 skill 只讲怎么取数与计算,不给买卖建议
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## 九、本 skill 版本
|
|
209
|
+
|
|
210
|
+
**当前版本:`0.2.4`(2026-09-30)**
|
|
211
|
+
|
|
212
|
+
### 怎么知道该更新
|
|
213
|
+
|
|
214
|
+
本 skill 是**随 API 演进的快照** —— 每次新增端点/字段,`references/` 里的清单会同步但**你手上装的可能还是旧版**。判断方法:
|
|
215
|
+
|
|
216
|
+
| 检查 | 说明 |
|
|
217
|
+
|---|---|
|
|
218
|
+
| `metadata.version` | 看本文件 frontmatter 的版本号 |
|
|
219
|
+
| **端点总数** | 对比现实:**当前 32 个**(`endpoints.md` 标题也是这个数)|
|
|
220
|
+
| **MCP 工具数** | 当前 **24 个**(`list_tools` 返回数量)|
|
|
221
|
+
|
|
222
|
+
**任一项对不上 → 你装的是旧版。**
|
|
223
|
+
|
|
224
|
+
### 怎么更新
|
|
225
|
+
|
|
226
|
+
1. 重新下载:**https://ashareapi.com/skill**(页面有 zip 下载)
|
|
227
|
+
2. 解压覆盖到你的 skills 目录(`.claude/skills/` · `.agents/skills/` · `.opencode/skills/` 等)
|
|
228
|
+
3. 重启客户端
|
|
229
|
+
|
|
230
|
+
> ⚠️ **API 本身不需要更新** —— 端点永远是最新的(服务端演进);需要更新的是**这份说明**(否则你可能不知道新端点存在)。
|
|
231
|
+
|
|
232
|
+
### 版本记录
|
|
233
|
+
|
|
234
|
+
| 版本 | 日期 | 变化 |
|
|
235
|
+
|---|---|---|
|
|
236
|
+
| `0.2.4` | 2026-09-30 | **文档表述统一**:全文改为面向使用者的表述;**端点 / 字段 / 口径无任何变化** |
|
|
237
|
+
| `0.2.3` | 2026-09-28 | **补 K 线价格口径**:`/v1/kline` 明确「口径**固定为前复权**(除权除息日不跳空)、**无 `adjust` 参数**、**不要再自己复权**(会二次复权)」(`SKILL.md` 速查表 + `references/endpoints.md` 同步)|
|
|
238
|
+
| `0.2.2` | 2026-09-26 | 换手率字段统一为 **`turnover`**(%)—— 与 `/v1/snapshot` 同名同值(`references/fields.md` 字段表 · `endpoints.md` · `sdk.md` 示例同步)|
|
|
239
|
+
| `0.2.1` | 2026-09-26 | **完善返回结构文档**:明确 `structured`/`tables` 是**条件字段**(仅 `data` 为 Markdown 时才附)· 新增「`data` 四种形状」速查表 + 通吃写法 · `snapshot` 字段数 → **35** · 补 `finance` 表列表 / `shareholder`·`calendar` 多段结构 / `fund` 扁平 dict · 补 `profile` 字段说明 |
|
|
240
|
+
| `0.2.0` | 2026-09-23 | 新增 `/v1/snapshot`(全字段画像)· `/v1/orderbook`(五档盘口)· 端点数 30→32 · MCP 工具 22→24 · 补字段单位与"计费=次数"说明 |
|
|
241
|
+
| `0.1.0` | 2026-09-20 | 首个版本(30 个端点 · 22 个 MCP 工具)|
|
|
242
|
+
|
|
243
|
+
**完整更新日志(含字段级变更)→ https://ashareapi.com/changelog**
|
|
244
|
+
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# 端点清单(32 个)
|
|
2
|
+
|
|
3
|
+
- **统一前缀**:`https://api.ashareapi.com/v1`
|
|
4
|
+
- **统一信封**:`{ ok, endpoint, tier, elapsed_ms, source, data }` —— 只有这 6 个键
|
|
5
|
+
- ⚠️ **`structured` / `tables` 是【条件字段】,不是每个端点都有**:**只有 `data` 是 Markdown 文本时才附**
|
|
6
|
+
(`market-overview` · `changedist` · `lhb` · `sector` · `sector-valuation` · `bond` · `etf` · `screen` · `macro`)。
|
|
7
|
+
**其余端点没有这两个键** → `body["structured"]` 会 **KeyError**,**一律用 `body.get("structured")`**。
|
|
8
|
+
- ⚠️ **3 个端点信封不同**(都**没有 `data`**):`/v1/health` = `{ok, uptime_s, data_ready, tiers}` ·
|
|
9
|
+
`/v1/challenge` = `{challenge, difficulty, expires_in, how_to}` · `/v1/usage` = `{ok, day, calls_today, tier, total_calls, total_quota, total_left, daily_quota, per_min}`
|
|
10
|
+
- **参数带 `*` = 必填**
|
|
11
|
+
- **实时规格**:`GET https://api.ashareapi.com/openapi.json`(本文件据其生成)
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## 一、免费端点(无需 Key,5 个数据端点 + 2 个工具端点)
|
|
16
|
+
|
|
17
|
+
| 端点 | 参数 | 返回要点 |
|
|
18
|
+
|---|---|---|
|
|
19
|
+
| `/v1/quote` | `code*` | 实时行情:`last` 现价 / `open/high/low` / `volume`(手)/ `amount`(元)/ `turnover` 换手率% / `date`。**最新一根 = 当日实时** |
|
|
20
|
+
| `/v1/kline` | `code*` · `period`(day/week/month) · `count` | OHLCV 历史 K 线。**只有日/周/月,无分钟级**。价格口径**固定为前复权**(除权除息日不跳空;无 `adjust` 参数)—— **不要再自己复权**(会二次复权)|
|
|
21
|
+
| `/v1/hot` | `limit`(默认 30,**上限 50**)| 全市场热搜榜(A股/美股/ETF):关注度排名 + 涨跌幅 |
|
|
22
|
+
| `/v1/market-overview` | `type`(summary/trade/interval/technical/margin/**valuation**/**rotation**) | 大盘画像。`valuation` = 中证全指 PE/PB/PS **历史百分位**;`rotation` = 风格轮动(大小盘/成长价值)。⚠️ `type=updown` 是 **T-1 口径**,涨跌家数请用 `/v1/changedist` |
|
|
23
|
+
| `/v1/changedist` | 无 | **当期**涨跌家数 / 涨跌停家数 / 停牌 / 成交额 / 区间分布 —— **市场广度推荐入口** |
|
|
24
|
+
| `/v1/health` | 无 | `data_ready=true` 表示数据通道可用(不含内部实现细节)|
|
|
25
|
+
| `/v1/challenge` | `difficulty` | PoW 挑战(匿名提额用)。解 nonce 后带 `X-PoW: <challenge>.<nonce>`,匿名配额 5 → 60 次/分 |
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## 二、行情与技术(需 Key)
|
|
30
|
+
|
|
31
|
+
| 端点 | 参数 | 返回要点 |
|
|
32
|
+
|---|---|---|
|
|
33
|
+
| `/v1/technical` | `code*` | MA / MACD / KDJ / RSI / BOLL 全家桶 |
|
|
34
|
+
| `/v1/chip` | `code*` | 筹码分布与持仓成本(获利盘 / 套牢盘比例)|
|
|
35
|
+
| `/v1/orderbook` | `code*` | **五档盘口(order book)**:买一~买五 / 卖一~卖五的**价 + 挂单量(手)**。字段:`b1_p`/`b1_v`~`b5_p`/`b5_v`(买档)、`a1_p`/`a1_v`~`a5_p`/`a5_v`(卖档)。⚠️ **秒级快照(10 秒缓存)· 盘中才有意义**;量单位是**手**(×100 = 股);**跌停买档全 0 / 涨停卖档全 0**(正常)|
|
|
36
|
+
| `/v1/snapshot` | `code*` | **全字段行情画像(35 字段)**:`code/name/sec_type/currency/status` + 价格(`price/prev_close/open/high/low/avg_price/change/change_pct/amplitude/speed`)+ 量(`volume/amount/turnover/outer_vol/inner_vol/volume_ratio`)+ 估值(`pe_ttm/pe_dynamic/pe_static/pb`)+ 市值股本(`float_market_cap/total_market_cap/float_shares/total_shares`)+ 涨跌停(`limit_up/limit_down`)+ 盘口(`bid/ask/bid_ask_diff`)+ `time`。⚠️ **与 quote 分工**:quote 轻(8 字段)免费 / snapshot 全(**35 字段**)付费,**只要现价用 quote 更轻**;✅ **计费=次数**:要估值+市值+股本+涨停价**多项**时,本端点 1 次搞定——比分别调 quote/valuation/orderbook **更省次数**。⚠️ **仅 A 股**(港股/美股字段布局不同,用 quote)——**传其他市场返回空,不返回错数据**|
|
|
37
|
+
| `/v1/search` | `q*` | 按名称/代码搜股票、基金、板块 —— **用户只给名称时先搜代码** |
|
|
38
|
+
| `/v1/profile` | `code*` | 公司简况:上市日期 / 主营业务 / 所属行业 |
|
|
39
|
+
|
|
40
|
+
## 三、财务
|
|
41
|
+
|
|
42
|
+
| 端点 | 参数 | 返回要点 |
|
|
43
|
+
|---|---|---|
|
|
44
|
+
| `/v1/finance` | `code*` · `num`(期数)| 利润表 / 资产负债表 / 现金流量表(多期)。营收 / 净利 / 毛利率 / 负债。⚠️ **`data` 是【表列表】`[[行...], [行...], ...]`**(外层=表,内层=行)→ **先按表索引再按行**:`data[0][0]["..."]` |
|
|
45
|
+
| `/v1/dividend` | `code*` · `years` | 分红送转历史:每股分红 / 送股 / 除权日 |
|
|
46
|
+
| `/v1/shareholder` | `code*` | 十大股东 / **股东户数(筹码集中度)** / 机构持仓。⚠️ **`data` 是【多段结构】**:`{code, market, tables: [{title, slug, rows}], data: {slug: rows}}`,slug = `top10_holders` / `top10_float_holders` / `holder_count` |
|
|
47
|
+
|
|
48
|
+
## 四、资金与交易(核心)
|
|
49
|
+
|
|
50
|
+
| 端点 | 参数 | 返回要点 |
|
|
51
|
+
|---|---|---|
|
|
52
|
+
| **`/v1/fund`** | `code*` | ⭐ **一接口拿全交易面**:主力资金(当日/5/10/20 日净流入 + 全市场排名)+ 龙虎榜(上榜原因 / 买卖总额 / **营业部明细**)+ 大宗交易 + 融资融券。**问"主力资金/资金流/龙虎榜"优先用它**。⚠️ `data` 是**扁平 dict**(不是多段):`{date, close, main_net, main_net_5d/10d/20d, main_rank, lhb, lhb_details, block_trades, margin, industry_rank}` |
|
|
53
|
+
| `/v1/lhb` | `type`(institution/hotmoney/activeseat) · `date` | 龙虎榜**分榜**:机构榜(机构数 / 机构买入 / 净买)· 游资榜 · 活跃席位榜 |
|
|
54
|
+
| `/v1/margin-trade` | `code`(**必填**,支持 `sh600667,sz000651` 批量)· `date` | 融资余额 / 买入 / 偿还 / 融券。未披露日上游会给出原因 |
|
|
55
|
+
| `/v1/block-trade` | `code` · `date` | 大宗交易:成交价 / 折溢价 / 量 / 买卖方营业部 |
|
|
56
|
+
| `/v1/events` | `code*` | 个股事件总览:**42 类**(大宗 / 龙虎榜 / 回购 / 定增 / 分红 / 业绩 / 解禁 …)|
|
|
57
|
+
| `/v1/calendar` | `date` · `limit` | **个股**事件日历(分红派息 / 解禁 / 财报披露排期)。**不是宏观日历**(宏观用 `/v1/macro`)。⚠️ **`data` 是【多段结构】**:`{tables: [{title, slug, rows}], data: {slug: rows}}`,**按事件类型分段保序**;slug 取值 = `financial_report` / `dividend` / `ipo` / `meeting` / `lockup_release` / `rights_issue` |
|
|
58
|
+
|
|
59
|
+
## 五、板块与产业链
|
|
60
|
+
|
|
61
|
+
| 端点 | 参数 | 返回要点 |
|
|
62
|
+
|---|---|---|
|
|
63
|
+
| `/v1/sector` | 无 | 行业 / 概念 / 地域板块涨幅榜 + 领涨股 |
|
|
64
|
+
| `/v1/sector-valuation` | `code*`(`pt01801780` 形式)| 申万板块 PE/PB/PS/PCF + 股息率 + **历史百分位** |
|
|
65
|
+
| `/v1/industry-chain` | `mode`(list/graph/stock) · `topic` · `code` | `list` = **183 个主题** · `graph&topic=X` = 图谱(关联个股 + 节点 + 上中下游)· `stock&code=X` = 该股所属链(主题/节点/位置/**关联度**/业务描述)|
|
|
66
|
+
|
|
67
|
+
## 六、可转债 / ETF / 新股
|
|
68
|
+
|
|
69
|
+
| 端点 | 参数 | 返回要点 |
|
|
70
|
+
|---|---|---|
|
|
71
|
+
| `/v1/bond` | `code*`(`sh113052`)| 转债完整条款:溢价率 / 转股价值 / **双低值** / **强赎触发价** / 回售触发价 / 转股价 / 正股 / 到期日 / 信用评级 |
|
|
72
|
+
| `/v1/etf` | `code*`(`sh510300`)| ETF 行情 / 规模 / 溢折率 / 资金流 |
|
|
73
|
+
| `/v1/ipo` | `days` | 新股发行 / 申购 / 中签 / 上市日历 |
|
|
74
|
+
|
|
75
|
+
## 七、选股与宏观
|
|
76
|
+
|
|
77
|
+
| 端点 | 参数 | 返回要点 |
|
|
78
|
+
|---|---|---|
|
|
79
|
+
| `/v1/screen` | `expr` 或 `preset` · `limit` · `orderby` · `desc` · `market` | **因子选股**。`expr` 多因子交集:`intersect([PE_TTM > 0, PE_TTM < 20, ROETTM > 15])`;`preset` 22 个官方预设(**大小写/下划线不敏感**:`low_pe` = `LowPE`)。常用因子:PE_TTM / PB / PS_TTM / TotalMV / DividendRatioTTM(估值)· ROE / ROETTM / ROIC / GrossIncomeRatioTTM(盈利)· OperatingRevenueGrowRate / NPParentCompanyYOY(成长)· CurrentRatio / DebtAssetsRatio(负债)· NetOperateCashFlowTTM(现金流)|
|
|
80
|
+
| `/v1/macro` | `region`(cn/us/jp/eu/hk) · `names` | 宏观:GDP / CPI / PMI / LPR / 国债收益率 / 财政 |
|
|
81
|
+
| `/v1/dehydrated` | `mode`(list/detail) · `symbol` · `limit` | 券商研报脱水摘要 |
|
|
82
|
+
| `/v1/usage` | 无 | 当前 Key 的今日调用次数 / 剩余总量 / 到期时间 / 限流额度 |
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## 返回形态:`data` 有四种形状(32 端点全量)
|
|
87
|
+
|
|
88
|
+
**程序化处理前必须先判形状** —— `data` **不是**永远同一个类型,写死 `data[0]["x"]` 会在 `finance` / `calendar` 上炸。
|
|
89
|
+
|
|
90
|
+
| 形状 | 长这样 | 哪些端点 | 怎么读 |
|
|
91
|
+
|---|---|---|---|
|
|
92
|
+
| **① 对象数组** | `[{"col": val}, ...]` | 多数:`quote` `kline` `hot` `technical` `chip` `orderbook` `snapshot` `profile` `dividend` `margin-trade` `ipo` | `for row in data: row["field"]` |
|
|
93
|
+
| **② 表列表** | `[[行...], [行...]]`(外层=表,内层=行)| `finance`(利润表 / 资产负债表 / 现金流量表)| `data[表号][行号]["字段"]` —— **先按表索引** |
|
|
94
|
+
| **③ 多段结构** | `{"tables": [{"title","slug","rows"}], "data": {"<slug>": rows}}` | `shareholder`(另带 `code`/`market`)· `calendar` | `data["tables"]` 保序分段;`data["data"]["<slug>"]` 便捷索引 |
|
|
95
|
+
| **④ Markdown 字符串** | `"\| 列 \| 列 \|\n..."` | 9 个(见信封说明)+ `events` · `dehydrated` | 优先读同响应的 `structured` / `tables`(**若存在**);不存在才正则解析 |
|
|
96
|
+
|
|
97
|
+
**特殊 dict(不属于上面四类)**:`fund` = 扁平 `{date, close, main_net, main_net_5d/10d/20d, main_rank, lhb, lhb_details, block_trades, margin, industry_rank}` ·
|
|
98
|
+
`industry-chain?mode=list` = `{mode, topics}`。
|
|
99
|
+
|
|
100
|
+
**空结果的三种表现**(都**不是**故障,`ok:true`):`data = []`(如 `block-trade` 当天无成交)· `data` 是含"数据为空"的 Markdown(`events`)· 多段结构里 `tables = []`。
|
|
101
|
+
|
|
102
|
+
**推荐写法(四种形状通吃)**:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
def rows_of(body):
|
|
106
|
+
d = body.get("data")
|
|
107
|
+
if isinstance(d, dict) and "tables" in d: # ③ 多段结构
|
|
108
|
+
return [r for t in d["tables"] for r in (t.get("rows") or [])]
|
|
109
|
+
if isinstance(d, list) and d and isinstance(d[0], list): # ② 表列表
|
|
110
|
+
return d[0] # 或按表号取
|
|
111
|
+
return body.get("structured") or d # ① / ④(④ 优先用 structured)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## 代码格式(`code` 参数)
|
|
117
|
+
|
|
118
|
+
| 写法 | 说明 |
|
|
119
|
+
|---|---|
|
|
120
|
+
| `sh600667` | 沪市(sh)· 深市(sz)· 北交所(bj)· 港股(hk)· 美股(us)|
|
|
121
|
+
| `600667.SH` | 后缀写法 |
|
|
122
|
+
| `600667` | 纯 6 位(按首位推断:5/6→sh · 0/3→sz · 4/8→bj)|
|
|
123
|
+
| `pt01801780` | **板块**代码(申万板块,用于 `sector-valuation`)|
|
|
124
|
+
| `sh113052` | **可转债**代码 |
|
|
125
|
+
| `sh510300` | **ETF** 代码 |
|
|
126
|
+
|
|
127
|
+
**官方 SDK 会自动归一化三种股票写法**;直接调 HTTP 时建议统一用 `sh600667` 形式。
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# 错误、限流与"无数据"
|
|
2
|
+
|
|
3
|
+
**核心区分**(先记住这条):**"取数失败" ≠ "当前没有这类数据"**。两者都有明确信号,不要混为一谈。
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 一、HTTP 状态码
|
|
8
|
+
|
|
9
|
+
| 状态 | 含义 | 处理 |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| **200** | 成功(但要看 `ok` 字段,见下)| 读 `data` / `structured` |
|
|
12
|
+
| **401** | 未授权 —— **没带 Key 调了付费端点**(或 Key 无效/已停用)| 拿 Key(https://ashareapi.com/pricing),或改用 5 个免费端点 |
|
|
13
|
+
| **403** | 禁止 —— 常见于 **IP 维度限制**(一个 Key 被多个 IP 共用,超出该档位允许的 IP 数)| 别把 Key 共享给多人;升级档位 |
|
|
14
|
+
| **404** | 路径不存在 | 核对端点名(见 `endpoints.md`)|
|
|
15
|
+
| **422** | 参数错误(缺必填 / 值非法)| 看返回里的说明;常见是缺 `code` |
|
|
16
|
+
| **429** | **超出限流** | 降频 · 解 PoW 提额 · 升级档位(见下)|
|
|
17
|
+
| **5xx** | 服务端/上游异常 | 稍后重试 |
|
|
18
|
+
|
|
19
|
+
## 二、`ok` 字段(HTTP 200 也要看它)
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{ "ok": false, "endpoint": "block-trade", "data": [] }
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
- **`ok:true`** = 取数成功
|
|
26
|
+
- **`ok:false`** = **上游取数失败**(我们**已自动换源**,且 **不扣调用次数**)→ **重试一次通常就好**
|
|
27
|
+
|
|
28
|
+
⚠️ **`ok:false` 不是"你写错了"** —— 是数据源那一侧的问题。所以**不要**因为 `ok:false` 就改代码逻辑。
|
|
29
|
+
|
|
30
|
+
## 三、空结果(≠ 失败)
|
|
31
|
+
|
|
32
|
+
**`ok:false` + `data: []`** 或 **`ok:true` 但列表为空** → 可能是"**当前确实没有这类数据**":
|
|
33
|
+
|
|
34
|
+
- 今天没有大宗交易
|
|
35
|
+
- 这个时间段没有解禁事件
|
|
36
|
+
- 该股今日不在龙虎榜
|
|
37
|
+
|
|
38
|
+
**处理**:换条件 / 换日期 / 稍后再试 —— **不是故障,不会计费**。
|
|
39
|
+
**不要**把它当成"上游挂了"去重试到超时。
|
|
40
|
+
|
|
41
|
+
## 四、限流(匿名很紧,这是设计)
|
|
42
|
+
|
|
43
|
+
| 档位 | 额度 |
|
|
44
|
+
|---|---|
|
|
45
|
+
| **匿名**(无 Key)| **5 次/分钟** |
|
|
46
|
+
| **匿名 + PoW** | **60 次/分钟** |
|
|
47
|
+
| 各付费档 | 见 https://ashareapi.com/pricing(含每分钟与总量限制)|
|
|
48
|
+
|
|
49
|
+
**触发时**:返回 **429**。
|
|
50
|
+
|
|
51
|
+
### 匿名提额:解一次 PoW 挑战
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
# 1) 取挑战
|
|
55
|
+
curl "https://api.ashareapi.com/v1/challenge"
|
|
56
|
+
# → { "challenge": "...", "difficulty": 18, "expires_in": 600, "how_to": "..." }
|
|
57
|
+
|
|
58
|
+
# 2) 算 nonce:要求 sha256("<challenge>.<nonce>") 的十六进制前 difficulty 位为 '0'
|
|
59
|
+
# (普通电脑毫秒~秒级;难度由服务端定,只允许调高不允许调低)
|
|
60
|
+
|
|
61
|
+
# 3) 带上去请求
|
|
62
|
+
curl -H "X-PoW: <challenge>.<nonce>" \
|
|
63
|
+
"https://api.ashareapi.com/v1/quote?code=sh600667"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
**要点**:
|
|
67
|
+
- 挑战**有有效期**(`expires_in`,通常 600 秒),过期重新取
|
|
68
|
+
- 挑战可以**预取 + 预解算 + 延后使用**(流水线化,不必每次现解)
|
|
69
|
+
- **付费 Key 用户不需要 PoW**(额度已够)
|
|
70
|
+
|
|
71
|
+
### 降频建议(比死磕 429 更实际)
|
|
72
|
+
|
|
73
|
+
- 批量取数时**加 `time.sleep()`**(匿名至少 12 秒/次;PoW 后 1 秒/次)
|
|
74
|
+
- **本地缓存**:同一标的一天内的行情/财务不必重复取
|
|
75
|
+
- 需要**高频**就升级档位 —— 比反复解 PoW 省事
|
|
76
|
+
|
|
77
|
+
## 五、官方 Python SDK 的 5 类异常(不用自己判状态码)
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from ashareapi import (AShareAPI, AuthError, RateLimitError,
|
|
81
|
+
UpstreamError, EmptyResultError, APIError)
|
|
82
|
+
|
|
83
|
+
try:
|
|
84
|
+
df = cli.fund("sh600667")
|
|
85
|
+
except AuthError: # 401 / 缺 Key
|
|
86
|
+
...
|
|
87
|
+
except RateLimitError: # 429(含提额提示)
|
|
88
|
+
...
|
|
89
|
+
except UpstreamError: # ok:false —— 上游失败,已换源,不扣次数 → 重试一次
|
|
90
|
+
...
|
|
91
|
+
except EmptyResultError: # 当前无数据(如当天无大宗交易)→ 不计费,换条件
|
|
92
|
+
...
|
|
93
|
+
except APIError: # 其他(网络 / 5xx 重试耗尽)
|
|
94
|
+
...
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
| SDK 异常 | 对应上面的情况 |
|
|
98
|
+
|---|---|
|
|
99
|
+
| `AuthError` | 401 / 403(鉴权与 IP 维度)|
|
|
100
|
+
| `RateLimitError` | 429 |
|
|
101
|
+
| `UpstreamError` | `ok:false` 且 `data` 非空(真失败)|
|
|
102
|
+
| **`EmptyResultError`** | `ok:false` 且 `data` 为空(**无数据 ≠ 失败**)|
|
|
103
|
+
| `APIError` | 网络异常 / 5xx 重试耗尽 |
|
|
104
|
+
|
|
105
|
+
**这正是 SDK 的价值**:把"该重试"和"该换条件"分开,不用自己猜。
|
|
106
|
+
|
|
107
|
+
## 六、排错顺序(省时间)
|
|
108
|
+
|
|
109
|
+
1. **401 还是 429?** → 缺 Key 还是超频(两者处理完全不同)
|
|
110
|
+
2. **HTTP 200 但没数据?** → 看 `ok` 与 `data`:`ok:false`+空 = 无数据(换条件);`ok:false`+有内容 = 上游失败(重试)
|
|
111
|
+
3. **403?** → 是不是把 Key 给多人用了(IP 维度)
|
|
112
|
+
4. **422?** → 看返回说明,多半少传了必填参数(如 `code`)
|
|
113
|
+
5. **数据看着不对?** → 先查单位与日期(见 `fields.md`),90% 是单位/日期问题,不是接口问题
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# 字段语义与单位(算数前必读)
|
|
2
|
+
|
|
3
|
+
**为什么单独一份**:这些是最容易踩的坑 —— **不看会算错**(单位错 100 倍、`+` 拼成字符串)。
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 一、单位(最容易错)
|
|
8
|
+
|
|
9
|
+
| 字段 | 单位 | 示例 |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| `volume` | **手**(1 手 = 100 股)| `volume: 12345` = 123.45 万股 |
|
|
12
|
+
| `amount` | **元** | `amount: 135160431.2` = 1.35 亿元(**不是万元**!)|
|
|
13
|
+
| `turnover` | **百分数**(换手率%)| `turnover: 1.88` = 换手 1.88%(与 `/v1/snapshot` **同名同值**)|
|
|
14
|
+
| `instBuyRate` / `netBuyRate` | **百分数** | `20` 表示 **20%**(不是 0.2)|
|
|
15
|
+
| `pe` / `pb` / `ps` | 倍数 | `pe: 18.5` = 18.5 倍 |
|
|
16
|
+
| `totalMV` / 市值类 | 看字段名后缀 | 一般**元**或**亿元** —— 用前先看一条真实数据对量级 |
|
|
17
|
+
|
|
18
|
+
**自检方法**:拿一个熟悉的股票对量级。比如茅台现价 ~1300 元、市值 ~1.6 万亿 —— 如果算出来是 1.6 亿,就是单位错了 10000 倍。
|
|
19
|
+
|
|
20
|
+
## 二、数字常以**字符串**返回
|
|
21
|
+
|
|
22
|
+
上游很多金额字段是**字符串**:
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
net = row["netBuyAmt"] # "135160431.2" ← 字符串!
|
|
26
|
+
# ❌ net / 1e8 → TypeError
|
|
27
|
+
# ❌ "100" + net → 拼字符串 "100135160431.2"
|
|
28
|
+
net = float(row["netBuyAmt"]) # ✅ 先转 float 再算
|
|
29
|
+
print(round(net / 1e8, 2), "亿元")
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
**通用做法**:拿不准就先 `float()`;用 pandas 时 `df["col"].astype(float)`。
|
|
33
|
+
|
|
34
|
+
## 三、日期语义(别把昨天当今天)
|
|
35
|
+
|
|
36
|
+
- **`date` 字段 = 数据所属交易日**(不是查询时间)
|
|
37
|
+
- **休市日不会变成"今天"**:周六查 `quote`,返回的是**上一交易日**的数据,`date` 会如实标出
|
|
38
|
+
- **`quote` 的最新一根**(`data[0]`)就是当日实时(交易时段内)或当日收盘(收盘后)
|
|
39
|
+
- **`ok:false` 时 `data` 可能为空**(无数据),此时**没有** `date` 可读
|
|
40
|
+
|
|
41
|
+
**用法**:要判断"这是不是今天的行情",看 `date` **而不是**看"我刚查的"。
|
|
42
|
+
|
|
43
|
+
## 四、返回结构:`data` 的四种形状(`structured` 是**条件字段**)
|
|
44
|
+
|
|
45
|
+
统一信封**只有 6 个键**:
|
|
46
|
+
|
|
47
|
+
```json
|
|
48
|
+
{ "ok": true, "endpoint": "quote", "tier": "free", "elapsed_ms": 42, "source": "multi",
|
|
49
|
+
"data": [ {"date": "2026-09-24", "last": "19.41", "turnover": "0.54"}, ... ] }
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
⚠️ **`structured` / `tables` 不是标配** —— **只有 `data` 是 Markdown 文本时才附**
|
|
53
|
+
(`market-overview` / `changedist` / `lhb` / `sector` / `sector-valuation` / `bond` / `etf` / `screen` / `macro`)。
|
|
54
|
+
**其余端点没有这两个键** ⇒ **代码写 `body.get("structured")`,不要写 `body["structured"]`**(会 KeyError)。
|
|
55
|
+
|
|
56
|
+
`data` 本身的形状有 **4 种**(判别 + 通吃写法见 `endpoints.md` §返回形态):
|
|
57
|
+
|
|
58
|
+
| `data` 形状 | 哪些端点 | 怎么读 |
|
|
59
|
+
|---|---|---|
|
|
60
|
+
| 对象数组 `[{"col": val}]` | 多数(`quote` `kline` `hot` `snapshot` `profile` …)| `data[0]["field"]` |
|
|
61
|
+
| 表列表 `[[行...], [行...]]` | `finance` | `data[表号][行号]["字段"]`(先按表索引)|
|
|
62
|
+
| 多段结构 `{tables:[{title,slug,rows}], data:{slug:rows}}` | `shareholder` · `calendar` | `data["tables"]` / `data["data"]["<slug>"]` |
|
|
63
|
+
| Markdown 字符串 | 上述 9 个 + `events` · `dehydrated` | 优先读同响应的 `structured` / `tables`(若存在)|
|
|
64
|
+
|
|
65
|
+
**纪律**:**程序化处理先判 `data` 形状**;**只有** Markdown 端点才退回 `structured`,**不要正则解析 Markdown**。
|
|
66
|
+
|
|
67
|
+
## 五、字段命名规律(好记)
|
|
68
|
+
|
|
69
|
+
| 后缀 / 词 | 含义 |
|
|
70
|
+
|---|---|
|
|
71
|
+
| `_TTM` | 滚动 12 个月(如 `ROETTM` / `PE_TTM`)|
|
|
72
|
+
| `YOY` | 同比(`NPParentCompanyYOY` = 归母净利同比)|
|
|
73
|
+
| `MOM` | 环比 |
|
|
74
|
+
| `Amt` | 金额(amount)|
|
|
75
|
+
| `Cnt` / `count` | 数量 |
|
|
76
|
+
| `Rate` / `Ratio` | **比率(多為百分数)**|
|
|
77
|
+
| `MV` | 市值(market value)|
|
|
78
|
+
| `NAV` / `NAPS` | 净值 / 每股净资产 |
|
|
79
|
+
|
|
80
|
+
## 六、几个具体端点的字段要点
|
|
81
|
+
|
|
82
|
+
| 端点 | 要点 |
|
|
83
|
+
|---|---|
|
|
84
|
+
| `fund` | 主力的**当日/5/10/20 日**净流入是不同字段;龙虎榜营业部明细在子结构里 |
|
|
85
|
+
| `lhb` | `activeseat` 的 `code` 与 `stockName` 是**分号分隔的等长列表**(`sh600127;sh600664`)→ `split(";")` 后按下标对应 |
|
|
86
|
+
| `changedist` | 是**当期**口径;`market-overview?type=updown` 是 **T-1** 口径,**两者数值会不同**(别混用)|
|
|
87
|
+
| `kline` | `period` 只支持 day/week/month;`count` 是根数 |
|
|
88
|
+
| `screen` | 返回的是股票列表(含命中因子值),排序用 `orderby` |
|
|
89
|
+
|
|
90
|
+
## 七、算数前检查清单
|
|
91
|
+
|
|
92
|
+
1. 单位对不对?(手/股 · 元/万元/亿元 · 百分数/小数)
|
|
93
|
+
2. 是不是字符串?(`float()` 了吗)
|
|
94
|
+
3. 日期是哪一天?(看 `date`)
|
|
95
|
+
4. 读的是 `structured` 还是 `data`?
|
|
96
|
+
5. 空结果 ≠ 0 —— 是"没有数据",不是"值为 0"
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# 官方 SDK(Python `pip install ashareapi` · Node.js `npm install ashareapi`)
|
|
2
|
+
|
|
3
|
+
**什么时候用 SDK 而不是直接打 HTTP**:
|
|
4
|
+
- ✅ **要写代码**(Python 脚本 / notebook / 回测 · Node 服务 / Next.js / 脚本)→ 用 SDK(省掉重试、异常分类、字段解析)
|
|
5
|
+
- ✅ Python 要成 **pandas DataFrame**(直接算 / 画图)→ 用 Python SDK
|
|
6
|
+
- ✅ Node 要 **TypeScript 类型**(编辑器补全 32 个方法与参数)→ 用 Node SDK
|
|
7
|
+
- ❌ 只是**取一个数看一眼** → 直接 `curl` / `fetch` 更快
|
|
8
|
+
- ❌ 用 **Go / Java / C# 等**(暂无官方 SDK)→ 直接调 HTTP(见 `endpoints.md`)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 一、安装
|
|
13
|
+
|
|
14
|
+
**Python**(要求 3.9+;强依赖只有 `requests`,pandas 可选):
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install ashareapi # 基础:返回 list[dict]
|
|
18
|
+
pip install "ashareapi[pandas]" # 加 DataFrame 支持(推荐)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Node.js / TypeScript**(要求 Node ≥ 18;**零运行时依赖**,用原生 `fetch`):
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install ashareapi
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
> ⚠️ Node SDK **只在服务端用**(Node / Next.js 服务端 / 云函数)—— 浏览器里会暴露你的 API Key,它刻意不提供浏览器构建。
|
|
28
|
+
|
|
29
|
+
## 二、快速开始
|
|
30
|
+
|
|
31
|
+
**Python**:
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from ashareapi import AShareAPI
|
|
35
|
+
|
|
36
|
+
cli = AShareAPI() # 免费端点无需 Key
|
|
37
|
+
df = cli.quote("sh600667") # 实时行情 → DataFrame
|
|
38
|
+
print(df[["date", "last", "turnover"]])
|
|
39
|
+
|
|
40
|
+
print(cli.kline("600667.SH", count=5)) # 代码格式随便写(自动归一化)
|
|
41
|
+
print(cli.hot(limit=10))
|
|
42
|
+
print(cli.changedist()) # 涨跌分布(市场广度)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
**Node.js / TypeScript**(ESM 与 CommonJS 都支持):
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { AShareAPI } from "ashareapi"; // CJS: const { AShareAPI } = require("ashareapi")
|
|
49
|
+
|
|
50
|
+
const cli = new AShareAPI(); // 免费端点无需 Key
|
|
51
|
+
const bars = await cli.quote("sh600667"); // → 对象数组
|
|
52
|
+
console.log(bars[0]); // { date, open, last, high, low, volume, amount, turnover }
|
|
53
|
+
|
|
54
|
+
console.log(await cli.kline("600667.SH", "day", 5));
|
|
55
|
+
console.log(await cli.hot(10));
|
|
56
|
+
console.log(await cli.changedist());
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
**付费端点**:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
# Python
|
|
63
|
+
cli = AShareAPI("ct-你的Key") # 或环境变量 ASHARE_API_KEY
|
|
64
|
+
print(cli.fund("sh600667")) # 资金流 + 龙虎榜 + 大宗 + 两融
|
|
65
|
+
print(cli.finance("sh600667")) # 三大报表
|
|
66
|
+
print(cli.lhb("institution")) # 龙虎榜机构榜
|
|
67
|
+
print(cli.screen(preset="low_pe", orderby="ROETTM", limit=10))
|
|
68
|
+
print(cli.industry_chain(mode="stock", code="sh600667"))
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
// Node.js
|
|
73
|
+
const cli = new AShareAPI({ apiKey: "ct-你的Key" }); // 或 process.env.ASHARE_API_KEY
|
|
74
|
+
console.log(await cli.fund("sh600667"));
|
|
75
|
+
console.log(await cli.finance("sh600667"));
|
|
76
|
+
console.log(await cli.lhb("institution"));
|
|
77
|
+
console.log(await cli.screen("", "low_pe", 10, "ROETTM")); // expr, preset, limit, orderby
|
|
78
|
+
console.log(await cli.industryChain("stock", "", "sh600667"));
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**环境变量**(推荐,别把 Key 写死在代码里):
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
export ASHARE_API_KEY=ct-你的Key # Linux/macOS
|
|
85
|
+
set ASHARE_API_KEY=ct-你的Key # Windows cmd
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## 三、代码格式:三种写法都认
|
|
89
|
+
|
|
90
|
+
两个 SDK 内部都会把它们统一成 `sh600667`:
|
|
91
|
+
|
|
92
|
+
| 你写的 | 结果 |
|
|
93
|
+
|---|---|
|
|
94
|
+
| `sh600667` | `sh600667` |
|
|
95
|
+
| `600667.SH` | `sh600667` |
|
|
96
|
+
| `600667` | `sh600667`(按首位推断:5/6→沪 · 0/3→深 · 4/8→北)|
|
|
97
|
+
|
|
98
|
+
→ **从别处迁过来的代码基本不用改**。
|
|
99
|
+
|
|
100
|
+
## 四、32 个方法(与 HTTP 端点 1:1)
|
|
101
|
+
|
|
102
|
+
| 端点 | Python | Node.js |
|
|
103
|
+
|---|---|---|
|
|
104
|
+
| `/v1/quote` | `quote(code)` | `quote(code)` |
|
|
105
|
+
| `/v1/kline` | `kline(code, period, count)` | `kline(code, period, count)` |
|
|
106
|
+
| `/v1/hot` | `hot(limit)` | `hot(limit)` |
|
|
107
|
+
| `/v1/market-overview` | `market_overview(type)` | `marketOverview(type)` · 别名 `market_overview` |
|
|
108
|
+
| `/v1/changedist` | `changedist()` | `changedist()` |
|
|
109
|
+
| `/v1/health` · `/v1/challenge` · `/v1/usage` | `health()` `challenge()` `usage()` | 同 |
|
|
110
|
+
| `/v1/fund` | `fund(code)` | `fund(code)` |
|
|
111
|
+
| `/v1/lhb` | `lhb(type, date)` | `lhb(type, date)` |
|
|
112
|
+
| `/v1/margin-trade` | `margin_trade(code, date)` | `marginTrade(code, date)` · 别名 `margin_trade` |
|
|
113
|
+
| `/v1/block-trade` | `block_trade(code, date)` | `blockTrade(code, date)` · 别名 `block_trade` |
|
|
114
|
+
| `/v1/finance` | `finance(code, num)` | `finance(code, num)` |
|
|
115
|
+
| `/v1/dividend` · `/v1/shareholder` | `dividend(code, years)` `shareholder(code)` | 同 |
|
|
116
|
+
| `/v1/technical` · `/v1/chip` · `/v1/profile` · `/v1/search` | `technical(code)` `chip(code)` `profile(code)` `search(q)` | 同 |
|
|
117
|
+
| **`/v1/orderbook`**|`orderbook(code)` | 同(五档盘口·**盘中**·量单位=手)|
|
|
118
|
+
| **`/v1/snapshot`**|`snapshot(code)` | 同(全字段画像·**仅 A 股**·付费)|
|
|
119
|
+
| `/v1/events` · `/v1/calendar` | `events(code)` `calendar(date, limit)` | 同 |
|
|
120
|
+
| `/v1/sector` | `sector()` | `sector()` |
|
|
121
|
+
| `/v1/sector-valuation` | `sector_valuation(code)` | `sectorValuation(code)` · 别名 `sector_valuation` |
|
|
122
|
+
| `/v1/industry-chain` | `industry_chain(mode, topic, code)` | `industryChain(mode, topic, code)` · 别名 `industry_chain` |
|
|
123
|
+
| `/v1/bond` · `/v1/etf` · `/v1/ipo` | `bond(code)` `etf(code)` `ipo(days)` | 同 |
|
|
124
|
+
| `/v1/screen` | `screen(expr, preset, limit, orderby, desc, market)` | 同(位置参数)|
|
|
125
|
+
| `/v1/macro` | `macro(region, names)` | `macro(region, names)` |
|
|
126
|
+
| `/v1/dehydrated` | `dehydrated(mode, symbol, limit)` | `dehydrated(mode, symbol, limit)` |
|
|
127
|
+
|
|
128
|
+
> **方法是否与线上一致?** 两个 SDK 都有**覆盖守护测试**:拉 `/openapi.json` 比对方法名,端点增减即测试红灯。
|
|
129
|
+
|
|
130
|
+
## 五、返回形态(两者不同,注意)
|
|
131
|
+
|
|
132
|
+
⚠️ **端点返回的不总是"一张表"** —— `data` 有 **4 种形状**(对象数组 / 表列表 / 多段结构 / Markdown 文本,见 `endpoints.md`)。
|
|
133
|
+
SDK **只把「对象数组」转成 DataFrame**,其余**原样返回**(不强行套成 DataFrame):
|
|
134
|
+
|
|
135
|
+
| 返回形状 | Python | Node.js |
|
|
136
|
+
|---|---|---|
|
|
137
|
+
| 对象数组(多数端点)| `pandas.DataFrame`(装了 pandas)/ `list[dict]` | **对象数组 `Row[]`** |
|
|
138
|
+
| 表列表(`finance`)| **`list[list[dict]]` 原样返回**(不套 DataFrame)| `TableList` = `Row[][]` |
|
|
139
|
+
| 多段结构(`shareholder` · `calendar`)| **`dict` 原样返回** | `SectionedResult` = `{tables, data}` |
|
|
140
|
+
| 完整信封 | `raw=True` | `new AShareAPI({ raw: true })` |
|
|
141
|
+
| 同步性 | **同步** | **全部返回 Promise**(要 `await`)|
|
|
142
|
+
|
|
143
|
+
**字段语义与单位**(手/元/百分数)见 `fields.md` —— 两个 SDK **都不会**帮你换算单位。
|
|
144
|
+
|
|
145
|
+
## 六、异常(5 类,各自告诉你做什么)
|
|
146
|
+
|
|
147
|
+
**Python**:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from ashareapi import (AShareAPI, AShareError, AuthError, RateLimitError,
|
|
151
|
+
UpstreamError, EmptyResultError, APIError)
|
|
152
|
+
|
|
153
|
+
try:
|
|
154
|
+
df = cli.fund("sh600667")
|
|
155
|
+
except AuthError as e: # 401/403 → 付费端点缺 Key / IP 维度限制
|
|
156
|
+
print(e)
|
|
157
|
+
except RateLimitError as e: # 429 → 降频 · 解 PoW 提额 · 升级档位
|
|
158
|
+
print(e)
|
|
159
|
+
except UpstreamError as e: # ok:false → 上游失败(已换源、不扣次数)→ 重试一次
|
|
160
|
+
print(e)
|
|
161
|
+
except EmptyResultError as e: # 当前无数据(如当天无大宗交易)→ 不计费,换条件
|
|
162
|
+
print(e)
|
|
163
|
+
except APIError as e: # 网络 / 5xx 重试耗尽
|
|
164
|
+
print(e)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
**Node.js**(同名 5 类,`instanceof` 判定):
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
import { AShareAPI, AuthError, RateLimitError, UpstreamError, EmptyResultError } from "ashareapi";
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
const rows = await cli.fund("sh600667");
|
|
174
|
+
} catch (e) {
|
|
175
|
+
if (e instanceof AuthError) console.log(e.message); // 401/403
|
|
176
|
+
else if (e instanceof RateLimitError) console.log(e.message); // 429
|
|
177
|
+
else if (e instanceof UpstreamError) console.log(e.message); // ok:false → 重试一次
|
|
178
|
+
else if (e instanceof EmptyResultError) console.log(e.message);// 当前无数据 → 不计费
|
|
179
|
+
else throw e;
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
全部继承自 `AShareError`(Python 要一把抓就 `except AShareError`)。
|
|
184
|
+
|
|
185
|
+
**`EmptyResultError` 是 SDK 相对裸 HTTP 的独有改进**:把"**没有数据**"和"**取数失败**"分开,避免把"今天没大宗交易"当故障反复重试(细节见 `errors.md`)。
|
|
186
|
+
|
|
187
|
+
## 七、SDK vs 直接 HTTP vs 其他数据源
|
|
188
|
+
|
|
189
|
+
| | 官方 SDK | 自己 requests / fetch | 其他开源库 |
|
|
190
|
+
|---|---|---|---|
|
|
191
|
+
| 上手 | **装完即用** | 自己封装重试/异常 | 装完即用 |
|
|
192
|
+
| 返回 | **DataFrame**(Python)/ **对象数组**(Node)| 自己解析 | DataFrame |
|
|
193
|
+
| 免费试用 | **5 端点免 Key** | 同样免 Key | 视来源 |
|
|
194
|
+
| 错误处理 | **5 类异常**(含"无数据≠失败")| 自己判断 | 自己判断 |
|
|
195
|
+
| 维护 | **我们维护**(多源自动切换)| 上游改了你改 | 上游改版常需跟进 |
|
|
196
|
+
| 依赖 | Python: `requests`(pandas 可选)/ Node: **零依赖** | 同 | 视库 |
|
|
197
|
+
|
|
198
|
+
## 八、边界(诚实)
|
|
199
|
+
|
|
200
|
+
- **K 线只有日/周/月**,**没有分钟级**(`period` 仅 `day`/`week`/`month`)
|
|
201
|
+
- **新闻/公告全文**不在 API 范围(有 `dehydrated` 研报摘要、`events` 事件标签)
|
|
202
|
+
- **不含投资建议** —— SDK 只负责取数
|
|
203
|
+
- **Node SDK 不要在浏览器里用**(会暴露 Key)
|
|
204
|
+
- 版本记录与更新:https://ashareapi.com/changelog
|