|
| 1 | +"""Validated, read-only benchmark bindings for lifecycle monitoring. |
| 2 | +
|
| 3 | +The catalog deliberately maps a *strategy profile* to the passive or |
| 4 | +unleveraged instrument used to judge it. It supplies monitoring context only: |
| 5 | +it cannot change a strategy, rebalance an account, or grant execution rights. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import json |
| 11 | +from collections.abc import Mapping, Sequence |
| 12 | +from dataclasses import dataclass |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | + |
| 16 | +STRATEGY_BENCHMARK_CATALOG_SCHEMA = "qsl.strategy-benchmark-catalog.v1" |
| 17 | +_BENCHMARK_KINDS = frozenset({"passive", "unleveraged_underlying"}) |
| 18 | + |
| 19 | + |
| 20 | +class StrategyBenchmarkCatalogError(ValueError): |
| 21 | + """Raised when a benchmark catalog cannot be safely used for monitoring.""" |
| 22 | + |
| 23 | + |
| 24 | +def _nonblank(value: object, label: str) -> str: |
| 25 | + if not isinstance(value, str) or not value or value != value.strip(): |
| 26 | + raise StrategyBenchmarkCatalogError(f"{label} must be a non-empty canonical string") |
| 27 | + return value |
| 28 | + |
| 29 | + |
| 30 | +@dataclass(frozen=True) |
| 31 | +class StrategyBenchmarkBinding: |
| 32 | + """One explicit, no-authority performance benchmark binding.""" |
| 33 | + |
| 34 | + strategy_profile: str |
| 35 | + benchmark_symbol: str |
| 36 | + benchmark_kind: str = "passive" |
| 37 | + relative_drawdown_required: bool = True |
| 38 | + |
| 39 | + def __post_init__(self) -> None: |
| 40 | + _nonblank(self.strategy_profile, "strategy profile") |
| 41 | + _nonblank(self.benchmark_symbol, "benchmark symbol") |
| 42 | + if self.benchmark_kind not in _BENCHMARK_KINDS: |
| 43 | + raise StrategyBenchmarkCatalogError("benchmark kind is not supported") |
| 44 | + if type(self.relative_drawdown_required) is not bool: |
| 45 | + raise StrategyBenchmarkCatalogError("relative drawdown requirement must be boolean") |
| 46 | + |
| 47 | + |
| 48 | +def build_strategy_benchmark_catalog( |
| 49 | + bindings: Sequence[StrategyBenchmarkBinding], |
| 50 | +) -> dict[str, object]: |
| 51 | + """Return a validated, JSON-ready catalog without execution authority.""" |
| 52 | + entries = tuple(bindings) |
| 53 | + if not entries or any(type(entry) is not StrategyBenchmarkBinding for entry in entries): |
| 54 | + raise StrategyBenchmarkCatalogError("catalog must contain immutable benchmark bindings") |
| 55 | + profiles = [entry.strategy_profile for entry in entries] |
| 56 | + if len(set(profiles)) != len(profiles): |
| 57 | + raise StrategyBenchmarkCatalogError("strategy profiles must be unique") |
| 58 | + return { |
| 59 | + "schema_version": STRATEGY_BENCHMARK_CATALOG_SCHEMA, |
| 60 | + "authority": {"monitoring_only": True, "no_order": True}, |
| 61 | + "bindings": [ |
| 62 | + { |
| 63 | + "strategy_profile": entry.strategy_profile, |
| 64 | + "benchmark_symbol": entry.benchmark_symbol, |
| 65 | + "benchmark_kind": entry.benchmark_kind, |
| 66 | + "relative_drawdown_required": entry.relative_drawdown_required, |
| 67 | + } |
| 68 | + for entry in entries |
| 69 | + ], |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | +def load_strategy_benchmark_catalog(path: str | Path) -> dict[str, str]: |
| 74 | + """Load explicit profile-to-benchmark bindings from a validated JSON catalog.""" |
| 75 | + try: |
| 76 | + payload = json.loads(Path(path).read_text(encoding="utf-8")) |
| 77 | + except (OSError, json.JSONDecodeError) as exc: |
| 78 | + raise StrategyBenchmarkCatalogError("benchmark catalog could not be read as JSON") from exc |
| 79 | + if not isinstance(payload, Mapping): |
| 80 | + raise StrategyBenchmarkCatalogError("benchmark catalog must be an object") |
| 81 | + if payload.get("schema_version") != STRATEGY_BENCHMARK_CATALOG_SCHEMA: |
| 82 | + raise StrategyBenchmarkCatalogError("benchmark catalog schema version is not supported") |
| 83 | + authority = payload.get("authority") |
| 84 | + if authority != {"monitoring_only": True, "no_order": True}: |
| 85 | + raise StrategyBenchmarkCatalogError("benchmark catalog must declare monitoring-only no-order authority") |
| 86 | + raw_bindings = payload.get("bindings") |
| 87 | + if not isinstance(raw_bindings, list): |
| 88 | + raise StrategyBenchmarkCatalogError("benchmark catalog bindings must be a list") |
| 89 | + bindings: list[StrategyBenchmarkBinding] = [] |
| 90 | + for raw in raw_bindings: |
| 91 | + if not isinstance(raw, Mapping): |
| 92 | + raise StrategyBenchmarkCatalogError("benchmark catalog binding must be an object") |
| 93 | + bindings.append( |
| 94 | + StrategyBenchmarkBinding( |
| 95 | + strategy_profile=raw.get("strategy_profile"), |
| 96 | + benchmark_symbol=raw.get("benchmark_symbol"), |
| 97 | + benchmark_kind=raw.get("benchmark_kind", "passive"), |
| 98 | + relative_drawdown_required=raw.get("relative_drawdown_required", True), |
| 99 | + ) |
| 100 | + ) |
| 101 | + catalog = build_strategy_benchmark_catalog(bindings) |
| 102 | + return { |
| 103 | + str(item["strategy_profile"]): str(item["benchmark_symbol"]) |
| 104 | + for item in catalog["bindings"] |
| 105 | + if isinstance(item, Mapping) |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | +__all__ = [ |
| 110 | + "STRATEGY_BENCHMARK_CATALOG_SCHEMA", |
| 111 | + "StrategyBenchmarkBinding", |
| 112 | + "StrategyBenchmarkCatalogError", |
| 113 | + "build_strategy_benchmark_catalog", |
| 114 | + "load_strategy_benchmark_catalog", |
| 115 | +] |
0 commit comments