ee3e799de1
주요 변경: - tools/build_rebalance_engine_v1.py: REBALANCE_ENGINE_V1 신규 * account_snapshot 직접 합산(_build_snap_position_map) → 소수주 분리 행 병합 * 레짐 소스 macro.REGIME_PRELIM 최우선 (GAS 와 동일) - src/gas_adapter_parts/gdf_06_rebalance.gs: runRebalanceSheet_() 신규 * Logger.log / getSpreadsheet_() 로 run_all 연동 수정 - src/gas_adapter_parts/gdc_01_fetch_fundamentals.gs * _mergePositionRecord_(): 소수주 중복 행 합산 신규 * parseInt → parseFloat (qty, availQty) - src/gas_adapter_parts/gdf_01_price_metrics.gs * 미보유 종목 SELL_READY → WATCH_EXIT_SIGNAL - spec/41_release_dag.yaml: build_rebalance_sheet 노드 추가 (step_count 63) - spec/51_formula_lifecycle_registry.yaml: REBALANCE_ENGINE_V1 등록 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
126 lines
3.9 KiB
Python
126 lines
3.9 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
DEFAULT_HISTORY = ROOT / "Temp" / "proposal_evaluation_history.json"
|
|
DEFAULT_OUT = ROOT / "Temp" / "execution_quality_harness_v1.json"
|
|
|
|
|
|
def _load(path: Path) -> dict[str, Any]:
|
|
if not path.exists():
|
|
return {}
|
|
try:
|
|
obj = json.loads(path.read_text(encoding="utf-8"))
|
|
except Exception:
|
|
return {}
|
|
return obj if isinstance(obj, dict) else {}
|
|
|
|
|
|
def _to_float(v: Any) -> float:
|
|
try:
|
|
return float(v)
|
|
except Exception:
|
|
return 0.0
|
|
|
|
|
|
def _max_drawdown_pct(returns_pct: list[float]) -> float:
|
|
equity = 1.0
|
|
peak = 1.0
|
|
max_dd = 0.0
|
|
for r in returns_pct:
|
|
equity *= (1.0 + r / 100.0)
|
|
if equity > peak:
|
|
peak = equity
|
|
dd = 0.0 if peak <= 0 else (peak - equity) / peak * 100.0
|
|
if dd > max_dd:
|
|
max_dd = dd
|
|
return round(max_dd, 2)
|
|
|
|
|
|
def main() -> int:
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--history", default=str(DEFAULT_HISTORY))
|
|
ap.add_argument("--out", default=str(DEFAULT_OUT))
|
|
ap.add_argument("--min-samples", type=int, default=30)
|
|
args = ap.parse_args()
|
|
|
|
hp = Path(args.history)
|
|
op = Path(args.out)
|
|
if not hp.is_absolute():
|
|
hp = ROOT / hp
|
|
if not op.is_absolute():
|
|
op = ROOT / op
|
|
|
|
history = _load(hp)
|
|
records = history.get("records") if isinstance(history.get("records"), list) else []
|
|
t20 = [r for r in records if isinstance(r, dict) and r.get("t20_evaluation_status") == "EVALUATED_T20"]
|
|
operational = [r for r in t20 if str(r.get("validation_status") or "").upper() != "REPLAY_BACKFILL"]
|
|
replay = [r for r in t20 if str(r.get("validation_status") or "").upper() == "REPLAY_BACKFILL"]
|
|
|
|
def eval_set(rows: list[dict[str, Any]]) -> dict[str, float]:
|
|
rets = [_to_float(r.get("t20_return_pct")) for r in rows]
|
|
wins = [x for x in rets if x > 0]
|
|
losses = [x for x in rets if x < 0]
|
|
n = len(rets)
|
|
win_rate = (len(wins) / n * 100.0) if n else 0.0
|
|
avg_win = (sum(wins) / len(wins)) if wins else 0.0
|
|
avg_loss = (sum(losses) / len(losses)) if losses else 0.0
|
|
expectancy = (win_rate / 100.0) * avg_win + (1.0 - win_rate / 100.0) * avg_loss
|
|
return {
|
|
"samples": n,
|
|
"win_rate_pct": round(win_rate, 2),
|
|
"avg_win_pct": round(avg_win, 2),
|
|
"avg_loss_pct": round(avg_loss, 2),
|
|
"expectancy_pct": round(expectancy, 3),
|
|
"max_drawdown_pct": _max_drawdown_pct(rets),
|
|
}
|
|
|
|
opm = eval_set(operational)
|
|
rpm = eval_set(replay)
|
|
|
|
gate = "PASS"
|
|
reasons: list[str] = []
|
|
if opm["samples"] < args.min_samples:
|
|
gate = "WATCH_PENDING_SAMPLE"
|
|
reasons.append("OPERATIONAL_SAMPLE_INSUFFICIENT")
|
|
else:
|
|
if opm["expectancy_pct"] <= 0:
|
|
gate = "FAIL"
|
|
reasons.append("NEGATIVE_EXPECTANCY")
|
|
if opm["max_drawdown_pct"] > 12.0:
|
|
gate = "FAIL"
|
|
reasons.append("MDD_ABOVE_12")
|
|
if opm["win_rate_pct"] < 45.0:
|
|
gate = "FAIL"
|
|
reasons.append("WIN_RATE_BELOW_45")
|
|
|
|
res = {
|
|
"formula_id": "EXECUTION_QUALITY_HARNESS_V1",
|
|
"gate": gate,
|
|
"reasons": reasons,
|
|
"targets": {
|
|
"min_samples": int(args.min_samples),
|
|
"expectancy_pct_min": 0.0,
|
|
"max_drawdown_pct_max": 12.0,
|
|
"win_rate_pct_min": 45.0,
|
|
},
|
|
"metrics": {
|
|
"operational_t20": opm,
|
|
"replay_t20": rpm,
|
|
},
|
|
}
|
|
|
|
op.parent.mkdir(parents=True, exist_ok=True)
|
|
op.write_text(json.dumps(res, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
print(json.dumps(res, ensure_ascii=False, indent=2))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|