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>
127 lines
4.7 KiB
Python
127 lines
4.7 KiB
Python
from __future__ import annotations
|
|
"""SMART_CASH_RECOVERY_V6 — ADV-1 지침 충족 빌더.
|
|
|
|
V5 위에서 추가 강화:
|
|
1. exec_mode=EXECUTE_REBOUND_ONLY 시 현재가 매도 금지, rebound_trigger_price 전사
|
|
2. numeric_generation_allowed=0 잠금 필드 추가
|
|
3. 결정론 체크섬(input_hash) — 동일 입력 = 동일 출력 보장
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
DEFAULT_JSON = ROOT / "GatherTradingData.json"
|
|
DEFAULT_REBOUND = ROOT / "Temp" / "rebound_sell_efficiency_v1.json"
|
|
DEFAULT_OUT = ROOT / "Temp" / "smart_cash_recovery_v6.json"
|
|
TEMP_V5 = ROOT / "Temp" / "_smart_cash_recovery_v5_for_v6.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 _input_hash(jp: Path, rp: Path) -> str:
|
|
h = hashlib.sha256()
|
|
for p in (jp, rp):
|
|
if p.exists():
|
|
h.update(p.read_bytes())
|
|
return h.hexdigest()[:16]
|
|
|
|
|
|
def _enrich_row_v6(row: dict[str, Any], global_exec_allowed: bool) -> dict[str, Any]:
|
|
"""ADV-1 Step3/4 강제 적용 — rebound 전사 및 exec_mode 잠금."""
|
|
enriched = dict(row)
|
|
exec_mode = str(row.get("exec_mode") or "")
|
|
rebound_price = row.get("rebound_trigger_price")
|
|
|
|
if exec_mode == "EXECUTE_REBOUND_ONLY":
|
|
enriched["hts_order_type"] = "LIMIT_SELL"
|
|
enriched["hts_limit_price"] = rebound_price
|
|
enriched["hts_immediate_sell_blocked"] = True
|
|
enriched["hts_block_reason"] = "EXECUTE_REBOUND_ONLY: 반등지정가만 허용"
|
|
else:
|
|
enriched["hts_order_type"] = row.get("sell_order_type", "LIMIT_SELL")
|
|
enriched["hts_limit_price"] = row.get("sell_limit_price") or rebound_price
|
|
enriched["hts_immediate_sell_blocked"] = False
|
|
enriched["hts_block_reason"] = ""
|
|
|
|
enriched["hts_candidate"] = global_exec_allowed and not enriched["hts_immediate_sell_blocked"]
|
|
enriched["ledger_only"] = not enriched["hts_candidate"]
|
|
return enriched
|
|
|
|
|
|
def main() -> int:
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--json", default=str(DEFAULT_JSON))
|
|
ap.add_argument("--rebound", default=str(DEFAULT_REBOUND))
|
|
ap.add_argument("--out", default=str(DEFAULT_OUT))
|
|
args = ap.parse_args()
|
|
|
|
jp = Path(args.json) if Path(args.json).is_absolute() else ROOT / args.json
|
|
rp = Path(args.rebound) if Path(args.rebound).is_absolute() else ROOT / args.rebound
|
|
op = Path(args.out) if Path(args.out).is_absolute() else ROOT / args.out
|
|
|
|
# 결정론 해시
|
|
ihash = _input_hash(jp, rp)
|
|
|
|
# V5 빌드
|
|
cmd = [
|
|
"python", "tools/build_smart_cash_recovery_v5.py",
|
|
"--json", str(jp), "--rebound", str(rp), "--out", str(TEMP_V5),
|
|
]
|
|
proc = subprocess.run(cmd, cwd=str(ROOT), text=True, capture_output=True, encoding="utf-8", errors="replace")
|
|
if proc.returncode != 0:
|
|
print((proc.stdout or "") + (proc.stderr or ""))
|
|
return proc.returncode
|
|
|
|
v5 = _load(TEMP_V5)
|
|
result = dict(v5)
|
|
global_exec = bool(result.get("execution_allowed"))
|
|
|
|
# V6: ADV-1 강화 — 각 행에 rebound 전사 + exec_mode 잠금
|
|
selected_combo = [
|
|
_enrich_row_v6(row, global_exec)
|
|
for row in (result.get("selected_sell_combo") or [])
|
|
if isinstance(row, dict)
|
|
]
|
|
result["selected_sell_combo"] = selected_combo
|
|
|
|
# ADV-1 필수 잠금 필드
|
|
result["formula_id"] = "SMART_CASH_RECOVERY_V6"
|
|
result["upgraded_from"] = "SMART_CASH_RECOVERY_V5"
|
|
result["numeric_generation_allowed"] = 0 # LLM 수치 생성 금지
|
|
result["input_hash"] = ihash # 결정론 체크섬
|
|
result["v6_enforced"] = {
|
|
"rebound_trigger_price_wired": True,
|
|
"exec_mode_lock": True,
|
|
"numeric_generation_allowed": 0,
|
|
"hts_candidate_rows": len([r for r in selected_combo if r.get("hts_candidate")]),
|
|
"ledger_only_rows": len([r for r in selected_combo if r.get("ledger_only")]),
|
|
"execute_rebound_only_rows": len([r for r in selected_combo if r.get("hts_immediate_sell_blocked")]),
|
|
}
|
|
|
|
op.parent.mkdir(parents=True, exist_ok=True)
|
|
op.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
vd = result.get("value_damage_pct_avg", 999)
|
|
covered = result.get("cash_shortfall_covered", False)
|
|
print(
|
|
f"SMART_CASH_RECOVERY_V6 input_hash={ihash} "
|
|
f"execution_allowed={global_exec} value_damage={vd} covered={covered} "
|
|
f"hts_rows={result['v6_enforced']['hts_candidate_rows']}"
|
|
)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|