fix: 하드닝 루프 8회차 — 체결 보정·고아 감지·청산 경보·실시간/차트·리스크 가드 일괄 수정
- 스윙 체결가 확인 전 executed_price NULL + fill_sweep, 진입가 resolve(ActiveTrade↔Portfolio), 0체결/부분체결 매수 장부 원복 - DT 고아 감지(근거 한정)·0체결 매도 복원·stale 점검 확대, 시간손절 -0.3% 원복 - 폴백 손절 ATR 캐시, 거래량부족-차단 신호 매수 차단, 현금 5% 유보 재적용, MDD peak/reset 수정 - KIS 서킷브레이커 청산 우회·경보, db_worker 재큐잉 순서, heartbeat·health-watch 사이드카 - 실시간 WS 재구독·41건 우선순위·다건 프레임 분할, 차트 샘플 제거·당일봉 합성, WS 서브프로토콜 인증 - 프론트: 미관리 그룹·확인 모달·차트 실시간 반영·예수금 표시 정정 - 전문가 에이전트 6종 추가(.claude/agents) - 유닛 스위트 2667 passed (소켓 차단 하), tsc OK Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Y83Za98XsNw9zuA4FTUytN
This commit is contained in:
@@ -408,7 +408,9 @@ async def test_m1r4_reset_mid_run_does_not_leave_stale_executed(env):
|
||||
await m.execute_reduction(_action(m))
|
||||
assert m._reduction_executed_10pct is False, "reset 이전 세대 결과로 executed 가 섰다"
|
||||
assert acct.attempts == {"005930": 1}, "reset 이후 남은 종목 매도를 중단해야 한다"
|
||||
r = await m.update(89.0)
|
||||
# [R1] reset 은 peak 를 현재 잔고(89)로 재설정 — 새 -10% 이벤트는 추가 하락 시 발생
|
||||
assert (await m.update(89.0))["action"] == "NONE"
|
||||
r = await m.update(79.0)
|
||||
assert r["action"] == "REDUCE_AND_PAUSE"
|
||||
await m.execute_reduction(_action(m))
|
||||
finally:
|
||||
@@ -430,3 +432,99 @@ async def test_l1r4_no_reduction_orders_when_market_closed(env, monkeypatch):
|
||||
monkeypatch.setattr(_ms, "is_market_open", lambda: True)
|
||||
await m.execute_reduction(_action(m))
|
||||
assert acct.sells["005930"] == [30]
|
||||
|
||||
|
||||
# ── [R1 2026-10-09] reset peak 재설정 / 갱신 신선도 ─────────────────────────────
|
||||
async def _noop_async(*a, **k):
|
||||
return None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reset_rebases_peak_so_reduction_not_retriggered(monkeypatch):
|
||||
m = mm.MDDManager()
|
||||
monkeypatch.setattr(m, "_write_state_v2", _noop_async)
|
||||
assert (await m.update(1_000_000))["action"] == "NONE"
|
||||
assert (await m.update(880_000))["action"] == "REDUCE_AND_PAUSE"
|
||||
await m.reset()
|
||||
assert m.peak_balance == 880_000
|
||||
assert (await m.update(880_000))["action"] == "NONE"
|
||||
assert await m.can_open_new_position() is True
|
||||
assert (await m.update(790_000))["action"] == "REDUCE_AND_PAUSE"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reset_without_known_balance_rebases_on_next_update(monkeypatch):
|
||||
m = mm.MDDManager()
|
||||
monkeypatch.setattr(m, "_write_state_v2", _noop_async)
|
||||
m.peak_balance = 1_000_000.0
|
||||
await m.reset()
|
||||
assert (await m.update(850_000))["action"] == "NONE"
|
||||
assert m.peak_balance == 850_000
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_reset_fail_safe_keeps_pause_and_peak(monkeypatch):
|
||||
m = mm.MDDManager()
|
||||
monkeypatch.setattr(m, "_write_state_v2", _noop_async)
|
||||
await m.update(1_000_000)
|
||||
await m.update(880_000)
|
||||
m._update_failures = 99
|
||||
await m.reset_fail_safe_v2()
|
||||
assert m.peak_balance == 1_000_000 and m.is_paused is True
|
||||
assert m._update_failures == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stale_update_blocks_entry_during_market(monkeypatch):
|
||||
from datetime import datetime, timedelta
|
||||
from app.core import market_schedule as ms
|
||||
m = mm.MDDManager()
|
||||
base = datetime(2026, 10, 7, 10, 0, tzinfo=ms.KST)
|
||||
clock = {"t": base}
|
||||
monkeypatch.setattr(mm.MDDManager, "_now_kst_v2", staticmethod(lambda: clock["t"]))
|
||||
monkeypatch.setattr(ms, "is_market_open", lambda: True)
|
||||
m._update_last_ok_at = base
|
||||
m._created_at = base
|
||||
stale = mm._stale_sec_v2()
|
||||
clock["t"] = base + timedelta(seconds=stale - 1)
|
||||
assert await m.can_open_new_position() is True
|
||||
clock["t"] = base + timedelta(seconds=stale + 1)
|
||||
assert await m.can_open_new_position() is False
|
||||
await m.record_update_success()
|
||||
assert await m.can_open_new_position() is True
|
||||
monkeypatch.setattr(ms, "is_market_open", lambda: False)
|
||||
clock["t"] = base + timedelta(hours=20)
|
||||
assert await m.can_open_new_position() is True
|
||||
|
||||
|
||||
# ── 하드닝 라운드 2 ──────────────────────────────────────────────────────────
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_r2_page_full_balance_does_not_mark_executed(env):
|
||||
"""잔고 응답이 페이지 한도에 도달하면 보이는 종목만 축소하고 executed 는 세우지 않는다."""
|
||||
_, make = env
|
||||
n = int(settings.SWING_BALANCE_FULL_PAGE_ROWS)
|
||||
acct = make(_Account({f"{i:06d}": 100 for i in range(n)}))
|
||||
m = mm.MDDManager()
|
||||
await _arm(m)
|
||||
await m.execute_reduction(_action(m))
|
||||
assert len(acct.sells) == n
|
||||
assert not getattr(m, f"_reduction_executed_{m.reduction_applied}")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_r2_below_page_still_marks_executed(env):
|
||||
_, make = env
|
||||
n = int(settings.SWING_BALANCE_FULL_PAGE_ROWS)
|
||||
make(_Account({f"{i:06d}": 100 for i in range(n - 1)}))
|
||||
m = mm.MDDManager()
|
||||
await _arm(m)
|
||||
await m.execute_reduction(_action(m))
|
||||
assert getattr(m, f"_reduction_executed_{m.reduction_applied}")
|
||||
|
||||
|
||||
def test_r2_stale_threshold_exceeds_swing_cycle_budget():
|
||||
from app.services.swing import scheduler as sw
|
||||
budget = (sw._DEFAULT_ENTRY_PHASE_TIMEOUT_SEC + sw._DEFAULT_EXIT_STEP_TIMEOUT_SEC
|
||||
+ sw._DEFAULT_BALANCE_TIMEOUT_SEC + 60.0)
|
||||
assert mm._stale_sec_v2() > budget
|
||||
|
||||
Reference in New Issue
Block a user