diff --git a/sanguo_trader/shadow/reconcile_report.py b/sanguo_trader/shadow/reconcile_report.py index 8bea2c4..a5840d7 100644 --- a/sanguo_trader/shadow/reconcile_report.py +++ b/sanguo_trader/shadow/reconcile_report.py @@ -264,12 +264,14 @@ def build_identity_report(db: str, date: Optional[str] = None) -> Dict[str, Any] "WHERE account_id=?", (inst["id"],)): inst_pos[_norm_symbol(r[0])] = inst_pos.get( _norm_symbol(r[0]), 0.0) + float(r[1] or 0) + # 双端键并集:快照独有=正向孤儿(遗留/手动仓),实例独有=负向幻影 + # (09-04 002836 实证:只遍历快照会漏幻影,导致修账只补一半) unattr_positions = [ - {"symbol": s, "snapshot_volume": v, + {"symbol": s, "snapshot_volume": snap_pos.get(s, 0.0), "instance_volume": inst_pos.get(s, 0.0), - "diff": v - inst_pos.get(s, 0.0)} - for s, v in sorted(snap_pos.items()) - if abs(v - inst_pos.get(s, 0.0)) > 0.5 + "diff": snap_pos.get(s, 0.0) - inst_pos.get(s, 0.0)} + for s in sorted(set(snap_pos) | set(inst_pos)) + if abs(snap_pos.get(s, 0.0) - inst_pos.get(s, 0.0)) > 0.5 ] if snap is None: status = "snapshot_missing" diff --git a/tests/trader/test_shadow_reconcile_report.py b/tests/trader/test_shadow_reconcile_report.py index 43b31c3..ebbd762 100644 --- a/tests/trader/test_shadow_reconcile_report.py +++ b/tests/trader/test_shadow_reconcile_report.py @@ -380,6 +380,23 @@ def test_identity_unattributed_position_listed(db): assert r["identity_passed"] is False +def test_identity_phantom_position_listed(db): + """实例账本有快照没有的票(幻影仓,如卖出迟到未记账)→ 负差票单列(09-04 002836 实证)。""" + _add_live_account(db, aid=5) + _seed_snapshot(db, mv=1_000_000, positions=[ + {"symbol": "600036.SH", "volume": 1000, "can_use": 1000, + "avg_price": 38, "mv": 38000}]) + _seed_inst_mv(db, 5, mv=997_000, + positions={"600036.XSHG": 1000, "002836.XSHE": 300}) # 幻影 300 股 + r = build_identity_report(db) + row = r["rows"][0] + unattr = row["unattributed_positions"] + assert [p["symbol"] for p in unattr] == ["002836"] + assert unattr[0]["snapshot_volume"] == 0 + assert unattr[0]["instance_volume"] == 300 + assert unattr[0]["diff"] == -300 + + def test_identity_instance_over_snapshot_negative(db): """Σ实例>快照(旧全账户行叠加期)→ 未归因为负,如实呈现 FAIL。""" _add_live_account(db, aid=5)