"""R605:合法参数批次与实际生效参数回执;只用合成价格。""" from itertools import product from hashlib import sha256 import json PRICES = [10, 11, 9, 12, 13, 10, 14, 15, 11, 16] def digest(value): return sha256(json.dumps(value, sort_keys=True).encode()).hexdigest() def strategy(prices, *, fast, slow, threshold): effective = dict(fast=fast, slow=slow, threshold=threshold) signals = [] for t in range(5, len(prices) - 1): quick = sum(prices[t-fast+1:t+1]) / fast long = sum(prices[t-slow+1:t+1]) / slow signals.append(int(quick > long * (1 + threshold))) return {"effective": effective, "signals": signals} def run(): proposed, excluded, completed = [], [], [] for fast, slow, threshold in product([2, 3, 4], [3, 4, 5], [0, 0.01]): config = dict(fast=fast, slow=slow, threshold=threshold) proposed.append(config) if fast >= slow: excluded.append({"config": config, "reason": "fast>=slow"}) continue result = strategy(PRICES, **config) assert result["effective"] == config completed.append({"id": digest(config)[:12], "config": config, "data": digest(PRICES), "code": "R605.py:strategy", "status": "completed", **result}) assert len(proposed) == len(excluded) + len(completed) assert len({row["id"] for row in completed}) == len(completed) assert len(completed) == 12 and len(excluded) == 6 try: strategy(PRICES, fast=2, slow=5, threshold=0, treshold=0.01) except TypeError: pass else: raise AssertionError("unknown field accepted") # 反例:包装器忽略传入值,实际重复运行同一个默认配置。 broken = [strategy(PRICES, fast=2, slow=5, threshold=0) for _ in completed] mismatches = sum(row["config"] != got["effective"] for row, got in zip(completed, broken)) assert mismatches == 11 print(f"proposed={len(proposed)} excluded={len(excluded)} completed={len(completed)}") for index in [0, -1]: row = completed[index] print("trace=" + json.dumps({"config": row["config"], "effective": row["effective"], "signals": row["signals"]}, sort_keys=True)) print(f"ignored_config_mismatches={mismatches}") return completed if __name__ == "__main__": run()