feat: 리밸런싱 엔진 V1 + GAS 버그 수정 (2026-06-13)
주요 변경: - 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>
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
DEFAULT_JSON = ROOT / "GatherTradingData.json"
|
||||
VALID_EXECUTION_STATUSES = {"PROPOSAL_ONLY", "EXECUTION_WAIT", "EXECUTION_READY"}
|
||||
REQUIRED_FIELDS = {
|
||||
"account",
|
||||
"ticker",
|
||||
"name",
|
||||
"proposal_type",
|
||||
"proposed_limit_price_krw",
|
||||
"proposed_price_basis",
|
||||
"proposed_quantity",
|
||||
"proposed_quantity_basis",
|
||||
"stop1_price_krw",
|
||||
"stop1_quantity",
|
||||
"stop2_price_krw",
|
||||
"stop2_quantity",
|
||||
"stop3_price_krw",
|
||||
"stop3_quantity",
|
||||
"tp1_price_krw",
|
||||
"tp1_quantity",
|
||||
"tp2_price_krw",
|
||||
"tp2_quantity",
|
||||
"tp3_price_krw",
|
||||
"tp3_quantity",
|
||||
"execution_status",
|
||||
"block_reason",
|
||||
}
|
||||
|
||||
|
||||
def load_harness(path: Path) -> dict[str, Any]:
|
||||
payload = json.loads(path.read_text(encoding="utf-8"))
|
||||
if isinstance(payload, dict) and isinstance(payload.get("data"), dict):
|
||||
maybe = payload["data"].get("_harness_context")
|
||||
if isinstance(maybe, dict):
|
||||
return maybe
|
||||
return payload
|
||||
|
||||
|
||||
def parse_bool(value: Any) -> bool | None:
|
||||
if isinstance(value, bool):
|
||||
return value
|
||||
if isinstance(value, str):
|
||||
lowered = value.strip().lower()
|
||||
if lowered in {"true", "1", "y", "yes"}:
|
||||
return True
|
||||
if lowered in {"false", "0", "n", "no"}:
|
||||
return False
|
||||
return None
|
||||
|
||||
|
||||
def parse_jsonish(value: Any) -> Any:
|
||||
if isinstance(value, (list, dict)):
|
||||
return value
|
||||
if isinstance(value, str) and value.strip():
|
||||
return json.loads(value)
|
||||
return None
|
||||
|
||||
|
||||
def validate_rows(rows: Any) -> list[str]:
|
||||
errors: list[str] = []
|
||||
if not isinstance(rows, list):
|
||||
return ["proposal_reference_json: must be a list"]
|
||||
for idx, row in enumerate(rows):
|
||||
if not isinstance(row, dict):
|
||||
errors.append(f"proposal_reference_json[{idx}]: must be an object")
|
||||
continue
|
||||
missing = sorted(REQUIRED_FIELDS - set(row))
|
||||
if missing:
|
||||
errors.append(f"proposal_reference_json[{idx}] missing fields: {missing}")
|
||||
for field in (
|
||||
"proposed_limit_price_krw",
|
||||
"proposed_quantity",
|
||||
"stop1_price_krw",
|
||||
"stop1_quantity",
|
||||
"stop2_price_krw",
|
||||
"stop2_quantity",
|
||||
"stop3_price_krw",
|
||||
"stop3_quantity",
|
||||
"tp1_price_krw",
|
||||
"tp1_quantity",
|
||||
"tp2_price_krw",
|
||||
"tp2_quantity",
|
||||
"tp3_price_krw",
|
||||
"tp3_quantity",
|
||||
):
|
||||
value = row.get(field)
|
||||
if value is not None and not isinstance(value, int):
|
||||
errors.append(f"proposal_reference_json[{idx}].{field}: must be integer or null")
|
||||
status = row.get("execution_status")
|
||||
if status is not None and status not in VALID_EXECUTION_STATUSES:
|
||||
errors.append(
|
||||
f"proposal_reference_json[{idx}].execution_status invalid: {status!r}"
|
||||
)
|
||||
return errors
|
||||
|
||||
|
||||
def main() -> int:
|
||||
args = sys.argv[1:]
|
||||
require = "--require" in args
|
||||
positional = [arg for arg in args if arg != "--require"]
|
||||
path = Path(positional[0]) if positional else DEFAULT_JSON
|
||||
if not path.is_absolute():
|
||||
path = ROOT / path
|
||||
|
||||
harness = load_harness(path)
|
||||
lock = parse_bool(harness.get("proposal_reference_lock"))
|
||||
raw = harness.get("proposal_reference_json")
|
||||
|
||||
if raw is None:
|
||||
if require:
|
||||
print("PROPOSAL REFERENCE FAIL")
|
||||
print("- proposal_reference_json missing")
|
||||
return 1
|
||||
print("PROPOSAL REFERENCE SKIP: proposal_reference_json not present yet")
|
||||
return 0
|
||||
|
||||
if lock is not True:
|
||||
if require:
|
||||
print("PROPOSAL REFERENCE FAIL")
|
||||
print(f"- proposal_reference_lock must be true when proposal_reference_json exists, found={harness.get('proposal_reference_lock')!r}")
|
||||
return 1
|
||||
print("PROPOSAL REFERENCE SKIP: proposal_reference_json present but proposal_reference_lock is not true")
|
||||
return 0
|
||||
|
||||
try:
|
||||
rows = parse_jsonish(raw)
|
||||
except Exception as exc:
|
||||
print("PROPOSAL REFERENCE FAIL")
|
||||
print(f"- proposal_reference_json invalid JSON: {exc}")
|
||||
return 1
|
||||
|
||||
errors = validate_rows(rows)
|
||||
if errors:
|
||||
print("PROPOSAL REFERENCE FAIL")
|
||||
for err in errors:
|
||||
print(f"- {err}")
|
||||
return 1
|
||||
|
||||
print(f"PROPOSAL REFERENCE OK: rows={len(rows)}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user