diff --git a/docs/ouroboros/80_implementation_audit.md b/docs/ouroboros/80_implementation_audit.md index 9eb0c5a..eb3d23a 100644 --- a/docs/ouroboros/80_implementation_audit.md +++ b/docs/ouroboros/80_implementation_audit.md @@ -35,7 +35,7 @@ Updated: 2026-03-02 | REQ-V2-004 | 4중 청산 로직 (Hard/BE/ATR Trailing/Model) | `src/strategy/exit_rules.py` | ⚠️ 부분 (`#369`) | | REQ-V2-005 | Triple Barrier 라벨링 | `src/analysis/triple_barrier.py` | ✅ 완료 | | REQ-V2-006 | Walk-Forward + Purge/Embargo 검증 | `src/analysis/walk_forward_split.py` | ✅ 완료 | -| REQ-V2-007 | 비용/슬리피지/체결실패 모델 필수 | `src/analysis/backtest_cost_guard.py` | ⚠️ 부분 (`#368`) | +| REQ-V2-007 | 비용/슬리피지/체결실패 모델 필수 | `src/analysis/backtest_cost_guard.py`, `src/analysis/backtest_pipeline.py` | ✅ 완료 | | REQ-V2-008 | Kill Switch 실행 순서 (Block→Cancel→Refresh→Reduce→Snapshot) | `src/core/kill_switch.py` | ⚠️ 부분 (`#377`) | ### 1.3 v3 구현 상태: 부분 완료 (2026-03-02 기준) diff --git a/src/analysis/backtest_cost_guard.py b/src/analysis/backtest_cost_guard.py index ae0d729..3215c90 100644 --- a/src/analysis/backtest_cost_guard.py +++ b/src/analysis/backtest_cost_guard.py @@ -45,6 +45,7 @@ def validate_backtest_cost_model( raise ValueError( f"missing failure_rate_by_session for sessions: {', '.join(missing_failure)}" ) + missing_partial_fill = [s for s in required_sessions if s not in partial_fill] if missing_partial_fill: raise ValueError( diff --git a/src/analysis/backtest_pipeline.py b/src/analysis/backtest_pipeline.py index da39cc5..ad8940e 100644 --- a/src/analysis/backtest_pipeline.py +++ b/src/analysis/backtest_pipeline.py @@ -56,6 +56,10 @@ class BacktestFoldResult: train_label_distribution: dict[int, int] test_label_distribution: dict[int, int] baseline_scores: list[BaselineScore] + execution_adjusted_avg_return_bps: float + execution_adjusted_trade_count: int + execution_rejected_count: int + execution_partial_count: int @dataclass(frozen=True) @@ -90,6 +94,14 @@ def run_v2_backtest_pipeline( else sorted({bar.session_id for bar in bars}) ) validate_backtest_cost_model(model=cost_model, required_sessions=resolved_sessions) + execution_model = BacktestExecutionModel( + ExecutionAssumptions( + slippage_bps_by_session=cost_model.slippage_bps_by_session or {}, + failure_rate_by_session=cost_model.failure_rate_by_session or {}, + partial_fill_rate_by_session=cost_model.partial_fill_rate_by_session or {}, + seed=0, + ) + ) highs = [float(bar.high) for bar in bars] lows = [float(bar.low) for bar in bars] @@ -142,8 +154,32 @@ def run_v2_backtest_pipeline( if not test_labels: continue execution_model = _build_execution_model(cost_model=cost_model, fold_seed=fold_idx) + execution_return_model = _build_execution_model( + cost_model=cost_model, + fold_seed=fold_idx, + ) b0_pred = _baseline_b0_pred(train_labels) m1_pred = _m1_pred(train_labels) + execution_returns_bps: list[float] = [] + execution_rejected = 0 + execution_partial = 0 + for rel_idx in fold.test_indices: + entry_bar_index = normalized_entries[rel_idx] + bar = bars[entry_bar_index] + trade = _simulate_execution_adjusted_return_bps( + execution_model=execution_return_model, + bar=bar, + label=ordered_labels[rel_idx], + side=side, + spec=triple_barrier_spec, + commission_bps=float(cost_model.commission_bps or 0.0), + ) + if trade["status"] == "REJECTED": + execution_rejected += 1 + continue + execution_returns_bps.append(float(trade["return_bps"])) + if trade["status"] == "PARTIAL": + execution_partial += 1 fold_results.append( BacktestFoldResult( fold_index=fold_idx, @@ -189,6 +225,12 @@ def run_v2_backtest_pipeline( ), ), ], + execution_adjusted_avg_return_bps=( + mean(execution_returns_bps) if execution_returns_bps else 0.0 + ), + execution_adjusted_trade_count=len(execution_returns_bps), + execution_rejected_count=execution_rejected, + execution_partial_count=execution_partial, ) ) @@ -294,3 +336,58 @@ def _build_run_id(*, n_entries: int, n_folds: int, sessions: Sequence[str]) -> s def fold_has_leakage(fold: WalkForwardFold) -> bool: """Utility for tests/verification: True when train/test overlap exists.""" return bool(set(fold.train_indices).intersection(fold.test_indices)) + + +def _simulate_execution_adjusted_return_bps( + *, + execution_model: BacktestExecutionModel, + bar: BacktestBar, + label: int, + side: int, + spec: TripleBarrierSpec, + commission_bps: float, +) -> dict[str, float | str]: + qty = 100 + entry_req = ExecutionRequest( + side="BUY" if side == 1 else "SELL", + session_id=bar.session_id, + qty=qty, + reference_price=float(bar.close), + ) + entry_fill = execution_model.simulate(entry_req) + if entry_fill.status == "REJECTED": + return {"status": "REJECTED", "return_bps": 0.0} + + exit_qty = entry_fill.filled_qty + if label == 1: + gross_return_bps = spec.take_profit_pct * 10000.0 + elif label == -1: + gross_return_bps = -spec.stop_loss_pct * 10000.0 + else: + gross_return_bps = 0.0 + + if side == 1: + exit_price = float(bar.close) * (1.0 + gross_return_bps / 10000.0) + else: + exit_price = float(bar.close) * (1.0 - gross_return_bps / 10000.0) + + exit_req = ExecutionRequest( + side="SELL" if side == 1 else "BUY", + session_id=bar.session_id, + qty=exit_qty, + reference_price=max(0.01, exit_price), + ) + exit_fill = execution_model.simulate(exit_req) + if exit_fill.status == "REJECTED": + return {"status": "REJECTED", "return_bps": 0.0} + + fill_ratio = min(entry_fill.filled_qty, exit_fill.filled_qty) / qty + cost_bps = ( + float(entry_fill.slippage_bps) + + float(exit_fill.slippage_bps) + + (2.0 * float(commission_bps)) + ) + net_return_bps = (gross_return_bps * fill_ratio) - cost_bps + is_partial = entry_fill.status == "PARTIAL" or exit_fill.status == "PARTIAL" + status = "PARTIAL" if is_partial else "FILLED" + return {"status": status, "return_bps": net_return_bps} diff --git a/tests/test_backtest_pipeline_integration.py b/tests/test_backtest_pipeline_integration.py index d63a540..9f05b82 100644 --- a/tests/test_backtest_pipeline_integration.py +++ b/tests/test_backtest_pipeline_integration.py @@ -73,6 +73,9 @@ def test_pipeline_happy_path_returns_fold_and_artifact_contract() -> None: for score in fold.baseline_scores: assert 0.0 <= score.accuracy <= 1.0 assert 0.0 <= score.cost_adjusted_accuracy <= 1.0 + assert fold.execution_adjusted_trade_count >= 0 + assert fold.execution_rejected_count >= 0 + assert fold.execution_partial_count >= 0 def test_pipeline_cost_guard_fail_fast() -> None: @@ -211,3 +214,7 @@ def test_pipeline_fold_scores_reflect_cost_and_execution_effects() -> None: optimistic_score = optimistic_out.folds[0].baseline_scores[1].cost_adjusted_accuracy conservative_score = conservative_out.folds[0].baseline_scores[1].cost_adjusted_accuracy assert conservative_score < optimistic_score + + optimistic_avg_return = optimistic_out.folds[0].execution_adjusted_avg_return_bps + conservative_avg_return = conservative_out.folds[0].execution_adjusted_avg_return_bps + assert conservative_avg_return < optimistic_avg_return diff --git a/workflow/session-handover.md b/workflow/session-handover.md index a3fd61b..bdd2d5a 100644 --- a/workflow/session-handover.md +++ b/workflow/session-handover.md @@ -89,3 +89,19 @@ - next_ticket: #316 - process_gate_checked: process_ticket=#306,#308 merged_to_feature_branch=yes - risks_or_notes: 모니터 판정을 liveness 중심에서 policy invariant(FORBIDDEN) 중심으로 전환 + +### 2026-03-01 | session=codex-v3-stream-next-ticket +- branch: feature/v3-session-policy-stream +- docs_checked: docs/workflow.md, docs/commands.md, docs/agent-constraints.md +- open_issues_reviewed: #368, #369, #370, #371, #374, #375, #376, #377, #381 +- next_ticket: #368 +- process_gate_checked: process_ticket=#306,#308 merged_to_feature_branch=yes +- risks_or_notes: 비블로킹 소견은 합당성(정확성/안정성/유지보수성) 기준으로 반영하고, 미반영 시 근거를 코멘트로 남긴다. + +### 2026-03-01 | session=codex-issue368-start +- branch: feature/issue-368-backtest-cost-execution +- docs_checked: docs/workflow.md, docs/commands.md, docs/agent-constraints.md +- open_issues_reviewed: #368 +- next_ticket: #368 +- process_gate_checked: process_ticket=#306,#308 merged_to_feature_branch=yes +- risks_or_notes: TASK-V2-012 구현 갭 보완을 위해 cost guard + execution-adjusted fold metric + 회귀 테스트를 함께 반영한다.