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
+22
View File
@@ -214,6 +214,28 @@ class AccountRiskLibTests(unittest.TestCase):
self.assertEqual(st["status"], STATUS_FREEZE_1H)
self.assertEqual(st["cooloff_until_ms"], _local_ms(journal_at) + 3600 * 1000)
def test_stale_4h_until_with_1h_hours_uses_shorter_end(self):
"""库内 cooloff_hours=1 但 cooloff_until_ms 仍为旧 4h 时,应按 last_close+1h 倒计时。"""
conn = _mem_conn()
now = datetime(2026, 6, 14, 12, 6, 0)
now_ms = _local_ms(now)
close_ms = now_ms - 6 * 60 * 1000
stale_until_4h = close_ms + 4 * 3600 * 1000
conn.execute(
"""UPDATE account_risk_state SET
trading_day='2026-06-14',
manual_close_count=1,
cooloff_until_ms=?,
cooloff_hours=1,
last_close_at_ms=?,
daily_frozen=0
WHERE id=1""",
(stale_until_4h, close_ms),
)
st = compute_account_risk_status(conn, trading_day="2026-06-14", now=now)
self.assertEqual(st["status"], STATUS_FREEZE_1H)
self.assertAlmostEqual(st["freeze_remaining_sec"], 54 * 60, delta=3)
def test_legacy_naive_utc_ms_countdown_normalized(self):
conn = _mem_conn()
now = datetime(2026, 6, 14, 12, 0, 0)