"""按证据顺序定位零成交首个失效阶段。""" import json def diagnose(rows, key, window, threshold, cash, price, allowed): if not rows: return 'data:no_rows' if key != 'US:ALFA': return 'signal:symbol_key' if len(rows) < window: return 'signal:warmup' signal = int(rows[-1] > threshold) if not signal: return 'signal:zero' quantity = int((cash - 1) // price) if quantity <= 0: return 'target:zero_quantity' if not allowed: return 'execution:blocked' return f'fill:{quantity}' base = dict(rows=[10, 11, 12], key='US:ALFA', window=3, threshold=11, cash=101, price=10, allowed=True) cases = {'no_rows': {'rows': []}, 'wrong_key': {'key': 'ALFA'}, 'warmup': {'window': 120}, 'zero_signal': {'threshold': 13}, 'zero_quantity': {'cash': 5}, 'blocked': {'allowed': False}, 'fixed': {}} report = {label: diagnose(**dict(base, **change)) for label, change in cases.items()} assert list(report.values()) == ['data:no_rows', 'signal:symbol_key', 'signal:warmup', 'signal:zero', 'target:zero_quantity', 'execution:blocked', 'fill:10'] assert diagnose(**dict(base, rows=[], allowed=False)) == 'data:no_rows' print(json.dumps(report))