fix: 매매 파라미터 3중 소스 정합 + 리스크 가드 복원 + 죽은 코드 2,695줄 제거

## 배경
"전략이 혼란스럽고 현재와 맞지 않는다"를 조사한 결과, 전략 설계가 아니라
전략을 검증했다는 도구와 파라미터 소스가 어긋나 있었다.

## 파라미터 소스 단일화
- 우선순위가 adaptive > strategy_cfg(DB) > config.py 인데 문서는 맨 마지막인
  config.py 를 기술하고 있었다. 11곳이 불일치(BULL RSI 46 vs DB 30, SL -7% vs -6%).
- DEFAULT_PARAMS 를 config.py 에 정합 + alembic 0012 로 기존 DB 행 갱신
  (운영자가 UI 로 바꾼 값은 보존, 옛 기본값과 같은 행만 갱신, 테이블 가드·멱등)

## 백테스트를 실전 로직에 정합
- calculate_adx_simple 이 정확히 period 배(14.0×) 발산 → Aggressive 게이트
  `ADX>25` 가 전 레짐 100% 통과. 전환 빈도 5.6%→2.0%(-65%), 성과 상방 편향
- 백테스트 RSI 는 SMA, 실전은 Wilder → 진입 판정 10~11% 불일치
- us_backtest_engine RSI 1봉 지연 제거 (룩어헤드 없음을 절단 테스트로 증명)
- backend/backtest_results/ 25건은 폐기 대상 (§8.4)

## 리스크 가드 복원
- 4% 절대 상한이 try 블록 안에 있어 예외 시 적응형 6~7% 가 통과 → try 밖으로
- 청산을 매수보다 먼저 평가 (보유 종목이 추천에 들면 손절 대신 물타기가 나갔다)
- 리스크 가드 4개를 매수 전용화 → MDD -10%·보유한도 도달 시에도 손절 작동
- MDD 갱신 5회 연속 실패 시 신규 진입 차단 fail-safe (청산은 계속 허용)
- 스윙 청산이 DT 보유분까지 매도하던 문제, sell_pct 기준 혼재 정정

## 죽은 코드 제거 (2,695줄)
- short_term_strategy, kosdaq_scanner, app/strategy/ 패키지
- app/trading/ + korea_investment_api_adapter (real_market_api 정본은
  korea_investment_api.py:1482, adapter 는 import 0건)

## 지표·사이징 단일화 (수치 무변경 증명)
- indicators_core.py: 3,274건 비교 최대 오차 0.000e+00
- sizing_core.py: 42,105건 비교 불일치 0
- 미국 전략 상수 13개 config 편입 (hex 까지 동일, 3,168 케이스 전수 비교)

## 테스트 안전 (실 KIS 호출 차단)
- 유닛 테스트가 실제 KIS 에 매도 주문을 2회 전송하고 있었다. 리팩터링이
  close_stale_positions 의 대상을 _execute_exit → _execute_exit_stale 로
  바꿨는데 목이 전자에 걸려 빗나갔다. test_api_endpoints 도 캡처된 싱글톤
  참조 누수로 실제 잔고 조회를 타 141초를 소비했다.
- backend/tests/conftest.py 신설: 주문·토큰·시세·잔고를 호출 즉시 예외로 차단.
  예외는 BaseException 파생(프로덕션 except Exception 폴백에 삼켜지지 않게),
  클래스 메서드에 적용(캡처된 참조까지 덮음), DATABASE_URL `_test` 가드 강제
- 전체 유닛 스위트: 365초/6 failed → 11.8초/0 failed (소켓 차단 하 559 passed)

## 신규 (둘 다 플래그 OFF, 백테스트 미검증)
- range_trading_strategy.py — SIDEWAYS 담당 전략이 없던 빈 칸. 미배선
- check_bear_buy_signal_v2 — BEAR 4중 AND(발화율 2.3%) → 점수제(12.4%)

## 문서
- CLAUDE.md 를 실제 코드 기준으로 재작성. §0 파라미터 우선순위, §4.1 테스트 규약,
  §6.4 문서화되지 않았던 진입 차단 게이트 9종, §8.4 백테스트 폐기 근거,
  §8.5 미해결 19항목
- 루트 md 6개 → 2개(docs/ 이동), 중복 배치 문서 병합, 스테일 worktree 제거

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-28 08:58:18 +09:00
parent b4608e5abf
commit 1b9257c4d2
71 changed files with 5510 additions and 3685 deletions

View File

@@ -25,22 +25,45 @@ S&P 500 / NASDAQ 100 기반 평균회귀 + 모멘텀 복합 전략
import logging
from typing import Optional, Dict, List
from app.core.config import settings
from app.services.mean_reversion_strategy import ExitSignal
# §8.5-2 포지션 사이징 단일 출처 (순수 계산 모듈)
from app.services.sizing_core import budget_by_ratios
logger = logging.getLogger(__name__)
# ── 상수 ────────────────────────────────────────────────────────────────────────
US_RSI_BUY_THRESHOLD = 35.0 # RSI 매수 기준 (국내 30 대비 여유)
US_BB_LOWER_MARGIN_LOW = 0.97 # BB 하단 -3%
US_BB_LOWER_MARGIN_HIGH = 1.02 # BB 하단 +2%
US_MIN_PRICE_USD = 4.0 # 페니스탁 필터
US_STOP_LOSS_PCT = -7.0 # 손절 -7%
US_ATR_STOP_MULT = 2.5 # ATR 손절 배수
US_FIRST_EXIT_RSI = 55.0 # 1차 익절 RSI 기준
US_FIRST_EXIT_SELL_PCT = 0.30 # 1차 익절 매도 비율 (30%)
US_TRAILING_STOP_PCT = 0.04 # 트레일링 스탑 -4%
US_MAX_PER_POSITION = 0.05 # 종목당 최대 5%
US_MAX_CAPITAL_RATIO = 0.30 # 미국 주식 총 비중 최대 30%
# [§8.5-8 상수 출처 단일화 2026-09-27]
# **config.py(settings) 가 정본이다.** 과거 이 자리에 있던 모듈 상수 11개는 전부
# config 로 옮겼고(값 변경 없음), 아래 이름들은 기존 호출부 호환을 위한 *얇은 별칭*이다.
# - 별칭은 모듈 `__getattr__`(PEP 562)로 구현해 **접근 시점에** settings 를 읽는다.
# import 시점에 값을 고정하지 않으므로 런타임 설정 변경(.env/DB 오버라이드)이 반영된다.
# 단 `from ... import US_X` 로 받아 모듈 전역에 묶어두는 호출부
# (us_backtest_engine, tests)는 그 시점 값으로 고정된다 — 이는 원래부터의 한계.
# - 이 모듈 내부 로직은 별칭을 쓰지 않고 settings 를 직접 읽는다.
# - US_MAX_CAPITAL_RATIO 는 원래 모듈 상수(0.30)와 config 키(0.30)가 완전 중복이었고
# 값이 같아 config 쪽을 정본으로 채택했다.
_SETTINGS_ALIASES = {
"US_RSI_BUY_THRESHOLD": "US_RSI_BUY_THRESHOLD",
"US_BB_LOWER_MARGIN_LOW": "US_BB_LOWER_MARGIN_LOW",
"US_BB_LOWER_MARGIN_HIGH": "US_BB_LOWER_MARGIN_HIGH",
"US_MIN_PRICE_USD": "US_MIN_PRICE_USD",
"US_STOP_LOSS_PCT": "US_STOP_LOSS_PCT", # 음수 규약(-7.0) 유지
"US_ATR_STOP_MULT": "US_ATR_STOP_MULT",
"US_FIRST_EXIT_RSI": "US_FIRST_EXIT_RSI",
"US_FIRST_EXIT_SELL_PCT": "US_FIRST_EXIT_SELL_PCT",
"US_TRAILING_STOP_PCT": "US_TRAILING_STOP_PCT",
"US_MAX_PER_POSITION": "US_MAX_POSITION_SIZE", # 모듈 상수명 ≠ config 키명
"US_MAX_CAPITAL_RATIO": "US_MAX_CAPITAL_RATIO",
}
def __getattr__(name: str):
"""모듈 상수 별칭 → settings 지연 조회 (PEP 562)."""
key = _SETTINGS_ALIASES.get(name)
if key is None:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
return getattr(settings, key)
def check_us_buy_signal(
@@ -80,10 +103,11 @@ def check_us_buy_signal(
# 페니스탁 필터
# [수정 10] trade_executor.py:1102의 `<= settings.MIN_PRICE_USD` 와 경계 조건 통일.
# CLAUDE.md 5.4: "$4 초과" → $4.00 이하(=포함)는 진입 불가.
if current_price_usd <= US_MIN_PRICE_USD:
min_price_usd = settings.US_MIN_PRICE_USD
if current_price_usd <= min_price_usd:
logger.debug(
"US BUY skip: price $%.2f <= $%.2f (penny stock filter)",
current_price_usd, US_MIN_PRICE_USD,
current_price_usd, min_price_usd,
)
return None
@@ -92,7 +116,8 @@ def check_us_buy_signal(
return None
# RSI 과매도 확인
if rsi > US_RSI_BUY_THRESHOLD:
rsi_threshold = settings.US_RSI_BUY_THRESHOLD
if rsi > rsi_threshold:
return None
# RSI 반등 확인 (rsi_prev 제공 시, RSI 하락 중이면 진입 차단)
@@ -104,8 +129,8 @@ def check_us_buy_signal(
return None
# BB 하단 구간 확인 (-3% ~ +2%)
bb_low_minus = bb_lower * US_BB_LOWER_MARGIN_LOW
bb_low_plus = bb_lower * US_BB_LOWER_MARGIN_HIGH
bb_low_minus = bb_lower * settings.US_BB_LOWER_MARGIN_LOW
bb_low_plus = bb_lower * settings.US_BB_LOWER_MARGIN_HIGH
if not (bb_low_minus <= close <= bb_low_plus):
return None
@@ -119,7 +144,7 @@ def check_us_buy_signal(
"market": "US",
"split_plan": [0.50, 0.50], # 2회 분할 매수
"description": (
f"RSI({rsi:.1f})<={US_RSI_BUY_THRESHOLD:.0f} + "
f"RSI({rsi:.1f})<={rsi_threshold:.0f} + "
f"BB 하단 -3%~+2% (${current_price_usd:.2f})"
f"{reversal_info}"
),
@@ -159,8 +184,10 @@ def check_us_exit_signal(
ExitSignal 또는 None
"""
# ── 1. 손절 (최우선) ──────────────────────────────────────────────
atr_stop = entry_price - US_ATR_STOP_MULT * atr
pct_stop = entry_price * (1 + US_STOP_LOSS_PCT / 100) # e.g. entry * 0.93
# 값·수식 모두 편입 전과 동일(settings 가 정본). SL 은 가격 기준이므로
# max() 가 타이트한 쪽 — 방향 변경 금지(CLAUDE.md §8.1-5).
atr_stop = entry_price - settings.US_ATR_STOP_MULT * atr
pct_stop = entry_price * (1 + settings.US_STOP_LOSS_PCT / 100) # 음수 규약 → e.g. entry * 0.93
stop_price = max(atr_stop, pct_stop) # 높은 값 = 타이트한 손절
if close <= stop_price:
@@ -176,27 +203,29 @@ def check_us_exit_signal(
# ── 2. 1차 익절: remaining_pct > 0.7 (아직 1차 안 했을 때) ─────────
# 최소 1×ATR 수익 조건
min_profit_price = entry_price + 1.0 * atr
first_exit_rsi = settings.US_FIRST_EXIT_RSI
if remaining_pct > 0.7:
if rsi >= US_FIRST_EXIT_RSI and close > bb_middle and close >= min_profit_price:
if rsi >= first_exit_rsi and close > bb_middle and close >= min_profit_price:
return ExitSignal(
action="SELL_PARTIAL",
reason=(
f"US 1차 익절(30%): RSI({rsi:.1f})>={US_FIRST_EXIT_RSI:.0f} + "
f"US 1차 익절(30%): RSI({rsi:.1f})>={first_exit_rsi:.0f} + "
f"BB중앙(${bb_middle:.2f}) 상회 + 1×ATR(${atr:.2f}) 수익"
),
sell_pct=US_FIRST_EXIT_SELL_PCT,
sell_pct=settings.US_FIRST_EXIT_SELL_PCT,
)
# ── 3. 2차 익절: 트레일링 스탑 -4% ──────────────────────────────────
if remaining_pct <= 0.7:
if trailing_high is not None and trailing_high > entry_price:
trail_stop = trailing_high * (1 - US_TRAILING_STOP_PCT)
trail_pct = settings.US_TRAILING_STOP_PCT
trail_stop = trailing_high * (1 - trail_pct)
if close < trail_stop:
return ExitSignal(
action="SELL_ALL",
reason=(
f"US 트레일링 청산: close=${close:.2f} < "
f"최고가×{1 - US_TRAILING_STOP_PCT:.2f}=${trail_stop:.2f} "
f"최고가×{1 - trail_pct:.2f}=${trail_stop:.2f} "
f"(최고가=${trailing_high:.2f})"
),
sell_pct=1.0,
@@ -221,8 +250,11 @@ def calculate_us_position_size(
available_cash_krw: float,
current_us_allocation_krw: float,
exchange_rate: float,
max_per_position: float = US_MAX_PER_POSITION,
max_us_capital: float = US_MAX_CAPITAL_RATIO,
# 기본값은 config 에서 읽는다(값은 편입 전과 동일: 0.05 / 0.30).
# 시그니처 변경 금지 규칙 때문에 기본값만은 import 시점에 고정된다 — 런타임 오버라이드가
# 필요한 호출부는 인자를 명시적으로 넘길 것.
max_per_position: float = settings.US_MAX_POSITION_SIZE,
max_us_capital: float = settings.US_MAX_CAPITAL_RATIO,
) -> float:
"""미국 주식 포지션 사이징 (KRW 기준 반환)
@@ -242,13 +274,18 @@ def calculate_us_position_size(
if total_assets_krw <= 0 or exchange_rate <= 0:
return 0.0
# 종목당 최대 금액
max_by_position = total_assets_krw * max_per_position
# 미국 주식 잔여 예산
us_budget_remaining = (total_assets_krw * max_us_capital) - current_us_allocation_krw
result = max(0.0, min(max_by_position, us_budget_remaining, available_cash_krw))
# [§8.5-2 통합 2026-09-27] 3중 min 식을 sizing_core.budget_by_ratios 로 위임.
# = max(0, min(종목당 최대, 미국 잔여 예산, 가용현금)) — 수식·금액 동일.
# (위 total<=0 / exchange_rate<=0 가드는 이 모듈 고유 로직이라 남긴다.)
max_by_position = total_assets_krw * max_per_position # 로그용
us_budget_remaining = (total_assets_krw * max_us_capital) - current_us_allocation_krw # 로그용
result = budget_by_ratios(
total_assets=total_assets_krw,
available_cash=available_cash_krw,
current_allocation=current_us_allocation_krw,
max_per_position=max_per_position,
max_strategy_ratio=max_us_capital,
)
logger.debug(
"US position_size: total=%.0f KRW, max_pos=%.0f, us_remaining=%.0f, cash=%.0f → %.0f KRW (≈$%.0f)",