fix(risk): align freeze countdown with latest close not stale 4h until

Pick the shortest active cooloff end and derive 1h/4h label from remaining time.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-18 20:27:13 +08:00
parent ff8caf7f8d
commit 9c778e0232
3 changed files with 38 additions and 8 deletions
+14 -7
View File
@@ -146,6 +146,7 @@ def _cooloff_hours_value(row) -> float:
def _resolved_cooloff_until_ms(row, now_ms: int) -> Optional[int]:
"""取仍有效的冷静期结束时刻(多源时用最短未过期时间,避免旧 4h 覆盖复盘后的 1h)。"""
raw_until = _cooloff_until_ms(row)
last = _row_get(row, "last_close_at_ms")
hours = _cooloff_hours_value(row)
@@ -161,10 +162,18 @@ def _resolved_cooloff_until_ms(row, now_ms: int) -> Optional[int]:
candidates.append(last_i + int(hours * 3600 * 1000))
except (TypeError, ValueError):
pass
if not candidates:
active = [c for c in candidates if c > now_ms]
if not active:
return None
end_ms = max(candidates)
return end_ms if end_ms > now_ms else None
return min(active)
def _freeze_tier_from_remaining_ms(remaining_ms: int) -> str:
journal_h = cooling_hours_manual_journal()
rh = remaining_ms / 3600000.0
if rh <= journal_h + (5 / 60):
return STATUS_FREEZE_1H
return STATUS_FREEZE_4H
def _ms_to_local_str(ms: Optional[int], fmt_local: Callable[[int], str]) -> Optional[str]:
@@ -524,7 +533,6 @@ def compute_account_risk_status(
now_ms = _now_ms(now)
daily_frozen = int(_row_get(row, "daily_frozen") or 0) == 1
cooloff_until_ms = _resolved_cooloff_until_ms(row, now_ms)
cooloff_hours = _row_get(row, "cooloff_hours")
manual_close_count = int(_row_get(row, "manual_close_count") or 0)
status = STATUS_NORMAL
@@ -533,9 +541,8 @@ def compute_account_risk_status(
status = STATUS_DAILY
reason = f"账户今日已冻结(手动平仓 {manual_close_count} 次或复盘情绪标签)"
elif cooloff_until_ms is not None:
h = float(cooloff_hours or cooling_hours_manual())
journal_h = cooling_hours_manual_journal()
status = STATUS_FREEZE_1H if h <= journal_h + 1e-6 else STATUS_FREEZE_4H
remaining_ms = cooloff_until_ms - now_ms
status = _freeze_tier_from_remaining_ms(remaining_ms)
until_str = _ms_to_local_str(cooloff_until_ms, fmt_local_ms) if fmt_local_ms else None
label = STATUS_LABELS[status]
reason = f"账户{label}"