tradingSystem/research/exit_invalidation_20261116/audit_coverage.py

64 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""只读既有沪深缓存,核验退出比较的数据边界;不回测收益、不联网。"""
import csv
import gzip
import hashlib
import json
from collections import Counter
from pathlib import Path
ROOT = Path(__file__).resolve().parent
CODES = {'688025.SH', '601126.SH', '002979.SZ', '603667.SH',
'603259.SH', '300124.SZ', '688100.SH', '300750.SZ'}
assert all(c.endswith(('.SH', '.SZ')) for c in CODES)
def main():
source = ROOT/'holding_snapshot.json'
old = ROOT.parent/'signal_independence_20261111/data/normalized.csv.gz'
snapshot = json.loads(source.read_text())
by_source = {}
with gzip.open(old, 'rt', encoding='utf-8') as stream:
for r in csv.DictReader(stream):
if r['code'] not in CODES or not '2026-11-02' <= r['day'] <= '2026-11-13':
continue
info = by_source.setdefault(r['source'], {'rows': 0, 'days': set(), 'codes': set(), 'old_qrs_evidence': 0})
info['rows'] += 1
info['days'].add(r['day']); info['codes'].add(r['code'])
# 仅统计明显早于重置日的数据;不以自然日差代替前一交易日核验。
if r['source'] == 'qrs' and r['evidence_date'] and r['evidence_date'] < '2026-11-02':
info['old_qrs_evidence'] += 1
for info in by_source.values():
info['days'] = sorted(info['days']); info['codes'] = sorted(info['codes'])
holdings = {}
for code, row in snapshot['stocks'].items():
assert code in CODES
state = row['risk_exit'] or {}
unique = {}
refs = {}
for record in row['ledger']:
hard = record['hard_numbers']
e = hard.get('timing_direction') or hard.get('event')
if not e or e.get('source') != 'timing_fusion':
continue
assert e['ts_code'] == code and len(e['key']) == 64
unique[e['key']] = e
refs.setdefault(e['key'], []).append(record['id'])
holdings[code] = {'phase': state.get('phase'), 'accepted_signals': len(state.get('events') or []),
'first_source': (state.get('events') or [{}])[0].get('source'),
'ledger_rows': len(row['ledger']), 'distinct_fusion_events': len(unique),
'actions': dict(Counter(e['action'] for e in unique.values())),
'same_event_ledger_rows': [ids for ids in refs.values() if len(ids)>1],
'latest_saved_verified_at': (row.get('timing') or {}).get('verified_at')}
output = {'snapshot_at': snapshot['at'], 'scope': sorted(CODES),
'inputs': {str(p.relative_to(ROOT.parent.parent)): hashlib.sha256(p.read_bytes()).hexdigest() for p in (source, old, ROOT/'PROTOCOL.md')},
'archived_old_signal_coverage': by_source, 'current_holdings': holdings,
'limitations': ['本次PMS账本查询从11月11日16:17:11起且只含4只当前持仓,不能代表完整8只轮次。',
'旧规范化缓存截止11月10日,不含新合成规则的完整历史输入。',
'未核齐原计划、全部历史命令、分钟可执行约束与首次可得时间,不具备反事实收益计算条件。']}
(ROOT/'coverage.json').write_text(json.dumps(output, ensure_ascii=False, indent=2)+'\n')
print(json.dumps(output, ensure_ascii=False, indent=2))
if __name__ == '__main__':
main()