feat: add offline data write-back and sync commands, bump to v1.6.0

- Add write_daily.py: encode/append daily bars to .day files
- Add write_ex_daily.py: encode/append extended market daily bars
- Add write_min_bar.py: encode/append minute bars (.5/.lc1/.lc5)
- Add sync-daily CLI: sync single stock with pagination support
- Add sync-all CLI: one-command sync for all SH/SZ .day files
- Update README with sync commands and Python write API docs
- 50 new unit tests covering encode round-trip, append dedup, edge cases
- Bump version 1.5.0 -> 1.6.0
This commit is contained in:
Justin Gu
2026-06-07 21:13:49 +08:00
parent b17e98468b
commit d01b11fa74
10 changed files with 1583 additions and 12 deletions
+53 -3
View File
@@ -3,7 +3,7 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
[![PyPI](https://img.shields.io/pypi/v/easy-tdx.svg)](https://pypi.org/project/easy-tdx/)
通达信 TCP 行情协议客户端。支持 A 股、港股、美股、期货全市场;内置 `easy-tdx` CLI 工具,默认 JSON 输出,天然适配 Claude Code、OpenClaw、Hermes 等 AI Agent 工具链。提供同步 + asyncio 双接口;strict mypy 通过;每一层编解码都有离线 fixture 测试覆盖。
通达信 TCP 行情协议客户端。支持 A 股、港股、美股、期货全市场;内置 `easy-tdx` CLI 工具,默认 JSON 输出,天然适配 Claude Code、OpenClaw、Hermes 等 AI Agent 工具链。提供同步 + asyncio 双接口;支持离线数据读取与写入同步;strict mypy 通过;每一层编解码都有离线 fixture 测试覆盖。
## 安装
@@ -213,7 +213,7 @@ easy-tdx ex quote-list HK_MAIN_BOARD --table # 港股商品列表
easy-tdx ex tick HK_MAIN_BOARD 00700 --table # 港股分时
```
### 离线数据(无需网络
### 离线数据(读取 + 写入同步
从本地通达信安装目录直接读取数据文件,无需网络连接:
@@ -228,6 +228,19 @@ easy-tdx offline financial C:\new_jyplug\vipdoc\fin\gpcw20260331.dat # 历史
easy-tdx offline blocks C:\new_jyplug\T0002\blocknew --table # 自定义板块
```
从服务端获取最新日线并写入本地 .day 文件,替代通达信内置下载功能:
```bash
# 同步单只股票日线(自动增量/全量)
easy-tdx offline sync-daily SZ 000001
easy-tdx offline sync-daily SH 600519 --vipdoc C:\new_jyplug\vipdoc
# 一键同步沪深全市场(每天一条命令)
easy-tdx offline sync-all
```
> 建议在通达信关闭时执行 sync 命令,避免文件被锁定。空文件自动全量下载,已有数据只做增量追加。
## CLI 命令汇总
| 命令 | 说明 |
@@ -261,6 +274,8 @@ easy-tdx offline blocks C:\new_jyplug\T0002\blocknew --table # 自定
| `ex markets` | 列出可用扩展市场 |
| `offline home` | 检测通达信安装目录 |
| `offline daily` | A 股日线(本地 .day 文件) |
| `offline sync-daily` | 从服务端同步单只股票日线到本地 .day 文件 |
| `offline sync-all` | 一键同步沪深全市场日线(扫描本地 .day 文件) |
| `offline min` | 分钟线(本地 .5/.lc1/.lc5 文件) |
| `offline ex-files` | 列出扩展市场可用文件 |
| `offline ex-daily` | 扩展市场日线(期货/港股/外盘) |
@@ -543,6 +558,29 @@ bars = read_daily_bars(filepath)
支持:日线、分钟线、扩展市场日线、板块、股本变迁、历史财务数据。
### 离线数据写入同步
从服务端获取最新数据并追加写入本地通达信数据文件:
```python
from easy_tdx.offline import (
encode_daily_bar, append_daily_bars, get_last_bar_date,
encode_5min_bar, append_5min_bars,
encode_lc_min_bar, append_lc_min_bars,
)
from easy_tdx import Market
from easy_tdx.client import TdxClient
# 追加日线到 .day 文件(自动跳过重复日期)
from easy_tdx.offline import sync_daily_bars_from_security_bars
# 手动编码单条记录
bar_bytes = encode_daily_bar(bar, price_coeff=0.01, vol_coeff=0.01)
# 获取文件末尾日期
last_date = get_last_bar_date("C:/new_jyplug/vipdoc/sh/lday/sh600000.day")
```
v1.5.0 起可通过 CLI 直接使用:
```bash
@@ -720,7 +758,7 @@ src/easy_tdx/
├── commands/ # 标准协议命令(无 IO)
├── codec/ # price / volume / datetime / frame / bitmap 编解码
├── models/ # 纯 dataclass,无业务逻辑
├── offline/ # 离线数据读模块
├── offline/ # 离线数据读模块(读取 + 写入同步)
└── cli/ # easy-tdx CLIclick
```
@@ -747,6 +785,18 @@ ruff format --check src/ tests/ # format check
## Changelog
### 1.6.0 (2026-06-07)
**离线数据写入同步** — 从服务端获取最新日线数据并写入本地通达信 .day 文件,替代通达信内置下载功能。
- 新增 `offline sync-daily` CLI 命令:同步单只股票日线,自动增量/全量判断,支持分页获取完整历史
- 新增 `offline sync-all` CLI 命令:一键扫描沪深全市场 .day 文件并同步
- 新增 `write_daily.py` 模块:日线编解码(`encode_daily_bar`)、追加写入(`append_daily_bars`)、末尾日期检测
- 新增 `write_ex_daily.py` 模块:扩展市场日线写入(期货/港股,价格 float32)
- 新增 `write_min_bar.py` 模块:分钟线写入(.5/.lc1/.lc5 格式)
- 写入自动跳过重复日期,空文件自动全量下载,已有数据只做增量追加
- 50 个新增单元测试覆盖编解码 round-trip、追加去重、边界条件
### 1.5.0 (2026-06-02)
**离线数据 CLI 命令** — 新增 `offline` 命令组,无需网络即可通过 CLI 读取本地通达信数据文件。