Skip to main content

通达信行情协议 Python 库,支持快照、分时、逐笔、K 线、集合竞价和历史 09:25 竞价接口

Project description

eltdx

通达信在线行情协议 Python 库。可以拿沪深北 A 股的实时快照、分时、逐笔、K 线、集合竞价、历史 09:25 竞价、股本变化和复权因子。

PyPI Python 3.10+ Build License

安装

pip install eltdx

需要 Python 3.10 或更高版本。

快速示例

from eltdx import TdxClient

with TdxClient() as client:
    quotes = client.get_quote(["sz000001", "sh600000"])

for quote in quotes:
    print(quote.code, quote.last_price, quote.last_close_price, quote.server_time)

K 线:

from eltdx import TdxClient

with TdxClient() as client:
    kline = client.get_kline("sz000001", "day", count=5)

for item in kline.items:
    print(item.time, item.close_price, item.volume)

历史 09:25 竞价:

from eltdx import TdxClient

with TdxClient() as client:
    row = client.get_auction_0925("000001", "2026-04-09")

print(row.code, row.trading_date, row.has_auction_0925)
print(row.price, row.volume, row.amount)

接口

数据 方法 说明
行情快照 get_quote() 最新价、昨收、今开、最高、最低、五档盘口、成交量额等
代码表 get_codes() / get_codes_all() 底层代码表,包含股票、指数、ETF、基金等
常用代码清单 get_a_share_codes_all() / get_etf_codes_all() / get_index_codes_all() 对代码表做常用过滤
分时 get_minute() / get_history_minute() 实时分时和历史分时
逐笔 get_trades() / get_trades_all() 实时逐笔、历史逐笔、自动翻页
K 线 get_kline() / get_kline_all() 支持分钟、日、周、月等周期
复权 K 线 get_adjusted_kline() / get_adjusted_kline_all() 支持前复权和后复权
集合竞价 get_call_auction() 集合竞价序列
历史 09:25 get_auction_0925() 快速定位指定交易日 09:25 那一笔
公司行为 / 股本 get_gbbq() / get_xdxr() / get_equity() / get_factors() 除权除息、股本变化、复权因子

完整参数和字段说明见 API_REFERENCE.mdFIELD_REFERENCE.md

服务器选择

不传服务器地址时,会使用包内 tdx_server.json 的默认列表,已按测速结果排序。

from eltdx import TdxClient

with TdxClient() as client:
    quote = client.get_quote("sz000001")[0]
    print(quote.last_price)

需要重新测速时打开 probe_hosts=True

from eltdx import TdxClient

with TdxClient(probe_hosts=True) as client:
    print(client.get_quote("sz000001")[0].last_price)

手动指定服务器:

from eltdx import TdxClient

hosts = ["116.205.183.150:7709", "116.205.171.132:7709"]

with TdxClient(hosts=hosts, pool_size=2, timeout=8.0) as client:
    print(client.get_quote(["sz000001", "sh600000"]))
参数 默认值 说明
host None 指定单个服务器
hosts None 指定多个服务器,优先级高于 host
pool_size 2 连接池大小
batch_size 80 get_quote() 自动分批大小,上限为 80
probe_hosts False 初始化时对候选服务器测速并重排
timeout 8.0 socket 请求超时秒数

MCP

普通 SDK 不依赖 MCP。需要给 MCP 客户端或 Agent 用时:

pip install "eltdx[mcp]"
eltdx-mcp
类别 工具 作用
行情 tdx_get_quote 实时行情快照
行情 tdx_get_minute 实时 / 历史分时
K 线 tdx_get_kline / tdx_get_kline_all 单页 / 全量 K 线,支持 qfqhfq 复权
逐笔 tdx_get_trades / tdx_get_trades_all 单页 / 全量逐笔成交
竞价 tdx_get_auction_0925 / tdx_get_call_auction 历史 09:25 竞价笔、实时集合竞价序列
代码表 tdx_get_count / tdx_get_codes / tdx_get_code_list 市场数量、分页代码表、A 股 / 股票 / ETF / 指数列表
股本复权 tdx_get_gbbq / tdx_get_xdxr / tdx_get_factors 股本变迁、除权除息、复权因子
股本复权 tdx_get_equity_changes / tdx_get_equity / tdx_get_turnover 股本变化、指定日期股本、换手率计算
衍生 tdx_get_trade_minute_kline 用逐笔聚合分钟 K 线

_all 的 MCP 工具默认只返回前 1000 条,并带 totalstartlimit 字段;需要全量时显式传 limit=null

注意

get_count() 不是股票总数

get_count("sh") 读的是通达信代码表条目数,不是股票数量。A 股数量和列表:

client.get_a_share_count("sh")
client.get_a_share_codes_all()

get_codes() 不只返回股票

底层代码表会混有股票、指数、ETF、基金、债券回购、板块分类项等。常用过滤:

client.get_a_share_codes_all()
client.get_etf_codes_all()
client.get_index_codes_all()

代码建议带市场前缀

推荐写完整代码,例如 sz000001sh600000bj920001

原始协议数据可以保留

排查协议解析问题时用 include_raw=True

with TdxClient() as client:
    minute = client.get_minute("sz000001", include_raw=True)
    print(minute.raw_frame_hex)
    print(minute.raw_payload_hex)

文档

文档 内容
docs/API_REFERENCE.md API 参数和返回值
docs/EXAMPLES.md 可直接复制的示例
docs/FIELD_REFERENCE.md 字段含义和口径
docs/DEBUG_GUIDE.md 连接、服务器和 raw 数据排查
scripts/README.md smoke / validation 脚本说明

开发

pytest tests/unit
python -m build

联网测试需要真实服务器:

set ELTDX_RUN_LIVE=1
pytest tests/integration

参考

感谢 injoyai/tdx 的启发。

联系

许可证

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

eltdx-0.5.1.tar.gz (98.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

eltdx-0.5.1-py3-none-any.whl (46.9 kB view details)

Uploaded Python 3

File details

Details for the file eltdx-0.5.1.tar.gz.

File metadata

  • Download URL: eltdx-0.5.1.tar.gz
  • Upload date:
  • Size: 98.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for eltdx-0.5.1.tar.gz
Algorithm Hash digest
SHA256 fa2b08a75978914f694e6cb155b0d2380ff6131129a432c5a6301125a0d91f88
MD5 e8585fbff2d95137d044caca7e5c6815
BLAKE2b-256 e2fadc4787fbc60b9aac3c0c8111ea01f7553772e03aee4af4b4d35cb6ef188c

See more details on using hashes here.

Provenance

The following attestation bundles were made for eltdx-0.5.1.tar.gz:

Publisher: publish.yml on electkismet/eltdx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file eltdx-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: eltdx-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 46.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for eltdx-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cdf50638c01418085cb1192b1b42d00db0cc0e38614e71f60f56cb3e83c09ba5
MD5 a08bff061f615f5fe14fd0b47c021687
BLAKE2b-256 5223cc071f42cc5afbe7412b176646f70f85148d3fadd44e4ea6280790c6431b

See more details on using hashes here.

Provenance

The following attestation bundles were made for eltdx-0.5.1-py3-none-any.whl:

Publisher: publish.yml on electkismet/eltdx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page