feat: 指数成分成交汇总 + 债券/回购价格标度修复 + 美股分时 hour 修复 (0.6.5) (#6)

* feat: 指数成分成交汇总 + 债券/回购价格标度修复 + 美股分时 hour 修复

新增
- get_index_members / get_index_trade_stats / get_index_trade_stats_multi:
  由成分股逐笔累加出指数的每分钟成交量与成交笔数(协议里指数本身没有笔数,
  trade_count 实为均价*1000)
- CLI: tdx index-stats

修复
- price_decimal_digits 补齐债券/回购 4 位小数档:沪市少 100 倍、深市少 10 倍,
  沪市 B 股 900xxx 改为 3 位。已用全量 51982 只标的的 SecurityInfo.decimal_point
  逐条比对,零例外
- MinuteBar.hour 对美股(EX)出现 24~27:改为 hour%=24 + day_offset,
  新增 datetime / trading_date 只读属性;A股/港股零行为变化

测试 217 -> 289

* chore: bump version 0.6.5

---------

Co-authored-by: openbot-coder <openbot-coder@users.noreply.github.com>
This commit is contained in:
openbotandopenbot-coder authored and GitHub committed 2026-10-22 13:54:35 +08:00
1 parent ca24521d2c
commit b84a6c8a1b
15 files changed
+1129 -21

No files matched your search

+18
View File
@@ -457,6 +457,24 @@ async def test_boards(td):
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
# --------------------------------------------------------------------------- #
# 六、服务器文件
# --------------------------------------------------------------------------- #
+303
View File
@@ -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)
+110 -1
View File
@@ -337,7 +337,37 @@ def test_finish_bars_sorts_us_session_chronologically():
(0, "000001", 2), # 平安银行(不是指数!)
(0, "300750", 2),
(0, "159915", 3), # 深市 ETF
(0, "128035", 3), # 深市转债
# 债券/回购: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
@@ -345,3 +375,82 @@ def test_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"}