fix(risk): allow journal to reduce 4h cooloff to 1h without pending trade id

Hub closes and late journal saves now shorten active manual cooloffs when exit trigger and note are filled in.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-18 16:27:21 +08:00
parent e470c5952f
commit f0a158686e
3 changed files with 130 additions and 7 deletions
+75 -6
View File
@@ -174,6 +174,69 @@ def _set_cooloff(
)
def _set_cooloff_until(
conn,
*,
trading_day: str,
until_ms: int,
hours: float,
now: Optional[datetime] = None,
) -> None:
_sync_trading_day(conn, trading_day, now=now)
h = max(0.0, float(hours))
conn.execute(
"""UPDATE account_risk_state SET
cooloff_until_ms=?,
cooloff_hours=?,
updated_at=?
WHERE id=1""",
(
int(until_ms),
int(h) if h == int(h) else int(round(h)),
(now or datetime.now()).strftime("%Y-%m-%d %H:%M:%S"),
),
)
def _cooloff_until_ms(row) -> Optional[int]:
raw = _row_get(row, "cooloff_until_ms")
try:
return int(raw) if raw is not None else None
except (TypeError, ValueError):
return None
def _in_active_manual_cooloff(row, now_ms: int) -> bool:
if int(_row_get(row, "daily_frozen") or 0) == 1:
return False
if int(_row_get(row, "manual_close_count") or 0) < 1:
return False
until_ms = _cooloff_until_ms(row)
return until_ms is not None and until_ms > now_ms
def _journal_can_reduce_cooloff(row, pending, now_ms: int) -> bool:
if pending is not None:
try:
if int(pending) != 0:
return True
except (TypeError, ValueError):
return True
return _in_active_manual_cooloff(row, now_ms)
def _journal_cooloff_until_ms(row, now_ms: int, journal_hours: float) -> int:
journal_ms = int(max(0.0, float(journal_hours)) * 3600 * 1000)
last_close_ms = _row_get(row, "last_close_at_ms")
base_ms = int(last_close_ms) if last_close_ms else now_ms
until_from_close = base_ms + journal_ms
until_ms = until_from_close if until_from_close > now_ms else now_ms + journal_ms
current_until = _cooloff_until_ms(row)
if current_until is not None and current_until > now_ms and until_ms > current_until:
until_ms = current_until
return until_ms
def _set_daily_frozen(conn, *, trading_day: str, now: Optional[datetime] = None) -> None:
_sync_trading_day(conn, trading_day, now=now)
conn.execute(
@@ -299,14 +362,20 @@ def on_journal_saved(
pending = _row_get(row, "pending_journal_trade_id")
trigger = (early_exit_trigger or "").strip()
note = (early_exit_note or "").strip()
if pending and trigger == "手动平仓" and note:
last_close_ms = _row_get(row, "last_close_at_ms")
base_ms = int(last_close_ms) if last_close_ms else _now_ms(now)
_set_cooloff(
now_ms = _now_ms(now)
if (
trigger == "手动平仓"
and note
and int(_row_get(row, "daily_frozen") or 0) != 1
and _journal_can_reduce_cooloff(row, pending, now_ms)
):
journal_h = cooling_hours_manual_journal()
until_ms = _journal_cooloff_until_ms(row, now_ms, journal_h)
_set_cooloff_until(
conn,
trading_day=trading_day,
close_at_ms=base_ms,
hours=cooling_hours_manual_journal(),
until_ms=until_ms,
hours=journal_h,
now=now,
)
conn.execute(