tdxhub-sdk 0.1.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.
- tdxhub_sdk-0.1.0/AUTHORS.rst +7 -0
- tdxhub_sdk-0.1.0/LICENSE +10 -0
- tdxhub_sdk-0.1.0/PKG-INFO +470 -0
- tdxhub_sdk-0.1.0/README.md +424 -0
- tdxhub_sdk-0.1.0/pyproject.toml +90 -0
- tdxhub_sdk-0.1.0/setup.cfg +4 -0
- tdxhub_sdk-0.1.0/tdxhub/__init__.py +9 -0
- tdxhub_sdk-0.1.0/tdxhub/__main__.py +149 -0
- tdxhub_sdk-0.1.0/tdxhub/affair.py +82 -0
- tdxhub_sdk-0.1.0/tdxhub/cache/__init__.py +6 -0
- tdxhub_sdk-0.1.0/tdxhub/cache/compat.py +4 -0
- tdxhub_sdk-0.1.0/tdxhub/cache/file.py +52 -0
- tdxhub_sdk-0.1.0/tdxhub/cache/persistent.py +188 -0
- tdxhub_sdk-0.1.0/tdxhub/cache/timed.py +60 -0
- tdxhub_sdk-0.1.0/tdxhub/cache/timer.py +20 -0
- tdxhub_sdk-0.1.0/tdxhub/call_auction.py +89 -0
- tdxhub_sdk-0.1.0/tdxhub/config.py +166 -0
- tdxhub_sdk-0.1.0/tdxhub/consts.py +237 -0
- tdxhub_sdk-0.1.0/tdxhub/contrib/__init__.py +5 -0
- tdxhub_sdk-0.1.0/tdxhub/contrib/adjust.py +84 -0
- tdxhub_sdk-0.1.0/tdxhub/contrib/compat.py +181 -0
- tdxhub_sdk-0.1.0/tdxhub/exceptions.py +42 -0
- tdxhub_sdk-0.1.0/tdxhub/f10.py +39 -0
- tdxhub_sdk-0.1.0/tdxhub/failover.py +356 -0
- tdxhub_sdk-0.1.0/tdxhub/financial/__init__.py +0 -0
- tdxhub_sdk-0.1.0/tdxhub/financial/base.py +81 -0
- tdxhub_sdk-0.1.0/tdxhub/financial/columns.py +583 -0
- tdxhub_sdk-0.1.0/tdxhub/financial/financial.py +218 -0
- tdxhub_sdk-0.1.0/tdxhub/gbbq.py +400 -0
- tdxhub_sdk-0.1.0/tdxhub/http/__init__.py +6 -0
- tdxhub_sdk-0.1.0/tdxhub/http/app.py +404 -0
- tdxhub_sdk-0.1.0/tdxhub/http/serialization.py +78 -0
- tdxhub_sdk-0.1.0/tdxhub/http/service.py +298 -0
- tdxhub_sdk-0.1.0/tdxhub/logger.py +6 -0
- tdxhub_sdk-0.1.0/tdxhub/minute.py +236 -0
- tdxhub_sdk-0.1.0/tdxhub/official/__init__.py +33 -0
- tdxhub_sdk-0.1.0/tdxhub/official/parser.py +597 -0
- tdxhub_sdk-0.1.0/tdxhub/parse.py +75 -0
- tdxhub_sdk-0.1.0/tdxhub/pull/__init__.py +30 -0
- tdxhub_sdk-0.1.0/tdxhub/pull/calendar.py +178 -0
- tdxhub_sdk-0.1.0/tdxhub/pull/fetcher.py +283 -0
- tdxhub_sdk-0.1.0/tdxhub/pull/merge.py +96 -0
- tdxhub_sdk-0.1.0/tdxhub/pull/planner.py +85 -0
- tdxhub_sdk-0.1.0/tdxhub/pull/service.py +140 -0
- tdxhub_sdk-0.1.0/tdxhub/pull/store.py +561 -0
- tdxhub_sdk-0.1.0/tdxhub/pull/trades.py +212 -0
- tdxhub_sdk-0.1.0/tdxhub/pull/volume.py +13 -0
- tdxhub_sdk-0.1.0/tdxhub/quotes.py +1591 -0
- tdxhub_sdk-0.1.0/tdxhub/reader.py +278 -0
- tdxhub_sdk-0.1.0/tdxhub/security.py +116 -0
- tdxhub_sdk-0.1.0/tdxhub/server.py +146 -0
- tdxhub_sdk-0.1.0/tdxhub/stock_info.py +258 -0
- tdxhub_sdk-0.1.0/tdxhub/tools/DownloadTDXCaiWu.py +209 -0
- tdxhub_sdk-0.1.0/tdxhub/tools/__init__.py +3 -0
- tdxhub_sdk-0.1.0/tdxhub/tools/customize.py +201 -0
- tdxhub_sdk-0.1.0/tdxhub/tools/reversion.py +159 -0
- tdxhub_sdk-0.1.0/tdxhub/tools/tdx2csv.py +57 -0
- tdxhub_sdk-0.1.0/tdxhub/utils/__init__.py +362 -0
- tdxhub_sdk-0.1.0/tdxhub/utils/adjust.py +109 -0
- tdxhub_sdk-0.1.0/tdxhub/utils/demjson.py +6320 -0
- tdxhub_sdk-0.1.0/tdxhub/utils/factor.py +55 -0
- tdxhub_sdk-0.1.0/tdxhub/utils/holiday.js +287 -0
- tdxhub_sdk-0.1.0/tdxhub/utils/holiday.py +149 -0
- tdxhub_sdk-0.1.0/tdxhub/utils/pandas_cache.py +68 -0
- tdxhub_sdk-0.1.0/tdxhub/utils/timer.py +33 -0
- tdxhub_sdk-0.1.0/tdxhub/version.py +3 -0
- tdxhub_sdk-0.1.0/tdxhub_sdk.egg-info/PKG-INFO +470 -0
- tdxhub_sdk-0.1.0/tdxhub_sdk.egg-info/SOURCES.txt +79 -0
- tdxhub_sdk-0.1.0/tdxhub_sdk.egg-info/dependency_links.txt +1 -0
- tdxhub_sdk-0.1.0/tdxhub_sdk.egg-info/entry_points.txt +2 -0
- tdxhub_sdk-0.1.0/tdxhub_sdk.egg-info/requires.txt +25 -0
- tdxhub_sdk-0.1.0/tdxhub_sdk.egg-info/top_level.txt +1 -0
- tdxhub_sdk-0.1.0/tests/test_adjust.py +73 -0
- tdxhub_sdk-0.1.0/tests/test_adjust2.py +28 -0
- tdxhub_sdk-0.1.0/tests/test_affairs.py +45 -0
- tdxhub_sdk-0.1.0/tests/test_bestip.py +18 -0
- tdxhub_sdk-0.1.0/tests/test_factor.py +33 -0
- tdxhub_sdk-0.1.0/tests/test_frequency.py +15 -0
- tdxhub_sdk-0.1.0/tests/test_reconnect.py +7 -0
- tdxhub_sdk-0.1.0/tests/test_useless.py +41 -0
- tdxhub_sdk-0.1.0/tests/test_xdxr.py +27 -0
tdxhub_sdk-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
MIT License
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2017, tdxhub
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
7
|
+
|
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
9
|
+
|
|
10
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tdxhub-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 通达信本地数据、行情与财务数据读取工具
|
|
5
|
+
Author-email: bopo <ibopo@126.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/RandomLiu2025/tdx-hub
|
|
8
|
+
Project-URL: Documentation, https://github.com/RandomLiu2025/tdx-hub/tree/main/docs/
|
|
9
|
+
Project-URL: Repository, https://github.com/RandomLiu2025/tdx-hub
|
|
10
|
+
Project-URL: Issues, https://github.com/RandomLiu2025/tdx-hub/issues
|
|
11
|
+
Keywords: tongdaxin,tdx,finance,market-data,pandas
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
License-File: AUTHORS.rst
|
|
25
|
+
Requires-Dist: click<9,>=8.1
|
|
26
|
+
Requires-Dist: httpx<1,>=0.27
|
|
27
|
+
Requires-Dist: numpy<3,>=1.26
|
|
28
|
+
Requires-Dist: pandas<4,>=2.2
|
|
29
|
+
Requires-Dist: prettytable<4,>=3.10
|
|
30
|
+
Requires-Dist: tdxpy<1,>=0.2.7
|
|
31
|
+
Requires-Dist: tenacity<10,>=8.2
|
|
32
|
+
Requires-Dist: tqdm<5,>=4.66
|
|
33
|
+
Provides-Extra: cli
|
|
34
|
+
Provides-Extra: holiday
|
|
35
|
+
Requires-Dist: mini-racer<1,>=0.12; extra == "holiday"
|
|
36
|
+
Provides-Extra: server
|
|
37
|
+
Requires-Dist: fastapi<1,>=0.115; extra == "server"
|
|
38
|
+
Requires-Dist: uvicorn<1,>=0.30; extra == "server"
|
|
39
|
+
Provides-Extra: all
|
|
40
|
+
Requires-Dist: mini-racer<1,>=0.12; extra == "all"
|
|
41
|
+
Requires-Dist: fastapi<1,>=0.115; extra == "all"
|
|
42
|
+
Requires-Dist: uvicorn<1,>=0.30; extra == "all"
|
|
43
|
+
Provides-Extra: test
|
|
44
|
+
Requires-Dist: pytest<9,>=8.3; extra == "test"
|
|
45
|
+
Dynamic: license-file
|
|
46
|
+
|
|
47
|
+
# tdxhub
|
|
48
|
+
|
|
49
|
+
`tdxhub` 是面向 Python 3.11+ 的通达信市场数据工具包。它将本地 VIPDOC 文件、标准/扩展在线行情、官方配置与财务文件、复权与换手率、增量拉取和可选 HTTP 服务统一到一套 Python API 中。
|
|
50
|
+
|
|
51
|
+
> 项目只提供数据读取、解析与本地存储能力,不包含交易下单功能。
|
|
52
|
+
|
|
53
|
+
## 功能概览
|
|
54
|
+
|
|
55
|
+
| 能力 | 主要入口 | 说明 |
|
|
56
|
+
|---|---|---|
|
|
57
|
+
| 本地行情 | `tdxhub.reader.Reader` | 日线、1 分钟、5 分钟、扩展市场、退市缓存、板块与官方配置 |
|
|
58
|
+
| 在线行情 | `tdxhub.quotes.Quotes` | 快照、分页/全量 K 线、指数、分时、逐笔、集合竞价、公司资料、财务与扩展行情 |
|
|
59
|
+
| 复权与换手率 | `tdxhub.gbbq` | GBBQ/XDXR 规范化、前复权、后复权、历史股本快照和日 K 换手率 |
|
|
60
|
+
| 官方文件 | `tdxhub.official` | 板块、指数、行业、北交所目录、统计、资金流和新股申购配置解析 |
|
|
61
|
+
| 专业财务 | `tdxhub.affair.Affair` | 财务压缩包清单、下载、校验和解析 |
|
|
62
|
+
| 增量同步 | `tdxhub.pull` | 缺口规划、分页抓取、逐笔合成分钟线、SQLite 幂等持久化 |
|
|
63
|
+
| HTTP API | `tdxhub.http` | 可选 FastAPI 服务,提供行情、K 线、指数、逐笔、财务和扩展行情路由 |
|
|
64
|
+
| CLI | `tdxhub` | 本地读取、在线行情、线路探测、财务文件和批量导出 |
|
|
65
|
+
|
|
66
|
+
## 安装
|
|
67
|
+
|
|
68
|
+
PyPI 发行包名为 `tdxhub-sdk`;安装后仍使用 `import tdxhub`,命令行入口仍为 `tdxhub`。
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
python -m pip install -U tdxhub-sdk
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
按需安装可选能力:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
# 节假日解析
|
|
78
|
+
python -m pip install -U "tdxhub-sdk[holiday]"
|
|
79
|
+
|
|
80
|
+
# FastAPI HTTP 服务
|
|
81
|
+
python -m pip install -U "tdxhub-sdk[server]"
|
|
82
|
+
|
|
83
|
+
# 安装全部可选依赖
|
|
84
|
+
python -m pip install -U "tdxhub-sdk[all]"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
源码开发:
|
|
88
|
+
|
|
89
|
+
```bash
|
|
90
|
+
git clone https://github.com/RandomLiu2025/tdx-hub.git
|
|
91
|
+
cd tdx-hub
|
|
92
|
+
uv sync --extra server --extra test
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## 快速开始
|
|
96
|
+
|
|
97
|
+
### 读取本地 VIPDOC
|
|
98
|
+
|
|
99
|
+
`tdxdir` 可以是通达信安装根目录,也可以直接指向 `vipdoc`。证券代码支持 `600000`、`SH600000`、`SH.600000` 等形式,并识别沪、深、北市场。
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
from tdxhub.reader import Reader
|
|
103
|
+
|
|
104
|
+
reader = Reader.factory("std", tdxdir=r"D:\new_tdx")
|
|
105
|
+
|
|
106
|
+
daily = reader.daily("SH.600000")
|
|
107
|
+
minute = reader.minute("600000")
|
|
108
|
+
five_minute = reader.fzline("600000")
|
|
109
|
+
|
|
110
|
+
print(daily.tail())
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
其他本地能力:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
# 退市证券 ds_cache/*.~~~day;普通 daily 缺文件时也会尝试该缓存
|
|
117
|
+
retired = reader.delisted_daily("sz000003")
|
|
118
|
+
|
|
119
|
+
# 传统板块文件和官方配置
|
|
120
|
+
concepts = reader.block("block_gn.dat")
|
|
121
|
+
beijing = reader.beijing_stocks()
|
|
122
|
+
industries = reader.stock_industries()
|
|
123
|
+
statistics = reader.official("tdxstat.cfg")
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
扩展市场本地文件使用独立 reader:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
ext_reader = Reader.factory("ext", tdxdir=r"D:\new_tdx")
|
|
130
|
+
future_daily = ext_reader.daily("4#CF7D0LAO")
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### 读取在线行情
|
|
134
|
+
|
|
135
|
+
`Quotes` 是标准市场和扩展市场的统一工厂。建议使用上下文管理器及时关闭连接:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from tdxhub.quotes import Quotes
|
|
139
|
+
|
|
140
|
+
with Quotes.factory("std", timeout=5) as client:
|
|
141
|
+
snapshot = client.quotes(["sh600000", "sz000001"])
|
|
142
|
+
daily = client.bars("sh600000", frequency=9, offset=100)
|
|
143
|
+
all_daily = client.bars_all("sh600000", frequency=9, since="20250101")
|
|
144
|
+
all_index = client.index_all("000001", frequency=9, since="20250101")
|
|
145
|
+
auction = client.call_auction("sh600000")
|
|
146
|
+
minute = client.minute("sz300394") # datetime 列/索引:09:31~11:30、13:01~15:00
|
|
147
|
+
minute_241 = client.minute_241("sh600000") # 最新交易日,收盘后最多 241 条
|
|
148
|
+
minute_history = client.minute_241("sh600000", since="20250901") # 日期范围
|
|
149
|
+
trades = client.transaction_all("sh600000")
|
|
150
|
+
history_trades = client.transactions_all("sh600000", date="20250910")
|
|
151
|
+
|
|
152
|
+
# 证券目录可按 A/B 股、指数、ETF、其他基金、债券或未识别品种筛选
|
|
153
|
+
etfs = client.stocks(market=1, security_type="etf")
|
|
154
|
+
|
|
155
|
+
# 在线下载通达信/申万行业配置;申万结果同时展开一、二、三级行业
|
|
156
|
+
industries = client.stock_industries(["sh600519", "sz000001"])
|
|
157
|
+
|
|
158
|
+
# 聚合目录、快照、行业、财务、除权与集合竞价;默认 4 路并行并复用连接
|
|
159
|
+
stock_summary = client.stock_info(["sh600519", "sz000001"])
|
|
160
|
+
index_count = client.stock_count(market=1, security_type="index")
|
|
161
|
+
a_stocks = client.stock_all(security_type="a_stock")
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
`stock_count()` 和 `stocks()` 不传 `security_type` 时保持完整证券目录口径。可选值为
|
|
165
|
+
`a_stock`、`b_stock`、`index`、`etf`、`fund`、`bond` 和 `other`;其中 `fund`
|
|
166
|
+
不包含 ETF。完整市场目录在进程内全局共享并持久化到 `~/.tdxhub/caches/quotes`,默认每 6 小时
|
|
167
|
+
按需更新;`stocks(..., refresh=True)` 可强制刷新。带筛选条件的 `stock_count()` 复用完整目录。
|
|
168
|
+
|
|
169
|
+
`stock_info()` 固定返回 37 列 DataFrame,保留输入顺序和重复代码,不包含证券目录、行情和
|
|
170
|
+
财务接口的原始字典。沪深证券的财务、XDXR、
|
|
171
|
+
集合竞价通过实例内有界连接池并行获取,默认 `max_workers=4`;市场目录和行业配置跨客户端共享
|
|
172
|
+
并持久化,默认分别每 6 小时和 24 小时按需更新,可用 `refresh_directories=True`、
|
|
173
|
+
`refresh_industries=True` 显式刷新。自动更新失败时会临时使用磁盘旧值,显式刷新失败仍抛错。
|
|
174
|
+
|
|
175
|
+
常用 K 线频率:`8` 1 分钟、`0` 5 分钟、`1` 15 分钟、`2` 30 分钟、`3` 60 分钟、`9` 日线、`5` 周线、`6` 月线。
|
|
176
|
+
|
|
177
|
+
`stock_industries()` 保留 `sw_industry_code/name/source`,并额外返回
|
|
178
|
+
`sw_level1_code/name`、`sw_level2_code/name` 和 `sw_level3_code/name`。新旧申万编码均在
|
|
179
|
+
命中的字典分区内展开,避免相同数字代码跨分区误配。
|
|
180
|
+
|
|
181
|
+
扩展市场接口:
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
with Quotes.factory("ext", timeout=5) as client:
|
|
185
|
+
markets = client.markets()
|
|
186
|
+
instruments = client.instruments()
|
|
187
|
+
bars = client.bars(market=47, symbol="IFL0", frequency=9, offset=100)
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
在线能力依赖可用的通达信行情节点及 `tdxpy` 协议实现。节点临时不可用、协议变化或数据源历史深度限制都可能影响结果。
|
|
191
|
+
|
|
192
|
+
标准行情和扩展行情默认启用**单活动连接容灾**:构造时最多保留 5 个健康候选节点;当前
|
|
193
|
+
节点在 `auto_retry` 重连后仍发生网络或协议错误时,单次请求最多切换 2 次,故障节点冷却
|
|
194
|
+
60 秒。不会因合法空结果或参数错误切换节点。
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
with Quotes.factory(
|
|
198
|
+
"std",
|
|
199
|
+
timeout=5,
|
|
200
|
+
failover=True,
|
|
201
|
+
max_failovers=2,
|
|
202
|
+
unhealthy_cooldown=60,
|
|
203
|
+
max_candidates=5,
|
|
204
|
+
) as client:
|
|
205
|
+
data = client.quotes("sh600000")
|
|
206
|
+
print(client.server) # 当前活动节点
|
|
207
|
+
print(client.server_status()) # 所有候选节点的健康状态
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
传入 `server=(ip, port)` 时默认只连接指定节点;如需失败后回退公共节点,显式设置
|
|
211
|
+
`fallback_servers=True`。设置 `failover=False` 可关闭跨节点切换,但不影响 `auto_retry`
|
|
212
|
+
对当前节点的重连。
|
|
213
|
+
|
|
214
|
+
加载 `~/.tdxhub/config.json` 时,持久化的 `SERVER.HQ` / `SERVER.EX` 顺序会优先保留,
|
|
215
|
+
并按 IP + 端口自动补入新版本内置但本地配置中缺失的节点。升级后无需删除旧配置文件,
|
|
216
|
+
也不会因旧配置快照而遗漏新节点。
|
|
217
|
+
|
|
218
|
+
## 复权、GBBQ 与历史换手率
|
|
219
|
+
|
|
220
|
+
全量日 K 可在分页合并后统一前复权/后复权,并按历史流通股本计算换手率:
|
|
221
|
+
|
|
222
|
+
```python
|
|
223
|
+
from tdxhub.quotes import Quotes
|
|
224
|
+
|
|
225
|
+
with Quotes.factory("std", timeout=5) as client:
|
|
226
|
+
qfq = client.bars_all(
|
|
227
|
+
"sh600000",
|
|
228
|
+
frequency=9,
|
|
229
|
+
since="20200101",
|
|
230
|
+
adjust="qfq",
|
|
231
|
+
turnover=True,
|
|
232
|
+
)
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
本地读取不会隐式联网。复权或换手率需要显式提供 GBBQ/XDXR 事件:
|
|
236
|
+
|
|
237
|
+
```python
|
|
238
|
+
from tdxhub.reader import Reader
|
|
239
|
+
|
|
240
|
+
reader = Reader.factory("std", tdxdir=r"D:\new_tdx")
|
|
241
|
+
bars = reader.daily(
|
|
242
|
+
"sh600000",
|
|
243
|
+
adjust="hfq",
|
|
244
|
+
turnover=True,
|
|
245
|
+
xdxr=actions, # pandas.DataFrame
|
|
246
|
+
)
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
在线 `xdxr()` 的原始流通股本字段以“万股”为单位,tdxhub 会在计算换手率时自动转换为
|
|
250
|
+
“股”;手工传入且没有单位标记的 `xdxr`/`gbbq` DataFrame 继续按“股”处理。返回的
|
|
251
|
+
`turnover` 是百分数,例如 `0.2784` 表示约 `0.2784%`。
|
|
252
|
+
|
|
253
|
+
底层函数可直接使用:
|
|
254
|
+
|
|
255
|
+
```python
|
|
256
|
+
from tdxhub.gbbq import adjust_prices, get_equity_snapshot, normalize_gbbq
|
|
257
|
+
|
|
258
|
+
events = normalize_gbbq(actions)
|
|
259
|
+
adjusted = adjust_prices(prices, events, method="qfq")
|
|
260
|
+
snapshot = get_equity_snapshot(events, at="2026-09-10")
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
价格复权不修改成交量和成交额;换手率返回百分数。完整数据口径见 [迁移能力使用指南](docs/api/migration.md)。
|
|
264
|
+
|
|
265
|
+
## 官方文件与财务数据
|
|
266
|
+
|
|
267
|
+
### 官方配置解析
|
|
268
|
+
|
|
269
|
+
```python
|
|
270
|
+
from tdxhub.official import associate_industries, parse_official
|
|
271
|
+
|
|
272
|
+
blocks = parse_official(r"D:\new_tdx\T0002\hq_cache\spblock.dat")
|
|
273
|
+
beijing = parse_official(r"D:\new_tdx\T0002\hq_cache\tdxbjmore.cfg")
|
|
274
|
+
|
|
275
|
+
assignments = parse_official(r"D:\new_tdx\T0002\hq_cache\tdxhy.cfg")
|
|
276
|
+
dictionary = parse_official(r"D:\new_tdx\T0002\hq_cache\incon.dat")
|
|
277
|
+
industries = associate_industries(assignments, dictionary)
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
不安装通达信客户端时,也可通过标准行情连接在线获取服务器发布的行业配置:
|
|
281
|
+
|
|
282
|
+
```python
|
|
283
|
+
from tdxhub.quotes import Quotes
|
|
284
|
+
|
|
285
|
+
with Quotes.factory("std", timeout=5) as client:
|
|
286
|
+
one = client.stock_industries("sh600519")
|
|
287
|
+
selected = client.stock_industries(["sz000001", "sh600519"])
|
|
288
|
+
latest = client.stock_industries(refresh=True)
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
在线接口直接下载 `tdxhy.cfg`,并从 `zhb.zip` 读取 `incon.dat` 后完成关联。结果在当前
|
|
292
|
+
进程内全局共享并持久化到 `~/.tdxhub/caches/quotes`,默认 24 小时按需更新;
|
|
293
|
+
`refresh=True` 强制刷新。它属于服务器发布的配置数据,不是
|
|
294
|
+
逐笔实时行情,更新时点取决于服务器文件版本。
|
|
295
|
+
|
|
296
|
+
支持的主要文件包括:
|
|
297
|
+
|
|
298
|
+
- `spblock.dat`
|
|
299
|
+
- `tdxzs.cfg`、`tdxzs3.cfg`、`tdxdszs.cfg`
|
|
300
|
+
- `tdxbk.cfg`、`tdxhy.cfg`、`incon.dat`、`hspy.dat`
|
|
301
|
+
- `tdxstat.cfg`、`tdxstat2.cfg`
|
|
302
|
+
- `tdxbjmore.cfg`、`xgsg.cfg`
|
|
303
|
+
|
|
304
|
+
### 专业财务文件
|
|
305
|
+
|
|
306
|
+
```python
|
|
307
|
+
from tdxhub.affair import Affair
|
|
308
|
+
|
|
309
|
+
manifest = Affair.files()
|
|
310
|
+
archive = Affair.fetch(downdir="output", filename="gpcw19960630.zip")
|
|
311
|
+
frame = Affair.parse(downdir="output", filename="gpcw19960630.zip")
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
不传 `filename` 时,`Affair.fetch()` 会并发下载清单中的全部文件,并对已有文件执行 MD5 校验。
|
|
315
|
+
|
|
316
|
+
## 增量拉取与 SQLite
|
|
317
|
+
|
|
318
|
+
`tdxhub.pull` 使用半开区间 `[start, end)`,按已记录 coverage 计算缺口,并将数据行与 coverage 在同一 SQLite 事务中写入。重复同步采用幂等 upsert。
|
|
319
|
+
|
|
320
|
+
```python
|
|
321
|
+
from tdxhub.pull import PullService, QuoteFetcher, SQLiteStore
|
|
322
|
+
from tdxhub.quotes import Quotes
|
|
323
|
+
|
|
324
|
+
with Quotes.factory("std", timeout=5) as client, SQLiteStore("market.db") as store:
|
|
325
|
+
fetcher = QuoteFetcher(client, source_volume_unit="lots")
|
|
326
|
+
service = PullService(store, fetcher)
|
|
327
|
+
bars = service.sync(
|
|
328
|
+
market="sh",
|
|
329
|
+
code="600000",
|
|
330
|
+
frequency=9,
|
|
331
|
+
start="2026-09-01",
|
|
332
|
+
end="2026-09-11",
|
|
333
|
+
adjustment="none",
|
|
334
|
+
overlap="1D",
|
|
335
|
+
)
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
分钟数据可使用逐笔 fallback:
|
|
339
|
+
|
|
340
|
+
```python
|
|
341
|
+
from tdxhub.pull import PullService, QuoteFetcher, TradeMinuteFetcher
|
|
342
|
+
|
|
343
|
+
service = PullService(
|
|
344
|
+
store,
|
|
345
|
+
QuoteFetcher(client, source_volume_unit="lots"),
|
|
346
|
+
fallback_fetcher=TradeMinuteFetcher(client),
|
|
347
|
+
)
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
当前限制:
|
|
351
|
+
|
|
352
|
+
- 增量同步仅支持 `adjustment="none"`,不接受 qfq/hfq 分区。
|
|
353
|
+
- coverage 表示请求区间已成功返回并写入,不等同于官方交易日历完整性证明。
|
|
354
|
+
- 标准市场成交量可从“手”归一为“股”;期货等扩展品种的单位需要调用方核验。
|
|
355
|
+
- `TradeMinuteFetcher` 仅支持沪深标准市场 1 分钟线。
|
|
356
|
+
|
|
357
|
+
## HTTP API
|
|
358
|
+
|
|
359
|
+
安装 `tdxhub[server]` 后,可通过依赖注入创建 FastAPI 应用:
|
|
360
|
+
|
|
361
|
+
```python
|
|
362
|
+
# serve_tdxhub.py
|
|
363
|
+
from contextlib import asynccontextmanager
|
|
364
|
+
|
|
365
|
+
from tdxhub.http import MarketDataService, create_app
|
|
366
|
+
from tdxhub.quotes import Quotes
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def make_app():
|
|
370
|
+
service = MarketDataService(standard_quotes=None)
|
|
371
|
+
app = create_app(service)
|
|
372
|
+
|
|
373
|
+
@asynccontextmanager
|
|
374
|
+
async def lifespan(_app):
|
|
375
|
+
client = Quotes.factory("std", timeout=5)
|
|
376
|
+
service.standard_quotes = client
|
|
377
|
+
try:
|
|
378
|
+
yield
|
|
379
|
+
finally:
|
|
380
|
+
service.standard_quotes = None
|
|
381
|
+
client.close()
|
|
382
|
+
|
|
383
|
+
app.router.lifespan_context = lifespan
|
|
384
|
+
return app
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
启动:
|
|
388
|
+
|
|
389
|
+
```bash
|
|
390
|
+
uvicorn serve_tdxhub:make_app --factory --host 127.0.0.1 --port 8000 --workers 1
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
OpenAPI 页面位于 <http://127.0.0.1:8000/docs>。主要 GET 路由包括:
|
|
394
|
+
|
|
395
|
+
- 健康检查:`/`、`/ready`
|
|
396
|
+
- 证券目录和快照:`/count`、`/code`、`/code/all`、`/quote`
|
|
397
|
+
- K 线和指数:`/kline/*`、`/index/*`、`/kline/minute/241`
|
|
398
|
+
- 盘中数据:`/minute`、`/trade/*`、`/call_auction`
|
|
399
|
+
- 公司与财务:`/stock/info`、`/gbbq`、`/finance`、`/company/*`、`/tdx/*`
|
|
400
|
+
- 扩展行情:`/ex/*`(需注入 `extended_quotes`)
|
|
401
|
+
|
|
402
|
+
成功响应为 `{"code": 0, "msg": "ok", "data": ...}`。当前 HTTP 层未内置认证、限流和通用请求并发隔离;仅 `/stock/info` 内部使用有界连接池,不应直接暴露到公网。
|
|
403
|
+
|
|
404
|
+
## 命令行
|
|
405
|
+
|
|
406
|
+
```bash
|
|
407
|
+
tdxhub --help
|
|
408
|
+
tdxhub --version
|
|
409
|
+
|
|
410
|
+
tdxhub reader --tdxdir D:\new_tdx --symbol 600000 --action daily
|
|
411
|
+
tdxhub quotes --symbol 600000 --action daily
|
|
412
|
+
tdxhub bestip --limit 5
|
|
413
|
+
tdxhub affair --listfile
|
|
414
|
+
tdxhub bundle --symbol 600000,000001 --action daily
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
各命令可通过 `tdxhub <command> --help` 查看完整参数。
|
|
418
|
+
|
|
419
|
+
## 测试与开发
|
|
420
|
+
|
|
421
|
+
默认测试排除真实网络和本地集成用例:
|
|
422
|
+
|
|
423
|
+
```bash
|
|
424
|
+
uv run pytest -q
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
使用真实通达信目录运行 VIPDOC 集成测试:
|
|
428
|
+
|
|
429
|
+
```bash
|
|
430
|
+
TDXHUB_TDXDIR=/mnt/d/new_tdx_mock uv run pytest -q -m integration tests/integration/test_vipdoc.py
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
Windows PowerShell:
|
|
434
|
+
|
|
435
|
+
```powershell
|
|
436
|
+
$env:TDXHUB_TDXDIR = "D:\new_tdx_mock"
|
|
437
|
+
uv run pytest -q -m integration tests\integration\test_vipdoc.py
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
也可以通过 Makefile 传入同名变量:
|
|
441
|
+
|
|
442
|
+
```bash
|
|
443
|
+
make test-vipdoc TDXHUB_TDXDIR=/mnt/d/new_tdx_mock
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
其他常用命令:
|
|
447
|
+
|
|
448
|
+
```bash
|
|
449
|
+
uv run ruff check tdxhub tests
|
|
450
|
+
uv run python -m compileall -q tdxhub tests
|
|
451
|
+
uv build
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
## 数据口径与使用限制
|
|
455
|
+
|
|
456
|
+
- 默认时区为 `Asia/Shanghai`;不同接口的时间字段和历史深度取决于上游数据源。
|
|
457
|
+
- 在线标准行情支持沪深京代码识别,但某些底层接口本身只覆盖沪深市场。
|
|
458
|
+
- 北交所本地目录来自 `tdxbjmore.cfg`;在线目录可能来自客户端配置包,不代表实时主站证券计数。
|
|
459
|
+
- 复权依赖正确的 GBBQ/XDXR 事件;债券、期货和衍生品的价格精度、成交量单位需单独核验。
|
|
460
|
+
- 请遵守通达信及相关数据源的服务条款。项目仅供学习、研究和个人数据处理使用。
|
|
461
|
+
|
|
462
|
+
## 文档
|
|
463
|
+
|
|
464
|
+
- [迁移能力使用指南](docs/api/migration.md)
|
|
465
|
+
- [架构与迁移设计](docs/design/202609101744-port-go-data-capabilities.md)
|
|
466
|
+
- [实施计划](IMPLEMENTATION_PLAN.md)
|
|
467
|
+
|
|
468
|
+
## License
|
|
469
|
+
|
|
470
|
+
[MIT License](LICENSE)
|