Skip to main content

Python client for Tongdaxin market data protocol

Project description

eltdx

通达信行情协议的 Python 客户端库

安装后就能直接调快照、分时、逐笔、K 线、集合竞价等接口,返回结果统一、字段清楚,也支持查看原始十六进制数据。

PyPI Python 3.10+ CI License

这是什么

eltdx 想做的事情很简单:

  • 给你一个统一入口:TdxClient
  • 让常用接口直接就能调:get_quote()get_minute()get_trades()get_kline()
  • 返回结果尽量好懂:统一 dataclass,不直接丢一堆裸 dict
  • 时间字段直接给你 Python 原生 date / datetime
  • 价格字段同时保留浮点值和 *_milli 整数值
  • 需要排查问题时,可以打开 include_raw=True 看原始十六进制

当前版本重点覆盖:

  • 行情快照
  • 分时
  • 逐笔
  • K 线
  • 集合竞价
  • 公司行为 / 股本变化 / 复权因子
  • 一些常用 helper

项目参考

安装

最推荐的安装方式:

python -m pip install eltdx

升级:

python -m pip install -U eltdx

如果你是拉源码自己调:

pip install -e .

环境要求:

  • 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.server_time)

如果你想看原始协议数据:

from eltdx import TdxClient

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

如果你想自己指定服务器地址:

from eltdx import TdxClient

with TdxClient(
    hosts=["124.71.187.122:7709", "122.51.120.217:7709"],
    pool_size=2,
    batch_size=80,
) as client:
    quote = client.get_quote("sz000001")[0]
    print(quote.last_price)

常用功能

先连上,再取数据

  • connect():主动建立连接
  • close():关闭连接
  • with TdxClient() as client::适合一次性脚本,跑完自动关

最常会用到的接口

  • get_quote():拿快照行情
  • get_minute() / get_history_minute():拿分时
  • get_trades() / get_trades_all():拿逐笔
  • get_kline() / get_kline_all():拿 K 线
  • get_call_auction():拿集合竞价
  • get_gbbq() / get_xdxr() / get_equity() / get_factors():拿公司行为、股本和复权相关数据

用起来比较省心的地方

  • with 可用可不用;短任务推荐用,长连接也可以手动控制
  • get_quote() 自带自动分批,不用你自己切列表
  • 默认支持多连接分发,批量取快照会更稳一些
  • 返回字段统一,做展示、落库、计算都比较顺手

使用时先知道

get_count(exchange) 不是股票总数

它更像“这个市场的代码表条目数”,不是“这个市场一共有多少只股票”。

如果你更想拿到股票口径的数量,优先试:

  • get_stock_count(exchange)
  • get_a_share_count(exchange)

get_codes() / get_codes_all() 不只是股票列表

这里面会混有股票、指数、板块分类项、ETF、基金、债券回购等条目。

如果你想直接拿一组更实用的代码去拉行情,优先试:

  • get_stock_codes_all()
  • get_a_share_codes_all()
  • get_etf_codes_all()
  • get_index_codes_all()

with 不是强制的

  • 你只是临时拉一把数据:用 with
  • 你要一直保持连接:手动 connect() / close() 也完全可以

hosthosts 都可以传

  • host=:只测一个地址时比较方便
  • hosts=:传多个地址,交给客户端自己处理
  • 不传时会回退到库内默认地址列表

文档怎么读

如果你是第一次用,建议按这个顺序看:

FAQ

如果我只拉一次数据,推荐怎么写?

直接用:

with TdxClient() as client:
    ...

这样最省心,代码块结束后会自动关连接。

如果我要做长连接实时场景呢?

那就自己控制连接生命周期:

client = TdxClient()
client.connect()
try:
    ...
finally:
    client.close()

为什么 get_count("sh") 看起来特别大?

因为它不是股票总数,而是代码表条目数。

为什么 get_codes() 里看起来不全是股票?

因为它本来就不只是股票,还会带上指数、ETF、板块分类项等条目。

我只想拿 A 股代码,应该用哪个?

优先用:

  • get_a_share_codes_all()

如果你希望把 B 股也一起算进去,再试:

  • get_stock_codes_all()

联系方式

  • QQ 群:复盘软件交流群(群号:982691484
  • 加群链接:点击链接加入群聊
  • 邮箱:dapaoxixixi@163.com

许可证

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.1.3.tar.gz (75.7 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.1.3-py3-none-any.whl (38.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for eltdx-0.1.3.tar.gz
Algorithm Hash digest
SHA256 6298835a75724434e1a0e3682a6fc375393e5c7a7b35425230b2879a491c8f21
MD5 e452088c2eb9ae0b21f2fbbd2c1040b9
BLAKE2b-256 58759d796d009b686402f1833a3be1a1dc18529344520879649794600032a216

See more details on using hashes here.

Provenance

The following attestation bundles were made for eltdx-0.1.3.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.1.3-py3-none-any.whl.

File metadata

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

File hashes

Hashes for eltdx-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b4bd663a4617e21f2733c66848986eede4f8e7607ece08f0b9431239642f1018
MD5 51efd5fd585e466e3236b8f4bbc42fee
BLAKE2b-256 27348fc9a8292581b2c3feb94ffde54807184beeb6b34dbcfa9ebaf197c09144

See more details on using hashes here.

Provenance

The following attestation bundles were made for eltdx-0.1.3-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