aquery-sdk 1.0.0__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 superxu520
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.
@@ -0,0 +1,4 @@
1
+ include README.md
2
+ include LICENSE
3
+ include requirements.txt
4
+ recursive-include aquery *.py
@@ -0,0 +1,276 @@
1
+ Metadata-Version: 2.4
2
+ Name: aquery-sdk
3
+ Version: 1.0.0
4
+ Summary: A-Query Open API Python SDK - A股20年全量数据查询服务
5
+ Author: superxu520
6
+ License: MIT
7
+ Project-URL: Homepage, https://huggingface.co/spaces/superxu520/A-query
8
+ Project-URL: Documentation, https://huggingface.co/spaces/superxu520/A-query
9
+ Project-URL: Repository, https://huggingface.co/spaces/superxu520/A-query
10
+ Project-URL: Issues, https://huggingface.co/spaces/superxu520/A-query/discussions
11
+ Keywords: aquery,a-share,stock,finance,tushare,quant
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Intended Audience :: Financial and Insurance Industry
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.8
19
+ Classifier: Programming Language :: Python :: 3.9
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Topic :: Office/Business :: Financial
24
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
25
+ Requires-Python: >=3.8
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Requires-Dist: pandas>=1.3.0
29
+ Requires-Dist: requests>=2.25.0
30
+ Provides-Extra: dev
31
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
32
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
33
+ Requires-Dist: black>=22.0.0; extra == "dev"
34
+ Requires-Dist: flake8>=4.0.0; extra == "dev"
35
+ Dynamic: license-file
36
+
37
+ ---
38
+ title: A-Query Open API
39
+ emoji: 📊
40
+ colorFrom: blue
41
+ colorTo: green
42
+ sdk: docker
43
+ pinned: false
44
+ ---
45
+
46
+ # A-Query Open API
47
+
48
+ A股20年全量数据查询服务 - 统一API接口
49
+
50
+ ## 项目概述
51
+
52
+ 基于Hugging Face数据集构建的A股数据查询服务,提供统一、高效、易用的数据查询接口。
53
+
54
+ **数据范围**: 2000年-2026年,覆盖全部A股上市公司
55
+ **数据来源**: Tushare Pro
56
+ **存储位置**: Hugging Face Datasets
57
+
58
+ ## 快速开始
59
+
60
+ ### 1. 安装客户端
61
+
62
+ ```bash
63
+ pip install pandas requests
64
+ # 然后将client文件夹复制到你的项目中
65
+ ```
66
+
67
+ ### 2. 使用Python SDK
68
+
69
+ ```python
70
+ from client import AQueryClient
71
+
72
+ # 初始化客户端
73
+ client = AQueryClient(token="your_token")
74
+
75
+ # 查询股票基础信息
76
+ df = client.stock_basic(market="主板")
77
+ print(df.head())
78
+
79
+ # 查询个股历史数据
80
+ df = client.daily_history(
81
+ ts_code="000001.SZ",
82
+ start_date=20240101,
83
+ end_date=20241231,
84
+ fields=["trade_date", "open", "close", "vol", "pe"]
85
+ )
86
+ print(df)
87
+
88
+ # 查询某日全市场数据
89
+ df = client.daily_all(
90
+ trade_date=20240320,
91
+ fields=["ts_code", "close", "pe", "net_mf_amount"]
92
+ )
93
+ print(df.head(10))
94
+ ```
95
+
96
+ ### 3. HTTP API调用
97
+
98
+ ```bash
99
+ curl -X POST https://your-space.hf.space/api/v1/query \
100
+ -H "Content-Type: application/json" \
101
+ -H "X-Token: your_token" \
102
+ -d '{
103
+ "api_name": "daily_history",
104
+ "params": {
105
+ "ts_code": "000001.SZ",
106
+ "start_date": 20240101,
107
+ "end_date": 20240131
108
+ },
109
+ "fields": "trade_date,open,high,low,close,vol"
110
+ }'
111
+ ```
112
+
113
+ ## 支持的API列表
114
+
115
+ | api_name | 数据集 | 说明 | 主要参数 |
116
+ |----------|--------|------|----------|
117
+ | stock_basic | A_meta_info | 股票基础信息 | ts_code, market |
118
+ | trade_cal | A_meta_info | 交易日历 | start_date, end_date |
119
+ | company_info | A_meta_info | 公司信息 | ts_code |
120
+ | industry_sw | A_meta_info | 申万行业映射 | ts_code, industry_level |
121
+ | industry_sw_class | A_meta_info | 申万行业分类 | level |
122
+ | fina_indicator | A_meta_info | 财务指标 | ts_code, end_date |
123
+ | daily | A_by_date | 日线行情 | ts_code, trade_date, start_date, end_date |
124
+ | daily_all | A_by_date | 某日全市场 | trade_date |
125
+ | daily_history | A_by_code | 个股历史 | ts_code, start_date, end_date |
126
+
127
+ ## 统一接口规范
128
+
129
+ ### 请求格式
130
+
131
+ ```json
132
+ {
133
+ "api_name": "daily_history",
134
+ "token": "your_token",
135
+ "params": {
136
+ "ts_code": "000001.SZ",
137
+ "start_date": 20240101,
138
+ "end_date": 20241231
139
+ },
140
+ "fields": "trade_date,open,close,vol"
141
+ }
142
+ ```
143
+
144
+ ### 响应格式
145
+
146
+ ```json
147
+ {
148
+ "code": 0,
149
+ "msg": "success",
150
+ "data": {
151
+ "fields": ["trade_date", "open", "close", "vol"],
152
+ "items": [
153
+ [20240102, 9.5, 9.7, 1000000],
154
+ [20240103, 9.7, 9.8, 1200000]
155
+ ],
156
+ "count": 2,
157
+ "has_more": false
158
+ },
159
+ "request_id": "req_abc123"
160
+ }
161
+ ```
162
+
163
+ ## 本地部署
164
+
165
+ ### 1. 克隆项目
166
+
167
+ ```bash
168
+ git clone <repository-url>
169
+ cd a-query
170
+ ```
171
+
172
+ ### 2. 安装依赖
173
+
174
+ ```bash
175
+ pip install -r requirements.txt
176
+ ```
177
+
178
+ ### 3. 运行服务
179
+
180
+ ```bash
181
+ python -m uvicorn app.main:app --host 0.0.0.0 --port 7860 --reload
182
+ ```
183
+
184
+ ### 4. Docker部署
185
+
186
+ ```bash
187
+ docker build -t aquery-api .
188
+ docker run -p 7860:7860 aquery-api
189
+ ```
190
+
191
+ ## Hugging Face Space部署
192
+
193
+ 1. 在Hugging Face创建新的Space,选择Docker模板
194
+ 2. 将代码推送到Space仓库
195
+ 3. 配置环境变量(如果需要)
196
+ 4. 访问Space URL查看API文档
197
+
198
+ ## 查询示例
199
+
200
+ ### 示例1: 获取某股票历史数据
201
+
202
+ ```python
203
+ df = client.daily_history(
204
+ ts_code="000001.SZ",
205
+ start_date=20240101,
206
+ end_date=20241231
207
+ )
208
+ ```
209
+
210
+ ### 示例2: 获取某日全市场数据
211
+
212
+ ```python
213
+ df = client.daily_all(
214
+ trade_date=20240320,
215
+ fields=["ts_code", "close", "pe", "net_mf_amount", "total_mv"]
216
+ )
217
+ ```
218
+
219
+ ### 示例3: 条件选股
220
+
221
+ ```python
222
+ # 查询某日PE<20且净流入>1亿的股票
223
+ df = client.daily_all(trade_date=20240320)
224
+ df = df[(df['pe'] < 20) & (df['net_mf_amount'] > 1e8)]
225
+ ```
226
+
227
+ ### 示例4: 获取财务指标
228
+
229
+ ```python
230
+ df = client.fina_indicator(
231
+ ts_code="000001.SZ",
232
+ fields=["end_date", "roe", "eps", "grossprofit_margin"]
233
+ )
234
+ ```
235
+
236
+ ## 性能指标
237
+
238
+ | 场景 | 数据量 | 响应时间 |
239
+ |------|--------|----------|
240
+ | 股票基础信息 | 5,200条 | < 50ms |
241
+ | 个股全历史 | 5,000条 | < 200ms |
242
+ | 单日全市场 | 5,200条 | < 100ms |
243
+ | 时间段截面 | 100万条 | < 2s |
244
+
245
+ ## 项目结构
246
+
247
+ ```
248
+ .
249
+ ├── app/ # FastAPI服务端
250
+ │ ├── main.py # 应用入口
251
+ │ ├── core/ # 配置、认证
252
+ │ ├── routers/ # API路由
253
+ │ └── services/ # 查询服务
254
+ ├── client/ # Python SDK
255
+ │ └── aquery.py # 客户端实现
256
+ ├── tests/ # 测试
257
+ ├── Dockerfile # 容器配置
258
+ ├── requirements.txt # Python依赖
259
+ └── README.md # 本文档
260
+ ```
261
+
262
+ ## 技术栈
263
+
264
+ - **FastAPI**: 高性能Web框架
265
+ - **DuckDB**: 嵌入式OLAP查询引擎
266
+ - **Polars**: 极速数据处理
267
+ - **Hugging Face Datasets**: 数据存储
268
+
269
+ ## 许可证
270
+
271
+ MIT License
272
+
273
+ ## 联系方式
274
+
275
+ - 数据集: https://huggingface.co/datasets/superxu520/
276
+ - 问题反馈: 请提交GitHub Issue
@@ -0,0 +1,240 @@
1
+ ---
2
+ title: A-Query Open API
3
+ emoji: 📊
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: docker
7
+ pinned: false
8
+ ---
9
+
10
+ # A-Query Open API
11
+
12
+ A股20年全量数据查询服务 - 统一API接口
13
+
14
+ ## 项目概述
15
+
16
+ 基于Hugging Face数据集构建的A股数据查询服务,提供统一、高效、易用的数据查询接口。
17
+
18
+ **数据范围**: 2000年-2026年,覆盖全部A股上市公司
19
+ **数据来源**: Tushare Pro
20
+ **存储位置**: Hugging Face Datasets
21
+
22
+ ## 快速开始
23
+
24
+ ### 1. 安装客户端
25
+
26
+ ```bash
27
+ pip install pandas requests
28
+ # 然后将client文件夹复制到你的项目中
29
+ ```
30
+
31
+ ### 2. 使用Python SDK
32
+
33
+ ```python
34
+ from client import AQueryClient
35
+
36
+ # 初始化客户端
37
+ client = AQueryClient(token="your_token")
38
+
39
+ # 查询股票基础信息
40
+ df = client.stock_basic(market="主板")
41
+ print(df.head())
42
+
43
+ # 查询个股历史数据
44
+ df = client.daily_history(
45
+ ts_code="000001.SZ",
46
+ start_date=20240101,
47
+ end_date=20241231,
48
+ fields=["trade_date", "open", "close", "vol", "pe"]
49
+ )
50
+ print(df)
51
+
52
+ # 查询某日全市场数据
53
+ df = client.daily_all(
54
+ trade_date=20240320,
55
+ fields=["ts_code", "close", "pe", "net_mf_amount"]
56
+ )
57
+ print(df.head(10))
58
+ ```
59
+
60
+ ### 3. HTTP API调用
61
+
62
+ ```bash
63
+ curl -X POST https://your-space.hf.space/api/v1/query \
64
+ -H "Content-Type: application/json" \
65
+ -H "X-Token: your_token" \
66
+ -d '{
67
+ "api_name": "daily_history",
68
+ "params": {
69
+ "ts_code": "000001.SZ",
70
+ "start_date": 20240101,
71
+ "end_date": 20240131
72
+ },
73
+ "fields": "trade_date,open,high,low,close,vol"
74
+ }'
75
+ ```
76
+
77
+ ## 支持的API列表
78
+
79
+ | api_name | 数据集 | 说明 | 主要参数 |
80
+ |----------|--------|------|----------|
81
+ | stock_basic | A_meta_info | 股票基础信息 | ts_code, market |
82
+ | trade_cal | A_meta_info | 交易日历 | start_date, end_date |
83
+ | company_info | A_meta_info | 公司信息 | ts_code |
84
+ | industry_sw | A_meta_info | 申万行业映射 | ts_code, industry_level |
85
+ | industry_sw_class | A_meta_info | 申万行业分类 | level |
86
+ | fina_indicator | A_meta_info | 财务指标 | ts_code, end_date |
87
+ | daily | A_by_date | 日线行情 | ts_code, trade_date, start_date, end_date |
88
+ | daily_all | A_by_date | 某日全市场 | trade_date |
89
+ | daily_history | A_by_code | 个股历史 | ts_code, start_date, end_date |
90
+
91
+ ## 统一接口规范
92
+
93
+ ### 请求格式
94
+
95
+ ```json
96
+ {
97
+ "api_name": "daily_history",
98
+ "token": "your_token",
99
+ "params": {
100
+ "ts_code": "000001.SZ",
101
+ "start_date": 20240101,
102
+ "end_date": 20241231
103
+ },
104
+ "fields": "trade_date,open,close,vol"
105
+ }
106
+ ```
107
+
108
+ ### 响应格式
109
+
110
+ ```json
111
+ {
112
+ "code": 0,
113
+ "msg": "success",
114
+ "data": {
115
+ "fields": ["trade_date", "open", "close", "vol"],
116
+ "items": [
117
+ [20240102, 9.5, 9.7, 1000000],
118
+ [20240103, 9.7, 9.8, 1200000]
119
+ ],
120
+ "count": 2,
121
+ "has_more": false
122
+ },
123
+ "request_id": "req_abc123"
124
+ }
125
+ ```
126
+
127
+ ## 本地部署
128
+
129
+ ### 1. 克隆项目
130
+
131
+ ```bash
132
+ git clone <repository-url>
133
+ cd a-query
134
+ ```
135
+
136
+ ### 2. 安装依赖
137
+
138
+ ```bash
139
+ pip install -r requirements.txt
140
+ ```
141
+
142
+ ### 3. 运行服务
143
+
144
+ ```bash
145
+ python -m uvicorn app.main:app --host 0.0.0.0 --port 7860 --reload
146
+ ```
147
+
148
+ ### 4. Docker部署
149
+
150
+ ```bash
151
+ docker build -t aquery-api .
152
+ docker run -p 7860:7860 aquery-api
153
+ ```
154
+
155
+ ## Hugging Face Space部署
156
+
157
+ 1. 在Hugging Face创建新的Space,选择Docker模板
158
+ 2. 将代码推送到Space仓库
159
+ 3. 配置环境变量(如果需要)
160
+ 4. 访问Space URL查看API文档
161
+
162
+ ## 查询示例
163
+
164
+ ### 示例1: 获取某股票历史数据
165
+
166
+ ```python
167
+ df = client.daily_history(
168
+ ts_code="000001.SZ",
169
+ start_date=20240101,
170
+ end_date=20241231
171
+ )
172
+ ```
173
+
174
+ ### 示例2: 获取某日全市场数据
175
+
176
+ ```python
177
+ df = client.daily_all(
178
+ trade_date=20240320,
179
+ fields=["ts_code", "close", "pe", "net_mf_amount", "total_mv"]
180
+ )
181
+ ```
182
+
183
+ ### 示例3: 条件选股
184
+
185
+ ```python
186
+ # 查询某日PE<20且净流入>1亿的股票
187
+ df = client.daily_all(trade_date=20240320)
188
+ df = df[(df['pe'] < 20) & (df['net_mf_amount'] > 1e8)]
189
+ ```
190
+
191
+ ### 示例4: 获取财务指标
192
+
193
+ ```python
194
+ df = client.fina_indicator(
195
+ ts_code="000001.SZ",
196
+ fields=["end_date", "roe", "eps", "grossprofit_margin"]
197
+ )
198
+ ```
199
+
200
+ ## 性能指标
201
+
202
+ | 场景 | 数据量 | 响应时间 |
203
+ |------|--------|----------|
204
+ | 股票基础信息 | 5,200条 | < 50ms |
205
+ | 个股全历史 | 5,000条 | < 200ms |
206
+ | 单日全市场 | 5,200条 | < 100ms |
207
+ | 时间段截面 | 100万条 | < 2s |
208
+
209
+ ## 项目结构
210
+
211
+ ```
212
+ .
213
+ ├── app/ # FastAPI服务端
214
+ │ ├── main.py # 应用入口
215
+ │ ├── core/ # 配置、认证
216
+ │ ├── routers/ # API路由
217
+ │ └── services/ # 查询服务
218
+ ├── client/ # Python SDK
219
+ │ └── aquery.py # 客户端实现
220
+ ├── tests/ # 测试
221
+ ├── Dockerfile # 容器配置
222
+ ├── requirements.txt # Python依赖
223
+ └── README.md # 本文档
224
+ ```
225
+
226
+ ## 技术栈
227
+
228
+ - **FastAPI**: 高性能Web框架
229
+ - **DuckDB**: 嵌入式OLAP查询引擎
230
+ - **Polars**: 极速数据处理
231
+ - **Hugging Face Datasets**: 数据存储
232
+
233
+ ## 许可证
234
+
235
+ MIT License
236
+
237
+ ## 联系方式
238
+
239
+ - 数据集: https://huggingface.co/datasets/superxu520/
240
+ - 问题反馈: 请提交GitHub Issue
@@ -0,0 +1,17 @@
1
+ """
2
+ A-Query Open API Python SDK
3
+
4
+ A股20年全量数据查询服务客户端
5
+
6
+ 快速开始:
7
+ >>> from aquery import AQueryClient
8
+ >>> client = AQueryClient(token="your_token")
9
+ >>> df = client.daily_history(ts_code="000001.SZ", start_date=20240101)
10
+
11
+ 文档: https://huggingface.co/spaces/superxu520/A-query
12
+ """
13
+ from .client import AQueryClient, AQueryError
14
+
15
+ __version__ = "1.0.0"
16
+ __author__ = "superxu520"
17
+ __all__ = ["AQueryClient", "AQueryError"]
@@ -0,0 +1,327 @@
1
+ """
2
+ A-Query Python Client SDK
3
+ 提供便捷的数据查询接口,自动转换DataFrame格式
4
+ """
5
+ import os
6
+ from typing import Dict, Any, Optional, List, Union
7
+ from urllib.parse import urljoin
8
+
9
+ import pandas as pd
10
+ import requests
11
+
12
+
13
+ class AQueryError(Exception):
14
+ """AQuery客户端异常"""
15
+ pass
16
+
17
+
18
+ class AQueryClient:
19
+ """
20
+ A-Query 数据查询客户端
21
+
22
+ Attributes:
23
+ token: API认证Token
24
+ base_url: API服务地址
25
+ timeout: 请求超时时间(秒)
26
+
27
+ Example:
28
+ >>> client = AQueryClient(token="your_token")
29
+ >>> # 查询股票基础信息
30
+ >>> df = client.stock_basic(market="主板")
31
+ >>> # 查询个股历史
32
+ >>> df = client.daily_history(ts_code="000001.SZ", start_date=20240101)
33
+ >>> # 查询某日全市场
34
+ >>> df = client.daily_all(trade_date=20240320)
35
+ """
36
+
37
+ def __init__(
38
+ self,
39
+ token: Optional[str] = None,
40
+ base_url: str = "https://superxu520-aquery.hf.space",
41
+ timeout: int = 60
42
+ ):
43
+ """
44
+ 初始化客户端
45
+
46
+ Args:
47
+ token: API认证Token,默认从环境变量AQUERY_TOKEN读取
48
+ base_url: API服务地址
49
+ timeout: 请求超时时间(秒)
50
+ """
51
+ self.token = token or os.getenv("AQUERY_TOKEN")
52
+ if not self.token:
53
+ raise AQueryError("请提供token参数或设置AQUERY_TOKEN环境变量")
54
+
55
+ self.base_url = base_url.rstrip("/")
56
+ self.timeout = timeout
57
+ self.session = requests.Session()
58
+ self.session.headers.update({
59
+ "Content-Type": "application/json",
60
+ "X-Token": self.token
61
+ })
62
+
63
+ def _request(
64
+ self,
65
+ method: str,
66
+ endpoint: str,
67
+ **kwargs
68
+ ) -> Dict[str, Any]:
69
+ """发送HTTP请求"""
70
+ url = urljoin(self.base_url, endpoint)
71
+
72
+ try:
73
+ response = self.session.request(
74
+ method=method,
75
+ url=url,
76
+ timeout=self.timeout,
77
+ **kwargs
78
+ )
79
+ response.raise_for_status()
80
+
81
+ data = response.json()
82
+
83
+ if data.get("code") != 0:
84
+ raise AQueryError(f"API错误: {data.get('msg', '未知错误')}")
85
+
86
+ return data
87
+
88
+ except requests.exceptions.Timeout:
89
+ raise AQueryError(f"请求超时(>{self.timeout}秒)")
90
+ except requests.exceptions.ConnectionError:
91
+ raise AQueryError(f"连接失败,请检查base_url: {self.base_url}")
92
+ except requests.exceptions.HTTPError as e:
93
+ raise AQueryError(f"HTTP错误: {e.response.status_code} - {e.response.text}")
94
+
95
+ def _to_dataframe(self, response: Dict[str, Any]) -> pd.DataFrame:
96
+ """将响应数据转换为DataFrame"""
97
+ data = response.get("data", {})
98
+ if "items" not in data or "fields" not in data:
99
+ raise AQueryError("响应数据格式错误,缺少items或fields字段")
100
+
101
+ df = pd.DataFrame(data["items"], columns=data["fields"])
102
+ # 附加元数据到DataFrame
103
+ df.attrs["count"] = response.get("count", 0)
104
+ df.attrs["has_more"] = response.get("has_more", False)
105
+ df.attrs["request_id"] = response.get("request_id")
106
+ return df
107
+
108
+ def query(
109
+ self,
110
+ api_name: str,
111
+ params: Optional[Dict[str, Any]] = None,
112
+ fields: Optional[Union[str, List[str]]] = None
113
+ ) -> pd.DataFrame:
114
+ """
115
+ 通用查询接口
116
+
117
+ Args:
118
+ api_name: 接口名称,如stock_basic, daily, daily_history等
119
+ params: 查询参数字典
120
+ fields: 返回字段列表,可以是逗号分隔字符串或列表
121
+
122
+ Returns:
123
+ pd.DataFrame: 查询结果
124
+
125
+ Example:
126
+ >>> df = client.query("stock_basic", params={"market": "主板"})
127
+ >>> df = client.query("daily_history",
128
+ ... params={"ts_code": "000001.SZ", "start_date": 20240101},
129
+ ... fields=["trade_date", "close", "pe"])
130
+ """
131
+ payload = {
132
+ "api_name": api_name,
133
+ "token": self.token,
134
+ "params": params or {}
135
+ }
136
+
137
+ if fields:
138
+ if isinstance(fields, list):
139
+ fields = ",".join(fields)
140
+ payload["fields"] = fields
141
+
142
+ result = self._request("POST", "/api/v1/query", json=payload)
143
+ return self._to_dataframe(result)
144
+
145
+ # ===== 便捷方法 =====
146
+
147
+ def stock_basic(
148
+ self,
149
+ ts_code: Optional[str] = None,
150
+ market: Optional[str] = None,
151
+ fields: Optional[List[str]] = None
152
+ ) -> pd.DataFrame:
153
+ """
154
+ 查询股票基础信息
155
+
156
+ Args:
157
+ ts_code: 股票代码,如000001.SZ
158
+ market: 市场类型,如主板/创业板/科创板/北交所
159
+ fields: 返回字段列表
160
+
161
+ Returns:
162
+ 包含ts_code, symbol, name, industry等字段的DataFrame
163
+ """
164
+ params = {}
165
+ if ts_code:
166
+ params["ts_code"] = ts_code
167
+ if market:
168
+ params["market"] = market
169
+
170
+ return self.query("stock_basic", params=params, fields=fields)
171
+
172
+ def trade_cal(
173
+ self,
174
+ start_date: Optional[int] = None,
175
+ end_date: Optional[int] = None,
176
+ is_open: Optional[int] = None,
177
+ exchange: str = "SSE",
178
+ fields: Optional[List[str]] = None
179
+ ) -> pd.DataFrame:
180
+ """
181
+ 查询交易日历
182
+
183
+ Args:
184
+ start_date: 开始日期,如20240101
185
+ end_date: 结束日期,如20241231
186
+ is_open: 是否交易日,1是/0否
187
+ exchange: 交易所,SSE/SZSE
188
+ fields: 返回字段列表
189
+ """
190
+ params = {"exchange": exchange}
191
+ if start_date:
192
+ params["start_date"] = start_date
193
+ if end_date:
194
+ params["end_date"] = end_date
195
+ if is_open is not None:
196
+ params["is_open"] = is_open
197
+
198
+ return self.query("trade_cal", params=params, fields=fields)
199
+
200
+ def daily(
201
+ self,
202
+ ts_code: str,
203
+ trade_date: Optional[int] = None,
204
+ start_date: Optional[int] = None,
205
+ end_date: Optional[int] = None,
206
+ fields: Optional[List[str]] = None
207
+ ) -> pd.DataFrame:
208
+ """
209
+ 查询日线行情
210
+
211
+ Args:
212
+ ts_code: 股票代码,如000001.SZ
213
+ trade_date: 特定交易日期
214
+ start_date: 开始日期
215
+ end_date: 结束日期
216
+ fields: 返回字段列表,如["trade_date", "open", "close", "vol"]
217
+ """
218
+ params = {"ts_code": ts_code}
219
+ if trade_date:
220
+ params["trade_date"] = trade_date
221
+ if start_date:
222
+ params["start_date"] = start_date
223
+ if end_date:
224
+ params["end_date"] = end_date
225
+
226
+ return self.query("daily", params=params, fields=fields)
227
+
228
+ def daily_history(
229
+ self,
230
+ ts_code: str,
231
+ start_date: Optional[int] = None,
232
+ end_date: Optional[int] = None,
233
+ fields: Optional[List[str]] = None
234
+ ) -> pd.DataFrame:
235
+ """
236
+ 查询个股历史数据(推荐用于回测)
237
+
238
+ Args:
239
+ ts_code: 股票代码
240
+ start_date: 开始日期
241
+ end_date: 结束日期
242
+ fields: 返回字段列表
243
+
244
+ Returns:
245
+ 按trade_date排序的历史数据
246
+ """
247
+ params = {"ts_code": ts_code}
248
+ if start_date:
249
+ params["start_date"] = start_date
250
+ if end_date:
251
+ params["end_date"] = end_date
252
+
253
+ return self.query("daily_history", params=params, fields=fields)
254
+
255
+ def daily_all(
256
+ self,
257
+ trade_date: int,
258
+ fields: Optional[List[str]] = None
259
+ ) -> pd.DataFrame:
260
+ """
261
+ 查询某日全市场数据(适合选股)
262
+
263
+ Args:
264
+ trade_date: 交易日期,如20240320
265
+ fields: 返回字段列表
266
+
267
+ Returns:
268
+ 该交易日所有股票的数据
269
+ """
270
+ params = {"trade_date": trade_date}
271
+ return self.query("daily_all", params=params, fields=fields)
272
+
273
+ def fina_indicator(
274
+ self,
275
+ ts_code: str,
276
+ end_date: Optional[int] = None,
277
+ fields: Optional[List[str]] = None
278
+ ) -> pd.DataFrame:
279
+ """
280
+ 查询财务指标
281
+
282
+ Args:
283
+ ts_code: 股票代码
284
+ end_date: 报告期,如20241231
285
+ fields: 返回字段列表,如["end_date", "roe", "eps", "grossprofit_margin"]
286
+ """
287
+ params = {"ts_code": ts_code}
288
+ if end_date:
289
+ params["end_date"] = end_date
290
+
291
+ return self.query("fina_indicator", params=params, fields=fields)
292
+
293
+ def industry_sw(
294
+ self,
295
+ ts_code: Optional[str] = None,
296
+ industry_level: Optional[str] = None,
297
+ fields: Optional[List[str]] = None
298
+ ) -> pd.DataFrame:
299
+ """
300
+ 查询申万行业分类映射
301
+
302
+ Args:
303
+ ts_code: 股票代码
304
+ industry_level: 行业级别,L1/L2/L3
305
+ fields: 返回字段列表
306
+ """
307
+ params = {}
308
+ if ts_code:
309
+ params["ts_code"] = ts_code
310
+ if industry_level:
311
+ params["industry_level"] = industry_level
312
+
313
+ return self.query("industry_sw", params=params, fields=fields)
314
+
315
+ def list_apis(self) -> List[Dict[str, Any]]:
316
+ """
317
+ 获取支持的API列表
318
+
319
+ Returns:
320
+ 包含所有API信息的列表
321
+ """
322
+ result = self._request("GET", "/api/v1/apis")
323
+ return result.get("apis", [])
324
+
325
+
326
+ # 向后兼容的别名
327
+ Client = AQueryClient
@@ -0,0 +1,276 @@
1
+ Metadata-Version: 2.4
2
+ Name: aquery-sdk
3
+ Version: 1.0.0
4
+ Summary: A-Query Open API Python SDK - A股20年全量数据查询服务
5
+ Author: superxu520
6
+ License: MIT
7
+ Project-URL: Homepage, https://huggingface.co/spaces/superxu520/A-query
8
+ Project-URL: Documentation, https://huggingface.co/spaces/superxu520/A-query
9
+ Project-URL: Repository, https://huggingface.co/spaces/superxu520/A-query
10
+ Project-URL: Issues, https://huggingface.co/spaces/superxu520/A-query/discussions
11
+ Keywords: aquery,a-share,stock,finance,tushare,quant
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Intended Audience :: Financial and Insurance Industry
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.8
19
+ Classifier: Programming Language :: Python :: 3.9
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Topic :: Office/Business :: Financial
24
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
25
+ Requires-Python: >=3.8
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Requires-Dist: pandas>=1.3.0
29
+ Requires-Dist: requests>=2.25.0
30
+ Provides-Extra: dev
31
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
32
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
33
+ Requires-Dist: black>=22.0.0; extra == "dev"
34
+ Requires-Dist: flake8>=4.0.0; extra == "dev"
35
+ Dynamic: license-file
36
+
37
+ ---
38
+ title: A-Query Open API
39
+ emoji: 📊
40
+ colorFrom: blue
41
+ colorTo: green
42
+ sdk: docker
43
+ pinned: false
44
+ ---
45
+
46
+ # A-Query Open API
47
+
48
+ A股20年全量数据查询服务 - 统一API接口
49
+
50
+ ## 项目概述
51
+
52
+ 基于Hugging Face数据集构建的A股数据查询服务,提供统一、高效、易用的数据查询接口。
53
+
54
+ **数据范围**: 2000年-2026年,覆盖全部A股上市公司
55
+ **数据来源**: Tushare Pro
56
+ **存储位置**: Hugging Face Datasets
57
+
58
+ ## 快速开始
59
+
60
+ ### 1. 安装客户端
61
+
62
+ ```bash
63
+ pip install pandas requests
64
+ # 然后将client文件夹复制到你的项目中
65
+ ```
66
+
67
+ ### 2. 使用Python SDK
68
+
69
+ ```python
70
+ from client import AQueryClient
71
+
72
+ # 初始化客户端
73
+ client = AQueryClient(token="your_token")
74
+
75
+ # 查询股票基础信息
76
+ df = client.stock_basic(market="主板")
77
+ print(df.head())
78
+
79
+ # 查询个股历史数据
80
+ df = client.daily_history(
81
+ ts_code="000001.SZ",
82
+ start_date=20240101,
83
+ end_date=20241231,
84
+ fields=["trade_date", "open", "close", "vol", "pe"]
85
+ )
86
+ print(df)
87
+
88
+ # 查询某日全市场数据
89
+ df = client.daily_all(
90
+ trade_date=20240320,
91
+ fields=["ts_code", "close", "pe", "net_mf_amount"]
92
+ )
93
+ print(df.head(10))
94
+ ```
95
+
96
+ ### 3. HTTP API调用
97
+
98
+ ```bash
99
+ curl -X POST https://your-space.hf.space/api/v1/query \
100
+ -H "Content-Type: application/json" \
101
+ -H "X-Token: your_token" \
102
+ -d '{
103
+ "api_name": "daily_history",
104
+ "params": {
105
+ "ts_code": "000001.SZ",
106
+ "start_date": 20240101,
107
+ "end_date": 20240131
108
+ },
109
+ "fields": "trade_date,open,high,low,close,vol"
110
+ }'
111
+ ```
112
+
113
+ ## 支持的API列表
114
+
115
+ | api_name | 数据集 | 说明 | 主要参数 |
116
+ |----------|--------|------|----------|
117
+ | stock_basic | A_meta_info | 股票基础信息 | ts_code, market |
118
+ | trade_cal | A_meta_info | 交易日历 | start_date, end_date |
119
+ | company_info | A_meta_info | 公司信息 | ts_code |
120
+ | industry_sw | A_meta_info | 申万行业映射 | ts_code, industry_level |
121
+ | industry_sw_class | A_meta_info | 申万行业分类 | level |
122
+ | fina_indicator | A_meta_info | 财务指标 | ts_code, end_date |
123
+ | daily | A_by_date | 日线行情 | ts_code, trade_date, start_date, end_date |
124
+ | daily_all | A_by_date | 某日全市场 | trade_date |
125
+ | daily_history | A_by_code | 个股历史 | ts_code, start_date, end_date |
126
+
127
+ ## 统一接口规范
128
+
129
+ ### 请求格式
130
+
131
+ ```json
132
+ {
133
+ "api_name": "daily_history",
134
+ "token": "your_token",
135
+ "params": {
136
+ "ts_code": "000001.SZ",
137
+ "start_date": 20240101,
138
+ "end_date": 20241231
139
+ },
140
+ "fields": "trade_date,open,close,vol"
141
+ }
142
+ ```
143
+
144
+ ### 响应格式
145
+
146
+ ```json
147
+ {
148
+ "code": 0,
149
+ "msg": "success",
150
+ "data": {
151
+ "fields": ["trade_date", "open", "close", "vol"],
152
+ "items": [
153
+ [20240102, 9.5, 9.7, 1000000],
154
+ [20240103, 9.7, 9.8, 1200000]
155
+ ],
156
+ "count": 2,
157
+ "has_more": false
158
+ },
159
+ "request_id": "req_abc123"
160
+ }
161
+ ```
162
+
163
+ ## 本地部署
164
+
165
+ ### 1. 克隆项目
166
+
167
+ ```bash
168
+ git clone <repository-url>
169
+ cd a-query
170
+ ```
171
+
172
+ ### 2. 安装依赖
173
+
174
+ ```bash
175
+ pip install -r requirements.txt
176
+ ```
177
+
178
+ ### 3. 运行服务
179
+
180
+ ```bash
181
+ python -m uvicorn app.main:app --host 0.0.0.0 --port 7860 --reload
182
+ ```
183
+
184
+ ### 4. Docker部署
185
+
186
+ ```bash
187
+ docker build -t aquery-api .
188
+ docker run -p 7860:7860 aquery-api
189
+ ```
190
+
191
+ ## Hugging Face Space部署
192
+
193
+ 1. 在Hugging Face创建新的Space,选择Docker模板
194
+ 2. 将代码推送到Space仓库
195
+ 3. 配置环境变量(如果需要)
196
+ 4. 访问Space URL查看API文档
197
+
198
+ ## 查询示例
199
+
200
+ ### 示例1: 获取某股票历史数据
201
+
202
+ ```python
203
+ df = client.daily_history(
204
+ ts_code="000001.SZ",
205
+ start_date=20240101,
206
+ end_date=20241231
207
+ )
208
+ ```
209
+
210
+ ### 示例2: 获取某日全市场数据
211
+
212
+ ```python
213
+ df = client.daily_all(
214
+ trade_date=20240320,
215
+ fields=["ts_code", "close", "pe", "net_mf_amount", "total_mv"]
216
+ )
217
+ ```
218
+
219
+ ### 示例3: 条件选股
220
+
221
+ ```python
222
+ # 查询某日PE<20且净流入>1亿的股票
223
+ df = client.daily_all(trade_date=20240320)
224
+ df = df[(df['pe'] < 20) & (df['net_mf_amount'] > 1e8)]
225
+ ```
226
+
227
+ ### 示例4: 获取财务指标
228
+
229
+ ```python
230
+ df = client.fina_indicator(
231
+ ts_code="000001.SZ",
232
+ fields=["end_date", "roe", "eps", "grossprofit_margin"]
233
+ )
234
+ ```
235
+
236
+ ## 性能指标
237
+
238
+ | 场景 | 数据量 | 响应时间 |
239
+ |------|--------|----------|
240
+ | 股票基础信息 | 5,200条 | < 50ms |
241
+ | 个股全历史 | 5,000条 | < 200ms |
242
+ | 单日全市场 | 5,200条 | < 100ms |
243
+ | 时间段截面 | 100万条 | < 2s |
244
+
245
+ ## 项目结构
246
+
247
+ ```
248
+ .
249
+ ├── app/ # FastAPI服务端
250
+ │ ├── main.py # 应用入口
251
+ │ ├── core/ # 配置、认证
252
+ │ ├── routers/ # API路由
253
+ │ └── services/ # 查询服务
254
+ ├── client/ # Python SDK
255
+ │ └── aquery.py # 客户端实现
256
+ ├── tests/ # 测试
257
+ ├── Dockerfile # 容器配置
258
+ ├── requirements.txt # Python依赖
259
+ └── README.md # 本文档
260
+ ```
261
+
262
+ ## 技术栈
263
+
264
+ - **FastAPI**: 高性能Web框架
265
+ - **DuckDB**: 嵌入式OLAP查询引擎
266
+ - **Polars**: 极速数据处理
267
+ - **Hugging Face Datasets**: 数据存储
268
+
269
+ ## 许可证
270
+
271
+ MIT License
272
+
273
+ ## 联系方式
274
+
275
+ - 数据集: https://huggingface.co/datasets/superxu520/
276
+ - 问题反馈: 请提交GitHub Issue
@@ -0,0 +1,11 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ pyproject.toml
5
+ aquery/__init__.py
6
+ aquery/client.py
7
+ aquery_sdk.egg-info/PKG-INFO
8
+ aquery_sdk.egg-info/SOURCES.txt
9
+ aquery_sdk.egg-info/dependency_links.txt
10
+ aquery_sdk.egg-info/requires.txt
11
+ aquery_sdk.egg-info/top_level.txt
@@ -0,0 +1,8 @@
1
+ pandas>=1.3.0
2
+ requests>=2.25.0
3
+
4
+ [dev]
5
+ pytest>=7.0.0
6
+ pytest-asyncio>=0.21.0
7
+ black>=22.0.0
8
+ flake8>=4.0.0
@@ -0,0 +1 @@
1
+ aquery
@@ -0,0 +1,61 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "aquery-sdk"
7
+ version = "1.0.0"
8
+ description = "A-Query Open API Python SDK - A股20年全量数据查询服务"
9
+ readme = "README.md"
10
+ license = {text = "MIT"}
11
+ requires-python = ">=3.8"
12
+ authors = [
13
+ {name = "superxu520"}
14
+ ]
15
+ keywords = ["aquery", "a-share", "stock", "finance", "tushare", "quant"]
16
+ classifiers = [
17
+ "Development Status :: 4 - Beta",
18
+ "Intended Audience :: Developers",
19
+ "Intended Audience :: Financial and Insurance Industry",
20
+ "License :: OSI Approved :: MIT License",
21
+ "Operating System :: OS Independent",
22
+ "Programming Language :: Python :: 3",
23
+ "Programming Language :: Python :: 3.8",
24
+ "Programming Language :: Python :: 3.9",
25
+ "Programming Language :: Python :: 3.10",
26
+ "Programming Language :: Python :: 3.11",
27
+ "Programming Language :: Python :: 3.12",
28
+ "Topic :: Office/Business :: Financial",
29
+ "Topic :: Software Development :: Libraries :: Python Modules",
30
+ ]
31
+ dependencies = [
32
+ "pandas>=1.3.0",
33
+ "requests>=2.25.0",
34
+ ]
35
+
36
+ [project.optional-dependencies]
37
+ dev = [
38
+ "pytest>=7.0.0",
39
+ "pytest-asyncio>=0.21.0",
40
+ "black>=22.0.0",
41
+ "flake8>=4.0.0",
42
+ ]
43
+
44
+ [project.urls]
45
+ Homepage = "https://huggingface.co/spaces/superxu520/A-query"
46
+ Documentation = "https://huggingface.co/spaces/superxu520/A-query"
47
+ Repository = "https://huggingface.co/spaces/superxu520/A-query"
48
+ Issues = "https://huggingface.co/spaces/superxu520/A-query/discussions"
49
+
50
+ [tool.setuptools.packages.find]
51
+ where = ["."]
52
+ include = ["aquery*"]
53
+ exclude = ["tests*", "app*"]
54
+
55
+ [tool.black]
56
+ line-length = 100
57
+ target-version = ['py38']
58
+
59
+ [tool.pytest.ini_options]
60
+ testpaths = ["tests"]
61
+ python_files = "test_*.py"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+