Files
easy-tdx/src/easy_tdx/codec/volume.py
T
GitHubandClaude Opus 4.8 4dfd18050e fix: resolve all CI mypy (265→0) and ruff (26→0) errors
- pyproject.toml: add mypy overrides for pandas/tabulate/matplotlib stubs,
  disable strict checking for vendored MyTT library
- config.py: use cast() for dict[str, Any] .get() returns
- beichi.py: widen _calc_bi_force param to BI | XD, import XD
- backtest/cli.py: split combo/single strategy into separate typed variables
- backtest/combo.py: add bool_array() helper for numpy return types
- chanlun/analyser.py: type ignore for pandas row access, fix dict type arg
- unified.py: change fields param from object to Any
- ex/mac_client.py: add type args to list literals
- cli/cmd_offline.py: wrap int market as Market enum before API call
- cli/cmd_chanlun.py: fix dict type arg
- offline/write_*.py: explicit int() cast for struct.unpack returns
- MyTT.py: fix line-too-long comments, UP038 isinstance syntax
- tests: fix E712 (==False → ~mask), E741 (noqa), F841, import sorting
- ruff format applied across codebase

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-10 15:03:41 +08:00

57 lines
1.4 KiB
Python

"""通达信 4 字节自定义浮点格式解码(成交量专用)。
格式:4 字节小端 uint32,分三段:
[3] logpoint — 指数部分
[2] hleax — 高精度部分
[1] lheax — 中精度部分
[0] lleax — 低精度部分
警告:此函数专为成交量设计,不可用于价格字段(pytdx Bug #3)。
"""
from .._binary import unpack_from
def get_volume(data: bytes | bytearray, pos: int) -> tuple[float, int]:
"""从 data[pos:pos+4] 解码成交量。
Returns:
(volume_float, new_pos)
"""
(ivol,) = unpack_from("<I", data, pos, "volume")
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