v0.4.0: 初始开源发布

通达信(TDX)行情数据客户端 —— 动态连接池 + 双层TTL缓存 + 3连接并发分页,
纯 asyncio、零运行时依赖。一套接口覆盖 A股(沪/深/北) / 港股 / 美股 / 期货 / 期权。

主要能力:
- 四层架构:api → pool(标准/MAC/EX 三池) → protocol(16 命令模块) → cache(内存+SQLite)
- TdxData 提供 51 个公开异步方法;CLI(tdx) 提供 19 个子命令
- 服务器文件下载 + 解析:板块 block_*.dat / 行业 tdxhy.cfg / 历史财报 gpcw*.zip
- 85 个离线测试(协议 / 连接池 / 缓存 / 分页 / API / 文件解析 / CLI)

协议层参照 easy-tdx 的字节级格式独立实现,不依赖 easy-tdx 包。
This commit is contained in:
openbot-coder committed 2026-09-15 11:38:44 +08:00
commit 9183c3294f
68 files changed
+9249

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
"""pytdxdata 快速上手:报价 / K线 / 分时 / 板块汇总。
运行:
python examples/quickstart.py
说明:数据来自通达信公开行情服务器(无需账号,15 分钟延时)。
"""
import asyncio
from pytdxdata import KlinePeriod, Market, TdxData
async def main() -> None:
# TdxData 为异步上下文管理器:进入时建连,退出时释放
async with TdxData() as td:
# 1) 批量五档报价(可一次传多只,超过 80 只自动切批)
quotes = await td.get_quotes([(Market.SH, "600000"), (Market.SZ, "000001")])
print("== 报价 ==")
for q in quotes:
print(
f"{q.code} 现价={q.price} 昨收={q.pre_close} "
f"买一={q.bid[0]} 卖一={q.ask[0]} 成交量={q.vol}"
)
# 2) 日K线(最近 10 根)
bars = await td.get_kline(Market.SH, "600000", KlinePeriod.DAY, count=10)
print("\n== 日K(最近 3 根)==")
for b in bars[-3:]:
print(
f"{b.datetime:%Y-%m-%d} "
f"开={b.open} 高={b.high} 低={b.low} 收={b.close} 量={b.vol}"
)
# 3) 当日分时
minutes = await td.get_minute(Market.SH, "600000")
latest = minutes[-1].price if minutes else "-"
print(f"\n== 分时 ==\n共 {len(minutes)} 个点,最新价={latest}")
# 4) 板块汇总(示例:通达信行业板块 881001)
summary = await td.get_board_summary("881001")
print("\n== 板块汇总 881001 ==")
for key, value in summary.items():
print(f" {key}: {value}")
if __name__ == "__main__":
asyncio.run(main())
+50
View File
@@ -0,0 +1,50 @@
"""服务器文件下载 + 解析:板块 / 行业分类 / 历史专业财报。
运行:
python examples/server_files.py
这组接口把通达信服务器上的二进制文件(block_*.dat / tdxhy.cfg / gpcw*.zip)
直接下载并解析为结构化数据(解析器位于 pytdxdata.codec,纯标准库、可脱网单测)。
"""
import asyncio
from pytdxdata import TdxData
async def main() -> None:
async with TdxData() as td:
# 1) 板块文件 -> 板块成分
# block_zs.dat=行业 / block_gn.dat=概念 / block_fg.dat=风格
blocks = await td.get_block_parsed("block_gn.dat")
print(f"== 概念板块(共 {len(blocks)} 个)==")
for blk in blocks[:5]:
print(f" [{blk.category}] {blk.name} 成分数={blk.count} 示例={blk.codes[:5]}")
# 2) 行业分类(tdxhy.cfg)-> {6位代码: 通达信行业 + 申万行业}
industry = await td.get_industry_map()
print(f"\n== 行业分类(共 {len(industry)} 只)==")
for code in ("600000", "000001", "300750"):
info = industry.get(code)
if info is not None:
sw = info.sw_industry or "-"
print(f" {code} 通达信={info.tdx_industry} 申万={sw}")
# 3) 历史专业财报文件索引(tdxfin/gpcw.txt)
infos = await td.get_financial_file_infos()
print(f"\n== 财报文件索引(共 {len(infos)} 个)==")
for fi in infos[-3:]:
print(f" {fi.filename} md5={fi.hash[:12]}... {fi.filesize} 字节")
# 4) 下载并解析最新一期财报(gpcw*.zip -> 每股原始 float 字段)
if infos:
latest = infos[-1].filename
records = await td.get_financial_records_parsed(latest)
print(f"\n== {latest} 财报记录(共 {len(records)} 条)==")
for r in records[:5]:
market = "沪" if r.market == 1 else "深"
print(f" {r.code} 市场={market} 报告期={r.report_date} 字段数={len(r.fields)}")
if __name__ == "__main__":
asyncio.run(main())