Skip to main content

pytdxdata

CI PyPI version Python License PyPI downloads

通达信(TongDaXin)行情数据客户端:动态连接池 + 双层 TTL 缓存 + 多连接并发分页,纯 asyncio、零 Python 三方依赖(Rust 核心随 wheel 提供)。

A股(沪/深/北) + 港股 + 美股 + 期货 + 期权,一套接口全市场。

📖 完整文档:https://openbot-coder.github.io/pytdxdata/

安装

pip install pytdxdata          # 或 uv add pytdxdata

从 0.7.x 升级? 1.0.0 带 Rust 核心(abi3 wheel,一份覆盖 Python 3.11+), 全部公开方法保持兼容;唯一语义变化是 get_market_stat() 的家数不再多乘 10。 见 迁移指南 与 版本支持策略(0.7.x 已冻结)。

可选异步加速:

pip install "pytdxdata[uvloop]"

30 秒上手

import asyncio
from pytdxdata import TdxData
from pytdxdata.models import KlinePeriod, Adjust

async def main():
    async with TdxData() as td:                     # 自动建连接池 + 缓存
        # 日K线(count 超过单页大小会自动多连接并发分页)
        bars = await td.get_bars(["sz000001"], KlinePeriod.DAY, count=240)
        # 前复权(自动走 MAC 通道)
        qfq = await td.get_bars(["sz300308"], KlinePeriod.DAY,
                                 count=500, adjust=Adjust.QFQ)
        # 跨市场报价:A股+港股+美股一次拿全
        quotes = await td.get_quotes(["sh600000", "hk00700", "usAAPL"])
        # 逐笔成交(MAC 通道,支持历史日期)
        ticks = await td.get_ticks(["sz000001"], date=20260811)
        print(bars[-1].close, quotes[0].price, len(ticks))

asyncio.run(main())

默认返回的是标准库 dataclass 组成的 list(核心库不依赖 pandas)。需要 DataFrame 就换个后端构造:

async with TdxData(df="polars") as td:       # 或 df="pandas";需 pip install "pytdxdata[polars]"
    df = await td.get_bars("sh600000", KlinePeriod.DAY, count=240)
# 列:market code open high low close vol amount datetime trading_date symbol settle

时间字段会被合并成一列 datetime(不再有 year/month/day/hour/minute),五档报价展开成 bid1_price…ask5_vol,详见 DataFrame 输出。

特性

特性 说明
🧩 动态连接池 启动时对服务器真实 K 线探测选优(剔除握手正常但数据为空的坏节点),健康分 + EWMA 延迟选路,跨服务器故障转移
🗄️ 双层 TTL 缓存 内存 LRU + 磁盘 SQLite(WAL),TTL 分级;single-flight 并发同键去重;缓存已解析记录(重复请求 0 网络 0 解析)
⚡ 并发分页 单请求拆 N 页(显式 start 偏移 → 无状态可并行)并行拉取、按序拼接、失败换连接重试
🔀 双通道整合 标准通道(经典行情)+ MAC 通道(逐笔 / 复权 / 1 分钟 K 线),按命令自动选路
🦀 Rust 核心 wheel 内置 pytdxdata._core(PyO3,abi3-py311):41 Cmd 协议编解码与 parquet 写出可走 Rust;与纯 Python 实现经 45 个黄金样本交叉验证
📦 零 Python 三方依赖 核心包 dependencies 为空;DataFrame 走 extras,报价 parquet 由 Rust 完成(无需 pyarrow)
🐼 DataFrame 直出 TdxData(df="polars") / df="pandas" 让 42 个表格型接口直接返回 DataFrame;时间列自动合并成 datetime,五档报价自动展开。后端是可选 extras,核心包仍零 Python 三方依赖
📊 全市场覆盖 A股、港股、美股、期货、期权(46 个扩展市场)
📄 服务器文件解析 板块 block_*.dat / 行业 tdxhy.cfg / 财报 gpcw* 下载即解析成 dataclass(索引名与服务器实际文件名不一致时自动纠正)
🤖 AI 助手友好 内置 SKILL.md 与 llms.txt

文档导航

我想… 去哪
装好跑起来 快速开始
抄场景代码(转 DataFrame / 批量下载 / 离线板块快照) 场景示例
查方法签名与参数 TdxData 方法 — 由 docstring 自动生成
搞清枚举该填几 枚举速查
看协议二进制格式、三池路由、缓存策略 深入原理
用命令行 命令行工具
完整接口清单(70 个公开方法 + raw 层 41 Cmd) docs/API_REFERENCE.md
从 0.7.x 升级 / 回退 迁移指南 · 版本支持策略
绕过上层直连协议(排障) 基础协议层 raw
逐字段核对清单 / 结果 核对清单 · 核对结果
重构审查(数据一致性 / 遗漏对账) 接口重构审查
版本变更 CHANGELOG.md

本 README 只做入口层。深度内容一律在文档站,避免两处维护造成漂移。

给 AI 助手用

仓库根的 SKILL.md 是一份可直接加载的技能:三条必守约定、KlinePeriod 等枚举取值表、 「想做什么 → 用哪个方法」决策表、以及 21 条真实陷阱。AI 编程助手读完就能一次写对,不必翻源码。

# 丢进你所用 AI 助手的 skills 目录即可
curl -o ~/.workbuddy/skills/pytdxdata/SKILL.md --create-dirs \
  https://raw.githubusercontent.com/openbot-coder/pytdxdata/main/SKILL.md

示例

开发

uv sync                        # 建虚拟环境 + 注册 tdx 命令(1.0 起会经 maturin 编译 _core,需 Rust stable)
uv run pytest tests/           # 576+ 测试用例(协议 / 池 / 缓存 / 分页 / 全接口回归 / 黄金回放 / Rust 绑定 / 文档漂移)
uv sync --group docs           # 文档站依赖(mkdocs-material + mkdocstrings)
uv run mkdocs serve            # 本地预览文档站

已知限制

  • 行情来自通达信公开服务器,15 分钟延时;需实时数据请使用券商行情
  • 标准通道 K 线 count < 100 部分服务器返回空 → 分页器已强制 min_page=100 规避
  • 港股 / 美股期权在通达信协议内无数据
  • EX 通道(港股 / 美股 / 期货 / 期权)的 K 线没有统一成交额:0x23FF 帧内只有一个可复用槽位, 权益类市场(港股主板/基金/创业板、新三板、美股)该槽位是成交额,已回填;期货 / 期权该槽位是持仓量, 所以它们的 SecurityBar.amount 恒为 0.0(协议限制,不是 bug)。期货/期权的结算价在 SecurityBar.settle。 ⚠️ 港股主板/基金的 vol 单位是「手」(=100 股),amount/vol 是均价的 100 倍
  • K 线时间戳统一为北京时间(v0.6.2 起)。美股原始报文把「美东交易日日期」与「北京时间时分」 拼在一起,库已自动归一化,bar.datetime 可直接排序 / 过滤;但美股一个交易日横跨两个北京 自然日,按交易日聚合请用 SecurityBar.trading_date,不要用 datetime.date()
  • 服务器可用性随时间变化。当日首次启动会自动在后台测速并缓存(对调用方无感,当天不再重测); 也可用 python -m pytdxdata.server_probe 离线重生成随包清单。 缓存路径与三个开关见 安装与配置 → 当日自动测速

免责声明

本包仅用于技术研究与教育目的。行情数据来自通达信公开接口,不保证实时性与准确性; 作者不对任何投资决策负责。使用前请遵守通达信服务条款及相关法律法规。

License

MIT

Metadata

Release files for pytdxdata 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytdxdata 1.0.0
File Size Uploaded Provenance Actions
pytdxdata-1.0.0.tar.gz 214.7 kB With provenance Details for pytdxdata-1.0.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for pytdxdata 1.0.0
File Actions
pytdxdata-1.0.0-cp311-abi3-win_arm64.whl CPython 3.11 abi3 Windows ARM64 Details for pytdxdata-1.0.0-cp311-abi3-win_arm64.whl
pytdxdata-1.0.0-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for pytdxdata-1.0.0-cp311-abi3-win_amd64.whl
pytdxdata-1.0.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details for pytdxdata-1.0.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
pytdxdata-1.0.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details for pytdxdata-1.0.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
pytdxdata-1.0.0-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for pytdxdata-1.0.0-cp311-abi3-macosx_11_0_arm64.whl
pytdxdata-1.0.0-cp311-abi3-macosx_10_12_x86_64.whl CPython 3.11 abi3 macOS 10.12+ x86-64 Details for pytdxdata-1.0.0-cp311-abi3-macosx_10_12_x86_64.whl

Total release size: 26.1 MB

Release files / pytdxdata-1.0.0.tar.gz

Download URL pytdxdata-1.0.0.tar.gz
Size 214.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b9104e21e5e345fe8f0d000aa206af031f717a7010861790baef9598059140c4
BLAKE2b-256 checksum
How to use checksums
e97a6f8056f11dfa3f18f07df99322472974e7f92510119cbb294e553bdbe985
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / pytdxdata-1.0.0-cp311-abi3-win_arm64.whl

Download URL pytdxdata-1.0.0-cp311-abi3-win_arm64.whl
Size 4.0 MB
Tags CPython 3.11 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
db46796dc018f2fc25cd8b5ae9c7adfdf3dbe36cdd17302f769a49f41b2e3162
BLAKE2b-256 checksum
How to use checksums
9ddfe5917180528b5bb8b616fe1482dc958a0a2c98036dca8b4507a8bcafa830
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / pytdxdata-1.0.0-cp311-abi3-win_amd64.whl

Download URL pytdxdata-1.0.0-cp311-abi3-win_amd64.whl
Size 4.4 MB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
658c32f3decfa0a76e1ec7cb84e3cef0ad859b4ae0ef4b45722245bb3c847ee7
BLAKE2b-256 checksum
How to use checksums
849b67b33341295a211531f4663abe1982f3043a1ee5def55ab8a1677da5e5d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / pytdxdata-1.0.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL pytdxdata-1.0.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 4.7 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
6b7c58c8e0dc26a402357b2ead7f68f7e3fd4ba30fdaeb2c721ca918f8687bc0
BLAKE2b-256 checksum
How to use checksums
a70644b09954f6b01e1ffc94e1e11dd4512f8844224171f73f9f5a8a3285f526
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / pytdxdata-1.0.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL pytdxdata-1.0.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 4.3 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
40bf77703c9ba492a2b3b2df2f29a8d4f70e436455301238226d8eee705581e0
BLAKE2b-256 checksum
How to use checksums
a3a0677a6378037085d38cb46d315c3d3745380d51880b2e8dd371f6a1c4e7bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / pytdxdata-1.0.0-cp311-abi3-macosx_11_0_arm64.whl

Download URL pytdxdata-1.0.0-cp311-abi3-macosx_11_0_arm64.whl
Size 4.1 MB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
2332283bbe751a1cc24556c089694c21e0a0fca647661b72390a2304eac757bd
BLAKE2b-256 checksum
How to use checksums
512c482dc516b66bff87820465348db421a715918ee698c33a6e7d73da47475e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Release files / pytdxdata-1.0.0-cp311-abi3-macosx_10_12_x86_64.whl

Download URL pytdxdata-1.0.0-cp311-abi3-macosx_10_12_x86_64.whl
Size 4.3 MB
Tags CPython 3.11 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
f4b75cf8aca4bdabee916818a7eb7420afeeda33a476edbe0c7f4abe776ea70d
BLAKE2b-256 checksum
How to use checksums
d9f59df0bb49dcf756a00896f677e989ffb0a6fd35190aa6fc3555ce24c202a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

Security for pytdxdata 1.0.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.

Full provenance

All 7 files in this release have been uploaded with provenance metadata.

Select from 7 files

Attestation for pytdxdata-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.

Transparency log

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page