mirror of
https://ghfast.top/https://github.com/aeroxw/easy_tdx_max.git
synced 2026-09-12 16:54:20 +08:00
替代年久失修的 pytdx,修复已知 bug,保留未解字段供逆向分析。 主要内容: - codec 层:get_price 变长编码、get_volume 自定义浮点、datetime/frame 解析 - transport 层:同步(socket)+ 异步(asyncio)双实现,共用命令层 - 命令层(11 条):security_count/list/quotes/bars、minute_time(今日+历史)、 transaction(当日+历史)、xdxr_info、finance_info、company_info - 高层 API:TdxClient + AsyncTdxClient - 单元测试 26 条,全部通过;真实服务器集成测试覆盖全部命令 修复 pytdx Bug #1–5:xdxr 循环读取错误位置、GBK 截断崩溃、 pre_close 误用 get_volume、逐笔/分时未解字段被丢弃 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
57 lines
1.4 KiB
Python
57 lines
1.4 KiB
Python
"""通达信 4 字节自定义浮点格式解码(成交量专用)。
|
|
|
|
格式:4 字节小端 uint32,分三段:
|
|
[3] logpoint — 指数部分
|
|
[2] hleax — 高精度部分
|
|
[1] lheax — 中精度部分
|
|
[0] lleax — 低精度部分
|
|
|
|
警告:此函数专为成交量设计,不可用于价格字段(pytdx Bug #3)。
|
|
"""
|
|
|
|
import struct
|
|
|
|
|
|
def get_volume(data: bytes | bytearray, pos: int) -> tuple[float, int]:
|
|
"""从 data[pos:pos+4] 解码成交量。
|
|
|
|
Returns:
|
|
(volume_float, new_pos)
|
|
"""
|
|
(ivol,) = struct.unpack_from("<I", data, pos)
|
|
return _decode_volume(ivol), pos + 4
|
|
|
|
|
|
def _decode_volume(ivol: int) -> float:
|
|
if ivol == 0:
|
|
return 0.0
|
|
|
|
logpoint = (ivol >> 24) & 0xFF
|
|
hleax = (ivol >> 16) & 0xFF
|
|
lheax = (ivol >> 8) & 0xFF
|
|
lleax = ivol & 0xFF
|
|
|
|
exp = logpoint * 2 - 0x7F
|
|
base = _pow2(exp)
|
|
|
|
exp_h = logpoint * 2 - 0x86
|
|
if hleax > 0x80:
|
|
hi = _pow2(exp_h) * 128 + (hleax & 0x7F) * _pow2(exp_h + 1)
|
|
else:
|
|
hi = _pow2(exp_h) * hleax
|
|
|
|
mid = _pow2(logpoint * 2 - 0x8E) * lheax
|
|
lo = _pow2(logpoint * 2 - 0x96) * lleax
|
|
|
|
if hleax & 0x80:
|
|
mid *= 2.0
|
|
lo *= 2.0
|
|
|
|
return base + hi + mid + lo
|
|
|
|
|
|
def _pow2(exp: int) -> float:
|
|
if exp >= 0:
|
|
return float(1 << exp) if exp < 63 else 2.0 ** exp
|
|
return 1.0 / (1 << (-exp)) if -exp < 63 else 2.0 ** exp
|