fix(ex): 期货/期权K线 amount 恒 0 + 补齐持仓量 position 字段 (#17)

- 0x23FF 帧内无成交额槽位(协议语义,非解析遗漏),期货/期权 amount
  按 close×vol×合约乘数 估算(新增 ex_multiplier 乘数表,覆盖
  cffex/shf/dl/zz/gz 主流品种 + 股票期权 10000;未知品种保持 0.0 不编造)
- position(持仓量)协议层早已解析但 api 层丢弃,v3 起透传至
  SecurityBar.position(权益类市场无此概念 → None)
- 缓存 schema v2→v3:旧缓存 amount 恒 0/无 position,自然失效重建
- 真机验证:rb2611/IFL0/au2611/上交所期权 10011425 日线
  amount/settle/position 全部有值
This commit is contained in:
openbot-coder committed 2026-11-14 14:16:10 +08:00
1 parent 1cd8f692a1
commit 7c4541a315
6 files changed
+187 -18

No files matched your search

+46 -1
View File
@@ -132,6 +132,7 @@ async def test_bars_ex_backfills_amount_for_hk() -> None:
assert bars[0].amount == pytest.approx(2_040_000.0)
assert bars[0].vol == 2000
assert bars[0].settle is None
assert bars[0].position is None # 权益类无持仓量
@pytest.mark.asyncio
@@ -140,7 +141,9 @@ async def test_bars_ex_backfills_settle_for_futures() -> None:
bars = await td.get_bars("cffex_futures:IFL0", count=1)
assert bars[0].settle == pytest.approx(4047.8)
assert bars[0].amount == 0.0
# v3 起期货按收盘价估算成交额:close 10.2 × vol 25392 × IF 乘数 200
assert bars[0].amount == pytest.approx(10.2 * 25392 * 200)
assert bars[0].position == 49_772.0
# ---------- 旧磁盘缓存兼容 ----------
@@ -173,3 +176,45 @@ def test_ex_symbols_route_to_ex_path() -> None:
"""守住前提:上面对 31/47 的断言确实走的是 EX 通道。"""
assert parse_symbols(["hk00700"])[0].is_ex
assert parse_symbols(["cffex_futures:IFL0"])[0].is_ex
# ---------- v3:期权持仓量 + 乘数表 ----------
def test_multiplier_known_products() -> None:
from pytdxdata.ex_multiplier import find_multiplier
assert find_multiplier("cffex", "IFL0") == 200
assert find_multiplier("shf", "rb2611") == 10
assert find_multiplier("shf", "au2611") == 100
assert find_multiplier("dl", "i2501") == 100
assert find_multiplier("zz", "AP0") == 10
def test_multiplier_unknown_returns_none() -> None:
from pytdxdata.ex_multiplier import find_multiplier
assert find_multiplier("shf", "xyz999") is None
assert find_multiplier("nosuch", "rb2611") is None
def test_stock_option_multiplier() -> None:
from pytdxdata.ex_multiplier import find_multiplier
assert find_multiplier("sh_stock_option", "10011425") == 10_000
@pytest.mark.asyncio
async def test_bars_ex_option_position_backfilled() -> None:
"""期权 K 线持仓量透传(用户主诉):协议槽位 5 已解析,v3 起到 SecurityBar。"""
td = _fake_api({8: [_row(8, "10011425", position=123_456, trade=6373, settle=0.0887)]})
bars = await td.get_bars("sh_stock_option:10011425", count=1)
assert bars[0].position == 123_456.0
# 股票期权乘数 10000:close 10.2 × vol 6373 × 10000
assert bars[0].amount == pytest.approx(10.2 * 6373 * 10_000)
@pytest.mark.asyncio
async def test_bars_ex_unknown_multiplier_keeps_amount_zero() -> None:
"""乘数表没有的品种:宁可不给 amount 也不编造。"""
td = _fake_api({30: [_row(30, "ZZZ2601", position=10, trade=5, settle=1.0)]})
bars = await td.get_bars("shf:ZZZ2601", count=1)
assert bars[0].amount == 0.0
assert bars[0].position == 10.0