fix: Gate force-close badge and classify midnight closes correctly
Narrow executing window to 15 minutes after the hour, show countdown when flat, run force_close before reconcile, and map 00:00 synced closes to 强制清仓 instead of 手动平仓. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,11 +2,16 @@ from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from lib.trade.force_close_lib import (
|
||||
apply_force_close_display_result,
|
||||
build_force_close_state,
|
||||
coerce_force_close_result,
|
||||
compute_next_force_close_at_ms,
|
||||
force_close_label,
|
||||
format_force_close_countdown,
|
||||
infer_force_close_result,
|
||||
is_close_at_force_close_window,
|
||||
is_force_close_active_hour,
|
||||
is_force_close_executing,
|
||||
)
|
||||
|
||||
TZ = ZoneInfo("Asia/Shanghai")
|
||||
@@ -36,14 +41,35 @@ def test_next_force_close_same_day_before_hour():
|
||||
)
|
||||
|
||||
|
||||
def test_active_hour_and_countdown():
|
||||
now = _ms(2026, 7, 6, 0, 15)
|
||||
def test_next_force_close_after_trigger_same_day():
|
||||
now = _ms(2026, 7, 7, 0, 18)
|
||||
assert compute_next_force_close_at_ms(bj_hour=0, now_ms=now, tz_name="Asia/Shanghai") == _ms(
|
||||
2026, 7, 8, 0, 0
|
||||
)
|
||||
|
||||
|
||||
def test_executing_window_and_countdown():
|
||||
now = _ms(2026, 7, 6, 0, 14)
|
||||
assert is_force_close_executing(0, now_ms=now, tz_name="Asia/Shanghai")
|
||||
assert is_force_close_active_hour(0, now_ms=now, tz_name="Asia/Shanghai")
|
||||
state = build_force_close_state(True, 0, now_ms=now, tz_name="Asia/Shanghai")
|
||||
state = build_force_close_state(
|
||||
True, 0, now_ms=now, tz_name="Asia/Shanghai", has_active_positions=True
|
||||
)
|
||||
assert state["enabled"] is True
|
||||
assert state["active"] is True
|
||||
assert state["countdown"] == "执行中"
|
||||
assert state["remaining_sec"] == 0
|
||||
assert state["next_at_ms"] == _ms(2026, 7, 7, 0, 0)
|
||||
|
||||
|
||||
def test_not_executing_after_grace_without_positions():
|
||||
now = _ms(2026, 7, 7, 0, 18)
|
||||
assert not is_force_close_executing(0, now_ms=now, tz_name="Asia/Shanghai")
|
||||
state = build_force_close_state(
|
||||
True, 0, now_ms=now, tz_name="Asia/Shanghai", has_active_positions=False
|
||||
)
|
||||
assert state["active"] is False
|
||||
assert state["countdown"] != "执行中"
|
||||
assert state["next_at_ms"] == _ms(2026, 7, 8, 0, 0)
|
||||
|
||||
|
||||
def test_disabled_state():
|
||||
@@ -55,3 +81,29 @@ def test_disabled_state():
|
||||
def test_format_countdown():
|
||||
assert format_force_close_countdown(3661) == "01:01:01"
|
||||
assert format_force_close_countdown(0, active=True) == "执行中"
|
||||
|
||||
|
||||
def test_infer_force_close_from_closed_at():
|
||||
assert is_close_at_force_close_window("2026-07-07 00:00", 0)
|
||||
assert infer_force_close_result("2026-07-07 00:00", enabled=True, bj_hour=0) == "强制清仓"
|
||||
assert infer_force_close_result("2026-07-07 00:20", enabled=True, bj_hour=0) is None
|
||||
|
||||
|
||||
def test_coerce_and_display_external_close_at_midnight():
|
||||
res, note = coerce_force_close_result(
|
||||
"外部平仓",
|
||||
"2026-07-07 00:00",
|
||||
enabled=True,
|
||||
bj_hour=0,
|
||||
)
|
||||
assert res == "强制清仓"
|
||||
assert "00:00" in note
|
||||
assert (
|
||||
apply_force_close_display_result(
|
||||
"手动平仓",
|
||||
"2026-07-07 00:00",
|
||||
enabled=True,
|
||||
bj_hour=0,
|
||||
)
|
||||
== "强制清仓"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user