Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b84a6c8a1b | ||
|
|
ca24521d2c | ||
|
|
8344ed6e6b | ||
|
|
dfd6fab3af | ||
|
|
0618062e54 | ||
|
|
9c97fea71b | ||
|
|
2d62d82b51 | ||
|
|
7e72f70eb2 | ||
|
|
b2f899df49 | ||
|
|
8193d560da | ||
|
|
5408903a4c |
No files matched your search
+251
@@ -2,6 +2,257 @@
|
|||||||
|
|
||||||
本项目遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
本项目遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
||||||
|
|
||||||
|
## [0.6.5] - 2026-10-22
|
||||||
|
|
||||||
|
### 新增
|
||||||
|
|
||||||
|
- **指数成分成交汇总:一条接口拿到「某指数下全部股票的成交量与成交笔数」**
|
||||||
|
(`get_index_members` / `get_index_trade_stats` / `get_index_trade_stats_multi`)。
|
||||||
|
|
||||||
|
起因是一个绕不开的协议事实:**指数本身没有成交笔数**。指数逐笔(`sh000001`)的
|
||||||
|
`trade_count` 字段实际是「当时均价 × 1000」(2026-10-21 已验证),`get_minutes` /
|
||||||
|
`get_market_stat` / `SecurityBar` / `get_quotes` 的 160 个字段位里也都**没有**笔数
|
||||||
|
(扫描 0x122C 全字段位确认,`0x8`/`0x9` 是外盘/内盘手数)。所以「指数笔数」唯一可行
|
||||||
|
的口径是**把成分股的逐笔笔数累加**。
|
||||||
|
|
||||||
|
```python
|
||||||
|
async with TdxData() as td:
|
||||||
|
st = await td.get_index_trade_stats("000300") # 沪深300,约 10 s
|
||||||
|
for hhmm, cum in st.cum_trade_count(): # 截止该分钟的累计笔数
|
||||||
|
print(hhmm, cum)
|
||||||
|
st.minutes[0] # MinuteTradeStat(time='09:25', volume=…, trade_count=…)
|
||||||
|
st.total_volume # 手
|
||||||
|
st.total_trade_count # 笔
|
||||||
|
```
|
||||||
|
|
||||||
|
| 方法 | 作用 |
|
||||||
|
|---|---|
|
||||||
|
| `get_index_members(index, *, fields=None, count=100000)` | 指数成分股(含实时报价)。裸 6 位按**指数语义**解析:`000001`=上证指数,不是平安银行 |
|
||||||
|
| `get_index_trade_stats(index, date=None, *, concurrency=24, batch_size=50, members=None, skip_errors=True, on_progress=None)` | 成分逐笔 → 每分钟成交量/笔数;`date` 支持历史 |
|
||||||
|
| `get_index_trade_stats_multi(index, dates, *, ...)` | 多日批量(成分只请求一次),用于「近 N 日同时点均值」基准 |
|
||||||
|
|
||||||
|
**口径校验(2026-10-22 实测)**:``total_amount`` 是**成分股**合计。对「成分 = 指数
|
||||||
|
覆盖面」的指数,它与指数快照 ``amount`` 完全相等 —— 说明成分清单与量能口径一致。
|
||||||
|
|
||||||
|
| 指数 | 成分 | 成分合计 | 指数快照 | 比值 | 耗时 |
|
||||||
|
|---|---|---|---|---|---|
|
||||||
|
| `sh000016` 上证50 | 50 | 661.38 亿 | 661.34 亿 | 1.0001 | 5.8 s |
|
||||||
|
| `sh000300` 沪深300 | 300 | 2373.98 亿 | 2373.98 亿 | 1.0000 | 9.7 s |
|
||||||
|
| `sh000001` 上证指数 | 2230 | 3907.00 亿 | 4111.56 亿 | 0.9503 | 35.6 s |
|
||||||
|
|
||||||
|
历史日期同样成立(2026-10-21 收盘数据):`sh000016` 1046.29 / 1045.59 亿(1.0007)、
|
||||||
|
`sh000300` 3881.65 / 3879.30 亿(1.0006)。
|
||||||
|
|
||||||
|
单只股票级别的口径更硬:`Σ(逐笔价 × 手 × 100)` 与日线 `amount` 比值
|
||||||
|
**1.0000~1.0004**(`sh600000` / `sz000001` / `sh601398` / `bj920002` / `bj920015`
|
||||||
|
全部吻合),`Σvol` 与快照 `vol` 比值 1.0000。
|
||||||
|
|
||||||
|
CLI 同步新增:`tdx index-stats 000300 --top 10`。
|
||||||
|
|
||||||
|
**三个必须知道的限制**:
|
||||||
|
|
||||||
|
1. **深市 `399xxx` 的指数成交额是「全板块」口径**,不是样本股 —— 实测
|
||||||
|
`sz399001` 快照 5081.40 亿 = 深市 2908 只股票合计 5081.01 亿,`sz399006`
|
||||||
|
2568.60 亿 = 创业板全部股票合计。所以拿它跟 `total_amount` 比会得到 0.43 这种
|
||||||
|
无意义比值,不是数据错。**`get_index_trade_stats` 返回的始终是成分口径。**
|
||||||
|
2. **北交所(`899xxx`)没有笔数字段** —— 逐笔 `trade_count` 恒为 0,此时
|
||||||
|
`trade_count_available` 为 `False`,只有成交量可用。
|
||||||
|
3. **成分表比全市场滞后** —— 上证指数返回 2230 只,而 `get_universe("sh")` 里
|
||||||
|
股票段共 2360 只,差 130 只(新上市尚未纳入服务端板块),这正是上表
|
||||||
|
0.9503 的来源;`sh000016`/`sh000300` 这类固定样本指数不受影响。
|
||||||
|
需要精确覆盖时可自行传 `members=[...]`。
|
||||||
|
|
||||||
|
### 修复
|
||||||
|
|
||||||
|
- **债券与回购价格全线偏差:沪市少 100 倍、深市少 10 倍**(`get_quotes`)。
|
||||||
|
|
||||||
|
`price_decimal_digits` 只覆盖了「股票 2 位 / 基金 3 位」两档,债券与回购没有对应
|
||||||
|
分支,落到默认档位。它们实际是 **4 位小数**,于是被读大了两个数量级:
|
||||||
|
|
||||||
|
| 标的 | 真实价 | 修复前 | 偏差 |
|
||||||
|
|---|---|---|---|
|
||||||
|
| `sh019742` 国债 24特国01 | 110.2280 | 11022.8000 | ×100 |
|
||||||
|
| `sh110077` 可转债 洪城转债 | 199.2790 | 19927.9000 | ×100 |
|
||||||
|
| `sh204001` 回购 GC001 | 1.3850 | 138.5000 | ×100 |
|
||||||
|
| `sh900901` B股 云赛B股 | 0.5670 | 5.6700 | ×10 |
|
||||||
|
| `sz123107` 可转债 温氏转债 | 112.1890 | 1121.8900 | ×10 |
|
||||||
|
| `sz131810` 回购 R-001 | 1.3800 | 13.8000 | ×10 |
|
||||||
|
|
||||||
|
沪市 `010/018/019/020/1xx/2xx` 与深市 `10x-14x/19x/50x/52x/56x` 全部改为 4 位;
|
||||||
|
沪市 B 股 `900xxx` 另改为 3 位(与深市 `200xxx` 的 2 位不同,两市不一致)。
|
||||||
|
股票 / ETF / 指数 / 板块的行为**不变**。
|
||||||
|
|
||||||
|
**验证方式(可作为该函数的验收标准)**:拿 `pre_close`(五档快照口径,走
|
||||||
|
`price_decimal_digits`)除以上一交易日**日线收盘**(K线口径,走独立的 `/1000`
|
||||||
|
路径),比值 1.0 即两口径一致。修复后 15 类标的全数为 1.0000,零偏差。
|
||||||
|
|
||||||
|
**权威来源已确认**:证券列表里协议**自带** `SecurityInfo.decimal_point` 字段。
|
||||||
|
2026-10-22 拉取全量 **51,982 只**(沪 27,520 + 深 24,462)逐条比对,本函数与
|
||||||
|
协议字段**完全一致、零例外**——因为全量清单里没有任何一个「三位前缀」出现混合
|
||||||
|
小数位。`get_quotes` 解析时拿不到证券清单,故仍用前缀推断;需要权威值时读
|
||||||
|
`SecurityInfo.decimal_point`。
|
||||||
|
|
||||||
|
- **美股(EX 通道)分时的 `hour` 是非法时钟值**(`get_minutes("usAAPL")`)。
|
||||||
|
|
||||||
|
EX 分时的 `minutes` 字段是**自开盘起的连续分钟数**,不是「当天的分钟数」。
|
||||||
|
旧代码直接 `minutes // 60`,于是 390 条里 **240 条** 的 `hour` ≥ 24:
|
||||||
|
|
||||||
|
```
|
||||||
|
修复前: 21:30, 21:31, ..., 23:59, 24:00, 24:01, ..., 27:59 ← hour=27 不存在
|
||||||
|
修复后: 21:30, 21:31, ..., 23:59, 00:00, 00:01, ..., 03:59 ← 真实北京时间
|
||||||
|
```
|
||||||
|
|
||||||
|
任何 `datetime(...)` 拼接都会因 `hour=27` 抛 `ValueError`,按小时聚合也会把
|
||||||
|
凌晨段错误地归到同一个自然日。
|
||||||
|
|
||||||
|
**修法**:`MinuteBar.__post_init__` 把越界的 `hour` 拆成
|
||||||
|
「`hour %= 24` + `day_offset += hour // 24`」,并新增两个只读属性:
|
||||||
|
|
||||||
|
| 属性 | 用途 |
|
||||||
|
|---|---|
|
||||||
|
| `MinuteBar.datetime` | 该分时点的时刻(北京时间,已含跨日偏移) |
|
||||||
|
| `MinuteBar.trading_date` | 归属的交易日 `YYYY-MM-DD`。美股一个交易日横跨两个北京自然日(21:30~次日 04:00),**按交易日聚合必须用它**,与 `SecurityBar.trading_date` 口径一致 |
|
||||||
|
|
||||||
|
A股(合成时间 09:30~15:00)/ 港股(09:30~16:00)/ 期货的 `hour` 本就合法
|
||||||
|
→ **零行为变化**,`day_offset` 恒为 0。
|
||||||
|
|
||||||
|
实测(2026-10-22):`usAAPL` / `usTSLA` / `usNVDA` 各 390 条,修复后
|
||||||
|
`hour` 全在 0~23、时间轴严格单调、`trading_date` 收敛为单一交易日;
|
||||||
|
`hk00700` 90 条无任何变化。
|
||||||
|
|
||||||
|
### 说明
|
||||||
|
|
||||||
|
- 新增回归测试:债券/回购/B股小数位 ×29、美股分时跨日 ×6、指数成分成交汇总 ×34、
|
||||||
|
全接口 ×1,测试总数 **217 → 289**。
|
||||||
|
|
||||||
|
## [0.6.4] - 2026-10-22
|
||||||
|
|
||||||
|
### 修复
|
||||||
|
|
||||||
|
- **`get_ticks()` 返回列表不是时间序,每 `page_size` 处时间回跳一次**。
|
||||||
|
|
||||||
|
`0x122F`(A股逐笔)与 EX 逐笔通道的偏移量 `start` 都是**从最新一笔往回数**的:
|
||||||
|
实测 `sh600000`,`start=0` 返回 `14:07:48..15:20:16`(当天最新 1000 条),
|
||||||
|
`start=4000` 返回 `09:15:03..09:41:18`(当天最早)。而 `paginator` 按 `start`
|
||||||
|
升序拼接,于是拼出"新的块在前、旧的块在后"的倒序结构:
|
||||||
|
|
||||||
|
| | 修复前 | 修复后 |
|
||||||
|
|---|---|---|
|
||||||
|
| `sh600000` 时间轴 | 4 处逆序跳跃(index 999 / 1999 / 2999) | 严格升序 |
|
||||||
|
| `hk00700` 时间轴 | 21 处逆序跳跃(每 1800 条一次) | 严格升序 |
|
||||||
|
|
||||||
|
修复分两层:① `PageSpec.newest_first=True` 让 `paginator` 反页序拼装(页内本身升序,
|
||||||
|
只翻页序),这条能力保留给将来需要按"最新优先"锚点分页的命令;② A股逐笔最终改为
|
||||||
|
**单连接串行游标**(见下一条),从根上避开多主机锚点错位。
|
||||||
|
|
||||||
|
**`start` 参数语义同时明确**:它是"从最新往回数的偏移",不是"从最早往后数的偏移",
|
||||||
|
`docstring` 已补充。注意这修正的是**顺序**,数据本身无缺口、无重复(4304 条唯一时间戳)。
|
||||||
|
|
||||||
|
- **`TransactionRecord.trade_count` 在集合竞价段被读成 42 亿量级脏值**。
|
||||||
|
|
||||||
|
该字段在协议里是**有符号** `int32`,但解析格式串写的是无符号 `I`。集合竞价段
|
||||||
|
(`bs_flag=8`)真实值为负数,按 uint32 解出 `4294966422`:
|
||||||
|
|
||||||
|
```
|
||||||
|
原始 18 字节: 17820000 0ad71b41 d9000000 96fcffff 0800
|
||||||
|
修复前: trade_count = 4294966422
|
||||||
|
修复后: trade_count = -874 ← 0xfffffc96 的 int32 补码
|
||||||
|
```
|
||||||
|
|
||||||
|
实测 `sh600000` 全天 4304 条中有 **38 条**命中(占 0.88%),全部落在 09:15~09:25
|
||||||
|
集合竞价段;正常交易段(`bs_flag` 0/1/2/5)**无一条为负**。旧行为会让
|
||||||
|
`max()` / `mean()` 等统计直接失真(曾算出 `mean=37,920,247`)。
|
||||||
|
|
||||||
|
格式串由 `<IfIIH` 改为 `<IfIiH`。**注意负值语义尚未确证**(可能是撤单量/净委托),
|
||||||
|
目前只是如实保留符号,未做业务解释。
|
||||||
|
|
||||||
|
- **`get_quotes()` 沪市指数价格整体少 10 倍**。
|
||||||
|
|
||||||
|
`price_decimal_digits` 把沪市 `000`(如 000001/000016/000300/000688)与 `8`
|
||||||
|
开头(如板块指数 880001/880305)一律判成 3 位小数(除 1000),但协议原始整数
|
||||||
|
实际是 **2 位**(除 100)→ 上证指数 3713 被读成 371、上证50 2747 被读成 274。
|
||||||
|
|
||||||
|
实证(2026-10-21):沪市指数 6 只 + 板块 2 只全部 `分时中位价 / 快照价 = 10.0`,
|
||||||
|
而沪市 ETF(510300/588000/511010/501018)、股票(60/601)、科创板(688)、
|
||||||
|
深市指数/股票/创业板/ETF 全部 `= 1.0` 正确。修法是**把沪市 `000`/`8` 从 3 位改回 2 位**,
|
||||||
|
其余前缀(`5` ETF/基金、深市 `1` 基金转债)保持 3 位不变。回归测试覆盖 21 组代码。
|
||||||
|
|
||||||
|
- **`get_ticks()` 把集合竞价**委托**(`bs_flag=8`)当成成交计入,导致 Σvol 虚高且跨主机不稳定**。
|
||||||
|
|
||||||
|
`0x122F` 第 5 字段 `bs_flag=8` 的记录是 **09:15~09:25 / 14:57~15:00 竞价段的撮合委托
|
||||||
|
快照**,`vol` 是**累计值**(可被同段后续记录覆盖),根本不是成交增量。旧代码把它一并
|
||||||
|
返回,两个后果:
|
||||||
|
|
||||||
|
1. **Σvol 比真值虚高**:`sh600000` 全天 Σvol 含 b8 时 = 668,693,快照 vol = 462,562,
|
||||||
|
比值 1.4456;剔掉 b8 后 Σvol = 462,634,比值 **1.000156**(与快照精确一致)。
|
||||||
|
2. **跨主机不稳定**:干净主机不收录 b8(0 条)、脏主机收录 135 条。并发分页时各页
|
||||||
|
来自不同主机,同一 `start`(从最新往回数的相对锚点)在不同主机落到**不同的真实成交**
|
||||||
|
上 → 页边界既**重叠**(sh600000 冗余 116 条)又**漏条**(13:03 附近丢 59 条),
|
||||||
|
同一查询两次调用结果不同。
|
||||||
|
|
||||||
|
修复:① 分页拼装后**统一剔除 `bs_flag=8`**;② A股逐笔改用**单连接串行游标翻页**
|
||||||
|
(`_ticks_cn_serial`),彻底消除跨主机锚点错位。修复后单主机 1.0002、剔 b8 后跨主机
|
||||||
|
公共时间窗**逐条一致**。
|
||||||
|
|
||||||
|
- **文档化指数逐笔字段的量纲差异**(非 bug,是数据源固有语义)。
|
||||||
|
|
||||||
|
指数(如 `sh000016`)的 `vol` 单位是**百元**(成交额/100),`trade_count` 是**均价×1000**
|
||||||
|
(不是笔数),与个股完全不同。实测 `快照amount / Σtick.vol = 100.0002`、`tc/1000` 与分时
|
||||||
|
`avg_price` 差 < 1.1。已写进 `TransactionRecord` 类注释与 `get_ticks` docstring,
|
||||||
|
避免用户把指数 Σvol 直接和成分股成交量比(会误得 ~26 倍)。
|
||||||
|
|
||||||
|
### 说明
|
||||||
|
|
||||||
|
- 新增回归测试:价格小数位 ×20(19 组参数化 + 1 组去 NUL)、`get_ticks` 剔 b8 /
|
||||||
|
单连接串行 / 多页无重叠 ×3,测试总数 **194 → 217**。
|
||||||
|
|
||||||
|
## [0.6.3] - 2026-10-18
|
||||||
|
|
||||||
|
- fix(pool): ExPool acquire 背压等待(Condition 排队,默认 30s 超时)替代 3x0.05s 快速失败;超时异常带池状态
|
||||||
|
- feat(api): 新增 ex_pool_cap 参数(默认 2),单 EX 服务器场景不再静默退化为 1 并发(fixes #1)
|
||||||
|
|
||||||
|
## [0.6.2] - 2026-10-13
|
||||||
|
|
||||||
|
### 修复
|
||||||
|
|
||||||
|
- **美股分钟级 K 线的 `datetime` 全部错位**(`get_bars("usAAPL", KlinePeriod.MIN_1)`)。
|
||||||
|
|
||||||
|
通达信 EX 通道返回的美股分钟线时间戳,把**两个时区拼进了同一个五元组**:日期分量是
|
||||||
|
**美东交易日**(协议里的 `zipday`),时分分量却是**北京时间**(`tminutes`)。直拼出的
|
||||||
|
`datetime` 会把同一交易日的**下午半场排到上午半场之前**——单日 390 根不仅首末颠倒,
|
||||||
|
而且全部落在同一个北京自然日里。于是任何依赖 `datetime` 的行为都是错的:排序、
|
||||||
|
`count` 切片("取最新 N 根")、日期区间二分与过滤、增量更新水位线。
|
||||||
|
|
||||||
|
| 观测项 | 修复前 | 修复后 |
|
||||||
|
|---|---|---|
|
||||||
|
| 单日 390 根的日期分布 | **全部**落在 10-12(hour 0~23 混在一起) | 10-12 的 21:31~23:59(149 根)+ 10-13 的 00:00~04:00(241 根) |
|
||||||
|
| 时间轴 | 上午半场被排到下午半场**之前** | 严格单调递增 |
|
||||||
|
| `count=30` 是否取到真正最新的 30 根 | 否(拿到上一交易日的 23:30~23:59) | 是(当日收盘段 03:31~04:00) |
|
||||||
|
| 每组首根 `open` / 末根 `close` 对日线 | 对不上 | **逐位相同**(10-07/08/09/12 四个交易日实测) |
|
||||||
|
|
||||||
|
现在 `SecurityBar.datetime` **统一为北京时间**:美股凌晨段(北京 hour < 12)的日期
|
||||||
|
自动 +1 天(含月末 / 年末进位)。A股 / 港股 / 期货原始即为北京时间,**零行为变化**。
|
||||||
|
|
||||||
|
新增两个只读属性:
|
||||||
|
|
||||||
|
| 属性 | 用途 |
|
||||||
|
|---|---|
|
||||||
|
| `SecurityBar.trading_date` | 该 bar 归属的交易日 `YYYY-MM-DD`。美股一个交易日横跨两个北京自然日,**按交易日聚合(分钟线合成日线、日内统计)必须用它,不能用 `datetime.date()`** |
|
||||||
|
| `SecurityBar.is_us` | 是否美股(EX 通道 `US_STOCK`) |
|
||||||
|
|
||||||
|
归一化在 `SecurityBar.__post_init__` 中实现,因此 **EX 的两条 K 线命令路径**
|
||||||
|
(`_bars_ex` 的 0x23FF 与日期区间二分所依赖的 MAC 0x122E)自动共用同一份口径,
|
||||||
|
不会出现"取数按一套时间、二分按另一套时间"的错位。
|
||||||
|
|
||||||
|
### 说明
|
||||||
|
|
||||||
|
- 分钟线缓存 TTL 为 **60 秒**,升级后旧缓存最多残留 1 分钟即自然失效,**无需手动清理**;
|
||||||
|
日线(`period >= DAY`)不受本缺陷影响。
|
||||||
|
- 本版只修 K 线。**同日发现的同类问题未包含在内**:EX 分时(`get_minutes`)对美股返回的
|
||||||
|
`MinuteBar.hour` 是**未取模的连续分钟数**(实测可达 27,即次日 03:xx),且 `MinuteBar`
|
||||||
|
无日期分量可承载跨日信息——修法涉及"是否新增字段 / 日期如何归属"的取舍,留待下一版决策。
|
||||||
|
- 新增 9 项针对性回归测试(分界点、月末与年末进位、390 根整日单调性、MAC 路径同样归一化)。
|
||||||
|
|
||||||
## [0.6.1] - 2026-09-21
|
## [0.6.1] - 2026-09-21
|
||||||
|
|
||||||
### 变更
|
### 变更
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ curl -o ~/.workbuddy/skills/pytdxdata/SKILL.md --create-dirs \
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
uv sync # 建虚拟环境 + 注册 tdx 命令
|
uv sync # 建虚拟环境 + 注册 tdx 命令
|
||||||
uv run pytest tests/ # 182 个测试用例(协议 / 池 / 缓存 / 分页 / 全接口回归 / 文件解析 / CLI / 文档漂移 / 当日测速缓存)
|
uv run pytest tests/ # 191 个测试用例(协议 / 池 / 缓存 / 分页 / 全接口回归 / 文件解析 / CLI / 文档漂移 / 当日测速缓存)
|
||||||
uv sync --group docs # 文档站依赖(mkdocs-material + mkdocstrings)
|
uv sync --group docs # 文档站依赖(mkdocs-material + mkdocstrings)
|
||||||
uv run mkdocs serve # 本地预览文档站
|
uv run mkdocs serve # 本地预览文档站
|
||||||
```
|
```
|
||||||
@@ -114,6 +114,9 @@ uv run mkdocs serve # 本地预览文档站
|
|||||||
- 行情来自通达信公开服务器,**15 分钟延时**;需实时数据请使用券商行情
|
- 行情来自通达信公开服务器,**15 分钟延时**;需实时数据请使用券商行情
|
||||||
- 标准通道 K 线 `count < 100` 部分服务器返回空 → 分页器已强制 `min_page=100` 规避
|
- 标准通道 K 线 `count < 100` 部分服务器返回空 → 分页器已强制 `min_page=100` 规避
|
||||||
- 港股 / 美股期权在通达信协议内无数据
|
- 港股 / 美股期权在通达信协议内无数据
|
||||||
|
- **K 线时间戳统一为北京时间**(v0.6.2 起)。美股原始报文把「美东交易日日期」与「北京时间时分」
|
||||||
|
拼在一起,库已自动归一化,`bar.datetime` 可直接排序 / 过滤;但**美股一个交易日横跨两个北京
|
||||||
|
自然日**,按交易日聚合请用 `SecurityBar.trading_date`,不要用 `datetime.date()`
|
||||||
- 服务器可用性随时间变化。**当日首次启动会自动在后台测速并缓存**(对调用方无感,当天不再重测);
|
- 服务器可用性随时间变化。**当日首次启动会自动在后台测速并缓存**(对调用方无感,当天不再重测);
|
||||||
也可用 `python -m pytdxdata.server_probe` 离线重生成随包清单。
|
也可用 `python -m pytdxdata.server_probe` 离线重生成随包清单。
|
||||||
缓存路径与三个开关见 [安装与配置 → 当日自动测速](https://openbot-coder.github.io/pytdxdata/getting-started/#当日自动测速无感)
|
缓存路径与三个开关见 [安装与配置 → 当日自动测速](https://openbot-coder.github.io/pytdxdata/getting-started/#当日自动测速无感)
|
||||||
|
|||||||
@@ -149,6 +149,8 @@ await td.get_bars("cffex:IFL0", KlinePeriod.DAY, count=700) # 期货合约
|
|||||||
| 除权除息 / 财务快照 | `get_xdxr(symbols)` / `get_finance(symbols)` |
|
| 除权除息 / 财务快照 | `get_xdxr(symbols)` / `get_finance(symbols)` |
|
||||||
| F10 公司资料 | `get_f10(symbol, section)` |
|
| F10 公司资料 | `get_f10(symbol, section)` |
|
||||||
| 板块列表 / 成分股 / 涨跌排行 | `get_boards` / `get_board_members` / `get_board_ranking` |
|
| 板块列表 / 成分股 / 涨跌排行 | `get_boards` / `get_board_members` / `get_board_ranking` |
|
||||||
|
| **指数的成交量 + 成交笔数**(每分钟曲线) | `get_index_trade_stats(index, date)` → `.cum_trade_count()` |
|
||||||
|
| **指数成分股** | `get_index_members(index)`(裸 `000001` 按指数语义解析) |
|
||||||
| 个股所属板块 | `get_board_of(symbols)` |
|
| 个股所属板块 | `get_board_of(symbols)` |
|
||||||
| 集合竞价 / 异动 / 资金流 | `get_auction` / `get_unusual` / `get_capital_flow` |
|
| 集合竞价 / 异动 / 资金流 | `get_auction` / `get_unusual` / `get_capital_flow` |
|
||||||
| 个股特征快照 | `get_snapshot(symbols)` |
|
| 个股特征快照 | `get_snapshot(symbols)` |
|
||||||
@@ -164,9 +166,11 @@ await td.get_bars("cffex:IFL0", KlinePeriod.DAY, count=700) # 期货合约
|
|||||||
|
|
||||||
- **数据有 15 分钟延时**(通达信公开服务器特性)。要实时数据请用券商行情。
|
- **数据有 15 分钟延时**(通达信公开服务器特性)。要实时数据请用券商行情。
|
||||||
- **复权 K 线与 1 分钟 K 线走 MAC 通道**。`get_bars(..., adjust=Adjust.QFQ)` 自动切换。
|
- **复权 K 线与 1 分钟 K 线走 MAC 通道**。`get_bars(..., adjust=Adjust.QFQ)` 自动切换。
|
||||||
|
- **K 线时间戳统一为北京时间**(v0.6.2 起)。美股原始报文里日期是美东交易日、时分是北京时间(两个时区拼在一起),库已自动归一化,`bar.datetime` 可直接排序 / 比较 / 过滤。但**美股一个交易日横跨两个北京自然日**,按交易日聚合(分钟线合成日线、日内统计)必须用 `bar.trading_date`。
|
||||||
- **`count` 很大时会自动多连接并发分页**,无需手动翻页。但标准通道 K 线 `count < 100` 部分服务器会返回空——内部已强制 `min_page=100` 规避。
|
- **`count` 很大时会自动多连接并发分页**,无需手动翻页。但标准通道 K 线 `count < 100` 部分服务器会返回空——内部已强制 `min_page=100` 规避。
|
||||||
- **`get_quotes` 一次 >80 只自动切批**;走 MAC 字段位通道(`fields=`)时上限为 80 只/次。
|
- **`get_quotes` 一次 >80 只自动切批**;走 MAC 字段位通道(`fields=`)时上限为 80 只/次。
|
||||||
- **历史逐笔没有笔数字段**,只有当日逐笔的 `trade_count` 有效。
|
- **指数本身没有成交笔数**。`get_ticks("sh000001")` 的 `trade_count` 是「当时均价×1000」、`vol` 单位是百元(成交额/100)。要笔数只能用 `get_index_trade_stats(index)`(成分股逐笔累加,2230 只约 36-60 s)。北交所成分无笔数字段,该方法会返回 `trade_count_available=False`。
|
||||||
|
- **历史逐笔有笔数**(MAC `0x122F` 支持历史日期,实测可回溯最近 5 个交易日;更早返回空)。
|
||||||
- 板块文件(`block_*.dat`)/ 行业(`tdxhy.cfg`)/ 财报(`gpcw*.zip`)是**服务器上的静态文件**,解析后是普通 dataclass,**落 JSON 后完全脱网可用**——适合做离线快照。
|
- 板块文件(`block_*.dat`)/ 行业(`tdxhy.cfg`)/ 财报(`gpcw*.zip`)是**服务器上的静态文件**,解析后是普通 dataclass,**落 JSON 后完全脱网可用**——适合做离线快照。
|
||||||
|
|
||||||
## 陷阱清单
|
## 陷阱清单
|
||||||
@@ -183,6 +187,9 @@ await td.get_bars("cffex:IFL0", KlinePeriod.DAY, count=700) # 期货合约
|
|||||||
| 用 `get_block_info` 又自己拆二进制 | 白写解析器 | 用 `get_block_parsed`,已经解析好了 |
|
| 用 `get_block_info` 又自己拆二进制 | 白写解析器 | 用 `get_block_parsed`,已经解析好了 |
|
||||||
| 循环里逐个 `await get_bars` | 慢 | 用列表传多个标的,自动并发 |
|
| 循环里逐个 `await get_bars` | 慢 | 用列表传多个标的,自动并发 |
|
||||||
| 港股/美股五档 bid/ask | 没有(协议不支持) | `fields` 字段位通道有 name 但无五档 |
|
| 港股/美股五档 bid/ask | 没有(协议不支持) | `fields` 字段位通道有 name 但无五档 |
|
||||||
|
| 美股分钟线用 `datetime.date()` 分组做日聚合 | 一个交易日被拆成两天(美股跨北京零点) | 用 `bar.trading_date`;`bar.datetime` 是归一化后的北京时间 |
|
||||||
|
| 拿指数逐笔的 `trade_count` 当笔数 | 拿到的是「均价×1000」,数值荒谬 | 用 `get_index_trade_stats(index)` 由成分累加 |
|
||||||
|
| 拿深市 `399xxx` 的 `amount` 校验成分合计 | 比值 0.43,误以为数据错 | 深市指数 amount 是**全板块**口径(`sz399001`=深市全部 5081 亿),不是样本股 |
|
||||||
| 依赖实时性做信号 | 数据是 15 分钟前的 | 换券商实时源 |
|
| 依赖实时性做信号 | 数据是 15 分钟前的 | 换券商实时源 |
|
||||||
| 手动改包内 `servers.json` / 写死 IP | 容器只读、升级被覆盖、快节点捞不回来 | 日常靠内置的**当日测速缓存**自动选路;要强制指定就用 `PYPYTDX_DATA_{STANDARD,MAC,EX}_HOSTS` 环境变量 |
|
| 手动改包内 `servers.json` / 写死 IP | 容器只读、升级被覆盖、快节点捞不回来 | 日常靠内置的**当日测速缓存**自动选路;要强制指定就用 `PYPYTDX_DATA_{STANDARD,MAC,EX}_HOSTS` 环境变量 |
|
||||||
|
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ from pytdxdata import (
|
|||||||
|
|
||||||
| 方法 | 返回 | 说明 |
|
| 方法 | 返回 | 说明 |
|
||||||
|------|------|------|
|
|------|------|------|
|
||||||
| `get_ticks(symbols, *, date=None, start=0, count=None, concurrency=6)` | `list[TransactionRecord]` | 跨市场逐笔成交 |
|
| `get_ticks(symbols, *, date=None, start=0, count=None, concurrency=6)` | `list[TransactionRecord]` | 跨市场逐笔成交。返回按时间升序;`start` **从最新一笔往回数**;`vol`/`trade_count` 为约 3 秒切片的增量值(非累计)。**已剔除 `bs_flag=8` 竞价委托**(非成交,`vol` 为累计撮合量)。**指数**:`vol` 单位是百元(成交额/100)、`trade_count` 是均价×1000,均非股票语义 |
|
||||||
| `get_minutes(symbols, *, date=None, sampling=False, concurrency=6)` | `list[MinuteBar]` | 跨市场分时(sampling=True 取缩略采样:A股由 0x122D 派生,扩展市场 0x254D) |
|
| `get_minutes(symbols, *, date=None, sampling=False, concurrency=6)` | `list[MinuteBar]` | 跨市场分时(sampling=True 取缩略采样:A股由 0x122D 派生,扩展市场 0x254D) |
|
||||||
|
|
||||||
#### 公司信息/文件/板块
|
#### 公司信息/文件/板块
|
||||||
@@ -81,6 +81,9 @@ from pytdxdata import (
|
|||||||
| `get_boards(kind="all", *, count=10000)` | `list[BoardInfo]` | 板块列表 |
|
| `get_boards(kind="all", *, count=10000)` | `list[BoardInfo]` | 板块列表 |
|
||||||
| `get_board_members(board, *, fields=None, count=100000)` | `list[SecurityQuote]` | 板块成分股报价(归一模型) |
|
| `get_board_members(board, *, fields=None, count=100000)` | `list[SecurityQuote]` | 板块成分股报价(归一模型) |
|
||||||
| `get_board_of(symbols)` | `list[BelongBoard]` | 个股所属板块 |
|
| `get_board_of(symbols)` | `list[BelongBoard]` | 个股所属板块 |
|
||||||
|
| `get_index_members(index, *, fields=None, count=100000)` | `list[SecurityQuote]` | **指数成分股**报价。裸 6 位按**指数语义**解析(`000001`=上证指数,不是平安银行):`000/999/880`→沪、`399`→深、`899`→北交所 |
|
||||||
|
| `get_index_trade_stats(index, date=None, *, concurrency=24, batch_size=50, members=None, skip_errors=True, on_progress=None)` | `IndexTradeStats` | **指数成分逐笔汇总**成每分钟成交量/成交笔数。协议里指数本身没有笔数(`trade_count` 是均价×1000),故由成分股逐笔累加;`.cumulative()` 取「截止该分钟累计」,`.cum_trade_count()` 直接给 `[(HH:MM, 累计笔数)]`。2230 只(上证)约 36-60 s |
|
||||||
|
| `get_index_trade_stats_multi(index, dates, *, ...)` | `list[IndexTradeStats]` | 多日批量版(成分只请求一次),用于「近 N 日同时点累计均值」基准 |
|
||||||
|
|
||||||
### 2.2 K线(旧接口,共 6 个) {#legacy-kline}
|
### 2.2 K线(旧接口,共 6 个) {#legacy-kline}
|
||||||
|
|
||||||
@@ -199,7 +202,7 @@ from pytdxdata import (
|
|||||||
| `SecurityQuote` | price/pre_close/OHLC/vol/amount/s_vol/b_vol/`bid[5]`/`ask[5]`/server_time/trading_status/cur_vol/rise_speed/limit_up/limit_down/decimal_point/open_amount |
|
| `SecurityQuote` | price/pre_close/OHLC/vol/amount/s_vol/b_vol/`bid[5]`/`ask[5]`/server_time/trading_status/cur_vol/rise_speed/limit_up/limit_down/decimal_point/open_amount |
|
||||||
| `SecurityInfo` | market/code/name/volunit/decimal_point/pre_close |
|
| `SecurityInfo` | market/code/name/volunit/decimal_point/pre_close |
|
||||||
| `TransactionRecord` | market/code/time/price/vol/`trade_count`/bs_flag |
|
| `TransactionRecord` | market/code/time/price/vol/`trade_count`/bs_flag |
|
||||||
| `MinuteBar` | price/vol/avg_price/hour/minute |
|
| `MinuteBar` | price/vol/avg_price/hour/minute/day_offset + `datetime`/`trading_date` 只读属性(hour 恒为合法时钟值 0~23;美股一个交易日跨两个北京自然日,按交易日聚合用 `trading_date`) |
|
||||||
| `XdxrRecord` | 分红送转/配股/缩股/行权 + 股本变动 + `date` 属性 |
|
| `XdxrRecord` | 分红送转/配股/缩股/行权 + 股本变动 + `date` 属性 |
|
||||||
| `FinanceRecord` | 股本/资产/负债/营收/利润等 36 个字段(万元/万股) |
|
| `FinanceRecord` | 股本/资产/负债/营收/利润等 36 个字段(万元/万股) |
|
||||||
| `BoardInfo` | 板块 + 领涨股信息 |
|
| `BoardInfo` | 板块 + 领涨股信息 |
|
||||||
|
|||||||
@@ -62,6 +62,22 @@ from pytdxdata.models import BoardInfo, MemberQuote, AuctionItem, CapitalFlow
|
|||||||
| 结构 | 具名属性(`zongguben`、`jingzichan` ...) | `fields: list[float]`,原始字段数组 |
|
| 结构 | 具名属性(`zongguben`、`jingzichan` ...) | `fields: list[float]`,原始字段数组 |
|
||||||
| 单位 | 万元 / 万股 | 原始 float,需自行按字段语义换算 |
|
| 单位 | 万元 / 万股 | 原始 float,需自行按字段语义换算 |
|
||||||
|
|
||||||
|
!!! tip "`SecurityBar.datetime` 的时间戳口径(v0.6.2 起)"
|
||||||
|
时间统一为**北京时间**。A股 / 港股 / 期货的原始时间戳即为北京时间;美股的原始报文
|
||||||
|
混用了「美东交易日日期」与「北京时间时分」两个时区,库在构造时已自动归一化
|
||||||
|
(凌晨段日期 +1 天),因此 `bar.datetime` 可直接排序 / 比较 / 过滤。
|
||||||
|
|
||||||
|
但要注意**美股一个交易日横跨两个北京自然日**,按交易日聚合请用 `bar.trading_date`:
|
||||||
|
|
||||||
|
```python
|
||||||
|
bars = await td.get_bars("usAAPL", KlinePeriod.MIN_1, count=390)
|
||||||
|
bars[0].datetime # 2026-10-12 21:31:00(北京时间)
|
||||||
|
bars[-1].datetime # 2026-10-13 04:00:00(次日)
|
||||||
|
bars[0].trading_date # "2026-10-12" —— 归属的美东交易日
|
||||||
|
```
|
||||||
|
|
||||||
|
`bar.is_us` 可判定是否美股。详见 [常见问题 → 美股分钟 K 线](../faq.md)。
|
||||||
|
|
||||||
## 完整字段定义
|
## 完整字段定义
|
||||||
|
|
||||||
以下由 `mkdocstrings` 从源码实时渲染(**以源码为准**):
|
以下由 `mkdocstrings` 从源码实时渲染(**以源码为准**):
|
||||||
|
|||||||
+17
@@ -99,6 +99,23 @@
|
|||||||
2. **是否含未收盘的那根** —— 盘中最后一根是动态的
|
2. **是否含未收盘的那根** —— 盘中最后一根是动态的
|
||||||
3. 分钟线注意**数据来源深度**不同
|
3. 分钟线注意**数据来源深度**不同
|
||||||
|
|
||||||
|
??? question "美股分钟 K 线的时间为什么跨了两个日期?"
|
||||||
|
**已在上游修好(v0.6.2)**。通达信 EX 通道对美股返回的原生时间戳把两个时区拼在一起:
|
||||||
|
日期分量是**美东交易日**、时分分量是**北京时间**。修复前,同一交易日的 390 根会全部
|
||||||
|
落在同一个北京自然日里,并且下午半场被排在上午半场**之前**。
|
||||||
|
|
||||||
|
现在 `SecurityBar.datetime` 已统一为**北京时间**(凌晨段日期自动 +1 天),可直接排序 /
|
||||||
|
过滤 / 做增量水位线。但要注意**美股一个交易日横跨两个北京自然日**:
|
||||||
|
|
||||||
|
```python
|
||||||
|
bars = await td.get_bars("usAAPL", KlinePeriod.MIN_1, count=390)
|
||||||
|
print(bars[0].datetime, bars[-1].datetime) # 2026-10-12 21:31:00 → 2026-10-13 04:00:00
|
||||||
|
print({b.trading_date for b in bars}) # {'2026-10-12'} —— 同一个交易日
|
||||||
|
```
|
||||||
|
|
||||||
|
按**交易日**聚合(分钟线合成日线、日内统计)请用 `b.trading_date`,不要用
|
||||||
|
`b.datetime.date()`。A股 / 港股 / 期货原始即为北京时间,不受影响。
|
||||||
|
|
||||||
??? question "缓存会不会返回过期数据?"
|
??? question "缓存会不会返回过期数据?"
|
||||||
缓存按数据类型分级 TTL,不会返回超出 TTL 的数据:
|
缓存按数据类型分级 TTL,不会返回超出 TTL 的数据:
|
||||||
|
|
||||||
|
|||||||
+5
-1
@@ -48,7 +48,7 @@ asyncio.run(main())
|
|||||||
- **K 线**:`get_bars`(自动并发分页、支持复权与日期区间、多标的并发)
|
- **K 线**:`get_bars`(自动并发分页、支持复权与日期区间、多标的并发)
|
||||||
- **技术指标**:`get_indicators`(K线 + MACD/KDJ/RSI/BOLL/MA/EMA)
|
- **技术指标**:`get_indicators`(K线 + MACD/KDJ/RSI/BOLL/MA/EMA)
|
||||||
- **报价**:`get_quotes`(五档,>80 只自动切批)、`get_snapshot`(个股特征)、`get_market_stat`
|
- **报价**:`get_quotes`(五档,>80 只自动切批)、`get_snapshot`(个股特征)、`get_market_stat`
|
||||||
- **逐笔 / 分时**:`get_ticks`(含 `trade_count`,支持历史日期)、`get_minutes`(`sampling=True` 取缩略采样)
|
- **逐笔 / 分时**:`get_ticks`(含 `trade_count`,支持历史日期,返回按时间升序;`vol`/`trade_count` 为约 3 秒切片增量而非累计;已剔除 `bs_flag=8` 竞价委托;**指数**的 `vol` 是百元、`trade_count` 是均价×1000)、`get_minutes`(`sampling=True` 取缩略采样;`hour` 恒为合法时钟值 0~23,美股一个交易日跨两个北京自然日,按交易日聚合用 `MinuteBar.trading_date`、取时刻用 `MinuteBar.datetime`)
|
||||||
- **财务 / 除权**:`get_finance`(最新快照)、`get_xdxr`、`get_price_limits`(本地计算涨跌停)
|
- **财务 / 除权**:`get_finance`(最新快照)、`get_xdxr`、`get_price_limits`(本地计算涨跌停)
|
||||||
- **F10**:`get_f10`(公司资料目录 + 正文)
|
- **F10**:`get_f10`(公司资料目录 + 正文)
|
||||||
- **板块**:`get_boards`、`get_board_members`、`get_board_of`、`get_board_summary`、
|
- **板块**:`get_boards`、`get_board_members`、`get_board_of`、`get_board_summary`、
|
||||||
@@ -93,6 +93,10 @@ tdx financial gpcw20260331.zip --limit 5
|
|||||||
- **`KlinePeriod.MIN_1` 走 MAC 通道并单连接串行分页**,这是刻意设计:并发分页在多服务器
|
- **`KlinePeriod.MIN_1` 走 MAC 通道并单连接串行分页**,这是刻意设计:并发分页在多服务器
|
||||||
历史深度不一致时会产生非确定性数据空洞。不要试图"优化"成并发。
|
历史深度不一致时会产生非确定性数据空洞。不要试图"优化"成并发。
|
||||||
- **复权 K 线走 MAC 通道**,标准通道不提供复权数据。
|
- **复权 K 线走 MAC 通道**,标准通道不提供复权数据。
|
||||||
|
- **K 线 `datetime` 统一为北京时间**(v0.6.2 起)。美股原始报文的日期分量是美东交易日、
|
||||||
|
时分分量是北京时间,库已自动归一化,`bar.datetime` 可直接排序 / 比较 / 过滤。
|
||||||
|
但**美股一个交易日横跨两个北京自然日**:按交易日聚合(分钟线合成日线、日内统计)
|
||||||
|
用 `bar.trading_date`,不要用 `bar.datetime.date()`。`bar.is_us` 判定美股。
|
||||||
- **标准通道部分服务器在 `count < 100` 时返回空**,分页器已强制 `min_page=100` 规避。
|
- **标准通道部分服务器在 `count < 100` 时返回空**,分页器已强制 `min_page=100` 规避。
|
||||||
- **本地 Windows 上 `asyncio.new_event_loop()` 可能永久挂起**,根因是回环 `socket.socketpair()`
|
- **本地 Windows 上 `asyncio.new_event_loop()` 可能永久挂起**,根因是回环 `socket.socketpair()`
|
||||||
被本地代理/安全软件拦截。异步测试请在 Linux/WSL 运行。
|
被本地代理/安全软件拦截。异步测试请在 Linux/WSL 运行。
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "pytdxdata"
|
name = "pytdxdata"
|
||||||
version = "0.6.1"
|
version = "0.6.5"
|
||||||
description = "通达信(TongDaXin)行情数据客户端:动态连接池 + 双层TTL缓存 + 3连接并发分页,纯asyncio零运行时依赖,支持A股/港股/美股/期货/期权"
|
description = "通达信(TongDaXin)行情数据客户端:动态连接池 + 双层TTL缓存 + 3连接并发分页,纯asyncio零运行时依赖,支持A股/港股/美股/期货/期权"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.11"
|
requires-python = ">=3.11"
|
||||||
|
|||||||
@@ -0,0 +1,108 @@
|
|||||||
|
"""可复现核查:指数成分逐笔汇总的口径是否与官方日线一致。
|
||||||
|
|
||||||
|
用法::
|
||||||
|
|
||||||
|
python scripts/verify_index_stats.py # 当日
|
||||||
|
python scripts/verify_index_stats.py --date 20261021 # 历史某日
|
||||||
|
python scripts/verify_index_stats.py --indices 000001 000300
|
||||||
|
|
||||||
|
两段校验:
|
||||||
|
|
||||||
|
1. **单股级**:``Σ(逐笔价 × 手 × 100)`` 对**当日日线 amount**。比值 1.0000~1.0002
|
||||||
|
说明逐笔没漏条、价格与单位都对(``bj`` 也行)。
|
||||||
|
2. **指数级**:成分股合计成交额对**该指数的日线 amount**。相等说明成分清单与
|
||||||
|
指数的量能口径一致;明显偏小则是成分表滞后(新上市未纳入)。
|
||||||
|
|
||||||
|
⚠️ **深市 `399xxx` 的指数 amount 是「全板块」口径**,不是样本股:实测
|
||||||
|
`sz399001` 5081.40 亿 = 深市 2908 只股票合计,`sz399006` 2568.60 亿 = 创业板
|
||||||
|
全部股票合计。所以这两个指数的比值天然远离 1(约 0.43),**不是数据错**。
|
||||||
|
|
||||||
|
为什么不用快照做基准:快照只有「此刻」,历史日期对不上。日线 amount 两者都能给。
|
||||||
|
|
||||||
|
注意 ``bj``(北交所)逐笔**没有笔数字段**(``trade_count`` 恒 0),脚本会打出
|
||||||
|
``有笔数`` 列便于识别。
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import asyncio
|
||||||
|
from datetime import date as _date
|
||||||
|
|
||||||
|
|
||||||
|
def _ymd(bar) -> int:
|
||||||
|
return bar.year * 10000 + bar.month * 100 + bar.day
|
||||||
|
|
||||||
|
|
||||||
|
async def _daily_amount(td, symbol: str, ymd: int) -> float | None:
|
||||||
|
from pytdxdata import KlinePeriod
|
||||||
|
|
||||||
|
bars = await td.get_bars(symbol, KlinePeriod.DAY, count=10)
|
||||||
|
for b in bars:
|
||||||
|
if _ymd(b) == ymd:
|
||||||
|
return b.amount
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
async def check_symbols(td, symbols: list[str], ymd: int) -> None:
|
||||||
|
print(f"## 单股级({ymd})\n")
|
||||||
|
print("| 标的 | Σ(p×手×100) / 日线amount | 笔数 |")
|
||||||
|
print("|---|---|---|")
|
||||||
|
for sym in symbols:
|
||||||
|
try:
|
||||||
|
ticks = await td.get_ticks(sym, date=ymd)
|
||||||
|
ref = await _daily_amount(td, sym, ymd)
|
||||||
|
except Exception as exc: # noqa: BLE001
|
||||||
|
print(f"| {sym} | ERR {type(exc).__name__} | - |")
|
||||||
|
continue
|
||||||
|
if not ticks:
|
||||||
|
print(f"| {sym} | 无逐笔(非交易日?) | - |")
|
||||||
|
continue
|
||||||
|
sa = sum(t.price * t.vol * 100 for t in ticks)
|
||||||
|
tc = sum(t.trade_count for t in ticks)
|
||||||
|
ratio = sa / ref if ref else 0.0
|
||||||
|
print(f"| {sym} | {ratio:.4f} | {tc:,} |")
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
async def check_indices(td, indices: list[str], ymd: int, day: int | None) -> None:
|
||||||
|
print(f"## 指数级({ymd})\n")
|
||||||
|
print("| 指数 | 成分 | 有成交 | 有笔数 | 成分合计 | 日线amount | 比值 | 耗时 |")
|
||||||
|
print("|---|---|---|---|---|---|---|---|")
|
||||||
|
for idx in indices:
|
||||||
|
try:
|
||||||
|
st = await td.get_index_trade_stats(idx, day)
|
||||||
|
ref = await _daily_amount(td, st.index, ymd)
|
||||||
|
except Exception as exc: # noqa: BLE001
|
||||||
|
print(f"| {idx} | ERR {type(exc).__name__}: {exc} | - | - | - | - | - | - |")
|
||||||
|
continue
|
||||||
|
ratio = st.total_amount / ref if ref else 0.0
|
||||||
|
print(f"| {st.index} | {st.member_count} | {st.active_members} | "
|
||||||
|
f"{st.trade_count_members} | {st.total_amount / 1e8:,.2f} 亿 | "
|
||||||
|
f"{(ref or 0) / 1e8:,.2f} 亿 | {ratio:.4f} | {st.elapsed:.1f}s |")
|
||||||
|
print()
|
||||||
|
print("提示:`399xxx` 的比值天然远离 1 —— 深市指数的 amount 字段是全板块口径。")
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
async def main() -> None:
|
||||||
|
ap = argparse.ArgumentParser(description=__doc__)
|
||||||
|
ap.add_argument("--symbols", nargs="*",
|
||||||
|
default=["sh600000", "sz000001", "sh601398"])
|
||||||
|
ap.add_argument("--indices", nargs="*", default=["000016", "000300"])
|
||||||
|
ap.add_argument("--date", default=None, help="YYYYMMDD(缺省=当日)")
|
||||||
|
ap.add_argument("--skip-symbols", action="store_true")
|
||||||
|
args = ap.parse_args()
|
||||||
|
|
||||||
|
from pytdxdata import TdxData
|
||||||
|
|
||||||
|
ymd = int(args.date) if args.date else int(_date.today().strftime("%Y%m%d"))
|
||||||
|
day = int(args.date) if args.date else None
|
||||||
|
async with TdxData(cache_dir=None, timeout=10.0) as td:
|
||||||
|
if not args.skip_symbols:
|
||||||
|
await check_symbols(td, args.symbols, ymd)
|
||||||
|
if args.indices:
|
||||||
|
await check_indices(td, args.indices, ymd, day)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
@@ -7,16 +7,18 @@ from .models import (
|
|||||||
Adjust,
|
Adjust,
|
||||||
ExMarket,
|
ExMarket,
|
||||||
FinanceRecord,
|
FinanceRecord,
|
||||||
|
IndexTradeStats,
|
||||||
KlinePeriod,
|
KlinePeriod,
|
||||||
Market,
|
Market,
|
||||||
MinuteBar,
|
MinuteBar,
|
||||||
|
MinuteTradeStat,
|
||||||
SecurityBar,
|
SecurityBar,
|
||||||
SecurityQuote,
|
SecurityQuote,
|
||||||
TransactionRecord,
|
TransactionRecord,
|
||||||
XdxrRecord,
|
XdxrRecord,
|
||||||
)
|
)
|
||||||
|
|
||||||
__version__ = "0.6.1"
|
__version__ = "0.6.5"
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"TdxData",
|
"TdxData",
|
||||||
"Market",
|
"Market",
|
||||||
@@ -27,6 +29,8 @@ __all__ = [
|
|||||||
"SecurityQuote",
|
"SecurityQuote",
|
||||||
"TransactionRecord",
|
"TransactionRecord",
|
||||||
"MinuteBar",
|
"MinuteBar",
|
||||||
|
"MinuteTradeStat",
|
||||||
|
"IndexTradeStats",
|
||||||
"XdxrRecord",
|
"XdxrRecord",
|
||||||
"FinanceRecord",
|
"FinanceRecord",
|
||||||
"__version__",
|
"__version__",
|
||||||
|
|||||||
+333
-22
@@ -23,7 +23,9 @@ from __future__ import annotations
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
import time
|
||||||
import warnings
|
import warnings
|
||||||
|
from collections.abc import Callable
|
||||||
from datetime import date as _date, datetime
|
from datetime import date as _date, datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
@@ -45,11 +47,13 @@ from .models import (
|
|||||||
FinancialFileInfo,
|
FinancialFileInfo,
|
||||||
FinancialRecord,
|
FinancialRecord,
|
||||||
IndicatorSet,
|
IndicatorSet,
|
||||||
|
IndexTradeStats,
|
||||||
IndustryInfo,
|
IndustryInfo,
|
||||||
KlinePeriod,
|
KlinePeriod,
|
||||||
Market,
|
Market,
|
||||||
MemberQuote,
|
MemberQuote,
|
||||||
MinuteBar,
|
MinuteBar,
|
||||||
|
MinuteTradeStat,
|
||||||
SecurityBar,
|
SecurityBar,
|
||||||
SecurityInfo,
|
SecurityInfo,
|
||||||
SecurityQuote,
|
SecurityQuote,
|
||||||
@@ -146,6 +150,8 @@ _EX_PAGE = 1000 # EX 商品列表单页
|
|||||||
_TICK_PAGE = 1000 # MAC 逐笔单页
|
_TICK_PAGE = 1000 # MAC 逐笔单页
|
||||||
_EX_TICK_PAGE = 1800 # 港股逐笔单页
|
_EX_TICK_PAGE = 1800 # 港股逐笔单页
|
||||||
_ALL_PAGES_CAP = 50 # count=None(全量)时的翻页上限
|
_ALL_PAGES_CAP = 50 # count=None(全量)时的翻页上限
|
||||||
|
# 集合竞价**委托**记录标记(0x122F 第 5 字段)。不是成交,见 _ticks_one 注释。
|
||||||
|
_BS_AUCTION_ORDER = 8
|
||||||
|
|
||||||
# 港股系市场:逐笔走 EX 专用 16B 协议(0x23FC/0x2406),其余扩展市场走 MAC 0x122F
|
# 港股系市场:逐笔走 EX 专用 16B 协议(0x23FC/0x2406),其余扩展市场走 MAC 0x122F
|
||||||
_HK_MARKETS = frozenset({27, 31, 48, 49, 71, 98})
|
_HK_MARKETS = frozenset({27, 31, 48, 49, 71, 98})
|
||||||
@@ -233,11 +239,13 @@ class TdxData:
|
|||||||
pool_max: int = 12,
|
pool_max: int = 12,
|
||||||
mac_pool_cap: int = 4,
|
mac_pool_cap: int = 4,
|
||||||
std_pool_cap: int = 4,
|
std_pool_cap: int = 4,
|
||||||
|
ex_pool_cap: int = 2,
|
||||||
) -> None:
|
) -> None:
|
||||||
self.port = port
|
self.port = port
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
self.default_adjust = default_adjust
|
self.default_adjust = default_adjust
|
||||||
self.mac_pool_cap = mac_pool_cap
|
self.mac_pool_cap = mac_pool_cap
|
||||||
|
self.ex_pool_cap = ex_pool_cap
|
||||||
self._std_servers = list(standard_servers or get_standard_hosts())
|
self._std_servers = list(standard_servers or get_standard_hosts())
|
||||||
self._mac_servers = list(mac_servers or get_mac_hosts())
|
self._mac_servers = list(mac_servers or get_mac_hosts())
|
||||||
self._ex_servers = list(ex_servers or get_ex_hosts())
|
self._ex_servers = list(ex_servers or get_ex_hosts())
|
||||||
@@ -262,8 +270,11 @@ class TdxData:
|
|||||||
probe_factory=lambda: MacKlineCmd(1, "600000", KlinePeriod.DAY, Adjust.NONE, 0, 100),
|
probe_factory=lambda: MacKlineCmd(1, "600000", KlinePeriod.DAY, Adjust.NONE, 0, 100),
|
||||||
)
|
)
|
||||||
# EX扩展市场池(港股/期货/美股,端口7727;MAC EX login + pytdx EX setup 混合)
|
# EX扩展市场池(港股/期货/美股,端口7727;MAC EX login + pytdx EX setup 混合)
|
||||||
|
# 单服务器场景(测速缓存收缩后仅 1 台)下,有效并发 = 服务器数 × ex_pool_cap,
|
||||||
|
# 默认 cap=2 保证至少 2 并发;acquire 有 Condition 背压等待,不再快速失败。
|
||||||
self._ex_pool = ExPool(self._ex_servers, timeout=timeout,
|
self._ex_pool = ExPool(self._ex_servers, timeout=timeout,
|
||||||
min_size=min(pool_min, 2), max_size=min(pool_max, 6),
|
min_size=min(pool_min, 2), max_size=min(pool_max, 6),
|
||||||
|
per_server_cap=ex_pool_cap,
|
||||||
handshake_map=EX_HANDSHAKES)
|
handshake_map=EX_HANDSHAKES)
|
||||||
disk = None
|
disk = None
|
||||||
if self._cache_dir is not None:
|
if self._cache_dir is not None:
|
||||||
@@ -790,12 +801,26 @@ class TdxData:
|
|||||||
|
|
||||||
参数:
|
参数:
|
||||||
date: ``None``=当日,``YYYYMMDD``=历史某日。
|
date: ``None``=当日,``YYYYMMDD``=历史某日。
|
||||||
start: 起始偏移。
|
start: 起始偏移,**从最新一笔往回数**(``0``=当天最后一条)。
|
||||||
|
``start=1000`` 表示跳过最近 1000 条、从更早的位置开始。
|
||||||
count: ``None``=尽量取全量(自动翻页,上限 50 页);给了就取这么多条。
|
count: ``None``=尽量取全量(自动翻页,上限 50 页);给了就取这么多条。
|
||||||
|
|
||||||
返回:
|
返回:
|
||||||
``list[TransactionRecord]``,按入参标的顺序拼接(A股含 ``trade_count``,
|
``list[TransactionRecord]``,按入参标的顺序拼接,**每条标的内部按时间升序**。
|
||||||
港股为 HH:MM 时间 + ``fields["zengcang"]`` 增仓)。
|
注意服务端是按约 3 秒聚合的切片,``vol``/``trade_count`` 是切片增量
|
||||||
|
而非当日累计。
|
||||||
|
A股含 ``trade_count``,港股为 HH:MM 时间 + ``fields["zengcang"]`` 增仓。
|
||||||
|
|
||||||
|
**集合竞价委托(``bs_flag=8``)不会出现在结果里**:它们不是成交,``vol``
|
||||||
|
是累计撮合量(会被后续记录覆盖),计入会让 Σvol 比真值虚高 20%~160%,
|
||||||
|
且不同主机收录的条数不同 → 同一查询两次调用结果不一致。
|
||||||
|
|
||||||
|
**指数(``sh000016`` 等)的两个字段语义与个股不同**:
|
||||||
|
|
||||||
|
- ``vol`` 单位是**百元**(成交额/100),不是手。
|
||||||
|
- ``trade_count`` 是**当时均价 × 1000**,不是笔数。
|
||||||
|
|
||||||
|
详见 :class:`~pytdxdata.models.TransactionRecord` 的类注释。
|
||||||
"""
|
"""
|
||||||
syms = parse_symbols(symbols)
|
syms = parse_symbols(symbols)
|
||||||
sem = asyncio.Semaphore(max(1, concurrency))
|
sem = asyncio.Semaphore(max(1, concurrency))
|
||||||
@@ -816,18 +841,19 @@ class TdxData:
|
|||||||
if sym.is_ex:
|
if sym.is_ex:
|
||||||
rows = await self._ticks_ex(sym, date=date, start=start, count=count)
|
rows = await self._ticks_ex(sym, date=date, start=start, count=count)
|
||||||
else:
|
else:
|
||||||
def make_cmd(s: int, c: int):
|
rows = await self._ticks_cn_serial(sym, date=date, start=start,
|
||||||
return MacTransactionCmd(sym.market, sym.code, date or 0, s, c)
|
count=count)
|
||||||
|
# ★ 过滤集合竞价**委托**记录(bs_flag=8)。必须在逐页拉取**全部结束之后**做:
|
||||||
spec = PageSpec(
|
# 分页靠 `len(rows) < page_size` 判终止,若在 parse_response 里过滤,
|
||||||
page_size=_TICK_PAGE,
|
# 满页会被剥短成"短页" → 提前终止 → 丢数据。
|
||||||
total=None if count is None else start + count,
|
# 实测(2026-10-21 sh600000):b8 是 09:15~09:25 / 14:57~15:00 竞价段的
|
||||||
min_page=100,
|
# 撮合快照,vol 是**累计值**(可被后续记录覆盖),不是成交增量;
|
||||||
stop_when_short=True,
|
# 计入后 Σvol 比真值虚高 20%~160%,且**不同主机的 b8 集合不同**
|
||||||
max_pages=_ALL_PAGES_CAP if count is None else None,
|
# (干净主机 0 条、脏主机 135 条)→ 同一查询两次调用结果不一致。
|
||||||
start_offset=start,
|
# 剔除后:Σvol 与快照 vol 精确相等(比值 1.0000),且跨主机逐条一致。
|
||||||
)
|
rows = [r for r in rows if r.bs_flag != _BS_AUCTION_ORDER]
|
||||||
rows = await self._fetch_paginated_list("mac", make_cmd, spec)
|
# 排序放在 pack 之前,让缓存直接存入正确顺序,避免每次命中都重排。
|
||||||
|
rows.sort(key=lambda r: r.time)
|
||||||
return pack_payload(rows)
|
return pack_payload(rows)
|
||||||
|
|
||||||
records = unpack_payload(await self._cache.get_or_fetch(key, fetch))
|
records = unpack_payload(await self._cache.get_or_fetch(key, fetch))
|
||||||
@@ -835,6 +861,54 @@ class TdxData:
|
|||||||
r.symbol = sym.text
|
r.symbol = sym.text
|
||||||
return records
|
return records
|
||||||
|
|
||||||
|
async def _ticks_cn_serial(self, sym: Symbol, *, date: int | None, start: int,
|
||||||
|
count: int | None) -> list[TransactionRecord]:
|
||||||
|
"""A股逐笔(0x122F)**单连接串行游标**翻页。
|
||||||
|
|
||||||
|
为什么不用并发分页器:``0x122F`` 的 ``start`` 是「从**最新一笔**往回数的偏移」,
|
||||||
|
是个**相对锚点**,不是稳定的绝对序号。而池里各主机对集合竞价委托(``bs_flag=8``)
|
||||||
|
的收录**不一致**(实测干净主机 0 条、脏主机 135 条),同一 ``start`` 在不同主机上
|
||||||
|
落到**不同的真实成交**上。并发分页 = 各页来自不同主机 → 页边界既**重叠**
|
||||||
|
(sh600000 冗余 116 条)又**漏条**(13:03 附近丢 59 条),Σvol 偏移 −1.2%。
|
||||||
|
|
||||||
|
逐笔单日仅 5~6 页(1000 条/页),串行开销可忽略、换来精确一致,
|
||||||
|
故这里固定**单连接**、按 ``start`` 递增(= 向更早方向)推进。
|
||||||
|
|
||||||
|
返回:按页序累加后再**整体翻转**,得到时间升序的完整列表。
|
||||||
|
"""
|
||||||
|
chunks: list[list[TransactionRecord]] = []
|
||||||
|
pos = start
|
||||||
|
remaining = count
|
||||||
|
pc = await self._mac_pool.acquire()
|
||||||
|
try:
|
||||||
|
for _ in range(_ALL_PAGES_CAP):
|
||||||
|
n = _TICK_PAGE if remaining is None else min(_TICK_PAGE, remaining)
|
||||||
|
if n <= 0:
|
||||||
|
break
|
||||||
|
rows = await pc.execute(
|
||||||
|
MacTransactionCmd(sym.market, sym.code, date or 0, pos, n))
|
||||||
|
if not rows:
|
||||||
|
break
|
||||||
|
chunks.append(list(rows))
|
||||||
|
pos += len(rows)
|
||||||
|
if remaining is not None:
|
||||||
|
remaining -= len(rows)
|
||||||
|
if remaining <= 0:
|
||||||
|
break
|
||||||
|
# 短页 = 已到最早一笔
|
||||||
|
if len(rows) < n:
|
||||||
|
break
|
||||||
|
except Exception:
|
||||||
|
await self._mac_pool.release(pc, broken=True)
|
||||||
|
raise
|
||||||
|
await self._mac_pool.release(pc)
|
||||||
|
out: list[TransactionRecord] = []
|
||||||
|
for chunk in reversed(chunks):
|
||||||
|
out.extend(chunk)
|
||||||
|
for r in out:
|
||||||
|
r.symbol = sym.text
|
||||||
|
return out
|
||||||
|
|
||||||
async def _ticks_ex(self, sym: Symbol, *, date: int | None, start: int,
|
async def _ticks_ex(self, sym: Symbol, *, date: int | None, start: int,
|
||||||
count: int | None) -> list[TransactionRecord]:
|
count: int | None) -> list[TransactionRecord]:
|
||||||
from .protocol.commands.ex_ext import (
|
from .protocol.commands.ex_ext import (
|
||||||
@@ -843,7 +917,7 @@ class TdxData:
|
|||||||
)
|
)
|
||||||
hk = sym.market in _HK_MARKETS
|
hk = sym.market in _HK_MARKETS
|
||||||
page = _EX_TICK_PAGE if hk else _TICK_PAGE
|
page = _EX_TICK_PAGE if hk else _TICK_PAGE
|
||||||
out: list[TransactionRecord] = []
|
chunks: list[list[TransactionRecord]] = []
|
||||||
pos = start
|
pos = start
|
||||||
remaining = count
|
remaining = count
|
||||||
for _ in range(_ALL_PAGES_CAP):
|
for _ in range(_ALL_PAGES_CAP):
|
||||||
@@ -855,22 +929,27 @@ class TdxData:
|
|||||||
if date else
|
if date else
|
||||||
GetExTransactionDataCmd(sym.market, sym.code, pos, n))
|
GetExTransactionDataCmd(sym.market, sym.code, pos, n))
|
||||||
rows = await self._execute_ex(cmd)
|
rows = await self._execute_ex(cmd)
|
||||||
out.extend(self._ex_tick(sym, r) for r in rows)
|
chunk = [self._ex_tick(sym, r) for r in rows]
|
||||||
else:
|
else:
|
||||||
cmd = MacTransactionCmd(sym.market, sym.code, date or 0, pos, n)
|
cmd = MacTransactionCmd(sym.market, sym.code, date or 0, pos, n)
|
||||||
rows = await self._execute_ex(cmd)
|
rows = await self._execute_ex(cmd)
|
||||||
for r in rows:
|
for r in rows:
|
||||||
r.symbol = sym.text
|
r.symbol = sym.text
|
||||||
out.extend(rows)
|
chunk = list(rows)
|
||||||
if not rows:
|
if not chunk:
|
||||||
break
|
break
|
||||||
pos += len(rows)
|
# EX 通道的 pos 同样是"从最新往回数",逐页倒序累加 → 最后整体翻转。
|
||||||
|
chunks.append(chunk)
|
||||||
|
pos += len(chunk)
|
||||||
if remaining is not None:
|
if remaining is not None:
|
||||||
remaining -= len(rows)
|
remaining -= len(chunk)
|
||||||
if remaining <= 0:
|
if remaining <= 0:
|
||||||
break
|
break
|
||||||
if len(rows) < n:
|
if len(chunk) < n:
|
||||||
break
|
break
|
||||||
|
out: list[TransactionRecord] = []
|
||||||
|
for chunk in reversed(chunks):
|
||||||
|
out.extend(chunk)
|
||||||
return out
|
return out
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -1216,6 +1295,238 @@ class TdxData:
|
|||||||
result.sort(key=lambda x: x["change_pct"], reverse=True)
|
result.sort(key=lambda x: x["change_pct"], reverse=True)
|
||||||
return result[:top_n]
|
return result[:top_n]
|
||||||
|
|
||||||
|
# ================================================================== #
|
||||||
|
# 七之二、指数成分成交汇总(成分股逐笔 → 每分钟成交量 / 笔数)
|
||||||
|
# ================================================================== #
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _index_pair(index: str) -> tuple[int, str]:
|
||||||
|
"""解析指数标的 → ``(市场号, 6 位代码)``。
|
||||||
|
|
||||||
|
与 :func:`~pytdxdata.symbols.parse_symbol` 的关键区别:**裸 6 位数字
|
||||||
|
一律按指数语义解析**,不做「``000xxx`` → 深市股票」的推断 ——
|
||||||
|
在指数接口里 ``000001`` 是**上证指数**,不是平安银行 ``sz000001``。
|
||||||
|
|
||||||
|
判定规则(与通达信板块码一致)::
|
||||||
|
|
||||||
|
000xxx / 999xxx / 880xxx -> 沪市(上证指数 / A股指数 / 板块指数)
|
||||||
|
399xxx -> 深市(深证成指 / 创业板指)
|
||||||
|
899xxx -> 北交所(北证50)
|
||||||
|
|
||||||
|
显式前缀 ``sh``/``sz``/``bj`` 优先,例如 ``sz399001``、``sh000300``。
|
||||||
|
"""
|
||||||
|
s = str(index).strip().lower().replace(".", "").replace(":", "")
|
||||||
|
if not s:
|
||||||
|
raise ValueError("指数标的不能为空")
|
||||||
|
prefix_table = {"sh": 1, "sz": 0, "bj": 2}
|
||||||
|
if s[:2] in prefix_table:
|
||||||
|
code = s[2:]
|
||||||
|
if not (code.isdigit() and len(code) == 6):
|
||||||
|
raise ValueError(f"非法指数标的: {index!r}(前缀后需 6 位数字)")
|
||||||
|
return prefix_table[s[:2]], code
|
||||||
|
if not (s.isdigit() and len(s) == 6):
|
||||||
|
raise ValueError(f"非法指数标的: {index!r}(需 6 位代码或 sh/sz/bj 前缀)")
|
||||||
|
if s.startswith(("000", "999", "880")):
|
||||||
|
return 1, s
|
||||||
|
if s.startswith("399"):
|
||||||
|
return 0, s
|
||||||
|
if s.startswith("899"):
|
||||||
|
return 2, s
|
||||||
|
raise ValueError(f"无法判定指数市场: {index!r}(不是 A股指数代码段)")
|
||||||
|
|
||||||
|
async def get_index_members(self, index: str, *, count: int = 100000,
|
||||||
|
fields: Sequence[int] | None = None
|
||||||
|
) -> list[SecurityQuote]:
|
||||||
|
"""指数**成分股**(含实时报价)。
|
||||||
|
|
||||||
|
走 MAC ``0x122C`` 板块成分命令,指数代码映射为服务端板块码:``000300`` →
|
||||||
|
31300、``399001`` → 30001、``899050`` → 32050(见 :meth:`_convert_board_code`)。
|
||||||
|
实测(2026-10-22):``sh000001`` 2230 只(沪市全部)、``sh000300`` 300 只、
|
||||||
|
``sh000016`` 50 只、``sh000905`` 500 只、``sh000852`` 1000 只、
|
||||||
|
``sz399006`` 100 只、``bj899050`` 50 只。
|
||||||
|
|
||||||
|
参数:
|
||||||
|
index: 指数代码,可裸写或带前缀(``"000300"`` / ``"sh000300"``)。
|
||||||
|
count: 最多取的成分数(分页上限,默认足够覆盖全部)。
|
||||||
|
fields: 附加报价字段位(同 :meth:`get_board_members`)。
|
||||||
|
|
||||||
|
注意:返回的是**当前**成分,指数调仓后历史日期不适用。
|
||||||
|
|
||||||
|
用法::
|
||||||
|
|
||||||
|
members = await td.get_index_members("000300")
|
||||||
|
syms = [m.symbol for m in members] # ['sz000001', ...]
|
||||||
|
"""
|
||||||
|
_market, code = self._index_pair(index)
|
||||||
|
board_code = self._convert_board_code(code)
|
||||||
|
bits = [int(b) for b in fields] if fields else list(_QUOTE_BITS)
|
||||||
|
items: list[SecurityQuote] = []
|
||||||
|
start = 0
|
||||||
|
while start < count:
|
||||||
|
batch = await self._execute_mac(MacBoardMembersQuotesCmd(
|
||||||
|
board_code, sort_type=0, start=start,
|
||||||
|
page_size=_QUOTE_PAGE, bits=bits))
|
||||||
|
if not batch:
|
||||||
|
break
|
||||||
|
items.extend(_quote_from_member(m) for m in batch)
|
||||||
|
start += len(batch)
|
||||||
|
if len(batch) < _QUOTE_PAGE:
|
||||||
|
break
|
||||||
|
return items[:count]
|
||||||
|
|
||||||
|
async def get_index_trade_stats(
|
||||||
|
self,
|
||||||
|
index: str,
|
||||||
|
date: int | None = None,
|
||||||
|
*,
|
||||||
|
concurrency: int = 24,
|
||||||
|
batch_size: int = 50,
|
||||||
|
members: Sequence[str] | None = None,
|
||||||
|
skip_errors: bool = True,
|
||||||
|
on_progress: Callable[[int, int], None] | None = None,
|
||||||
|
) -> IndexTradeStats:
|
||||||
|
"""按指数**全部成分股**的逐笔汇总出每分钟成交量与成交笔数。
|
||||||
|
|
||||||
|
为什么必须绕这一圈:**通达信协议里指数本身没有成交笔数** —— 指数逐笔的
|
||||||
|
``trade_count`` 是「当时均价 × 1000」,不是笔数(见
|
||||||
|
:class:`TransactionRecord` 类注释)。所以「指数笔数」只能用成分股逐笔累加。
|
||||||
|
|
||||||
|
口径校验:``total_amount`` 是**成分股**合计。对「成分 = 指数覆盖面」的指数
|
||||||
|
它与指数快照 ``amount`` 完全相等(``sh000016`` 661.34 亿、``sh000300``
|
||||||
|
2373.98 亿,比值 1.0000);但深市 ``399xxx`` 的指数 ``amount`` 是**全板块**
|
||||||
|
口径(``sz399001`` = 深市全部股票 5081 亿、``sz399006`` = 创业板全部),
|
||||||
|
两者不可直接比。详见 :class:`IndexTradeStats`。
|
||||||
|
|
||||||
|
参数:
|
||||||
|
index: 指数代码(``"000300"`` / ``"sh000300"``,规则见 :meth:`_index_pair`)。
|
||||||
|
date: ``None`` = 当日;``YYYYMMDD`` = 历史某日(通达信逐笔仅保留最近
|
||||||
|
若干交易日,实测 2026-10-22 可回溯到 10-14,更早返回空)。
|
||||||
|
concurrency: 逐笔并发数(受 MAC 连接池上限约束,池满自动排队)。
|
||||||
|
batch_size: 每批处理的成分数 —— 批内并发拉取、批间串行聚合,
|
||||||
|
用于压住内存峰值(全量 tick 不驻留)。
|
||||||
|
members: 直接给成分股列表(跳过 :meth:`get_index_members`,
|
||||||
|
多日循环时可复用一次成分请求)。
|
||||||
|
skip_errors: 单只成分失败是否忽略(默认忽略并计入 ``failed_members``)。
|
||||||
|
on_progress: 进度回调 ``(已处理, 总数)``,每批结束调用一次。
|
||||||
|
|
||||||
|
返回:
|
||||||
|
:class:`IndexTradeStats`;``minutes`` 是**每分钟增量**,
|
||||||
|
「当日截止该分钟累计」用 ``.cumulative()`` / ``.cum_trade_count()``。
|
||||||
|
|
||||||
|
性能(实测 2026-10-22,盘中):
|
||||||
|
|
||||||
|
====================== ========== ============
|
||||||
|
指数 成分数 耗时
|
||||||
|
====================== ========== ============
|
||||||
|
``sh000016`` 上证50 50 ~5 s
|
||||||
|
``sh000300`` 沪深300 300 ~10 s
|
||||||
|
``sh000001`` 上证指数 2230 ~60-90 s
|
||||||
|
====================== ========== ============
|
||||||
|
|
||||||
|
**北交所成分没有笔数**(实测 ``trade_count`` 恒 0),此时
|
||||||
|
``trade_count_available`` 为 ``False``,只有成交量可用。
|
||||||
|
|
||||||
|
用法::
|
||||||
|
|
||||||
|
st = await td.get_index_trade_stats("000300")
|
||||||
|
for hhmm, cum in st.cum_trade_count():
|
||||||
|
print(hhmm, cum)
|
||||||
|
"""
|
||||||
|
started = time.perf_counter()
|
||||||
|
market, code = self._index_pair(index)
|
||||||
|
idx_text = format_symbol(market, code)
|
||||||
|
|
||||||
|
if members is None:
|
||||||
|
members = [m.symbol for m in await self.get_index_members(code)]
|
||||||
|
syms = [s for s in members if s]
|
||||||
|
total = len(syms)
|
||||||
|
if total == 0:
|
||||||
|
raise ValueError(f"指数 {index!r} 没有取到成分股")
|
||||||
|
|
||||||
|
vol_by_min: dict[str, float] = {}
|
||||||
|
tc_by_min: dict[str, int] = {}
|
||||||
|
amt_by_min: dict[str, float] = {}
|
||||||
|
active: set[str] = set()
|
||||||
|
with_tc: set[str] = set()
|
||||||
|
failed = 0
|
||||||
|
step = max(1, int(batch_size))
|
||||||
|
done = 0
|
||||||
|
for i in range(0, total, step):
|
||||||
|
batch = syms[i:i + step]
|
||||||
|
try:
|
||||||
|
ticks = await self.get_ticks(batch, date=date, concurrency=concurrency)
|
||||||
|
except Exception:
|
||||||
|
if not skip_errors:
|
||||||
|
raise
|
||||||
|
# 整批失败 → 退化为逐只重试,避免一只坏股拖垮一批
|
||||||
|
ticks = []
|
||||||
|
for one in batch:
|
||||||
|
try:
|
||||||
|
ticks.extend(await self.get_ticks(one, date=date))
|
||||||
|
except Exception:
|
||||||
|
failed += 1
|
||||||
|
for t in ticks:
|
||||||
|
hhmm = t.time[:5]
|
||||||
|
vol_by_min[hhmm] = vol_by_min.get(hhmm, 0.0) + t.vol
|
||||||
|
tc_by_min[hhmm] = tc_by_min.get(hhmm, 0) + t.trade_count
|
||||||
|
amt_by_min[hhmm] = amt_by_min.get(hhmm, 0.0) + t.price * t.vol * 100.0
|
||||||
|
active.add(t.symbol)
|
||||||
|
if t.trade_count:
|
||||||
|
with_tc.add(t.symbol)
|
||||||
|
done += len(batch)
|
||||||
|
if on_progress is not None:
|
||||||
|
on_progress(min(done, total), total)
|
||||||
|
|
||||||
|
minutes = [
|
||||||
|
MinuteTradeStat(time=k, volume=vol_by_min[k],
|
||||||
|
trade_count=tc_by_min[k], amount=amt_by_min[k])
|
||||||
|
for k in sorted(vol_by_min)
|
||||||
|
]
|
||||||
|
return IndexTradeStats(
|
||||||
|
index=idx_text,
|
||||||
|
date=date,
|
||||||
|
member_count=total,
|
||||||
|
active_members=len(active),
|
||||||
|
trade_count_members=len(with_tc),
|
||||||
|
minutes=minutes,
|
||||||
|
total_volume=sum(vol_by_min.values()),
|
||||||
|
total_trade_count=sum(tc_by_min.values()),
|
||||||
|
total_amount=sum(amt_by_min.values()),
|
||||||
|
failed_members=failed,
|
||||||
|
elapsed=time.perf_counter() - started,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def get_index_trade_stats_multi(
|
||||||
|
self,
|
||||||
|
index: str,
|
||||||
|
dates: Sequence[int | None],
|
||||||
|
*,
|
||||||
|
concurrency: int = 24,
|
||||||
|
batch_size: int = 50,
|
||||||
|
skip_errors: bool = True,
|
||||||
|
on_progress: Callable[[int, int], None] | None = None,
|
||||||
|
) -> list[IndexTradeStats]:
|
||||||
|
"""多日批量版 :meth:`get_index_trade_stats`(成分只取一次)。
|
||||||
|
|
||||||
|
用于「近 N 日同时点累计平均笔数」这类基准曲线:一次拿到 N 天数据,
|
||||||
|
避免每天重复请求成分表。
|
||||||
|
|
||||||
|
用法::
|
||||||
|
|
||||||
|
# 最近 5 个交易日的笔数曲线(日期自己给,节假日请自行跳过)
|
||||||
|
stats = await td.get_index_trade_stats_multi(
|
||||||
|
"000300", [20261016, 20261019, 20261020, 20261021, 20261022])
|
||||||
|
"""
|
||||||
|
market, code = self._index_pair(index)
|
||||||
|
members = [m.symbol for m in await self.get_index_members(code)]
|
||||||
|
out: list[IndexTradeStats] = []
|
||||||
|
for d in dates:
|
||||||
|
out.append(await self.get_index_trade_stats(
|
||||||
|
code, d, concurrency=concurrency, batch_size=batch_size,
|
||||||
|
members=members, skip_errors=skip_errors,
|
||||||
|
on_progress=on_progress))
|
||||||
|
return out
|
||||||
|
|
||||||
# ================================================================== #
|
# ================================================================== #
|
||||||
# 八、服务器文件(原始字节 + 解析)
|
# 八、服务器文件(原始字节 + 解析)
|
||||||
# ================================================================== #
|
# ================================================================== #
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
tdx transactions sz000001 --count 50 # 逐笔成交
|
tdx transactions sz000001 --count 50 # 逐笔成交
|
||||||
tdx minute sz000001 # 分时(今日/历史--date)
|
tdx minute sz000001 # 分时(今日/历史--date)
|
||||||
tdx index 000001 --count 30 # 指数K线
|
tdx index 000001 --count 30 # 指数K线
|
||||||
|
tdx index-stats 000300 --top 10 # 指数成分成交汇总(每分钟成交量/笔数)
|
||||||
tdx board list [--type 1] # 板块列表(0全部/1行业/2概念)
|
tdx board list [--type 1] # 板块列表(0全部/1行业/2概念)
|
||||||
tdx board members 881001 --count 20 # 板块成分
|
tdx board members 881001 --count 20 # 板块成分
|
||||||
tdx board ranking [--top 10] # 板块涨跌幅排行
|
tdx board ranking [--top 10] # 板块涨跌幅排行
|
||||||
@@ -204,6 +205,43 @@ async def cmd_board(td: TdxData, args) -> None:
|
|||||||
print(" %-12s %+8.2f%%" % (r.get("name", ""), r.get("change_pct", 0)))
|
print(" %-12s %+8.2f%%" % (r.get("name", ""), r.get("change_pct", 0)))
|
||||||
|
|
||||||
|
|
||||||
|
async def cmd_index_stats(td: TdxData, index: str, date: int | None,
|
||||||
|
show_all: bool, top: int, concurrency: int) -> None:
|
||||||
|
"""指数成分成交汇总:每分钟成交量 / 笔数(笔数=成分股逐笔累加)。"""
|
||||||
|
total = 0
|
||||||
|
|
||||||
|
def on_progress(done: int, all_: int) -> None:
|
||||||
|
nonlocal total
|
||||||
|
total = all_
|
||||||
|
print("\r 拉取成分逐笔 %d/%d ..." % (done, all_), end="", file=sys.stderr,
|
||||||
|
flush=True)
|
||||||
|
|
||||||
|
st = await td.get_index_trade_stats(
|
||||||
|
index, date, concurrency=concurrency, on_progress=on_progress)
|
||||||
|
if total:
|
||||||
|
print("\r" + " " * 40 + "\r", end="", file=sys.stderr, flush=True)
|
||||||
|
|
||||||
|
print("%s 成分成交汇总" % st.index)
|
||||||
|
print(" 成分 %d 只 / 有成交 %d 只 / 有笔数 %d 只%s" % (
|
||||||
|
st.member_count, st.active_members, st.trade_count_members,
|
||||||
|
"" if st.trade_count_available else " ★该指数无笔数口径"))
|
||||||
|
print(" 成交量 %s 手 成交笔数 %s 笔 成交额 %.2f 亿 耗时 %.1fs" % (
|
||||||
|
_fmt_num(st.total_volume),
|
||||||
|
_fmt_num(st.total_trade_count) if st.trade_count_available else "-",
|
||||||
|
st.total_amount / 1e8, st.elapsed))
|
||||||
|
if st.failed_members:
|
||||||
|
print(" (%d 只成分逐笔拉取失败,已跳过)" % st.failed_members)
|
||||||
|
print()
|
||||||
|
print(" 时间 累计成交量 累计笔数")
|
||||||
|
rows = st.cumulative()
|
||||||
|
if not show_all:
|
||||||
|
rows = rows[-top:]
|
||||||
|
for m in rows:
|
||||||
|
print(" %-6s %12s %12s" % (
|
||||||
|
m.time, _fmt_num(m.volume),
|
||||||
|
_fmt_num(m.trade_count) if st.trade_count_available else "-"))
|
||||||
|
|
||||||
|
|
||||||
async def cmd_info(td: TdxData, sym: str) -> None:
|
async def cmd_info(td: TdxData, sym: str) -> None:
|
||||||
s = parse_symbol(sym)
|
s = parse_symbol(sym)
|
||||||
info = await td.get_symbol_info(Market(s.market), s.code)
|
info = await td.get_symbol_info(Market(s.market), s.code)
|
||||||
@@ -390,6 +428,17 @@ def main() -> None:
|
|||||||
ix.set_defaults(func=lambda td, a: cmd_kline(
|
ix.set_defaults(func=lambda td, a: cmd_kline(
|
||||||
td, a.code, KlinePeriod.DAY, a.count, 0, Adjust.NONE))
|
td, a.code, KlinePeriod.DAY, a.count, 0, Adjust.NONE))
|
||||||
|
|
||||||
|
ist = add("index-stats", "指数成分成交汇总(每分钟成交量/笔数)")
|
||||||
|
ist.add_argument("index", help="指数代码,如 000300 / 000001 / sh000016")
|
||||||
|
ist.add_argument("--date", type=str, default=None, help="YYYYMMDD,缺省=当日")
|
||||||
|
ist.add_argument("--top", type=int, default=10, help="只显示最后 N 分钟(默认10)")
|
||||||
|
ist.add_argument("--all", dest="show_all", action="store_true",
|
||||||
|
help="显示全部分钟")
|
||||||
|
ist.add_argument("--concurrency", type=int, default=24, help="逐笔并发数")
|
||||||
|
ist.set_defaults(func=lambda td, a: cmd_index_stats(
|
||||||
|
td, a.index, int(a.date) if a.date else None,
|
||||||
|
a.show_all, a.top, a.concurrency))
|
||||||
|
|
||||||
b = add("board", "板块")
|
b = add("board", "板块")
|
||||||
b.add_argument("action", choices=["list", "members", "ranking", "change"])
|
b.add_argument("action", choices=["list", "members", "ranking", "change"])
|
||||||
b.add_argument("symbol", nargs="?", default="")
|
b.add_argument("symbol", nargs="?", default="")
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import struct
|
import struct
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from datetime import datetime
|
from datetime import date, datetime, timedelta
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -92,9 +92,25 @@ class Adjust(IntEnum):
|
|||||||
HFQ = 2 # 后复权
|
HFQ = 2 # 后复权
|
||||||
|
|
||||||
|
|
||||||
|
# 扩展市场中「时间戳跨时区拼接」的市场号:通达信 EX 通道对美股返回的分钟级 K线
|
||||||
|
# 里,日期字段是**美东交易日**、时分字段是**北京时间**,两个时区被拼进同一个
|
||||||
|
# 五元组,必须归一化(见 SecurityBar.__post_init__)。
|
||||||
|
_US_EX_MARKETS: frozenset[int] = frozenset({int(ExMarket.US_STOCK)})
|
||||||
|
|
||||||
|
# 归一化判据:北京时间 hour >= 12 的 bar 与美东交易日属同一自然日。美股常规时段
|
||||||
|
# 09:30~16:00 EDT = 北京 21:30~次日 04:00(冬令时 22:30~次日 05:00),夏令时/
|
||||||
|
# 冬令时下分界都稳定落在 12 点。
|
||||||
|
_US_DAY_SPLIT_HOUR = 12
|
||||||
|
|
||||||
|
|
||||||
@dataclass(slots=True)
|
@dataclass(slots=True)
|
||||||
class SecurityBar:
|
class SecurityBar:
|
||||||
"""K线记录(价格单位:元)。跨市场统一模型(A股/港股/美股/期货共用)。"""
|
"""K线记录(价格单位:元)。跨市场统一模型(A股/港股/美股/期货共用)。
|
||||||
|
|
||||||
|
时间戳口径**统一为北京时间**:A股/港股/期货原始即为北京时间;美股原始是
|
||||||
|
「日期=美东交易日 + 时分=北京时间」的混合值,构造时已还原为真实北京时间
|
||||||
|
(见 ``__post_init__``)。该 bar 归属的交易日见 :attr:`trading_date`。
|
||||||
|
"""
|
||||||
market: int
|
market: int
|
||||||
code: str
|
code: str
|
||||||
open: float
|
open: float
|
||||||
@@ -110,10 +126,40 @@ class SecurityBar:
|
|||||||
minute: int = 0
|
minute: int = 0
|
||||||
symbol: str = "" # 规范化标的串(sz000001 / hk00700),由 API 层填充
|
symbol: str = "" # 规范化标的串(sz000001 / hk00700),由 API 层填充
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
"""美股(EX 通道)时间戳归一化:还原成真实北京时间。
|
||||||
|
|
||||||
|
原始报文里 ``zipday`` 是**美东交易日**、``tminutes`` 是**北京时间分钟数**,
|
||||||
|
直拼出的 ``datetime`` 会把同一交易日的下午半场(北京 00:00~04:00)排到
|
||||||
|
上午半场(北京 21:30~23:59)**之前** —— 单日 390 根全部错位、首末颠倒。
|
||||||
|
这里把凌晨段(北京 hour < 12)的日期 +1 天,得到严格单调的北京时间序列。
|
||||||
|
"""
|
||||||
|
if self.market in _US_EX_MARKETS and self.hour < _US_DAY_SPLIT_HOUR:
|
||||||
|
d = date(self.year, self.month, self.day) + timedelta(days=1)
|
||||||
|
self.year, self.month, self.day = d.year, d.month, d.day
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_us(self) -> bool:
|
||||||
|
"""是否美股(EX 通道 ``US_STOCK``)。"""
|
||||||
|
return self.market in _US_EX_MARKETS
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def datetime(self) -> datetime:
|
def datetime(self) -> datetime:
|
||||||
|
"""bar 的时刻(北京时间)。"""
|
||||||
return datetime(self.year, self.month, self.day, self.hour, self.minute)
|
return datetime(self.year, self.month, self.day, self.hour, self.minute)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def trading_date(self) -> str:
|
||||||
|
"""该 bar 归属的交易日,``YYYY-MM-DD``。
|
||||||
|
|
||||||
|
美股:``datetime`` 已还原为北京时间,凌晨段(北京 hour < 12)属于**前一天**
|
||||||
|
的美东交易日,故回退一天;A股/港股/期货的交易日即自然日。按交易日聚合
|
||||||
|
(如分钟线合成日线)时应当用本字段,而不是 ``datetime.date()``。
|
||||||
|
"""
|
||||||
|
if self.is_us and self.hour < _US_DAY_SPLIT_HOUR:
|
||||||
|
return (date(self.year, self.month, self.day) - timedelta(days=1)).isoformat()
|
||||||
|
return f"{self.year:04d}-{self.month:02d}-{self.day:02d}"
|
||||||
|
|
||||||
|
|
||||||
@dataclass(slots=True)
|
@dataclass(slots=True)
|
||||||
class IndexBar(SecurityBar):
|
class IndexBar(SecurityBar):
|
||||||
@@ -182,21 +228,45 @@ class SecurityInfo:
|
|||||||
|
|
||||||
@dataclass(slots=True)
|
@dataclass(slots=True)
|
||||||
class TransactionRecord:
|
class TransactionRecord:
|
||||||
"""逐笔成交(跨市场统一模型;A股 MAC 通道含 trade_count=成交笔数)"""
|
"""逐笔成交(跨市场统一模型;A股 MAC 通道含 trade_count=成交笔数)。
|
||||||
|
|
||||||
|
注意(实测 2026-10-21):
|
||||||
|
|
||||||
|
- 服务端是按**约 3 秒聚合**返回的切片,不是交易所原始逐笔成交。
|
||||||
|
- ``vol`` / ``trade_count`` 都是**该切片自身的增量**,不是当日累计值。
|
||||||
|
- ``trade_count`` 为**有符号**值:集合竞价段(``bs_flag=8``)会是负数
|
||||||
|
(实测 sh600000 09:15 段为 -874),不要对全表直接求 max/mean。
|
||||||
|
- 返回列表已按时间升序,但同秒多条时顺序不保证。
|
||||||
|
- ``bs_flag=8`` 的**竞价委托记录已被 ``get_ticks()`` 过滤掉**,不会出现在结果里。
|
||||||
|
|
||||||
|
**指数语义完全不同**(``is_cn_index`` 为真时,如 ``sh000016``):
|
||||||
|
|
||||||
|
- ``vol`` 单位是**百元**(= 成交额 / 100),不是「手」。实测
|
||||||
|
``快照amount / Σtick.vol = 100.0002``,且 ``Σtick.vol`` 与分时 Σvol **逐位相等**。
|
||||||
|
想和成分股成交量对比必须先换算,直接比会得到 ~26 倍的假比例。
|
||||||
|
- ``trade_count`` **不是笔数**,而是**当时均价 × 1000**(实测 tc/1000 与分时
|
||||||
|
``avg_price`` 的差 < 1.1)。指数没有「成交笔数」概念。
|
||||||
|
- ``bs_flag`` 语义不成立(实测返回 2,而模型只定义 0=卖 / 1=买)。
|
||||||
|
"""
|
||||||
|
|
||||||
market: int
|
market: int
|
||||||
code: str
|
code: str
|
||||||
time: str # HH:MM:SS(扩展市场为 HH:MM)
|
time: str # HH:MM:SS(扩展市场为 HH:MM)
|
||||||
price: float
|
price: float
|
||||||
vol: float # 手(MAC逐笔)
|
vol: float # 股票=手;指数=百元(成交额/100),见类注释
|
||||||
trade_count: int # 成交笔数(MAC=逐笔记录字段;标准当日逐笔协议也有但需单独解析)
|
trade_count: int # 股票=笔数增量;指数=均价×1000,见类注释
|
||||||
bs_flag: int # 1=主动买 0=主动卖
|
bs_flag: int # 1=主动买 0=主动卖 2=中性 5=盘后固定价(指数语义不成立)
|
||||||
symbol: str = ""
|
symbol: str = ""
|
||||||
fields: dict[str, float] = field(default_factory=dict) # 扩展市场额外字段(增仓等)
|
fields: dict[str, float] = field(default_factory=dict) # 扩展市场额外字段(增仓等)
|
||||||
|
|
||||||
|
|
||||||
@dataclass(slots=True)
|
@dataclass(slots=True)
|
||||||
class MinuteBar:
|
class MinuteBar:
|
||||||
"""分时(跨市场统一模型;A股 MAC 0x122D / 扩展市场 0x122D 同构)"""
|
"""分时(跨市场统一模型;A股 MAC 0x122D / 扩展市场 0x122D 同构)。
|
||||||
|
|
||||||
|
``hour``/``minute`` 是**真实时钟**(``0 <= hour < 24``),跨日的分时请用
|
||||||
|
:attr:`datetime` / :attr:`trading_date`,不要自己拿 ``hour`` 拼日期。
|
||||||
|
"""
|
||||||
price: float
|
price: float
|
||||||
vol: float
|
vol: float
|
||||||
avg_price: float = 0.0 # 均价(协议第2字段)
|
avg_price: float = 0.0 # 均价(协议第2字段)
|
||||||
@@ -207,6 +277,50 @@ class MinuteBar:
|
|||||||
code: str = ""
|
code: str = ""
|
||||||
date: int = 0 # YYYYMMDD
|
date: int = 0 # YYYYMMDD
|
||||||
fields: dict[str, float] = field(default_factory=dict) # 扩展市场额外字段(动量等)
|
fields: dict[str, float] = field(default_factory=dict) # 扩展市场额外字段(动量等)
|
||||||
|
day_offset: int = 0 # 相对 date 的自然日偏移:美股凌晨段为 +1
|
||||||
|
|
||||||
|
def __post_init__(self) -> None:
|
||||||
|
"""美股(EX 通道)分时时间归一化。
|
||||||
|
|
||||||
|
EX 分时的 ``minutes`` 字段是**自开盘起的连续分钟数**,不是「当天的分钟数」:
|
||||||
|
实测 ``usAAPL`` 返回 ``21:30`` ~ ``27:59``(390 条里 240 条的 ``hour`` ≥ 24)。
|
||||||
|
``27:59`` 实际是次日 ``03:59`` 北京时间。直接 ``minutes // 60`` 会得到
|
||||||
|
``hour=27`` 这种**非法时钟值**,任何 ``datetime(...)`` 拼接都会抛异常,
|
||||||
|
按小时聚合也会把凌晨段错误地归到同一天。
|
||||||
|
|
||||||
|
这里拆成「真实小时 + 日偏移」:``hour = (minutes // 60) % 24``,
|
||||||
|
``day_offset = (minutes // 60) // 24``。A股/港股/期货的 ``hour`` 本就合法
|
||||||
|
(A股合成时间 09:30~15:00、港股 09:30~16:00),**零行为变化**。
|
||||||
|
"""
|
||||||
|
if self.hour >= 24:
|
||||||
|
self.day_offset += self.hour // 24
|
||||||
|
self.hour %= 24
|
||||||
|
|
||||||
|
@property
|
||||||
|
def datetime(self) -> datetime:
|
||||||
|
"""该分时点的时刻(北京时间,已含跨日偏移)。"""
|
||||||
|
base = date(self.date // 10000, self.date // 100 % 100, self.date % 100)
|
||||||
|
d = base + timedelta(days=self.day_offset)
|
||||||
|
return datetime(d.year, d.month, d.day, self.hour, self.minute)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_us(self) -> bool:
|
||||||
|
"""是否美股(EX 通道 ``US_STOCK``)。"""
|
||||||
|
return self.market in _US_EX_MARKETS
|
||||||
|
|
||||||
|
@property
|
||||||
|
def trading_date(self) -> str:
|
||||||
|
"""该分时点归属的交易日,``YYYY-MM-DD``。
|
||||||
|
|
||||||
|
美股:一个交易日横跨两个北京自然日(21:30~次日 04:00),凌晨段
|
||||||
|
(北京 hour < 12)归属**前一天**的美东交易日,与
|
||||||
|
:attr:`SecurityBar.trading_date` 口径一致。A股/港股/期货即 ``date`` 本身。
|
||||||
|
按交易日聚合分时(合成日线、日内统计)**必须用它**。
|
||||||
|
"""
|
||||||
|
base = self.datetime
|
||||||
|
if self.is_us and base.hour < _US_DAY_SPLIT_HOUR:
|
||||||
|
base -= timedelta(days=1)
|
||||||
|
return base.date().isoformat()
|
||||||
|
|
||||||
|
|
||||||
@dataclass(slots=True)
|
@dataclass(slots=True)
|
||||||
@@ -448,3 +562,82 @@ class FinancialRecord:
|
|||||||
market: int # 0=深 1=沪
|
market: int # 0=深 1=沪
|
||||||
report_date: int # 报告期 YYYYMMDD
|
report_date: int # 报告期 YYYYMMDD
|
||||||
fields: list[float] = field(default_factory=list)
|
fields: list[float] = field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
# ===== 指数成分成交汇总(TdxData.get_index_trade_stats) =====
|
||||||
|
|
||||||
|
@dataclass(slots=True)
|
||||||
|
class MinuteTradeStat:
|
||||||
|
"""指数**全部成分股**汇总后的单分钟成交**增量**(不是累计)。
|
||||||
|
|
||||||
|
累计曲线用 :meth:`IndexTradeStats.cumulative`,不要自己再对同一字段反复累加。
|
||||||
|
"""
|
||||||
|
time: str # HH:MM
|
||||||
|
volume: float # 成交量合计(手,1 手 = 100 股)
|
||||||
|
trade_count: int # 成交笔数合计
|
||||||
|
amount: float # 成交额估算(元)= Σ(逐笔价 × 手 × 100)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(slots=True)
|
||||||
|
class IndexTradeStats:
|
||||||
|
"""某个指数「全部成分股逐笔」汇总出的成交曲线。
|
||||||
|
|
||||||
|
为什么需要它:**通达信协议里指数本身没有成交笔数** —— 指数逐笔
|
||||||
|
(``sh000001``)的 ``trade_count`` 字段实际是「当时均价 × 1000」(见
|
||||||
|
:class:`TransactionRecord`)。所以「指数笔数」唯一可行的口径是
|
||||||
|
**把成分股的逐笔笔数累加**,本类即该口径。
|
||||||
|
|
||||||
|
口径校验(2026-10-22 实测):
|
||||||
|
|
||||||
|
- ``total_amount`` 是**成分股**合计。对「成分恰好就是指数覆盖面」的指数,它与
|
||||||
|
指数快照 ``amount`` **完全相等**:``sh000016`` 661.34 亿 = 661.34 亿、
|
||||||
|
``sh000300`` 2373.98 亿 = 2373.98 亿(比值 1.0000)。
|
||||||
|
- **但深市 ``399xxx`` 的指数成交额字段是「全板块」口径**,不是样本股:
|
||||||
|
实测 ``sz399001`` 快照 5081.40 亿 = 深市 2908 只股票合计 5081.01 亿、
|
||||||
|
``sz399006`` 2568.60 亿 = 创业板全部股票合计。所以拿深市指数 ``amount``
|
||||||
|
与 ``total_amount`` 比会得到 ~0.43 这类无意义比值——不是数据错,是口径不同。
|
||||||
|
- ``total_volume`` 单位是**手**(与指数逐笔的「百元」单位不同,勿混用)。
|
||||||
|
- 北交所成分(``bj`` 前缀)**没有笔数字段**(实测 ``trade_count`` 恒为 0):
|
||||||
|
此时 ``trade_count_members = 0``、:attr:`trade_count_available` 为 ``False``,
|
||||||
|
只有成交量可用。``sh``/``sz`` 成分均正常返回笔数。
|
||||||
|
|
||||||
|
``minutes`` 只包含**实际有成交**的分钟(午休 11:30~13:00 之间无桶),
|
||||||
|
画累计曲线时请按 ``time`` 自行补齐或直接连线。
|
||||||
|
"""
|
||||||
|
index: str # 规范化指数标的(如 sh000300)
|
||||||
|
date: int | None # None=当日;否则 YYYYMMDD
|
||||||
|
member_count: int # 指数成分数
|
||||||
|
active_members: int # 实际有逐笔成交的成分数(停牌/无成交不计)
|
||||||
|
trade_count_members: int # 返回了笔数的成分数(北交所为 0)
|
||||||
|
minutes: list[MinuteTradeStat] = field(default_factory=list)
|
||||||
|
total_volume: float = 0.0
|
||||||
|
total_trade_count: int = 0
|
||||||
|
total_amount: float = 0.0
|
||||||
|
failed_members: int = 0 # 逐笔拉取失败的成分数(skip_errors=True 时)
|
||||||
|
elapsed: float = 0.0 # 本次汇总耗时(秒)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def trade_count_available(self) -> bool:
|
||||||
|
"""是否有笔数口径(北交所成分为 ``False``,此时 ``trade_count`` 全 0)。"""
|
||||||
|
return self.trade_count_members > 0
|
||||||
|
|
||||||
|
def cumulative(self) -> list[MinuteTradeStat]:
|
||||||
|
"""累计曲线:当日**截止该分钟**的累计量,时间升序。
|
||||||
|
|
||||||
|
这是「当日截止该分钟累计成交笔数」的直接数据源。
|
||||||
|
"""
|
||||||
|
out: list[MinuteTradeStat] = []
|
||||||
|
vol = 0.0
|
||||||
|
tc = 0
|
||||||
|
amt = 0.0
|
||||||
|
for m in self.minutes:
|
||||||
|
vol += m.volume
|
||||||
|
tc += m.trade_count
|
||||||
|
amt += m.amount
|
||||||
|
out.append(MinuteTradeStat(time=m.time, volume=vol,
|
||||||
|
trade_count=tc, amount=amt))
|
||||||
|
return out
|
||||||
|
|
||||||
|
def cum_trade_count(self) -> list[tuple[str, int]]:
|
||||||
|
"""``[(HH:MM, 累计笔数)]``,画「截止该分钟累计笔数」曲线用。"""
|
||||||
|
return [(m.time, m.trade_count) for m in self.cumulative()]
|
||||||
@@ -1,4 +1,11 @@
|
|||||||
"""EX扩展市场连接池(简化版:固定连接数,借还+坏连接重建+login自动)"""
|
"""EX扩展市场连接池(固定连接数,借还+坏连接重建+login自动)。
|
||||||
|
|
||||||
|
与标准池 pool.py 的背压语义对齐:acquire 在无空闲连接且无法扩容时,
|
||||||
|
基于 asyncio.Condition 排队等待 release 唤醒(默认 acquire_timeout=30s),
|
||||||
|
而不是快速失败。per_server_cap 限制单台服务器连接数;单服务器场景下
|
||||||
|
有效并发 = servers × per_server_cap(默认 1),通过 TdxData(ex_pool_cap=N)
|
||||||
|
可对同一 EX 主机开多连接。
|
||||||
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
@@ -9,6 +16,23 @@ from .ex_connection import ExTdxConnection
|
|||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DEFAULT_ACQUIRE_TIMEOUT = 30.0
|
||||||
|
|
||||||
|
|
||||||
|
class ExPoolTimeoutError(ConnectionError):
|
||||||
|
"""acquire 等待超时(携带池状态快照,便于排障)。"""
|
||||||
|
|
||||||
|
def __init__(self, pool: "ExPool", timeout: float) -> None:
|
||||||
|
idle = len(pool._idle)
|
||||||
|
total = pool._total
|
||||||
|
servers = len(pool.servers)
|
||||||
|
cap = pool.per_server_cap
|
||||||
|
super().__init__(
|
||||||
|
f"EX连接池获取超时(等了 {timeout:.1f}s):"
|
||||||
|
f"idle={idle} total={total} servers={servers} per_server_cap={cap} "
|
||||||
|
f"有效并发={servers * cap}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ExPool:
|
class ExPool:
|
||||||
"""EX通道连接池(港股/期货,端口7727,连接时自动login)。"""
|
"""EX通道连接池(港股/期货,端口7727,连接时自动login)。"""
|
||||||
@@ -16,6 +40,7 @@ class ExPool:
|
|||||||
def __init__(self, servers: list[str], *, timeout: float = 10.0,
|
def __init__(self, servers: list[str], *, timeout: float = 10.0,
|
||||||
min_size: int = 2, max_size: int = 6,
|
min_size: int = 2, max_size: int = 6,
|
||||||
per_server_cap: int = 1,
|
per_server_cap: int = 1,
|
||||||
|
acquire_timeout: float | None = None,
|
||||||
handshake: str = "login",
|
handshake: str = "login",
|
||||||
handshake_map: dict | None = None) -> None:
|
handshake_map: dict | None = None) -> None:
|
||||||
self.servers = servers
|
self.servers = servers
|
||||||
@@ -25,14 +50,17 @@ class ExPool:
|
|||||||
self.min_size = min_size
|
self.min_size = min_size
|
||||||
self.max_size = max_size
|
self.max_size = max_size
|
||||||
self.per_server_cap = max(1, per_server_cap) # 一台服务器最多几个连接(默认1=单连接)
|
self.per_server_cap = max(1, per_server_cap) # 一台服务器最多几个连接(默认1=单连接)
|
||||||
|
self.acquire_timeout = (
|
||||||
|
DEFAULT_ACQUIRE_TIMEOUT if acquire_timeout is None else acquire_timeout
|
||||||
|
)
|
||||||
self._idle: list[tuple[str, ExTdxConnection]] = []
|
self._idle: list[tuple[str, ExTdxConnection]] = []
|
||||||
self._server_count: dict[str, int] = {}
|
self._server_count: dict[str, int] = {}
|
||||||
self._total = 0
|
self._total = 0
|
||||||
self._closed = False
|
self._closed = False
|
||||||
self._lock = asyncio.Lock()
|
self._cond = asyncio.Condition()
|
||||||
|
|
||||||
async def start(self) -> None:
|
async def start(self) -> None:
|
||||||
async with self._lock:
|
async with self._cond:
|
||||||
for host in self.servers[: self.min_size]:
|
for host in self.servers[: self.min_size]:
|
||||||
if self._server_count.get(host, 0) >= self.per_server_cap:
|
if self._server_count.get(host, 0) >= self.per_server_cap:
|
||||||
continue
|
continue
|
||||||
@@ -47,7 +75,7 @@ class ExPool:
|
|||||||
|
|
||||||
async def close(self) -> None:
|
async def close(self) -> None:
|
||||||
self._closed = True
|
self._closed = True
|
||||||
async with self._lock:
|
async with self._cond:
|
||||||
for _, conn in self._idle:
|
for _, conn in self._idle:
|
||||||
try:
|
try:
|
||||||
await conn.close()
|
await conn.close()
|
||||||
@@ -56,48 +84,61 @@ class ExPool:
|
|||||||
self._idle.clear()
|
self._idle.clear()
|
||||||
self._total = 0
|
self._total = 0
|
||||||
self._server_count.clear()
|
self._server_count.clear()
|
||||||
|
self._cond.notify_all()
|
||||||
|
|
||||||
async def acquire(self) -> ExTdxConnection:
|
async def acquire(self, timeout: float | None = None) -> ExTdxConnection:
|
||||||
for _ in range(3):
|
"""借出一条连接;无空闲且无法扩容时排队等待 release 唤醒。
|
||||||
stale_conn = None
|
|
||||||
new_conn = None
|
timeout 默认取 self.acquire_timeout(30s);传 None 表示无限等待。
|
||||||
new_host = None
|
"""
|
||||||
async with self._lock:
|
if timeout is None:
|
||||||
|
timeout = self.acquire_timeout
|
||||||
|
deadline = None if timeout is None else time.monotonic() + timeout
|
||||||
|
async with self._cond:
|
||||||
|
while True:
|
||||||
|
# 1. 优先借用空闲连接(过期则丢弃,槽位在锁内回收)
|
||||||
if self._idle:
|
if self._idle:
|
||||||
_, conn = self._idle.pop()
|
_, conn = self._idle.pop()
|
||||||
fresh = time.monotonic() - conn.last_used < 30.0
|
fresh = time.monotonic() - conn.last_used < 30.0
|
||||||
if conn.alive and fresh:
|
if conn.alive and fresh:
|
||||||
return conn
|
return conn
|
||||||
# 连接空闲过久(login会话可能失效)→ 丢弃重连
|
# 连接空闲过久(login会话可能失效)→ 丢弃,槽位让给扩容
|
||||||
self._total = max(0, self._total - 1)
|
self._total = max(0, self._total - 1)
|
||||||
self._server_count[conn.host] = max(0, self._server_count.get(conn.host, 0) - 1)
|
self._server_count[conn.host] = max(0, self._server_count.get(conn.host, 0) - 1)
|
||||||
stale_conn = conn
|
self._schedule_close(conn)
|
||||||
# 扩容:在锁内预留槽位,锁外执行网络IO
|
|
||||||
elif self._total < self.max_size and self.servers:
|
# 2. 可扩容 → 锁内预留槽位,锁外建连
|
||||||
|
new_conn = None
|
||||||
|
new_host = None
|
||||||
|
if self._total < self.max_size and self.servers:
|
||||||
host = self._next_available_server()
|
host = self._next_available_server()
|
||||||
if host:
|
if host is not None:
|
||||||
self._total += 1
|
self._total += 1
|
||||||
self._server_count[host] = self._server_count.get(host, 0) + 1
|
self._server_count[host] = self._server_count.get(host, 0) + 1
|
||||||
new_conn = ExTdxConnection(host, 7727, self.timeout, self.handshake_map.get(host, self.handshake))
|
new_conn = ExTdxConnection(host, 7727, self.timeout, self.handshake_map.get(host, self.handshake))
|
||||||
new_host = host
|
new_host = host
|
||||||
# 锁外:关闭过期连接、建立新连接
|
|
||||||
if stale_conn is not None:
|
|
||||||
try:
|
|
||||||
await stale_conn.close()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if new_conn is not None:
|
if new_conn is not None:
|
||||||
|
# 锁外网络 IO:连接失败回滚槽位后继续等待
|
||||||
try:
|
try:
|
||||||
await new_conn.connect()
|
await new_conn.connect()
|
||||||
return new_conn
|
return new_conn
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.warning("EX扩容失败 %s: %s", new_host, str(e)[:60])
|
log.warning("EX扩容失败 %s: %s", new_host, str(e)[:60])
|
||||||
# 连接失败,回滚预留槽位
|
async with self._cond:
|
||||||
async with self._lock:
|
|
||||||
self._total = max(0, self._total - 1)
|
self._total = max(0, self._total - 1)
|
||||||
self._server_count[new_host] = max(0, self._server_count.get(new_host, 0) - 1)
|
self._server_count[new_host] = max(0, self._server_count.get(new_host, 0) - 1)
|
||||||
await asyncio.sleep(0.05)
|
|
||||||
raise ConnectionError("EX连接池无可用连接")
|
# 3. 池满:排队等待 release 唤醒(或超时)
|
||||||
|
now = time.monotonic()
|
||||||
|
if deadline is not None and now >= deadline:
|
||||||
|
raise ExPoolTimeoutError(self, timeout)
|
||||||
|
try:
|
||||||
|
if deadline is None:
|
||||||
|
await self._cond.wait()
|
||||||
|
else:
|
||||||
|
await asyncio.wait_for(self._cond.wait(), timeout=deadline - now)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
raise ExPoolTimeoutError(self, timeout)
|
||||||
|
|
||||||
def _next_available_server(self) -> str | None:
|
def _next_available_server(self) -> str | None:
|
||||||
for h in self.servers:
|
for h in self.servers:
|
||||||
@@ -105,15 +146,23 @@ class ExPool:
|
|||||||
return h
|
return h
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def release(self, conn: ExTdxConnection, *, broken: bool = False) -> None:
|
def _schedule_close(self, conn: ExTdxConnection) -> None:
|
||||||
if broken or not conn.alive:
|
async def _close():
|
||||||
async with self._lock:
|
|
||||||
self._total = max(0, self._total - 1)
|
|
||||||
self._server_count[conn.host] = max(0, self._server_count.get(conn.host, 0) - 1)
|
|
||||||
try:
|
try:
|
||||||
await conn.close()
|
await conn.close()
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
return
|
try:
|
||||||
async with self._lock:
|
asyncio.get_running_loop().create_task(_close())
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def release(self, conn: ExTdxConnection, *, broken: bool = False) -> None:
|
||||||
|
async with self._cond:
|
||||||
|
if broken or not conn.alive:
|
||||||
|
self._total = max(0, self._total - 1)
|
||||||
|
self._server_count[conn.host] = max(0, self._server_count.get(conn.host, 0) - 1)
|
||||||
|
self._schedule_close(conn)
|
||||||
|
else:
|
||||||
self._idle.append((conn.host, conn))
|
self._idle.append((conn.host, conn))
|
||||||
|
self._cond.notify_all()
|
||||||
@@ -13,16 +13,46 @@ MAX_QUOTES = 80
|
|||||||
|
|
||||||
|
|
||||||
def price_decimal_digits(market: int, code: str) -> int:
|
def price_decimal_digits(market: int, code: str) -> int:
|
||||||
"""推断报价有效小数位:股票2位(/100)、指数/ETF/基金/转债/国债3位(/1000)。"""
|
"""推断报价的小数位:**协议原始整数除以 ``10**该值``** 才是真实价格。
|
||||||
|
|
||||||
|
四档:股票/指数/板块 ``2``、基金/ETF/B股 ``3``、债券/回购 ``4``。
|
||||||
|
|
||||||
|
该函数只是**离线推断**(``get_quotes`` 解析时拿不到证券清单)。真正的权威来源
|
||||||
|
是证券列表里协议自带的 ``SecurityInfo.decimal_point`` 字段——2026-10-22 拉取
|
||||||
|
全量 51,982 只(沪 27,520 + 深 24,462)逐条比对,本函数与之一一吻合、**零例外**。
|
||||||
|
能对上是因为「三位前缀 → 小数位」在全量清单里没有任何一个前缀出现混合小数位。
|
||||||
|
|
||||||
|
历史修正(两次都是同一类错误:把某段前缀想当然地归错档):
|
||||||
|
|
||||||
|
- **2026-10-21**:沪市 ``000``/``8`` 被判成 3 位 → 上证指数/上证50/科创50/
|
||||||
|
板块指数**价格少 10 倍**(3713 读成 371)。它们实际是 2 位。
|
||||||
|
- **2026-10-22**:债券与回购全线偏差 —— 沪市 ``010/018/019/020/1xx/2xx``
|
||||||
|
与深市 ``10x/11x/12x/13x/14x/19x/50x/52x/56x`` 实际是 **4 位**,原先
|
||||||
|
沪市落到 2 位(**少 100 倍**)、深市落到 3 位(**少 10 倍**)。实测例:
|
||||||
|
``sh019742`` 110.228 读成 11022.8、``sh204001`` 1.385 读成 138.5、
|
||||||
|
``sz131810`` 1.38 读成 13.8。同时沪市 B 股 ``900xxx`` 是 3 位(原先 2 位)。
|
||||||
|
|
||||||
|
实证方法:拿 ``pre_close``(快照口径)除以上一交易日**日线收盘**(K线口径,
|
||||||
|
走 ``/1000`` 独立路径),比值即标度误差 —— 1.0 正确、10 少 10 倍、100 少 100 倍。
|
||||||
|
"""
|
||||||
code = (code or "").strip().rstrip("\x00")
|
code = (code or "").strip().rstrip("\x00")
|
||||||
if market == 1: # SH
|
if market == 1: # 沪市
|
||||||
if code.startswith(("5", "000", "8")):
|
if code.startswith("5"):
|
||||||
return 3
|
return 3 # 基金/ETF/LOF(50/51/52/56/58 系)
|
||||||
return 2
|
if code.startswith("900"):
|
||||||
if market == 0: # SZ
|
return 3 # B股
|
||||||
if code.startswith("1"):
|
if code.startswith(("000", "36", "88", "99", "7", "6")):
|
||||||
return 3
|
return 2 # 指数/板块/优先股/配售/股票(60/601/603/605/688)
|
||||||
return 2
|
return 4 # 债券(01x/1xx/2xx)、回购(204xxx)、ABS
|
||||||
|
if market == 0: # 深市
|
||||||
|
if code.startswith(("080", "158", "159", "160", "161", "162", "163",
|
||||||
|
"164", "165", "166", "167", "168", "169",
|
||||||
|
"180", "181")):
|
||||||
|
return 3 # 基金/ETF/LOF/配债
|
||||||
|
if code.startswith(("000", "001", "002", "003", "070", "140",
|
||||||
|
"200", "201", "300", "301", "302", "395", "399")):
|
||||||
|
return 2 # 股票/指数/B股/优先股
|
||||||
|
return 4 # 债券(10x-14x/19x)、回购(131xxx)、ABS(50x/52x/56x)
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -111,8 +111,11 @@ class MacTransactionCmd(BaseCommand[list[TransactionRecord]]):
|
|||||||
for i in range(count):
|
for i in range(count):
|
||||||
offset = 39 + i * 18
|
offset = 39 + i * 18
|
||||||
try:
|
try:
|
||||||
|
# trade_count 必须按**有符号** int32 解析:集合竞价段(bs_flag=8)
|
||||||
|
# 该字段实为负值,按 uint32 会读出 42 亿量级的脏数据。
|
||||||
|
# 实测(2026-10-21 sh600000):09:15:03 原始 96fcffff = -874。
|
||||||
time_sec, price, volume, trade_count, bs_flag = unpack_from(
|
time_sec, price, volume, trade_count, bs_flag = unpack_from(
|
||||||
"<IfIIH", body, offset, "mac transaction item"
|
"<IfIiH", body, offset, "mac transaction item"
|
||||||
)
|
)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ class PageSpec:
|
|||||||
ordered: bool = True # 按 start 升序拼接
|
ordered: bool = True # 按 start 升序拼接
|
||||||
min_page: int = 100 # 规避标准K线 count<100 空响应坑
|
min_page: int = 100 # 规避标准K线 count<100 空响应坑
|
||||||
start_offset: int = 0 # 起始偏移(从该偏移开始分页,避免拉取历史全量)
|
start_offset: int = 0 # 起始偏移(从该偏移开始分页,避免拉取历史全量)
|
||||||
|
newest_first: bool = False # 服务端 start 从最新往回数(0x122F 逐笔即如此)
|
||||||
|
|
||||||
|
|
||||||
@dataclass(slots=True)
|
@dataclass(slots=True)
|
||||||
@@ -94,7 +95,7 @@ class Paginator:
|
|||||||
if s.pc is not None:
|
if s.pc is not None:
|
||||||
pc, s.pc = s.pc, None
|
pc, s.pc = s.pc, None
|
||||||
await self.pool.release(pc)
|
await self.pool.release(pc)
|
||||||
return self._assemble(out, spec.ordered)
|
return self._assemble(out, spec.ordered, spec.newest_first)
|
||||||
|
|
||||||
def _plan_starts(self, spec: PageSpec) -> list[tuple[int, int]]:
|
def _plan_starts(self, spec: PageSpec) -> list[tuple[int, int]]:
|
||||||
"""切页:返回 [(start, count), ...]。"""
|
"""切页:返回 [(start, count), ...]。"""
|
||||||
@@ -158,9 +159,12 @@ class Paginator:
|
|||||||
return None, False
|
return None, False
|
||||||
return None, False
|
return None, False
|
||||||
|
|
||||||
def _assemble(self, results: list[PageResult], ordered: bool) -> list[Any]:
|
def _assemble(self, results: list[PageResult], ordered: bool,
|
||||||
|
newest_first: bool = False) -> list[Any]:
|
||||||
|
# newest_first=True:start=0 是最新一页,start 越大越旧 → 反向拼才对得上时间序。
|
||||||
|
# 单页内部服务端已按时间升序,所以只翻页序,不翻页内元素。
|
||||||
if ordered:
|
if ordered:
|
||||||
results.sort(key=lambda r: r.start)
|
results.sort(key=lambda r: r.start, reverse=newest_first)
|
||||||
out: list[Any] = []
|
out: list[Any] = []
|
||||||
for r in results:
|
for r in results:
|
||||||
out.extend(r.items)
|
out.extend(r.items)
|
||||||
|
|||||||
+197
-140
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"version": "2026-09-21",
|
"version": "2026-10-16",
|
||||||
"note": "离线探针复测(2026-09-21 19:03): 可达=25/33, 入池fast_pool=10台",
|
"note": "离线探针复测(2026-10-16 13:46): 可达=30/36, 入池fast_pool=10台",
|
||||||
"servers": [
|
"servers": [
|
||||||
{
|
{
|
||||||
"ip": "139.9.52.158",
|
"ip": "139.9.52.158",
|
||||||
@@ -11,14 +11,14 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.22,
|
"latency": 0.172,
|
||||||
"checked_at": "2026-09-21 19:03:04",
|
"checked_at": "2026-10-16 13:46:17",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": true,
|
"fast_pool": true,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"standard": 0.226,
|
"standard": 0.172,
|
||||||
"mac": 0.22
|
"mac": 0.183
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -30,14 +30,14 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.366,
|
"latency": 0.298,
|
||||||
"checked_at": "2026-09-21 19:03:04",
|
"checked_at": "2026-10-16 13:46:18",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": true,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"standard": 0.396,
|
"standard": 0.316,
|
||||||
"mac": 0.366
|
"mac": 0.298
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -48,13 +48,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.257,
|
"latency": 0.236,
|
||||||
"checked_at": "2026-09-21 19:03:09",
|
"checked_at": "2026-10-16 13:46:23",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": true,
|
"fast_pool": true,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.257
|
"mac": 0.236
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -65,13 +65,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.248,
|
"latency": 0.213,
|
||||||
"checked_at": "2026-09-21 19:03:04",
|
"checked_at": "2026-10-16 13:46:17",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": true,
|
"fast_pool": true,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.248
|
"mac": 0.213
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -84,15 +84,15 @@
|
|||||||
],
|
],
|
||||||
"handshake": "setup",
|
"handshake": "setup",
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.221,
|
"latency": 0.316,
|
||||||
"checked_at": "2026-09-21 19:03:00",
|
"checked_at": "2026-10-16 13:46:13",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": true,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"standard": 0.399,
|
"standard": 0.316,
|
||||||
"mac": 0.414,
|
"mac": 0.329,
|
||||||
"ex": 0.221
|
"ex": 0.539
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -104,10 +104,10 @@
|
|||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "fail",
|
"status": "fail",
|
||||||
"latency": null,
|
"latency": null,
|
||||||
"checked_at": "2026-09-21 19:03:20",
|
"checked_at": "2026-10-16 13:46:34",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {}
|
"_per_proto": {}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -118,13 +118,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.363,
|
"latency": 0.682,
|
||||||
"checked_at": "2026-09-21 19:03:10",
|
"checked_at": "2026-10-16 13:46:24",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": true,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.363
|
"mac": 0.682
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -136,10 +136,10 @@
|
|||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "fail",
|
"status": "fail",
|
||||||
"latency": null,
|
"latency": null,
|
||||||
"checked_at": "2026-09-21 19:03:20",
|
"checked_at": "2026-10-16 13:46:34",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {}
|
"_per_proto": {}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -150,13 +150,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.344,
|
"latency": 0.607,
|
||||||
"checked_at": "2026-09-21 19:03:10",
|
"checked_at": "2026-10-16 13:46:23",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": true,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.344
|
"mac": 0.607
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -167,13 +167,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.404,
|
"latency": 1.264,
|
||||||
"checked_at": "2026-09-21 19:03:10",
|
"checked_at": "2026-10-16 13:46:24",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.404
|
"mac": 1.264
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -185,10 +185,10 @@
|
|||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "fail",
|
"status": "fail",
|
||||||
"latency": null,
|
"latency": null,
|
||||||
"checked_at": "2026-09-21 19:03:24",
|
"checked_at": "2026-10-16 13:46:34",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {}
|
"_per_proto": {}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -200,10 +200,10 @@
|
|||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "fail",
|
"status": "fail",
|
||||||
"latency": null,
|
"latency": null,
|
||||||
"checked_at": "2026-09-21 19:03:21",
|
"checked_at": "2026-10-16 13:46:34",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {}
|
"_per_proto": {}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -214,13 +214,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.376,
|
"latency": 0.612,
|
||||||
"checked_at": "2026-09-21 19:03:10",
|
"checked_at": "2026-10-16 13:46:23",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.376
|
"mac": 0.612
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -231,13 +231,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.377,
|
"latency": 0.613,
|
||||||
"checked_at": "2026-09-21 19:03:10",
|
"checked_at": "2026-10-16 13:46:23",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.377
|
"mac": 0.613
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -248,13 +248,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.401,
|
"latency": 0.624,
|
||||||
"checked_at": "2026-09-21 19:03:10",
|
"checked_at": "2026-10-16 13:46:23",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.401
|
"mac": 0.624
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -265,13 +265,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.42,
|
"latency": 0.592,
|
||||||
"checked_at": "2026-09-21 19:03:10",
|
"checked_at": "2026-10-16 13:46:23",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.42
|
"mac": 0.592
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -282,13 +282,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.401,
|
"latency": 0.624,
|
||||||
"checked_at": "2026-09-21 19:03:10",
|
"checked_at": "2026-10-16 13:46:23",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.401
|
"mac": 0.624
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -300,14 +300,14 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.485,
|
"latency": 0.306,
|
||||||
"checked_at": "2026-09-21 19:03:05",
|
"checked_at": "2026-10-16 13:46:18",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"standard": 0.485,
|
"standard": 0.352,
|
||||||
"mac": 0.605
|
"mac": 0.306
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -319,10 +319,10 @@
|
|||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "fail",
|
"status": "fail",
|
||||||
"latency": null,
|
"latency": null,
|
||||||
"checked_at": "2026-09-21 19:03:04",
|
"checked_at": "2026-10-16 13:46:18",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {}
|
"_per_proto": {}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -334,14 +334,14 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.444,
|
"latency": 0.307,
|
||||||
"checked_at": "2026-09-21 19:03:05",
|
"checked_at": "2026-10-16 13:46:18",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"standard": 0.444,
|
"standard": 0.35,
|
||||||
"mac": 0.903
|
"mac": 0.307
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -352,13 +352,16 @@
|
|||||||
"mac"
|
"mac"
|
||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "fail",
|
"status": "ok",
|
||||||
"latency": null,
|
"latency": 0.395,
|
||||||
"checked_at": "2026-09-21 19:03:15",
|
"checked_at": "2026-10-16 13:46:18",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {}
|
"_per_proto": {
|
||||||
|
"standard": 0.395,
|
||||||
|
"mac": 0.675
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ip": "117.34.114.15",
|
"ip": "117.34.114.15",
|
||||||
@@ -369,14 +372,14 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.435,
|
"latency": 0.345,
|
||||||
"checked_at": "2026-09-21 19:03:05",
|
"checked_at": "2026-10-16 13:46:18",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"standard": 0.463,
|
"standard": 0.345,
|
||||||
"mac": 0.435
|
"mac": 0.726
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -387,13 +390,16 @@
|
|||||||
"mac"
|
"mac"
|
||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "fail",
|
"status": "ok",
|
||||||
"latency": null,
|
"latency": 0.326,
|
||||||
"checked_at": "2026-09-21 19:03:14",
|
"checked_at": "2026-10-16 13:46:18",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {}
|
"_per_proto": {
|
||||||
|
"standard": 0.326,
|
||||||
|
"mac": 0.743
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ip": "117.34.114.27",
|
"ip": "117.34.114.27",
|
||||||
@@ -404,14 +410,14 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.482,
|
"latency": 0.394,
|
||||||
"checked_at": "2026-09-21 19:03:05",
|
"checked_at": "2026-10-16 13:46:18",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"standard": 0.482,
|
"standard": 0.394,
|
||||||
"mac": 0.579
|
"mac": 0.78
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -423,14 +429,14 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.609,
|
"latency": 0.327,
|
||||||
"checked_at": "2026-09-21 19:03:05",
|
"checked_at": "2026-10-16 13:46:18",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"standard": 0.946,
|
"standard": 0.327,
|
||||||
"mac": 0.609
|
"mac": 0.328
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -442,14 +448,14 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.46,
|
"latency": 0.348,
|
||||||
"checked_at": "2026-09-21 19:03:05",
|
"checked_at": "2026-10-16 13:46:18",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"standard": 0.46,
|
"standard": 0.348,
|
||||||
"mac": 0.521
|
"mac": 0.74
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -461,9 +467,9 @@
|
|||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "fail",
|
"status": "fail",
|
||||||
"latency": null,
|
"latency": null,
|
||||||
"checked_at": "2026-09-21 19:03:24",
|
"checked_at": "2026-10-16 13:46:37",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"_per_proto": {}
|
"_per_proto": {}
|
||||||
},
|
},
|
||||||
@@ -475,13 +481,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.271,
|
"latency": 0.726,
|
||||||
"checked_at": "2026-09-21 19:03:04",
|
"checked_at": "2026-10-16 13:46:18",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"fast_pool": true,
|
"fast_pool": false,
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.271
|
"mac": 0.726
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -492,13 +498,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.375,
|
"latency": 0.624,
|
||||||
"checked_at": "2026-09-21 19:03:05",
|
"checked_at": "2026-10-16 13:46:18",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.375
|
"mac": 0.624
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -509,13 +515,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": null,
|
"handshake": null,
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.424,
|
"latency": 0.56,
|
||||||
"checked_at": "2026-09-21 19:03:05",
|
"checked_at": "2026-10-16 13:46:30",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"fast_pool": false,
|
"fast_pool": false,
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"mac": 0.424
|
"mac": 0.56
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -526,13 +532,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": "login",
|
"handshake": "login",
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.176,
|
"latency": 0.097,
|
||||||
"checked_at": "2026-09-21 19:03:08",
|
"checked_at": "2026-10-16 13:46:21",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"fast_pool": true,
|
"fast_pool": true,
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"ex": 0.176
|
"ex": 0.097
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -543,13 +549,13 @@
|
|||||||
],
|
],
|
||||||
"handshake": "login",
|
"handshake": "login",
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.148,
|
"latency": 0.097,
|
||||||
"checked_at": "2026-09-21 19:03:08",
|
"checked_at": "2026-10-16 13:46:21",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"fast_pool": true,
|
"fast_pool": true,
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"ex": 0.148
|
"ex": 0.097
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -560,15 +566,66 @@
|
|||||||
],
|
],
|
||||||
"handshake": "setup",
|
"handshake": "setup",
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"latency": 0.166,
|
"latency": 0.103,
|
||||||
"checked_at": "2026-09-21 19:03:08",
|
"checked_at": "2026-10-16 13:46:21",
|
||||||
"source": "probe-2026-08-12",
|
"source": "probe-2026-08-12",
|
||||||
"checked_date": "2026-09-21",
|
"checked_date": "2026-10-16",
|
||||||
"fast_pool": true,
|
"fast_pool": true,
|
||||||
"_per_proto": {
|
"_per_proto": {
|
||||||
"ex": 0.166
|
"ex": 0.103
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ip": "112.74.214.43",
|
||||||
|
"name": "扩展市场深圳双线1",
|
||||||
|
"protocols": [
|
||||||
|
"ex"
|
||||||
|
],
|
||||||
|
"handshake": "login",
|
||||||
|
"status": "ok",
|
||||||
|
"latency": 0.095,
|
||||||
|
"checked_at": "2026-10-16 13:46:21",
|
||||||
|
"source": "connect.cfg",
|
||||||
|
"fast_pool": true,
|
||||||
|
"checked_date": "2026-10-16",
|
||||||
|
"_per_proto": {
|
||||||
|
"ex": 0.095
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ip": "120.25.218.6",
|
||||||
|
"name": "扩展市场深圳双线2",
|
||||||
|
"protocols": [
|
||||||
|
"ex"
|
||||||
|
],
|
||||||
|
"handshake": "login",
|
||||||
|
"status": "ok",
|
||||||
|
"latency": 0.101,
|
||||||
|
"checked_at": "2026-10-16 13:46:21",
|
||||||
|
"source": "connect.cfg",
|
||||||
|
"fast_pool": true,
|
||||||
|
"checked_date": "2026-10-16",
|
||||||
|
"_per_proto": {
|
||||||
|
"ex": 0.101
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ip": "47.102.108.214",
|
||||||
|
"name": "扩展市场上海双线2",
|
||||||
|
"protocols": [
|
||||||
|
"ex"
|
||||||
|
],
|
||||||
|
"handshake": "login",
|
||||||
|
"status": "ok",
|
||||||
|
"latency": 0.149,
|
||||||
|
"checked_at": "2026-10-16 13:46:22",
|
||||||
|
"source": "connect.cfg",
|
||||||
|
"fast_pool": true,
|
||||||
|
"checked_date": "2026-10-16",
|
||||||
|
"_per_proto": {
|
||||||
|
"ex": 0.149
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"checked_date": "2026-09-21"
|
"checked_date": "2026-10-16"
|
||||||
}
|
}
|
||||||
@@ -457,6 +457,24 @@ async def test_boards(td):
|
|||||||
assert isinstance(chg, list)
|
assert isinstance(chg, list)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_index_trade_stats(td):
|
||||||
|
"""指数成分成交汇总:成分 → 逐笔 → 每分钟量/笔数。"""
|
||||||
|
for name in ("get_index_members", "get_index_trade_stats",
|
||||||
|
"get_index_trade_stats_multi"):
|
||||||
|
_use(name)
|
||||||
|
mem = await td.get_index_members("000016")
|
||||||
|
assert isinstance(mem, list) and mem and mem[0].symbol
|
||||||
|
st = await td.get_index_trade_stats("000016")
|
||||||
|
assert st.index == "sh000016"
|
||||||
|
assert st.member_count >= 1
|
||||||
|
assert st.minutes and st.minutes[0].time == "09:30"
|
||||||
|
assert st.total_trade_count >= 1
|
||||||
|
assert st.cum_trade_count()[-1][1] == st.total_trade_count
|
||||||
|
multi = await td.get_index_trade_stats_multi("000016", [20261020, None])
|
||||||
|
assert len(multi) == 2
|
||||||
|
assert multi[0].date == 20261020 and multi[1].date is None
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------------- #
|
# --------------------------------------------------------------------------- #
|
||||||
# 六、服务器文件
|
# 六、服务器文件
|
||||||
# --------------------------------------------------------------------------- #
|
# --------------------------------------------------------------------------- #
|
||||||
|
|||||||
+211
-1
@@ -8,7 +8,13 @@ import unittest.mock as mock
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pytdxdata.api import TdxData
|
from pytdxdata.api import TdxData
|
||||||
from pytdxdata.models import KlinePeriod, Market, MinuteBar, SecurityBar
|
from pytdxdata.models import (
|
||||||
|
KlinePeriod,
|
||||||
|
Market,
|
||||||
|
MinuteBar,
|
||||||
|
SecurityBar,
|
||||||
|
TransactionRecord,
|
||||||
|
)
|
||||||
from pytdxdata.protocol.commands.mac_tick import MacTickChartCmd
|
from pytdxdata.protocol.commands.mac_tick import MacTickChartCmd
|
||||||
from pytdxdata.scheduler.paginator import PageSpec, Paginator
|
from pytdxdata.scheduler.paginator import PageSpec, Paginator
|
||||||
|
|
||||||
@@ -243,6 +249,70 @@ async def test_paginator_start_offset_default_zero():
|
|||||||
assert result[0].close == 10.0 # 从 offset 0 开始
|
assert result[0].close == 10.0 # 从 offset 0 开始
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- Paginator 逆序偏移(服务端 start 从最新往回数) ----------
|
||||||
|
|
||||||
|
class NewestFirstConn:
|
||||||
|
"""模拟 0x122F 真实行为:start=0 返回**最新**的一页,start 越大越旧。
|
||||||
|
|
||||||
|
实测依据(2026-10-21,sh600000):
|
||||||
|
start=0 -> 14:07:48 .. 15:20:16(当天最新 1000 条)
|
||||||
|
start=4000 -> 09:15:03 .. 09:41:18(当天最早)
|
||||||
|
每页内部按时间升序,页与页之间是倒序。
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, total: int, page_size: int):
|
||||||
|
self.total = total
|
||||||
|
self.page_size = page_size
|
||||||
|
self.executed: list[tuple[int, int]] = []
|
||||||
|
|
||||||
|
async def execute(self, cmd):
|
||||||
|
start, count = cmd
|
||||||
|
self.executed.append((start, count))
|
||||||
|
# 服务端语义:偏移 start 是从"最新"往回数,所以取倒数区间
|
||||||
|
end_from_oldest = self.total - start
|
||||||
|
begin_from_oldest = max(0, end_from_oldest - count)
|
||||||
|
return [_make_bar(i) for i in range(begin_from_oldest, end_from_oldest)]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_paginator_newest_first_offset_returns_chronological():
|
||||||
|
"""服务端 start 从最新往回数时,交付列表必须仍是时间升序(BUG 回归)。"""
|
||||||
|
conns = [NewestFirstConn(3000, 1000) for _ in range(3)]
|
||||||
|
pool = FakePool(conns)
|
||||||
|
pag = Paginator(pool, concurrency=3) # type: ignore[arg-type]
|
||||||
|
|
||||||
|
def factory(s, c):
|
||||||
|
return (s, c)
|
||||||
|
|
||||||
|
result = await pag.fetch_all(
|
||||||
|
factory, PageSpec(page_size=1000, total=3000, newest_first=True))
|
||||||
|
assert len(result) == 3000
|
||||||
|
# 时间升序:close 随 index 单调递增
|
||||||
|
closes = [b.close for b in result]
|
||||||
|
assert closes == sorted(closes), "返回列表必须按时间升序拼接"
|
||||||
|
assert result[0].close == pytest.approx(10.0)
|
||||||
|
assert result[-1].close == pytest.approx(10.0 + 2999 * 0.01)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_paginator_newest_first_respects_start_offset():
|
||||||
|
"""newest_first + start_offset:只取最近 N 条里的后段。"""
|
||||||
|
conns = [NewestFirstConn(3000, 1000) for _ in range(3)]
|
||||||
|
pool = FakePool(conns)
|
||||||
|
pag = Paginator(pool, concurrency=3) # type: ignore[arg-type]
|
||||||
|
|
||||||
|
def factory(s, c):
|
||||||
|
return (s, c)
|
||||||
|
|
||||||
|
result = await pag.fetch_all(
|
||||||
|
factory,
|
||||||
|
PageSpec(page_size=1000, total=1000, newest_first=True, start_offset=0))
|
||||||
|
assert len(result) == 1000
|
||||||
|
# 只取了最新的 1000 条 → 是 2000..2999 这一段
|
||||||
|
assert result[0].close == pytest.approx(10.0 + 2000 * 0.01)
|
||||||
|
assert result[-1].close == pytest.approx(10.0 + 2999 * 0.01)
|
||||||
|
|
||||||
|
|
||||||
# ---------- get_kline 日期过滤 ----------
|
# ---------- get_kline 日期过滤 ----------
|
||||||
|
|
||||||
class DateBarApiConn(FakeApiConn):
|
class DateBarApiConn(FakeApiConn):
|
||||||
@@ -329,3 +399,143 @@ async def test_get_kline_since():
|
|||||||
assert bars[-1].year == 2027
|
assert bars[-1].year == 2027
|
||||||
finally:
|
finally:
|
||||||
await td.close()
|
await td.close()
|
||||||
|
|
||||||
|
# ---------- A股逐笔:b8 过滤 + 单连接串行游标(回归) ----------
|
||||||
|
|
||||||
|
class TickApiConn(FakeApiConn):
|
||||||
|
"""模拟 0x122F:服务端 start 从**最新**往回数,且可注入 bs_flag=8 委托记录。"""
|
||||||
|
|
||||||
|
# 类级:[(time, price, vol, trade_count, bs_flag), ...],时间升序(最早在前)
|
||||||
|
rows: list[tuple] = []
|
||||||
|
calls: list[tuple[int, int]] = []
|
||||||
|
|
||||||
|
async def execute(self, cmd):
|
||||||
|
if "MacTransactionCmd" not in type(cmd).__name__:
|
||||||
|
return await super().execute(cmd)
|
||||||
|
TickApiConn.calls.append((cmd.start, cmd.count))
|
||||||
|
# 从最新往回数:start 偏移落在末尾,往前取 count 条
|
||||||
|
end = len(TickApiConn.rows) - cmd.start
|
||||||
|
begin = max(0, end - cmd.count)
|
||||||
|
chunk = TickApiConn.rows[begin:end]
|
||||||
|
return [TransactionRecord(market=1, code="600000", time=t, price=p,
|
||||||
|
vol=v, trade_count=tc, bs_flag=bs)
|
||||||
|
for (t, p, v, tc, bs) in chunk]
|
||||||
|
|
||||||
|
|
||||||
|
def _build_tick_rows(n_trade: int = 25, n_b8: int = 7) -> list[tuple]:
|
||||||
|
"""构造 n_trade 条正常成交 + n_b8 条竞价委托(穿插在中间)。"""
|
||||||
|
rows: list[tuple] = []
|
||||||
|
for i in range(n_trade):
|
||||||
|
rows.append((f"09:{30 + i // 60:02d}:{i % 60:02d}", 10.0 + i * 0.01,
|
||||||
|
100.0 + i, 3 + i, 1 if i % 2 else 0))
|
||||||
|
if i % 4 == 0 and len([r for r in rows if r[4] == 8]) < n_b8:
|
||||||
|
# 竞价委托:(time, price, vol, tc, bs=8),vol 是累计值、不计入成交
|
||||||
|
rows.append((f"09:15:{i:02d}", 10.0, 9999.0, -1, 8))
|
||||||
|
return rows
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_ticks_filters_auction_orders():
|
||||||
|
"""bs_flag=8 是竞价**委托**快照,不是成交 → 必须从 get_ticks 结果中剔除。
|
||||||
|
|
||||||
|
实测(2026-10-21 sh600000):b8 的 vol 是累计撮合量(可被覆盖),
|
||||||
|
计入后 Σvol 比快照 vol 虚高 20%~160%,且不同主机的 b8 集合不同。
|
||||||
|
"""
|
||||||
|
TickApiConn.rows = _build_tick_rows(n_trade=25, n_b8=7)
|
||||||
|
TickApiConn.calls = []
|
||||||
|
assert sum(1 for r in TickApiConn.rows if r[4] == 8) == 7
|
||||||
|
|
||||||
|
with mock.patch("pytdxdata.api.get_standard_hosts", return_value=["s1"]), \
|
||||||
|
mock.patch("pytdxdata.api.get_mac_hosts", return_value=["m1"]), \
|
||||||
|
mock.patch("pytdxdata.pool.pool.TdxConnection",
|
||||||
|
side_effect=lambda h, p=7709, t=10.0: TickApiConn(h)):
|
||||||
|
td = TdxData(cache_dir=None, pool_min=1, pool_max=2)
|
||||||
|
await td.start()
|
||||||
|
try:
|
||||||
|
out = await td.get_ticks("sh600000")
|
||||||
|
finally:
|
||||||
|
await td.close()
|
||||||
|
|
||||||
|
assert out, "应返回成交记录"
|
||||||
|
assert all(r.bs_flag != 8 for r in out), "bs_flag=8 竞价委托必须被剔除"
|
||||||
|
assert len(out) == 25
|
||||||
|
assert sum(r.vol for r in out) == sum(100.0 + i for i in range(25))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_ticks_pages_on_single_connection():
|
||||||
|
"""翻页必须走**同一连接**:并发多主机会让页边界错位(重叠 + 漏条)。"""
|
||||||
|
TickApiConn.rows = _build_tick_rows(n_trade=25, n_b8=0)
|
||||||
|
TickApiConn.calls = []
|
||||||
|
|
||||||
|
with mock.patch("pytdxdata.api.get_standard_hosts", return_value=["s1"]), \
|
||||||
|
mock.patch("pytdxdata.api.get_mac_hosts", return_value=["m1", "m2", "m3"]), \
|
||||||
|
mock.patch("pytdxdata.pool.pool.TdxConnection",
|
||||||
|
side_effect=lambda h, p=7709, t=10.0: TickApiConn(h)):
|
||||||
|
td = TdxData(cache_dir=None, pool_min=1, pool_max=4)
|
||||||
|
await td.start()
|
||||||
|
try:
|
||||||
|
out = await td.get_ticks("sh600000")
|
||||||
|
finally:
|
||||||
|
await td.close()
|
||||||
|
|
||||||
|
# 单页装得下 → 只发一次请求
|
||||||
|
assert len(TickApiConn.calls) == 1
|
||||||
|
assert len(out) == 25
|
||||||
|
# 时间升序
|
||||||
|
times = [r.time for r in out]
|
||||||
|
assert times == sorted(times)
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_ticks_multi_page_uses_one_connection_and_no_overlap():
|
||||||
|
"""多页(>_TICK_PAGE):同一连接、游标按 len(rows) 推进、页间不重叠不漏条。
|
||||||
|
|
||||||
|
回归:并发分页时各页来自不同主机,而 0x122F 的 start 是「从最新往回数」的
|
||||||
|
相对锚点 —— 各主机对 bs_flag=8 收录不同,同一 start 落到不同真实成交上,
|
||||||
|
结果页边界既重叠又漏条(实测 sh600000 冗余 116 条、13:03 附近丢 59 条)。
|
||||||
|
"""
|
||||||
|
from pytdxdata.api import _TICK_PAGE
|
||||||
|
|
||||||
|
n = _TICK_PAGE * 2 + 137 # 3 页,末页短页 → 触发 stop_when_short
|
||||||
|
TickApiConn.rows = [
|
||||||
|
(f"{9 + i // 3600:02d}:{i // 60 % 60:02d}:{i % 60:02d}",
|
||||||
|
10.0 + i * 0.001, 1.0 + i, 1 + i, 1)
|
||||||
|
for i in range(n)
|
||||||
|
]
|
||||||
|
TickApiConn.calls = []
|
||||||
|
|
||||||
|
with mock.patch("pytdxdata.api.get_standard_hosts", return_value=["s1"]), \
|
||||||
|
mock.patch("pytdxdata.api.get_mac_hosts", return_value=["m1", "m2", "m3"]), \
|
||||||
|
mock.patch("pytdxdata.pool.pool.TdxConnection",
|
||||||
|
side_effect=lambda h, p=7709, t=10.0: TickApiConn(h)):
|
||||||
|
td = TdxData(cache_dir=None, pool_min=1, pool_max=4)
|
||||||
|
await td.start()
|
||||||
|
try:
|
||||||
|
out = await td.get_ticks("sh600000")
|
||||||
|
finally:
|
||||||
|
await td.close()
|
||||||
|
|
||||||
|
# 恰好 3 页:0 / 1000 / 2000,游标按实际返回条数推进
|
||||||
|
assert TickApiConn.calls == [(0, _TICK_PAGE),
|
||||||
|
(_TICK_PAGE, _TICK_PAGE),
|
||||||
|
(_TICK_PAGE * 2, _TICK_PAGE)]
|
||||||
|
assert len(out) == n, f"必须无重叠、无漏条:期望 {n},实得 {len(out)}"
|
||||||
|
vols = [r.vol for r in out]
|
||||||
|
assert len(set(vols)) == n, "vol 全唯一(无重复记录)"
|
||||||
|
assert vols == sorted(vols), "时间升序"
|
||||||
|
|
||||||
|
# count 参数:游标逐页扣减
|
||||||
|
TickApiConn.calls = []
|
||||||
|
with mock.patch("pytdxdata.api.get_standard_hosts", return_value=["s1"]), \
|
||||||
|
mock.patch("pytdxdata.api.get_mac_hosts", return_value=["m1", "m2", "m3"]), \
|
||||||
|
mock.patch("pytdxdata.pool.pool.TdxConnection",
|
||||||
|
side_effect=lambda h, p=7709, t=10.0: TickApiConn(h)):
|
||||||
|
td = TdxData(cache_dir=None, pool_min=1, pool_max=4)
|
||||||
|
await td.start()
|
||||||
|
try:
|
||||||
|
out2 = await td.get_ticks("sh600000", count=_TICK_PAGE + 50)
|
||||||
|
finally:
|
||||||
|
await td.close()
|
||||||
|
assert len(out2) == _TICK_PAGE + 50
|
||||||
|
assert TickApiConn.calls[1][1] == 50, "末页只补足剩余 50 条"
|
||||||
|
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
"""ExPool 背压/并发语义回归(Issue #1:并发超限 0.15s 秒失败)。
|
||||||
|
|
||||||
|
用 mock 连接模拟:单服务器 + per_server_cap=1 时,
|
||||||
|
- 并发 acquire 超过有效并发 → 排队等待而不是快速失败;
|
||||||
|
- release 唤醒等待者;
|
||||||
|
- acquire 超时异常带池状态;
|
||||||
|
- ex_pool_cap 提升同主机并发。
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import time
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from pytdxdata.pool.ex_pool import ExPool, ExPoolTimeoutError
|
||||||
|
|
||||||
|
|
||||||
|
class MockConn:
|
||||||
|
"""Minimal stand-in for ExTdxConnection."""
|
||||||
|
|
||||||
|
last_created = 0
|
||||||
|
|
||||||
|
def __init__(self, host: str, port: int, timeout: float, handshake: str = "login"):
|
||||||
|
self.host = host
|
||||||
|
self.port = port
|
||||||
|
self.alive = True
|
||||||
|
self.last_used = time.monotonic()
|
||||||
|
self.closed = False
|
||||||
|
MockConn.last_created += 1
|
||||||
|
|
||||||
|
async def connect(self) -> None:
|
||||||
|
await asyncio.sleep(0) # instant "connect"
|
||||||
|
|
||||||
|
async def close(self) -> None:
|
||||||
|
self.closed = True
|
||||||
|
self.alive = False
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def patch_conn(monkeypatch):
|
||||||
|
monkeypatch.setattr("pytdxdata.pool.ex_pool.ExTdxConnection", MockConn)
|
||||||
|
MockConn.last_created = 0
|
||||||
|
|
||||||
|
|
||||||
|
async def test_acquire_queues_instead_of_fast_fail(patch_conn):
|
||||||
|
"""并发 > 有效并发时,后续 acquire 等待 release,而非 0.15s 抛错。"""
|
||||||
|
pool = ExPool(["e1"], min_size=1, max_size=6, per_server_cap=1)
|
||||||
|
await pool.start()
|
||||||
|
c1 = await pool.acquire() # 唯一连接被占用
|
||||||
|
t0 = time.monotonic()
|
||||||
|
|
||||||
|
async def holder():
|
||||||
|
await asyncio.sleep(0.5) # 持有 0.5s 后归还
|
||||||
|
await pool.release(c1)
|
||||||
|
|
||||||
|
asyncio.create_task(holder())
|
||||||
|
c2 = await pool.acquire() # 应在 ~0.5s 后拿到,而不是 0.15s 抛错
|
||||||
|
waited = time.monotonic() - t0
|
||||||
|
assert waited >= 0.4, f"acquire 没有等待释放(仅 {waited:.2f}s)"
|
||||||
|
assert c2 is c1 # 归还的是同一条连接
|
||||||
|
|
||||||
|
|
||||||
|
async def test_acquire_timeout_carries_pool_state(patch_conn):
|
||||||
|
"""超时异常信息带池状态(idle/total/servers/cap)。"""
|
||||||
|
pool = ExPool(["e1"], min_size=1, max_size=6, per_server_cap=1, acquire_timeout=0.3)
|
||||||
|
await pool.start()
|
||||||
|
c1 = await pool.acquire()
|
||||||
|
with pytest.raises(ExPoolTimeoutError) as ei:
|
||||||
|
await pool.acquire()
|
||||||
|
msg = str(ei.value)
|
||||||
|
assert "total=1" in msg and "per_server_cap=1" in msg and "servers=1" in msg
|
||||||
|
await pool.release(c1)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_ex_pool_cap_allows_parallel_conns_same_host(patch_conn):
|
||||||
|
"""per_server_cap=2 时单主机可并发持有 2 条连接。"""
|
||||||
|
pool = ExPool(["e1"], min_size=1, max_size=6, per_server_cap=2)
|
||||||
|
await pool.start()
|
||||||
|
got = await asyncio.gather(pool.acquire(), pool.acquire())
|
||||||
|
assert len(got) == 2 and got[0] is not got[1]
|
||||||
|
assert MockConn.last_created == 2
|
||||||
|
for c in got:
|
||||||
|
await pool.release(c)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_concurrent_acquire_all_succeed(patch_conn):
|
||||||
|
"""Issue 场景:单服务器 + cap=2,6 个并发任务应全部拿到连接(排队)而非部分失败。"""
|
||||||
|
pool = ExPool(["e1"], min_size=2, max_size=6, per_server_cap=2)
|
||||||
|
await pool.start()
|
||||||
|
|
||||||
|
async def task(i):
|
||||||
|
c = await pool.acquire()
|
||||||
|
await asyncio.sleep(0.1) # 模拟一次 1m 分页拉取
|
||||||
|
await pool.release(c)
|
||||||
|
return i
|
||||||
|
|
||||||
|
results = await asyncio.gather(*[task(i) for i in range(6)])
|
||||||
|
assert sorted(results) == list(range(6)) # 全部成功,零失败
|
||||||
|
|
||||||
|
|
||||||
|
async def test_release_notifies_waiters(patch_conn):
|
||||||
|
"""broken 归还(连接死亡)也要唤醒等待者(槽位回收后可扩容)。"""
|
||||||
|
pool = ExPool(["e1"], min_size=1, max_size=1, per_server_cap=1)
|
||||||
|
await pool.start()
|
||||||
|
c1 = await pool.acquire()
|
||||||
|
c1.alive = False
|
||||||
|
|
||||||
|
async def broken_release():
|
||||||
|
await asyncio.sleep(0.2)
|
||||||
|
await pool.release(c1, broken=True)
|
||||||
|
|
||||||
|
asyncio.create_task(broken_release())
|
||||||
|
c2 = await pool.acquire() # 槽位被回收→扩容新建
|
||||||
|
assert c2 is not c1
|
||||||
|
await pool.release(c2)
|
||||||
@@ -0,0 +1,303 @@
|
|||||||
|
"""指数成分成交汇总(get_index_members / get_index_trade_stats)单元测试。
|
||||||
|
|
||||||
|
全部离线:成分与逐笔都用实例属性打补丁,不建立任何真实连接。
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from types import SimpleNamespace
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from pytdxdata import TdxData
|
||||||
|
from pytdxdata.models import (
|
||||||
|
IndexTradeStats,
|
||||||
|
MemberQuote,
|
||||||
|
MinuteTradeStat,
|
||||||
|
TransactionRecord,
|
||||||
|
)
|
||||||
|
|
||||||
|
# ---------- _index_pair:裸代码按指数语义解析 ----------
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("text,expect", [
|
||||||
|
("000001", (1, "000001")), # 上证指数(不是平安银行!)
|
||||||
|
("sh000001", (1, "000001")),
|
||||||
|
("000300", (1, "000300")),
|
||||||
|
("sh000300", (1, "000300")),
|
||||||
|
("000905", (1, "000905")),
|
||||||
|
("999999", (1, "999999")),
|
||||||
|
("880305", (1, "880305")),
|
||||||
|
("399001", (0, "399001")), # 深证成指
|
||||||
|
("sz399006", (0, "399006")),
|
||||||
|
("899050", (2, "899050")), # 北证50
|
||||||
|
("bj899050", (2, "899050")),
|
||||||
|
("sh.000300", (1, "000300")), # 点号/冒号都容忍
|
||||||
|
("sz:399001", (0, "399001")),
|
||||||
|
])
|
||||||
|
def test_index_pair_parses(text, expect):
|
||||||
|
assert TdxData._index_pair(text) == expect
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("text", [
|
||||||
|
"", "600000", "sz000001x", "sh12345", "12345", "abc", "zzz000001",
|
||||||
|
])
|
||||||
|
def test_index_pair_rejects(text):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
TdxData._index_pair(text)
|
||||||
|
|
||||||
|
|
||||||
|
def test_index_pair_rejects_non_index_cn_code():
|
||||||
|
"""000001 之外的普通股票段(如 600xxx)不是指数,裸写必须报错。"""
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
TdxData._index_pair("600000")
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- 模型:累计曲线 ----------
|
||||||
|
|
||||||
|
def test_index_trade_stats_cumulative():
|
||||||
|
st = IndexTradeStats(
|
||||||
|
index="sh000300", date=None, member_count=2, active_members=2,
|
||||||
|
trade_count_members=2,
|
||||||
|
minutes=[
|
||||||
|
MinuteTradeStat("09:30", 100.0, 10, 1000.0),
|
||||||
|
MinuteTradeStat("09:31", 200.0, 20, 2000.0),
|
||||||
|
MinuteTradeStat("09:32", 50.0, 5, 500.0),
|
||||||
|
],
|
||||||
|
total_volume=350.0, total_trade_count=35, total_amount=3500.0,
|
||||||
|
)
|
||||||
|
cum = st.cumulative()
|
||||||
|
assert [c.time for c in cum] == ["09:30", "09:31", "09:32"]
|
||||||
|
assert [c.volume for c in cum] == [100.0, 300.0, 350.0]
|
||||||
|
assert [c.trade_count for c in cum] == [10, 30, 35]
|
||||||
|
assert [c.amount for c in cum] == [1000.0, 3000.0, 3500.0]
|
||||||
|
# 累计必须是新对象,不污染原始增量
|
||||||
|
assert st.minutes[1].volume == 200.0
|
||||||
|
assert st.cum_trade_count() == [("09:30", 10), ("09:31", 30), ("09:32", 35)]
|
||||||
|
|
||||||
|
|
||||||
|
def test_index_trade_stats_trade_count_available():
|
||||||
|
ok = IndexTradeStats(index="sh000300", date=None, member_count=1,
|
||||||
|
active_members=1, trade_count_members=1)
|
||||||
|
assert ok.trade_count_available is True
|
||||||
|
bj = IndexTradeStats(index="bj899050", date=None, member_count=1,
|
||||||
|
active_members=1, trade_count_members=0)
|
||||||
|
assert bj.trade_count_available is False
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- get_index_members:分页终止 ----------
|
||||||
|
|
||||||
|
def _member(market: int, code: str, name: str = "") -> MemberQuote:
|
||||||
|
return MemberQuote(market=market, code=code, name=name,
|
||||||
|
fields={"0x0": 9.9, "0x5": 1000.0, "0x7": 99000.0})
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_index_members_pages_until_short_batch():
|
||||||
|
"""短页(不足 80 条)即视为最后一页,不再多发一次空请求。"""
|
||||||
|
calls: list[int] = []
|
||||||
|
|
||||||
|
async def fake_exec(cmd):
|
||||||
|
calls.append(cmd.start)
|
||||||
|
if cmd.start == 0:
|
||||||
|
return [_member(1, "600000"), _member(1, "600004")]
|
||||||
|
return []
|
||||||
|
|
||||||
|
td = TdxData(cache_dir=None)
|
||||||
|
td._execute_mac = fake_exec
|
||||||
|
members = await td.get_index_members("000016")
|
||||||
|
assert [m.symbol for m in members] == ["sh600000", "sh600004"]
|
||||||
|
assert calls == [0]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_index_members_continues_on_full_page():
|
||||||
|
"""满页(80 条)后继续翻页,直到空页。"""
|
||||||
|
calls: list[int] = []
|
||||||
|
|
||||||
|
async def fake_exec(cmd):
|
||||||
|
calls.append(cmd.start)
|
||||||
|
if cmd.start == 0:
|
||||||
|
return [_member(1, "600000")] * cmd.page_size
|
||||||
|
return []
|
||||||
|
|
||||||
|
td = TdxData(cache_dir=None)
|
||||||
|
td._execute_mac = fake_exec
|
||||||
|
members = await td.get_index_members("000016")
|
||||||
|
assert len(members) == 80
|
||||||
|
assert calls == [0, 80]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_index_members_respects_count_cap():
|
||||||
|
async def fake_exec(cmd):
|
||||||
|
# 永远返回满页,验证 count 能截断结果
|
||||||
|
return [_member(1, "600000")] * cmd.page_size
|
||||||
|
|
||||||
|
td = TdxData(cache_dir=None)
|
||||||
|
td._execute_mac = fake_exec
|
||||||
|
members = await td.get_index_members("000016", count=5)
|
||||||
|
assert len(members) == 5
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- get_index_trade_stats:按分钟聚合 ----------
|
||||||
|
|
||||||
|
def _tick(sym: str, hhmm: str, price: float, vol: float, tc: int) -> TransactionRecord:
|
||||||
|
market, code = (1, sym[2:]) if sym.startswith("sh") else (0, sym[2:])
|
||||||
|
return TransactionRecord(market=market, code=code, time=f"{hhmm}:00",
|
||||||
|
price=price, vol=vol, trade_count=tc, bs_flag=1,
|
||||||
|
symbol=sym)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_index_trade_stats_aggregates_minutes():
|
||||||
|
async def fake_ticks(symbols, date=None, concurrency=6, **kw):
|
||||||
|
out = []
|
||||||
|
for s in symbols:
|
||||||
|
if s == "sh600000":
|
||||||
|
out += [_tick(s, "09:30", 10.0, 100.0, 10),
|
||||||
|
_tick(s, "09:30", 10.0, 50.0, 5),
|
||||||
|
_tick(s, "09:31", 11.0, 200.0, 20)]
|
||||||
|
else:
|
||||||
|
out += [_tick(s, "09:30", 5.0, 1000.0, 7)]
|
||||||
|
return out
|
||||||
|
|
||||||
|
td = TdxData(cache_dir=None)
|
||||||
|
td.get_ticks = fake_ticks
|
||||||
|
st = await td.get_index_trade_stats(
|
||||||
|
"000016", members=["sh600000", "sz000001"])
|
||||||
|
|
||||||
|
assert st.index == "sh000016"
|
||||||
|
assert st.member_count == 2
|
||||||
|
assert st.active_members == 2
|
||||||
|
assert st.trade_count_members == 2
|
||||||
|
assert st.trade_count_available is True
|
||||||
|
assert st.failed_members == 0
|
||||||
|
|
||||||
|
m = {x.time: x for x in st.minutes}
|
||||||
|
assert sorted(m) == ["09:30", "09:31"]
|
||||||
|
assert m["09:30"].volume == 1150.0 # 100 + 50 + 1000
|
||||||
|
assert m["09:30"].trade_count == 22 # 10 + 5 + 7
|
||||||
|
assert m["09:31"].volume == 200.0
|
||||||
|
# amount = Σ(price × 手 × 100)
|
||||||
|
assert m["09:30"].amount == pytest.approx(
|
||||||
|
10.0 * 100 * 100 + 10.0 * 50 * 100 + 5.0 * 1000 * 100)
|
||||||
|
assert st.total_volume == 1350.0
|
||||||
|
assert st.total_trade_count == 42
|
||||||
|
assert st.total_amount == pytest.approx(
|
||||||
|
10.0 * 100 * 100 + 10.0 * 50 * 100 + 5.0 * 1000 * 100 + 11.0 * 200 * 100)
|
||||||
|
assert st.cum_trade_count() == [("09:30", 22), ("09:31", 42)]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_index_trade_stats_bj_has_no_trade_count():
|
||||||
|
"""北交所成分返回 trade_count=0 → trade_count_available False。"""
|
||||||
|
async def fake_ticks(symbols, date=None, concurrency=6, **kw):
|
||||||
|
return [_tick("sh600000", "09:30", 10.0, 100.0, 0)]
|
||||||
|
|
||||||
|
td = TdxData(cache_dir=None)
|
||||||
|
td.get_ticks = fake_ticks
|
||||||
|
st = await td.get_index_trade_stats("899050", members=["sh600000"])
|
||||||
|
assert st.trade_count_members == 0
|
||||||
|
assert st.trade_count_available is False
|
||||||
|
assert st.total_volume == 100.0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_index_trade_stats_retries_per_symbol_on_batch_failure():
|
||||||
|
"""整批失败 → 退化为逐只重试,只有真正失败的计入 failed_members。"""
|
||||||
|
async def fake_ticks(symbols, date=None, concurrency=6, **kw):
|
||||||
|
single = isinstance(symbols, str)
|
||||||
|
if not single:
|
||||||
|
raise ConnectionError("批量炸了")
|
||||||
|
if symbols == "sh600004":
|
||||||
|
raise ConnectionError("这只也炸了")
|
||||||
|
return [_tick(symbols, "09:30", 10.0, 100.0, 3)]
|
||||||
|
|
||||||
|
td = TdxData(cache_dir=None)
|
||||||
|
td.get_ticks = fake_ticks
|
||||||
|
st = await td.get_index_trade_stats(
|
||||||
|
"000016", members=["sh600000", "sh600004", "sh600006"])
|
||||||
|
assert st.failed_members == 1
|
||||||
|
assert st.active_members == 2
|
||||||
|
assert st.total_trade_count == 6
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_index_trade_stats_raises_when_not_skip_errors():
|
||||||
|
async def fake_ticks(symbols, date=None, concurrency=6, **kw):
|
||||||
|
raise ConnectionError("boom")
|
||||||
|
|
||||||
|
td = TdxData(cache_dir=None)
|
||||||
|
td.get_ticks = fake_ticks
|
||||||
|
with pytest.raises(ConnectionError):
|
||||||
|
await td.get_index_trade_stats(
|
||||||
|
"000016", members=["sh600000"], skip_errors=False)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_index_trade_stats_progress_callback():
|
||||||
|
seen: list[tuple[int, int]] = []
|
||||||
|
|
||||||
|
async def fake_ticks(symbols, date=None, concurrency=6, **kw):
|
||||||
|
return [_tick(s, "09:30", 10.0, 1.0, 1) for s in symbols]
|
||||||
|
|
||||||
|
td = TdxData(cache_dir=None)
|
||||||
|
td.get_ticks = fake_ticks
|
||||||
|
st = await td.get_index_trade_stats(
|
||||||
|
"000016", members=[f"sh60000{i}" for i in range(5)],
|
||||||
|
batch_size=2, on_progress=lambda d, t: seen.append((d, t)))
|
||||||
|
assert seen == [(2, 5), (4, 5), (5, 5)]
|
||||||
|
assert st.member_count == 5
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_index_trade_stats_fetches_members_when_absent():
|
||||||
|
asked: list[str] = []
|
||||||
|
|
||||||
|
async def fake_members(code):
|
||||||
|
asked.append(code)
|
||||||
|
return [SimpleNamespace(symbol="sh600000")]
|
||||||
|
|
||||||
|
async def fake_ticks(symbols, date=None, concurrency=6, **kw):
|
||||||
|
return [_tick(s, "09:30", 10.0, 1.0, 1) for s in symbols]
|
||||||
|
|
||||||
|
td = TdxData(cache_dir=None)
|
||||||
|
td.get_index_members = fake_members
|
||||||
|
td.get_ticks = fake_ticks
|
||||||
|
await td.get_index_trade_stats("sh000300")
|
||||||
|
assert asked == ["000300"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_index_trade_stats_rejects_empty_members():
|
||||||
|
td = TdxData(cache_dir=None)
|
||||||
|
|
||||||
|
async def fake_members(code):
|
||||||
|
return []
|
||||||
|
|
||||||
|
td.get_index_members = fake_members
|
||||||
|
with pytest.raises(ValueError, match="没有取到成分股"):
|
||||||
|
await td.get_index_trade_stats("000300")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_index_trade_stats_multi_reuses_members():
|
||||||
|
member_calls: list[str] = []
|
||||||
|
tick_dates: list[int | None] = []
|
||||||
|
|
||||||
|
async def fake_members(code):
|
||||||
|
member_calls.append(code)
|
||||||
|
return [SimpleNamespace(symbol="sh600000")]
|
||||||
|
|
||||||
|
async def fake_ticks(symbols, date=None, concurrency=6, **kw):
|
||||||
|
tick_dates.append(date)
|
||||||
|
return [_tick(s, "09:30", 10.0, 1.0, 2) for s in symbols]
|
||||||
|
|
||||||
|
td = TdxData(cache_dir=None)
|
||||||
|
td.get_index_members = fake_members
|
||||||
|
td.get_ticks = fake_ticks
|
||||||
|
out = await td.get_index_trade_stats_multi(
|
||||||
|
"000300", [20261020, 20261021, None])
|
||||||
|
assert len(out) == 3
|
||||||
|
assert member_calls == ["000300"] # 成分只取一次
|
||||||
|
assert tick_dates == [20261020, 20261021, None]
|
||||||
|
assert all(s.total_trade_count == 2 for s in out)
|
||||||
+282
-4
@@ -2,17 +2,21 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import struct
|
import struct
|
||||||
|
from collections import Counter
|
||||||
|
from datetime import date, datetime
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pytdxdata.protocol.frame import FrameHeader, decompress_body, parse_header
|
from pytdxdata.models import Adjust, KlinePeriod, SecurityBar
|
||||||
from pytdxdata.protocol.price import get_price, put_price, decode_ohlc_diffs
|
|
||||||
from pytdxdata.protocol.volume import decode_volume
|
|
||||||
from pytdxdata.protocol.commands.ex_proto import GetExInstrumentBarsCmd
|
from pytdxdata.protocol.commands.ex_proto import GetExInstrumentBarsCmd
|
||||||
from pytdxdata.protocol.commands.kline import GetSecurityBarsCmd
|
from pytdxdata.protocol.commands.kline import GetSecurityBarsCmd
|
||||||
|
from pytdxdata.protocol.commands.mac import MacKlineCmd
|
||||||
from pytdxdata.protocol.commands.minute import GetHistoryMinuteTimeDataCmd
|
from pytdxdata.protocol.commands.minute import GetHistoryMinuteTimeDataCmd
|
||||||
|
from pytdxdata.protocol.commands.quotes import price_decimal_digits
|
||||||
from pytdxdata.protocol.commands.transaction import MacTransactionCmd
|
from pytdxdata.protocol.commands.transaction import MacTransactionCmd
|
||||||
|
from pytdxdata.protocol.frame import FrameHeader, decompress_body, parse_header
|
||||||
|
from pytdxdata.protocol.price import decode_ohlc_diffs, get_price, put_price
|
||||||
|
from pytdxdata.protocol.volume import decode_volume
|
||||||
|
|
||||||
# ---------- 变长有符号整数 ----------
|
# ---------- 变长有符号整数 ----------
|
||||||
|
|
||||||
@@ -176,3 +180,277 @@ def test_mac_transaction_parse():
|
|||||||
assert recs[0].trade_count == 3
|
assert recs[0].trade_count == 3
|
||||||
assert recs[0].bs_flag == 1
|
assert recs[0].bs_flag == 1
|
||||||
assert recs[1].trade_count == 1
|
assert recs[1].trade_count == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_mac_transaction_parse_negative_trade_count_auction():
|
||||||
|
"""集合竞价段 trade_count 是有符号 int32,负数不能被读成 42 亿(BUG 回归)。
|
||||||
|
|
||||||
|
实测(2026-10-21,sh600000 09:15:03):18 字节为
|
||||||
|
17820000 0ad71b41 d9000000 96fcffff 0800
|
||||||
|
最后四字节前的 96fcffff = int32 -874,旧解析按 uint32 得 4294966422。
|
||||||
|
"""
|
||||||
|
body = bytearray(39 + 2 * 18)
|
||||||
|
struct.pack_into("<H", body, 29, 2)
|
||||||
|
# 第1条:集合竞价,trade_count = -874
|
||||||
|
struct.pack_into("<IfIiH", body, 39, 33303, 9.74, 217, -874, 8)
|
||||||
|
# 第2条:正常交易,trade_count = 3
|
||||||
|
struct.pack_into("<IfIiH", body, 39 + 18, 34200, 9.75, 100, 3, 1)
|
||||||
|
|
||||||
|
cmd = MacTransactionCmd(1, "600000", 20261021, 0, 100)
|
||||||
|
recs = cmd.parse_response(bytes(body))
|
||||||
|
assert len(recs) == 2
|
||||||
|
assert recs[0].trade_count == -874, "负值必须保留符号,不能变成 4294966422"
|
||||||
|
assert recs[0].bs_flag == 8
|
||||||
|
assert recs[1].trade_count == 3
|
||||||
|
# 正常交易段的量级不受影响
|
||||||
|
assert max(r.trade_count for r in recs) == 3
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- 跨时区时间戳归一化(美股 EX 通道) ----------
|
||||||
|
#
|
||||||
|
# 通达信 EX 通道对美股返回的时间戳把两个时区拼在一起:日期字段是**美东交易日**、
|
||||||
|
# 时分字段是**北京时间**。直拼出的 datetime 会把同一交易日的下午半场(北京
|
||||||
|
# 00:00~04:00)排到上午半场(北京 21:30~23:59)之前,单日 390 根全部错位。
|
||||||
|
|
||||||
|
def _bar(market: int, y: int, mo: int, d: int, h: int, mi: int) -> SecurityBar:
|
||||||
|
return SecurityBar(market=market, code="X", open=1.0, high=1.0, low=1.0,
|
||||||
|
close=1.0, vol=1.0, amount=1.0,
|
||||||
|
year=y, month=mo, day=d, hour=h, minute=mi)
|
||||||
|
|
||||||
|
|
||||||
|
def test_us_evening_segment_keeps_date():
|
||||||
|
# 北京 21:31 ~ 23:59 与美东交易日属同一自然日,日期不动
|
||||||
|
b = _bar(74, 2026, 10, 12, 21, 31)
|
||||||
|
assert b.datetime == datetime(2026, 10, 12, 21, 31)
|
||||||
|
assert b.trading_date == "2026-10-12"
|
||||||
|
|
||||||
|
|
||||||
|
def test_us_midnight_segment_rolls_to_next_day():
|
||||||
|
# 北京次日 00:00 ~ 04:00 实际是美东 12:00~16:00,日期要 +1 天
|
||||||
|
b = _bar(74, 2026, 10, 12, 4, 0)
|
||||||
|
assert b.datetime == datetime(2026, 10, 13, 4, 0)
|
||||||
|
assert (b.hour, b.minute) == (4, 0) # 时分不参与归一化
|
||||||
|
assert b.trading_date == "2026-10-12" # 仍归属美东 10-12 交易日
|
||||||
|
|
||||||
|
|
||||||
|
def test_us_split_hour_boundary():
|
||||||
|
assert _bar(74, 2026, 10, 12, 11, 59).day == 13
|
||||||
|
assert _bar(74, 2026, 10, 12, 12, 0).day == 12
|
||||||
|
|
||||||
|
|
||||||
|
def test_us_month_and_year_rollover():
|
||||||
|
assert _bar(74, 2026, 10, 31, 4, 0).datetime == datetime(2026, 11, 1, 4, 0)
|
||||||
|
assert _bar(74, 2026, 12, 31, 4, 0).datetime == datetime(2027, 1, 1, 4, 0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_cn_and_hk_bars_untouched():
|
||||||
|
cn = _bar(1, 2026, 10, 12, 9, 31) # A股/标准通道
|
||||||
|
assert (cn.year, cn.month, cn.day) == (2026, 10, 12)
|
||||||
|
assert cn.trading_date == "2026-10-12"
|
||||||
|
hk = _bar(31, 2026, 10, 12, 4, 0) # 港股同为 UTC+8,凌晨段不应 +1
|
||||||
|
assert (hk.year, hk.month, hk.day) == (2026, 10, 12)
|
||||||
|
assert hk.trading_date == "2026-10-12"
|
||||||
|
|
||||||
|
|
||||||
|
def test_us_full_session_is_monotonic_and_single_trading_day():
|
||||||
|
# 实测口径:北京 21:31~23:59(149 根)+ 次日 00:00~04:00(241 根)= 390 根
|
||||||
|
mins = list(range(21 * 60 + 31, 24 * 60)) + list(range(4 * 60 + 1))
|
||||||
|
assert len(mins) == 390
|
||||||
|
bars = [_bar(74, 2026, 10, 12, m // 60, m % 60) for m in mins]
|
||||||
|
bars.sort(key=lambda b: b.datetime)
|
||||||
|
dts = [b.datetime for b in bars]
|
||||||
|
assert dts == sorted(dts) # 归一化后严格单调
|
||||||
|
assert dts[0] == datetime(2026, 10, 12, 21, 31)
|
||||||
|
assert dts[-1] == datetime(2026, 10, 13, 4, 0)
|
||||||
|
# 全部归属于同一个美东交易日
|
||||||
|
assert {b.trading_date for b in bars} == {"2026-10-12"}
|
||||||
|
# 但横跨两个北京自然日(这正是不能用 datetime.date() 聚合的原因)
|
||||||
|
by_day = Counter(b.datetime.date() for b in bars)
|
||||||
|
assert by_day[date(2026, 10, 12)] == 149
|
||||||
|
assert by_day[date(2026, 10, 13)] == 241
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- MAC 通道 K线(0x122E)同样归一化 ----------
|
||||||
|
|
||||||
|
def _mac_bars_body(records: list[tuple[int, int]]) -> bytes:
|
||||||
|
"""构造 0x122E 响应体:33B头(count@24) + 每条36B(4B ymd + 4B time秒 + 7float)。"""
|
||||||
|
body = bytearray(33 + len(records) * 36)
|
||||||
|
struct.pack_into("<HBHI", body, 24, 7, 1, len(records), 0)
|
||||||
|
for i, (ymd, tsec) in enumerate(records):
|
||||||
|
struct.pack_into("<II7f", body, 33 + i * 36,
|
||||||
|
ymd, tsec, 10.0, 10.5, 9.5, 10.2, 0.0, 1000.0, 0.0)
|
||||||
|
return bytes(body)
|
||||||
|
|
||||||
|
|
||||||
|
def test_mac_kline_us_bar_normalized():
|
||||||
|
# _probe_bar(日期区间二分依赖它)对美股走的就是这条命令
|
||||||
|
cmd = MacKlineCmd(74, "AAPL", KlinePeriod.MIN_1, Adjust.NONE, 0, 5)
|
||||||
|
bars = cmd.parse_response(_mac_bars_body([(20261012, 4 * 3600)])) # 北京 04:00
|
||||||
|
assert len(bars) == 1
|
||||||
|
assert bars[0].datetime == datetime(2026, 10, 13, 4, 0)
|
||||||
|
assert bars[0].trading_date == "2026-10-12"
|
||||||
|
|
||||||
|
|
||||||
|
def test_mac_kline_hk_bar_untouched():
|
||||||
|
cmd = MacKlineCmd(31, "00700", KlinePeriod.MIN_1, Adjust.NONE, 0, 5)
|
||||||
|
bars = cmd.parse_response(_mac_bars_body([(20260729, 13 * 3600 + 40 * 60)]))
|
||||||
|
assert (bars[0].datetime) == datetime(2026, 7, 29, 13, 40)
|
||||||
|
assert bars[0].trading_date == "2026-07-29"
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- 排序:EX 返回顺序按时间正序(切片取「最新 N 根」依赖它) ----------
|
||||||
|
|
||||||
|
def test_finish_bars_sorts_us_session_chronologically():
|
||||||
|
from pytdxdata.api import TdxData
|
||||||
|
from pytdxdata.symbols import parse_symbols
|
||||||
|
|
||||||
|
sym = parse_symbols("usAAPL")[0]
|
||||||
|
# 故意按错误的顺序给(凌晨段在前)
|
||||||
|
out = TdxData._finish_bars([_bar(74, 2026, 10, 12, 4, 0),
|
||||||
|
_bar(74, 2026, 10, 12, 21, 31)], sym)
|
||||||
|
assert [b.datetime for b in out] == [datetime(2026, 10, 12, 21, 31),
|
||||||
|
datetime(2026, 10, 13, 4, 0)]
|
||||||
|
assert {b.symbol for b in out} == {"usAAPL"}
|
||||||
|
|
||||||
|
# ---------- 报价小数位:沪市指数必须 2 位(回归:曾整体少 10 倍) ----------
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("market,code,expect", [
|
||||||
|
# 沪市指数:回归 bug —— 曾被判 3 位,导致价格少 10 倍
|
||||||
|
(1, "000001", 2), # 上证指数
|
||||||
|
(1, "000016", 2), # 上证50
|
||||||
|
(1, "000300", 2), # 沪深300
|
||||||
|
(1, "000688", 2), # 科创50
|
||||||
|
(1, "880001", 2), # 板块指数(8 开头,曾被判 3 位)
|
||||||
|
(1, "880305", 2),
|
||||||
|
# 沪市 ETF/基金:仍是 3 位
|
||||||
|
(1, "510300", 3),
|
||||||
|
(1, "588000", 3),
|
||||||
|
(1, "511010", 3),
|
||||||
|
(1, "501018", 3),
|
||||||
|
# 沪市股票:2 位
|
||||||
|
(1, "600000", 2),
|
||||||
|
(1, "601398", 2),
|
||||||
|
(1, "688111", 2), # 科创板
|
||||||
|
# 深市
|
||||||
|
(0, "399001", 2), # 深证成指
|
||||||
|
(0, "399006", 2), # 创业板指
|
||||||
|
(0, "000001", 2), # 平安银行(不是指数!)
|
||||||
|
(0, "300750", 2),
|
||||||
|
(0, "159915", 3), # 深市 ETF
|
||||||
|
# 债券/回购:2026-10-22 修正(沪市曾少 100 倍、深市曾少 10 倍)
|
||||||
|
(1, "019742", 4), # 沪市国债 24特国01(110.228,曾读成 11022.8)
|
||||||
|
(1, "019547", 4), # 沪市国债
|
||||||
|
(1, "010706", 4), # 沪市老国债 07国债06
|
||||||
|
(1, "018014", 4), # 沪市 018 段
|
||||||
|
(1, "020812", 4), # 沪市贴债
|
||||||
|
(1, "110077", 4), # 沪市可转债 洪城转债(199.279,曾读成 19927.9)
|
||||||
|
(1, "113616", 4), # 沪市可转债 韦尔转债
|
||||||
|
(1, "118031", 4), # 沪市可转债
|
||||||
|
(1, "111000", 4), # 沪市 111 段
|
||||||
|
(1, "132024", 4), # 沪市可交换债
|
||||||
|
(1, "184832", 4), # 沪市 184 段
|
||||||
|
(1, "204001", 4), # 沪市国债逆回购 GC001(1.385,曾读成 138.5)
|
||||||
|
(1, "204028", 4),
|
||||||
|
(0, "123107", 4), # 深市可转债 温氏转债(112.189,曾读成 1121.89)
|
||||||
|
(0, "128136", 4), # 深市可转债 立讯转债
|
||||||
|
(0, "127025", 4), # 深市可转债
|
||||||
|
(0, "131810", 4), # 深市国债逆回购 R-001(1.38,曾读成 13.8)
|
||||||
|
(0, "148004", 4), # 深市 148 段
|
||||||
|
(0, "112001", 4), # 深市公司债
|
||||||
|
(0, "101917", 4), # 深市国债
|
||||||
|
(0, "520261", 4), # 深市 520 段
|
||||||
|
# 沪市 B 股:3 位(2026-10-22 修正,曾按 2 位少 10 倍)
|
||||||
|
(1, "900901", 3), # 云赛B股
|
||||||
|
(1, "900948", 3),
|
||||||
|
# 深市 B 股:2 位(与沪市不同!)
|
||||||
|
(0, "200011", 2), # 深物业B
|
||||||
|
(0, "200488", 2),
|
||||||
|
# 边界:999xxx 是「指数」,不能被 9 开头当成 B 股
|
||||||
|
(1, "999999", 2), # 上证指数
|
||||||
|
(1, "999998", 2), # A股指数
|
||||||
|
])
|
||||||
|
def test_price_decimal_digits(market, code, expect):
|
||||||
|
assert price_decimal_digits(market, code) == expect
|
||||||
|
|
||||||
|
|
||||||
|
def test_price_decimal_digits_strips_nul():
|
||||||
|
assert price_decimal_digits(1, "000016\x00\x00") == 2
|
||||||
|
|
||||||
|
|
||||||
|
# ---------- 美股分时跨日:hour 必须是合法时钟值(回归:曾出现 hour=27) ----------
|
||||||
|
|
||||||
|
def _us_minutes(n: int = 390) -> list:
|
||||||
|
"""构造 usAAPL 的 390 条分时:从北京 21:30 起连续 n 分钟。"""
|
||||||
|
from pytdxdata.models import MinuteBar
|
||||||
|
|
||||||
|
start = 21 * 60 + 30
|
||||||
|
return [
|
||||||
|
MinuteBar(price=100.0 + i * 0.01, vol=1.0,
|
||||||
|
hour=(start + i) // 60, minute=(start + i) % 60,
|
||||||
|
market=74, code="AAPL", date=20261022)
|
||||||
|
for i in range(n)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_minute_bar_us_hour_is_valid_clock_value():
|
||||||
|
"""EX 分时的 minutes 是「自开盘起的连续分钟数」,直接 //60 会得到 hour=27。"""
|
||||||
|
bars = _us_minutes()
|
||||||
|
assert all(0 <= b.hour < 24 for b in bars)
|
||||||
|
# 归一化前的原始值确实越界(21:30 + 390min = 28:00)
|
||||||
|
assert max(b.hour + b.day_offset * 24 for b in bars) > 23
|
||||||
|
|
||||||
|
|
||||||
|
def test_minute_bar_us_day_offset_marks_next_day():
|
||||||
|
bars = _us_minutes()
|
||||||
|
assert bars[0].day_offset == 0 # 21:30 当晚
|
||||||
|
assert bars[-1].day_offset == 1 # 03:59 次日
|
||||||
|
assert (bars[-1].hour, bars[-1].minute) == (3, 59)
|
||||||
|
|
||||||
|
|
||||||
|
def test_minute_bar_us_datetime_is_monotonic_across_midnight():
|
||||||
|
bars = _us_minutes()
|
||||||
|
dts = [b.datetime for b in bars]
|
||||||
|
assert dts == sorted(dts)
|
||||||
|
assert dts[0] == datetime(2026, 10, 22, 21, 30)
|
||||||
|
assert dts[-1] == datetime(2026, 10, 23, 3, 59)
|
||||||
|
|
||||||
|
|
||||||
|
def test_minute_bar_us_trading_date_spans_two_calendar_days():
|
||||||
|
"""一个美股交易日的分时横跨两个北京自然日,trading_date 必须都是同一天。"""
|
||||||
|
bars = _us_minutes()
|
||||||
|
tds = {b.trading_date for b in bars}
|
||||||
|
assert tds == {"2026-10-22"}
|
||||||
|
# 而 datetime.date() 会给出两天 —— 这正是不能用它的原因
|
||||||
|
assert len({b.datetime.date() for b in bars}) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_minute_bar_cn_unchanged():
|
||||||
|
"""A股/港股分时 hour 本就合法,归一化必须零行为变化。"""
|
||||||
|
from pytdxdata.models import MinuteBar
|
||||||
|
|
||||||
|
for hour, minute in ((9, 30), (11, 29), (13, 0), (14, 59)):
|
||||||
|
b = MinuteBar(price=10.0, vol=1.0, hour=hour, minute=minute,
|
||||||
|
market=1, code="600000", date=20261022)
|
||||||
|
assert (b.hour, b.minute) == (hour, minute)
|
||||||
|
assert b.day_offset == 0
|
||||||
|
assert b.datetime == datetime(2026, 10, 22, hour, minute)
|
||||||
|
assert b.trading_date == "2026-10-22"
|
||||||
|
|
||||||
|
|
||||||
|
def test_minute_bar_cn_datetime_monotonic_full_session():
|
||||||
|
"""A股全天 240 条(09:30~11:29 + 13:00~14:59)时间轴严格递增。"""
|
||||||
|
from pytdxdata.models import MinuteBar
|
||||||
|
|
||||||
|
bars = []
|
||||||
|
for idx in range(240):
|
||||||
|
if idx < 120:
|
||||||
|
hour, minute = 9 + (30 + idx) // 60, (30 + idx) % 60
|
||||||
|
else:
|
||||||
|
hour, minute = 13 + (idx - 120) // 60, (idx - 120) % 60
|
||||||
|
bars.append(MinuteBar(price=10.0, vol=1.0, hour=hour, minute=minute,
|
||||||
|
market=1, code="600000", date=20261022))
|
||||||
|
dts = [b.datetime for b in bars]
|
||||||
|
assert dts == sorted(dts)
|
||||||
|
assert dts[0] == datetime(2026, 10, 22, 9, 30)
|
||||||
|
assert dts[-1] == datetime(2026, 10, 22, 14, 59)
|
||||||
|
assert {b.trading_date for b in bars} == {"2026-10-22"}
|
||||||
Reference in new issue
Block a user