|
4 | 4 | from __future__ import annotations |
5 | 5 |
|
6 | 6 | import json |
| 7 | +from datetime import datetime |
7 | 8 | from collections.abc import Mapping |
8 | 9 | from typing import Any, Dict, List, Optional |
9 | 10 |
|
10 | | -from src.core.trading_calendar import MarketPhase |
| 11 | +from src.core.trading_calendar import MarketPhase, build_market_phase_context, get_market_for_stock |
11 | 12 |
|
12 | 13 |
|
13 | 14 | MARKET_PHASE_SUMMARY_KEY = "market_phase_summary" |
@@ -111,6 +112,56 @@ def extract_market_phase_summary(context_snapshot: Any) -> Optional[Dict[str, An |
111 | 112 | return render_market_phase_summary(summary) |
112 | 113 |
|
113 | 114 |
|
| 115 | +def _parse_phase_local_time(value: Any) -> Optional[datetime]: |
| 116 | + if isinstance(value, datetime): |
| 117 | + return value |
| 118 | + if isinstance(value, str): |
| 119 | + try: |
| 120 | + return datetime.fromisoformat(value) |
| 121 | + except ValueError: |
| 122 | + return None |
| 123 | + return None |
| 124 | + |
| 125 | + |
| 126 | +def rebuild_market_phase_summary_for_stock_code( |
| 127 | + stock_code: Any, |
| 128 | + context_snapshot: Any, |
| 129 | +) -> Optional[Dict[str, Any]]: |
| 130 | + """Rebuild phase summary with derived fields for JP/KR display codes. |
| 131 | +
|
| 132 | + Legacy CN snapshots on JP/KR stock records can retain CN-local values. This |
| 133 | + helper recomputes those derived fields using the target market context while |
| 134 | + preserving non-derived source fields when possible. |
| 135 | + """ |
| 136 | + summary = extract_market_phase_summary(context_snapshot) |
| 137 | + if not isinstance(summary, Mapping): |
| 138 | + return None |
| 139 | + |
| 140 | + market = get_market_for_stock(str(stock_code or "").strip()) |
| 141 | + if market not in {"jp", "kr"}: |
| 142 | + return dict(summary) |
| 143 | + |
| 144 | + phase = str(summary.get("phase", "")).strip() |
| 145 | + analysis_phase = phase if phase in _ALLOWED_PHASES else "auto" |
| 146 | + analysis_intent = str(summary.get("analysis_intent") or "auto").strip() |
| 147 | + if not analysis_intent: |
| 148 | + analysis_intent = "auto" |
| 149 | + |
| 150 | + rebuilt = build_market_phase_context( |
| 151 | + market=market, |
| 152 | + current_time=_parse_phase_local_time(summary.get("market_local_time")), |
| 153 | + trigger_source=str(summary.get("trigger_source") or "system").strip() or "system", |
| 154 | + analysis_intent=analysis_intent, |
| 155 | + analysis_phase=analysis_phase, |
| 156 | + ).to_dict() |
| 157 | + |
| 158 | + rebuilt.setdefault("warnings", list(summary.get("warnings") or [])) |
| 159 | + if not rebuilt.get("warnings"): |
| 160 | + rebuilt["warnings"] = list(summary.get("warnings") or []) |
| 161 | + |
| 162 | + return rebuilt |
| 163 | + |
| 164 | + |
114 | 165 | def normalize_analysis_phase_bucket(value: Any) -> str: |
115 | 166 | """Fold detailed phase labels into the public backtest/statistics buckets.""" |
116 | 167 | phase = _safe_text(value) |
|
0 commit comments