하네스 규칙 검사 전역 위반 117→0, 회차별 검증(code-verifier opus 4도메인)으로 Critical/High 해소. 주문·리스크 - MDD 강제축소 반복 매도(종목별 완료 집합·재시도 상한·날짜 롤오버·잔고 오류 보류·epoch·장외 미주문) - KIS 거부 FAILED 기록, 결과 불명(전송 후) 재전송 금지·PENDING, 미전송 실패만 재시도(msg_cd 분류) - 국내 전량·손실 매도 시장가, 부분 익절 직전 현재가, 지정가만 미체결 취소(cancel_order_v2: TR 0013U·조직번호) + 0체결 롤백 - 누적 4%·현금·레버리지(신규분 포함)·사이클 내 매수 누적, 수동매수 공용 가드(buy_guards), 수동매도 DT 차감·락 - 가드 우회 엔드포인트 차단(/recommendations/auto-trade 위임, US /execute 매수 410·매도 가드) - 매수 슬롯=성공 매수, 평가 상한·회전, 업무거부/계좌 쿨다운, 프로세스 내 in-flight 주문 중복 차단 데이트레이딩 - 진입·청산 shield, 청산 진행 중 표식(소유 토큰), 2단계 청산 체크, 결과 불명 → 보유 감소 판정·비DT 차감 상한 - 전일 포지션 재시도(일시 실패 미산정), 분할매도 영속, ORB 구간 손절, /cycle 락, 사이클 내 현금 차감, 체결 수량 보정 스윙 - 손절 공백(판정 예외 시 하드 바닥), 단일 손절 소스·범위 검증, 매도 직전 KIS 잔고 상한(min(주문가능,보유)−DT) - 결과 불명 체결 대조·장부 반영, 대체가(나이·진입 이후·ATR 캐시), 분할 단계 기록, 국면 실패 fail-closed - 듀얼 플레이북·서킷브레이커 게이트, 평균회귀 우선·듀얼 독립, 시간손절 거래일 - 거래 확대(사용자 결정, 백테스트 미검증): RSI 46/35 정본 경로, 모멘텀 v2 ON, SIDEWAYS ADX AND, 트레일링 1차 익절 후, 20일 시간손절 ON 인프라·보안 - 계좌·손익 GET 인증, 세션 is_active·관리자 경로, 서명키 파생, realip, WS 토큰 마스킹 - KIS 토큰 즉시 실패(sleep 제거)·무효 시 캐시 삭제, 주문 5xx+JSON 본문 반환 - db_worker 종료 drain(멱등 재실행·커밋 불명 격리·예산·밴 커밋), stop_grace 40s, Redis 타임아웃·volatile-lru, /health DLQ·캐시 - KRX 휴장일, py3.11 비호환 f-string 수정 + Jenkins 이미지 내 compileall·헬스 실패 시 빌드 실패 하네스: rules_check 에 PEP 701 f-string 검사, 마커(allow-*), deploy 스모크 401 기준 CLAUDE.md: 실제 동작·플래그·백로그(§8.5-27~38) 갱신 backend 1451 passed / hooks 112 passed / tsc ok / py3.9 compile 0 errors Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
114 lines
4.8 KiB
Python
114 lines
4.8 KiB
Python
"""종목당 4% 절대 상한(MAX_POSITION_SIZE) 회귀 테스트 — CLAUDE.md §5.1 / §4.
|
|
|
|
배경(2026-09-27): 패턴메모리 조정 뒤의 상한 재클램프가 `try` 블록 **안**에 있어
|
|
`get_pattern_adjusted_invest_amount()` 가 예외를 던지면 클램프를 건너뛰고
|
|
적응형 포지션(6~7%)이 그대로 주문 금액이 됐다. §4 는 "리스크 가드·한도 클램프를
|
|
`try` 안에 두지 말 것" 으로 이것을 규칙화했다.
|
|
|
|
여기서는 두 층을 검증한다.
|
|
1) 구조: `_execute_buy` 의 절대 상한 클램프가 패턴메모리 `try` **밖**에 있다
|
|
(예외 발생 케이스를 실제 주문 경로 없이 확정적으로 잡는다).
|
|
2) 수치: 적응형 Normal(6%)·Aggressive(7%) 산출액이 클램프 후 총자산 대비
|
|
MAX_POSITION_SIZE 를 넘지 않는다.
|
|
|
|
기대값은 전부 `settings.*` 참조다(§4.1 — config 값 하드코딩 금지).
|
|
"""
|
|
import ast
|
|
import inspect
|
|
|
|
import pytest
|
|
|
|
from app.core.config import settings
|
|
from app.services import trade_executor
|
|
from app.services.sizing_core import clamp_by_ratios
|
|
|
|
|
|
# ── 1) 구조 검증 ────────────────────────────────────────────────────────────
|
|
|
|
def _execute_buy_ast() -> ast.AST:
|
|
src = inspect.getsource(trade_executor._execute_buy)
|
|
return ast.parse(ast.unparse(ast.parse(_dedent(src))))
|
|
|
|
|
|
def _dedent(src: str) -> str:
|
|
import textwrap
|
|
return textwrap.dedent(src)
|
|
|
|
|
|
def _contains_call(node: ast.AST, name: str) -> bool:
|
|
return any(
|
|
isinstance(n, ast.Call)
|
|
and (
|
|
(isinstance(n.func, ast.Name) and n.func.id == name)
|
|
or (isinstance(n.func, ast.Attribute) and n.func.attr == name)
|
|
)
|
|
for n in ast.walk(node)
|
|
)
|
|
|
|
|
|
def _mentions_max_position_size(node: ast.AST) -> bool:
|
|
return any(
|
|
isinstance(n, ast.Attribute) and n.attr == "MAX_POSITION_SIZE"
|
|
for n in ast.walk(node)
|
|
)
|
|
|
|
|
|
def test_pattern_memory_clamp_is_outside_try():
|
|
"""패턴메모리 호출은 try 안, 절대 상한 클램프는 그 try 뒤의 문장이어야 한다."""
|
|
tree = _execute_buy_ast()
|
|
fn = tree.body[0]
|
|
assert isinstance(fn, (ast.AsyncFunctionDef, ast.FunctionDef))
|
|
|
|
body = fn.body
|
|
pm_try_idx = None
|
|
for idx, stmt in enumerate(body):
|
|
if isinstance(stmt, ast.Try) and _contains_call(stmt, "get_pattern_adjusted_invest_amount"):
|
|
pm_try_idx = idx
|
|
break
|
|
assert pm_try_idx is not None, "패턴메모리 조정 try 블록을 찾지 못했다"
|
|
|
|
# try 블록 안에는 절대 상한 클램프가 없어야 한다
|
|
assert not (
|
|
_contains_call(body[pm_try_idx], "clamp_by_ratios")
|
|
and _mentions_max_position_size(body[pm_try_idx])
|
|
), "절대 상한 클램프가 패턴메모리 try 안에 있다 (예외 시 가드가 통과된다)"
|
|
|
|
# try 이후 문장 중에 MAX_POSITION_SIZE 클램프가 있어야 한다
|
|
after = body[pm_try_idx + 1:]
|
|
assert any(
|
|
_contains_call(s, "clamp_by_ratios") and _mentions_max_position_size(s)
|
|
for s in after
|
|
), "패턴메모리 try 뒤에 MAX_POSITION_SIZE 재클램프가 없다"
|
|
|
|
|
|
def test_adaptive_position_cap_is_clamped_at_source():
|
|
"""적응형 position_size 를 소비할 때 min(..., MAX_POSITION_SIZE) 가 적용된다.
|
|
|
|
[2026-10-02 §4] 클램프가 try 안에 있으면 예외 시 건너뛰므로 try 밖에서 무조건
|
|
적용되는 형태로 바뀌었다(동작 검증은 test_swing_safety_20261002.py).
|
|
"""
|
|
src = inspect.getsource(trade_executor._execute_buy)
|
|
assert "_adaptive_position_cap = max(0.0, min(_adaptive_position_cap, settings.MAX_POSITION_SIZE))" in src
|
|
|
|
|
|
# ── 2) 수치 검증 ────────────────────────────────────────────────────────────
|
|
|
|
TOTAL_ASSETS = 10_000_000.0
|
|
AVAILABLE_CASH = 10_000_000.0
|
|
|
|
|
|
@pytest.mark.parametrize("mode_position_size", [0.06, 0.07]) # Normal / Aggressive
|
|
def test_adaptive_modes_never_exceed_hard_cap(mode_position_size):
|
|
"""적응형 6~7% 예산도 최종 클램프 후에는 MAX_POSITION_SIZE 이하."""
|
|
adaptive_cap = min(mode_position_size, settings.MAX_POSITION_SIZE)
|
|
raw = TOTAL_ASSETS * adaptive_cap
|
|
final = clamp_by_ratios(raw, TOTAL_ASSETS, [settings.MAX_POSITION_SIZE], AVAILABLE_CASH)
|
|
assert final / TOTAL_ASSETS <= settings.MAX_POSITION_SIZE + 1e-9
|
|
|
|
|
|
def test_pattern_memory_uplift_is_clamped():
|
|
"""패턴메모리가 +30% 상향해도 최종 클램프가 상한을 지킨다."""
|
|
raw = TOTAL_ASSETS * settings.MAX_POSITION_SIZE * 1.3
|
|
final = clamp_by_ratios(raw, TOTAL_ASSETS, [settings.MAX_POSITION_SIZE], AVAILABLE_CASH)
|
|
assert final / TOTAL_ASSETS <= settings.MAX_POSITION_SIZE + 1e-9
|