tdxdata
通达信(TDX)新版行情协议 Python 客户端 — 2026-09 协议逆向适配。
老 pytdx / mootdx / xmtdx 的协议 2026-09-10 起被通达信服务器拒绝(服务端协议改动:版本标识 + 命令族 + zlib 流式压缩)。tdxdata 是逆向新协议后的独立实现。
A-share (CN) realtime quote client for TDX's 2026-09 protocol.
快速开始
pip install -e .
from tdxdata import TdxClient
c = TdxClient() # 默认连 101.35.121.35(43 主站之一,见 servers.txt)
c.connect() # 0c02 init + 0c03 tdxlevel 认证
c.snap('600519') # 分时快照: 最新价 + 当日分时点
c.bars(9, '000059', 0, 100) # 历史K线: 9=日线 0=5min 8=1min 6=周 7=月
c.quotes([(0, '000059')]) # 盘口报价: 现价/昨收/高低/量/额/五档
c.finance('000059') # 财务: 流通/总股本/净利润/每股净资产
c.close()
全市场并发快照(多主站摊掉每请求 ~3s 延迟):
from tdxdata.pool2 import Pool
p = Pool() # 43 个主站 IP × 并发连接
r = p.snap('600519') # 单只: {'last': 1277.56, 'prices': [32点分时...]}
rows = p.snap_all(['000001', '600519']) # 并发全列表 → [(code, last, ok)]
支持深市 / 沪市 / 指数(market 自动推断,也可显式传):
p.snap('000001') # 平安银行(深 market=0)
p.snap('000001', market=1) # 上证指数(沪 market=1)
p.snap('688981', market=1) # 中芯国际(科创板)
CLI 连通性自检:
tdxdata-probe # 探测 600519 的 snap/bars/quotes/finance
tdxdata-probe --codes 000001,600519 --host 101.35.121.35
功能
| 接口 | 命令 | 状态 |
|---|---|---|
| 分时快照(最新价+32-51点分时) | 0c09 | ✅ 深沪深+指数 |
| 历史K线(日/5min/15/30/60/1min/周/月) | 0x010c (GetSecurityBars) | ✅ 单只任意历史段 |
| 盘口报价(现价/昨收/高低/量/额/五档) | 0x010c (GetSecurityQuotes) | ✅ |
| 财务(流通/总股本/净利润/每股净资产) | 0c1f | ✅ |
| 多主站并发(43 IP 轮询、坏 IP 剔除) | — | ✅ 全市场快照 ~12-15 分钟 |
| 批量快照(单包多只) | 0c01 身份会话 | ⚠️ 服务端需 0c01 会话,已放弃(单只循环+并发替代) |
| 全量历史K线(单请求批量全市场) | — | ❌ 新版客户端用本地文件+增量,服务器无此接口(单只循环可拉任意股票) |
已知限制
- 每连接每请求固定 ~3s 延迟(服务器行为)→ 用多连接并发摊掉,勿压单连接频率
- 单出口并发 >60 会触发出口级限流(热状态,需长冷却)→ 默认 43 连接
- 3 个主站 IP 连接即 reset(已从 servers.txt 剔除)
- 大数据量响应为 zlib 压缩流(小数据量明文),客户端已统一处理(
client.maybe_decompress) - 少数股票 5min 含 13:00 一根 bar(服务器行为,与旧 pytdx 主库一致)
- 无效代码(不存在的股票代码)返回未定义数据(bars 可能含乱码日期、quotes 可能重定向到其他代码),调用方需保证传入有效代码
- quotes 对基金(ETF)价格精度为 0.001 元 → 返回价 ×10(510300 实际 4.523 会显示 45.23,pytdx 同款行为);基金行情请用 snap/bars(float32 直读,精度正确)
- 市场自动判断规则:5/6/9 开头=沪(5 基金/6 A/9 B),0/1/2/3 开头=深(0 A/1 基金/2 B/3 创业板);北交所(4/8/92)暂不支持
协议要点(完整版见 docs/protocol.md)
TCP 7709 → 0c02(init) → 0c03(tdxlevel) → 数据请求
单只快照 = 0c09 0041 0300 2700 2700 d10f [market] [code6] + 29B 后缀
market: 0=深市 1=沪市(同 code 不同 market = 不同标的!)
历史K线 = 0x010c + 版本标识 18 7b 00 01(老 pytdx 命令仅改此标识即复用)
借用与版权
解码层部分逻辑参考 pytdx(MIT, rainx)——详见 NOTICE.md。协议逆向过程见 docs/(protocol.md / extractable.md / analysis_round1~3.md)。
License
MIT — 详见 LICENSE。
⚠️ 仅供学习研究。行情数据版权归交易所所有;请遵守通达信服务条款,控制请求频率。
Metadata
Release files for tdxdata 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tdxdata-0.2.0.tar.gz | 21.0 kB | Without provenance | Details for tdxdata-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tdxdata-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for tdxdata-0.2.0-py3-none-any.whl |
Total release size: 44.9 kB
Release files / tdxdata-0.2.0.tar.gz
| Download URL | tdxdata-0.2.0.tar.gz |
|---|---|
| Size | 21.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
430e015102b9cdd85868e546fbeccf7e992da2601857fe74fa59dd997cd7f03e
|
|
BLAKE2b-256 checksum How to use checksums |
bbd0f2701040c2e7952798535b28670322f9b8e39239800438d13f9f752d9e7d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.15
|
Release files / tdxdata-0.2.0-py3-none-any.whl
| Download URL | tdxdata-0.2.0-py3-none-any.whl |
|---|---|
| Size | 23.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
668aea8f4e56401ddc51f22235f13c8d63841028a464f7d51cadda69ab65acfd
|
|
BLAKE2b-256 checksum How to use checksums |
1ae3cf7a5fde49d59a70589c09e52ea6d2258549cb1fbb922348dd751e4dd0af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.15
|
Security for tdxdata 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.