fix: 修复沪市指数价格少10倍 + 逐笔竞价委托污染(Σvol 虚高/跨主机不稳定) (#3)
核对 sh000016 上证50 与 50 只成分股的 tick 汇总时,发现三个口径
(快照/分时/逐笔)互不一致,追查到 5 个缺陷。
1. 沪市指数价格整体少 10 倍
price_decimal_digits 把沪市 000xxx(上证/上证50/沪深300/科创50)与
8xxxxx(板块指数)判为 3 位小数,实际协议是 2 位。
上证指数 3713 -> 371、上证50 2747 -> 274。
实证 6 只指数 + 2 只板块指数全部 ×10,而 ETF/股票/科创板/深市全类 ×1 正确。
改法:沪市仅 ETF(5开头) 保留 3 位,其余 2 位。
2. get_ticks 把 bs_flag=8 竞价委托当成成交
该记录是 09:15~09:25 / 14:57~15:00 竞价段的撮合委托快照,
vol 是累计值(可被后续覆盖),不是成交增量。
- Σvol 虚高:sh600000 含 b8 = 668,693 vs 快照 462,562(1.4456);
剔 b8 = 462,634(1.000156,精确一致)
- 跨主机不稳定:干净主机 0 条、脏主机 135 条 -> 同一 start 落到不同真实成交
改法:分页后统一剔除 b8。
3. A股逐笔改单连接串行游标(_ticks_cn_serial)
并发分页各页来自不同主机,而 0x122F 的 start 是「从最新往回数」的相对锚点,
主机间 b8 收录差异导致页边界既重叠(冗余 116 条)又漏条(13:03 附近丢 59 条)。
逐笔单日仅 5~6 页,串行开销可忽略。
4. trade_count 有符号解析(前序)、返回列表时间序(前序)保留。
- sh000016 三口径闭合:快照 vol/amount 比值 1.00000061/0.99999997;
指数逐笔 Σvol = 分时 Σvol = 1,045,584,467(完全相等)
- 6 只标的 Σvol/快照 vol 全部回到 1.0001~1.0007
- 跨主机公共时间窗逐条一致(5/5 标的,条数差 0)
- 测试 194 -> 217 passed(+20 小数位、+3 逐笔回归)
- TransactionRecord 类注释 + get_ticks docstring 明示指数语义:
vol 单位「百元」(成交额/100)、trade_count 为「均价×1000」而非笔数
- API_REFERENCE / llms.txt 同步
Co-authored-by: openbot-coder <openbot-coder@users.noreply.github.com>
This commit is contained in:
1 parent
dfd6fab3af
commit
8344ed6e6b
10 files changed
+493
-39
No files matched your search
+211
-1
@@ -8,7 +8,13 @@ import unittest.mock as mock
|
||||
import pytest
|
||||
|
||||
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.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 开始
|
||||
|
||||
|
||||
# ---------- 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 日期过滤 ----------
|
||||
|
||||
class DateBarApiConn(FakeApiConn):
|
||||
@@ -329,3 +399,143 @@ async def test_get_kline_since():
|
||||
assert bars[-1].year == 2027
|
||||
finally:
|
||||
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 条"
|
||||
|
||||
@@ -12,6 +12,7 @@ from pytdxdata.protocol.commands.ex_proto import GetExInstrumentBarsCmd
|
||||
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.quotes import price_decimal_digits
|
||||
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
|
||||
@@ -181,6 +182,30 @@ def test_mac_transaction_parse():
|
||||
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 通道对美股返回的时间戳把两个时区拼在一起:日期字段是**美东交易日**、
|
||||
@@ -286,3 +311,37 @@ def test_finish_bars_sorts_us_session_chronologically():
|
||||
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
|
||||
(0, "128035", 3), # 深市转债
|
||||
])
|
||||
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
|
||||
Reference in new issue
Block a user