diff --git a/backend/app/api/endpoints/day_trading.py b/backend/app/api/endpoints/day_trading.py index 34be1b4..1590190 100644 --- a/backend/app/api/endpoints/day_trading.py +++ b/backend/app/api/endpoints/day_trading.py @@ -74,6 +74,12 @@ def _today_kst() -> date: return datetime.now(KST).date() +def _is_new_entry_allowed() -> bool: + """신규 진입 마감 시간(CLAUDE.md 7.3: 14:30, settings.DAY_TRADE_ENTRY_CUTOFF_HOUR/MIN) + 이전인지 확인. 청산 경로에는 영향 없음 — 신규 진입 체크에서만 사용.""" + return _kst_time() < ENTRY_CUTOFF + + # 감시 종목 (code → name) _watchlist: Dict[str, str] = {} @@ -176,8 +182,8 @@ async def _get_portfolio_summary(force: bool = False) -> Dict[str, float]: try: balance = await korea_investment_api.get_balance() - output2 = balance.get("output2", [{}]) - summary = output2[0] if output2 else {} + # output2가 빈 리스트([])로 오는 경우까지 방어 (get의 default는 키가 없을 때만 적용됨) + summary = (balance.get("output2") or [{}])[0] total_assets = float(summary.get("tot_evlu_amt", 0) or 0) available_cash = float(summary.get("prvs_rcdl_excc_amt", 0) or 0) _portfolio_summary_cache = {"total_assets": total_assets, "available_cash": available_cash} @@ -339,6 +345,11 @@ async def run_day_trading_cycle() -> Dict[str, Any]: result["phase"] = "active" return result + if not _is_new_entry_allowed(): + # 14:30(DAY_TRADE_ENTRY_CUTOFF) 이후 — 신규 진입 마감 (CLAUDE.md 7.3), 청산은 위에서 이미 처리됨 + result["phase"] = "entry_cutoff" + return result + if len(_positions) < MAX_POSITIONS and settings.AUTO_TRADE_ENABLED: portfolio = await _get_portfolio_summary() total_assets = portfolio["total_assets"] @@ -846,6 +857,8 @@ async def execute_day_trade(req: ExecuteRequest): raise HTTPException(400, f"최대 포지션 수({MAX_POSITIONS}종목)에 도달했습니다.") if not is_day_trade_entry_window(): raise HTTPException(400, "데이트레이딩 진입 시간이 아닙니다 (09:30~15:00).") + if not _is_new_entry_allowed(): + raise HTTPException(400, f"신규 진입 마감 시간({ENTRY_CUTOFF.strftime('%H:%M')}) 이후에는 진입할 수 없습니다.") portfolio = await _get_portfolio_summary() total_assets = portfolio["total_assets"] diff --git a/backend/app/api/endpoints/portfolio.py b/backend/app/api/endpoints/portfolio.py index 750dfaf..09c0d4e 100644 --- a/backend/app/api/endpoints/portfolio.py +++ b/backend/app/api/endpoints/portfolio.py @@ -495,6 +495,15 @@ async def sync_portfolio(db: AsyncSession = Depends(get_db), force: bool = True) result = await db.execute(select(Portfolio).where(Portfolio.quantity > 0)) portfolio_cache.holdings = list(result.scalars().all()) logger.info(f"📦 DB에서 포트폴리오 로드: {len(portfolio_cache.holdings)}종목") + # summary는 절대 None으로 덮어쓰지 않는다 — 마지막 성공 summary를 그대로 보존한다. + # 단, 최초 동기화부터 실패하여 summary가 아직 한 번도 채워진 적 없다면(None) + # 현금 계산이 영영 막히지 않도록 holdings 기반 부분값(available_cash=0)이라도 채워둔다. + if portfolio_cache.summary is None: + try: + await portfolio_cache.cache_summary() # available_cash=0이지만 holdings는 즉시 반영 + logger.info("📦 summary 최초 미존재 — holdings 기반 부분값으로 채움 (available_cash=0)") + except Exception as summary_err: + logger.warning(f"⚠️ 부분 summary 생성 실패 (summary는 None 유지): {summary_err}") return {"message": f"Balance API error: {error_msg}", "cached": True, "count": len(portfolio_cache.holdings)} portfolio_cache.record_success() @@ -1370,9 +1379,9 @@ async def sell_stock( logger.info(f"✅ 수동 매도 완료: {stock_name} {quantity}주 @ {executed_price:,.0f}원 (손익: {profit_loss:+,.0f}원)") - # 매매일지 백그라운드 생성 + # 매매일지 백그라운드 생성 — trade_id 미확보이므로 0 전달 asyncio.create_task(_create_journal_background( - trade_id=trade.id, + trade_id=0, stock_code=stock_code, stock_name=stock_name, order_type="SELL", @@ -1403,6 +1412,7 @@ class ActiveTradeStatusUpdateRequest(BaseModel): status: str # 'active', 'pending_sell', 'closed' reason: Optional[str] = "" updated_at: Optional[str] = None + remaining_pct: Optional[float] = None # 부분 매도 후 잔여 비율 (예: 0.5). 제공 시 ActiveTrade.remaining_pct 갱신 @router.patch("/active-trades/{stock_code}/status") @@ -1465,6 +1475,10 @@ async def update_active_trade_status( active_trade.status = new_status active_trade.updated_at = datetime.fromisoformat(request.updated_at) if request.updated_at else datetime.utcnow() + # 부분 매도 시 잔여 비율 갱신 (CLAUDE.md 5.2: +2R 50% 분할 매도 후 잔량 추적 유지) + if request.remaining_pct is not None: + active_trade.remaining_pct = Decimal(str(round(request.remaining_pct, 4))) + db.add(active_trade) await db.commit() await db.refresh(active_trade) @@ -1475,7 +1489,8 @@ async def update_active_trade_status( "old_status": old_status, "new_status": new_status, "reason": reason, - "updated_at": active_trade.updated_at.isoformat() + "updated_at": active_trade.updated_at.isoformat(), + "remaining_pct": float(active_trade.remaining_pct) if active_trade.remaining_pct is not None else None } except HTTPException: diff --git a/backend/app/core/config.py b/backend/app/core/config.py index f413747..78a65fe 100644 --- a/backend/app/core/config.py +++ b/backend/app/core/config.py @@ -148,6 +148,11 @@ class Settings(BaseSettings): MIN_PRICE_KRW: int = 5000 # 국내 5,000원 이하 매수 금지 MIN_PRICE_USD: float = 4.0 # 미국 $4 이하 매수 금지 + # 최소 매수 금액 가드 (수수료 대비 비효율 방지, trade_executor._execute_buy) + # 큰 계좌는 이 값이 그대로 하한으로 사용되고, 소액 계좌는 자산 대비로 완화된다 + # (trade_executor._MIN_BUY_AMOUNT 산출 로직 참고). + MIN_BUY_AMOUNT_KRW: float = 500_000 # 기존 하드코딩 _MIN_BUY_AMOUNT와 동일한 기본값 + # 연속 손실 관리 (섹션 5.5) CONSECUTIVE_LOSS_BAN_COUNT: int = 3 # 3회 연속 손실 CONSECUTIVE_LOSS_BAN_DAYS: int = 30 # 30거래일 진입 금지 @@ -309,7 +314,7 @@ class Settings(BaseSettings): API_RETRY_BACKOFF: float = 2.0 # 지수 백오프 배율 API_MIN_REQUEST_INTERVAL: float = 0.15 # 최소 요청 간격 (초) — 기존 0.5→0.15 (캐시+커넥션풀로 부하 감소) API_MAX_CONCURRENT_REQUESTS: int = 8 # 동시 최대 요청 수 (기존 4→8, 커넥션풀로 안정적) - API_TIMEOUT: float = 10.0 # API 타임아웃 (초) — 기존 30→10 (블로킹 방지) + API_TIMEOUT: float = 20.0 # API 타임아웃 (초) — 기존 30→10→20 (KIS 느린 조회가 10초 넘어 ReadTimeout 폭증 완화, connect는 10초 유지) API_ERROR_THRESHOLD: float = 0.3 # 오류율 임계값 (30%) API_MONITORING_WINDOW: int = 100 # 모니터링 윈도우 (최근 N개 요청) diff --git a/backend/app/services/adaptive_strategy.py b/backend/app/services/adaptive_strategy.py index 4b1414a..664061b 100644 --- a/backend/app/services/adaptive_strategy.py +++ b/backend/app/services/adaptive_strategy.py @@ -630,7 +630,8 @@ async def refresh_adaptive_params() -> AdaptiveParams: chart_data = None try: from app.services.naver_finance import get_naver_daily_chart - naver_data = await get_naver_daily_chart("069500", days=120) + # ATR 백분위 산출 히스토리 확보 — 캘린더일 기준 400일 요청(≈270거래일, 백테스트와 정합) + naver_data = await get_naver_daily_chart("069500", days=400) if naver_data and len(naver_data) >= 60: chart_data = list(reversed(naver_data)) # newest first -> list except Exception: @@ -672,7 +673,7 @@ async def refresh_adaptive_params() -> AdaptiveParams: try: from app.services.korea_investment_api import korea_investment_api bal = await korea_investment_api.get_balance() - current_balance = float(bal.get("output2", [{}])[0].get("tot_evlu_amt", 0) or 0) + current_balance = float((bal.get("output2") or [{}])[0].get("tot_evlu_amt", 0) or 0) if current_balance > 0: mdd_val = ((current_balance - mdd_manager.peak_balance) / mdd_manager.peak_balance * 100) @@ -750,15 +751,22 @@ def get_current_adaptive_params() -> AdaptiveParams: return _current_params # TTL 초과 — 비동기 갱신 트리거 (호출자를 블록하지 않음) try: - loop = asyncio.get_event_loop() - if loop.is_running(): - asyncio.ensure_future(refresh_adaptive_params()) + # get_event_loop()는 실행 중인 루프가 없는 동기 컨텍스트에서 예외/경고를 + # 유발할 수 있으므로 get_running_loop()를 우선 사용하고, 실행 중인 루프가 + # 없으면(RuntimeError) 백그라운드 갱신을 조용히 스킵한다. + asyncio.get_running_loop() + asyncio.ensure_future(refresh_adaptive_params()) + except RuntimeError: + pass # 실행 중인 이벤트 루프 없음 — 동기 컨텍스트에서 호출된 경우 except Exception: pass return _current_params # 캐시 없음 — fallback + # [FIX] _last_update_ts를 갱신하지 않으면 다음 호출에서 age가 (monotonic()-0.0)으로 + # 거대해져 매번 TTL 초과로 판정되고 refresh_adaptive_params()가 남발된다. _current_params = _get_fallback_params() + _last_update_ts = time.monotonic() return _current_params @@ -796,17 +804,18 @@ def force_mode_override(mode: str, reason: str = "") -> None: return params_dict = _MODE_PARAMS[mode] + # [FIX] 이전에는 dataclass 필드 일부만 채워 나머지(특히 max_daily_trades)가 기본값(5)으로 + # 남았다. 예: crisis 오버라이드가 max_daily_trades=0을 의도했지만 실제로는 5가 적용되어 + # 진입 차단이 되지 않았다. get_adaptive_params()와 동일하게 _MODE_PARAMS 전체를 + # **params_dict로 펼쳐 계산된 모드와 완전히 동일한 필드 구성을 보장한다. + import pytz as _pytz _current_params = AdaptiveParams( trading_mode=mode, - rsi_entry=params_dict["rsi_entry"], - position_size=params_dict["position_size"], - stop_loss_pct=params_dict["stop_loss_pct"], - stop_loss_high_vol=params_dict.get("stop_loss_high_vol", params_dict["stop_loss_pct"] - 3.0), - atr_stop_multiplier=params_dict["atr_stop_multiplier"], - rsi_partial_exit=params_dict["rsi_partial_exit"], - rsi_full_exit=params_dict["rsi_full_exit"], - daytrading_enabled=params_dict["daytrading_enabled"], + condition=MarketCondition(trading_mode=mode), + mdd_current=_current_params.mdd_current if _current_params else 0.0, + updated_at=datetime.now(_pytz.timezone("Asia/Seoul")).strftime("%Y-%m-%d %H:%M:%S"), fallback_used=False, + **params_dict, ) _last_update_ts = time.monotonic() @@ -816,9 +825,12 @@ def force_mode_override(mode: str, reason: str = "") -> None: # Redis에 이력 기록 try: - loop = asyncio.get_event_loop() - if loop.is_running(): - asyncio.ensure_future(_record_mode_change("override", mode, MarketCondition(trading_mode=mode))) + # get_event_loop()는 실행 중인 루프가 없는 동기 컨텍스트에서 예외/경고를 + # 유발할 수 있으므로 get_running_loop()를 우선 사용한다. + asyncio.get_running_loop() + asyncio.ensure_future(_record_mode_change("override", mode, MarketCondition(trading_mode=mode))) + except RuntimeError: + pass # 실행 중인 이벤트 루프 없음 — 동기 컨텍스트에서 호출된 경우 except Exception: pass diff --git a/backend/app/services/daytrading/entry_service.py b/backend/app/services/daytrading/entry_service.py index 51f4ce6..cbb26a5 100644 --- a/backend/app/services/daytrading/entry_service.py +++ b/backend/app/services/daytrading/entry_service.py @@ -250,8 +250,8 @@ async def scan_and_enter() -> list[dict]: # 포트폴리오 총자산 조회 (캐시 활용) try: balance = await korea_investment_api.get_balance() - output2 = balance.get("output2", [{}]) - summary = output2[0] if output2 else {} + # output2가 빈 리스트([])로 오는 경우까지 방어 (get의 default는 키가 없을 때만 적용됨) + summary = (balance.get("output2") or [{}])[0] total_assets = float(summary.get("tot_evlu_amt", 0) or 0) available_cash = float(summary.get("prvs_rcdl_excc_amt", 0) or 0) day_trading_state.last_total_assets = total_assets @@ -473,6 +473,8 @@ async def _try_enter( "entry_price": current_price, "strategy_name": "day_trade", "entry_reason": signal.reason, + "stop_loss": signal.stop_loss, + "take_profit": signal.target_price, }) logger.info( diff --git a/backend/app/services/korea_investment_api.py b/backend/app/services/korea_investment_api.py index 7b204e7..55d8e85 100644 --- a/backend/app/services/korea_investment_api.py +++ b/backend/app/services/korea_investment_api.py @@ -13,6 +13,7 @@ from typing import Dict, Any, Optional, Deque from datetime import datetime, timedelta import logging import asyncio +import random import time import json import os @@ -194,6 +195,9 @@ class KoreaInvestmentAPI: instance.access_token = None instance.token_expires_at = None instance.last_request_time = None + instance._throttle_lock = asyncio.Lock() + instance._http_client = None + instance._http_client_lock = asyncio.Lock() instance.TOKEN_CACHE_FILE = Path.home() / ".cache" / "auto-stock" / "kis_token_cache_real.json" instance.min_request_interval = settings.API_MIN_REQUEST_INTERVAL instance.max_retries = settings.API_MAX_RETRIES @@ -203,7 +207,7 @@ class KoreaInvestmentAPI: instance.semaphore = asyncio.Semaphore(settings.API_MAX_CONCURRENT_REQUESTS) instance._token_lock = asyncio.Lock() instance.monitor = APICallMonitor(window_size=settings.API_MONITORING_WINDOW) - instance.circuit_breaker = CircuitBreaker(failure_threshold=15, recovery_timeout=120) + instance.circuit_breaker = CircuitBreaker(failure_threshold=7, recovery_timeout=120) logger.info( "🔧 KIS 실계좌 시세조회 API 초기화: base_url=%s, app_key=%s...", instance.base_url, instance.app_key[:6] @@ -219,6 +223,9 @@ class KoreaInvestmentAPI: self.access_token: Optional[str] = None self.token_expires_at: Optional[datetime] = None self.last_request_time: Optional[float] = None + # 스로틀 계산(간격 측정+sleep+last_request_time 갱신)을 직렬화하는 락 + # (thundering herd 방지: 여러 코루틴이 동시에 깨어나 동시에 요청 쏘는 것 방지) + self._throttle_lock = asyncio.Lock() # 설정 기반 쓰로틀링 파라미터 self.min_request_interval = settings.API_MIN_REQUEST_INTERVAL # 기본 0.5초 (초당 2회) @@ -240,8 +247,8 @@ class KoreaInvestmentAPI: # 모니터링 self.monitor = APICallMonitor(window_size=settings.API_MONITORING_WINDOW) - # Circuit Breaker (5회 연속 실패 → 10분 차단) - self.circuit_breaker = CircuitBreaker(failure_threshold=15, recovery_timeout=120) + # Circuit Breaker (7회 연속 실패 → 2분 차단) + self.circuit_breaker = CircuitBreaker(failure_threshold=7, recovery_timeout=120) # 시작 시 설정 로깅 (키 마스킹) logger.info(f"🔧 KIS API 초기화: mode={settings.KIS_MODE}, " @@ -446,13 +453,16 @@ class KoreaInvestmentAPI: for attempt in range(self.max_retries + 1): try: # 쓰로틀링: 최소 요청 간격 보장 - if self.last_request_time: - elapsed = time.time() - self.last_request_time - if elapsed < self.min_request_interval: - await asyncio.sleep(self.min_request_interval - elapsed) - - self.last_request_time = time.time() - + # _throttle_lock으로 "간격 계산 + sleep + last_request_time 갱신"을 직렬화하여 + # 여러 코루틴이 락 없이 동시에 깨어나 한꺼번에 요청을 쏘는 thundering herd를 방지한다. + async with self._throttle_lock: + if self.last_request_time: + elapsed = time.time() - self.last_request_time + if elapsed < self.min_request_interval: + await asyncio.sleep(self.min_request_interval - elapsed) + + self.last_request_time = time.time() + # 토큰 갱신 token = await self._get_access_token() @@ -474,6 +484,7 @@ class KoreaInvestmentAPI: method, f"{self.base_url}{endpoint}", headers=headers, + timeout=timeout, **kwargs ) @@ -490,7 +501,7 @@ class KoreaInvestmentAPI: elif response.status_code in [429, 500, 502, 503, 504]: retry_count += 1 if attempt < self.max_retries: - delay = self.retry_delay * (self.retry_backoff ** attempt) + delay = self.retry_delay * (self.retry_backoff ** attempt) + random.uniform(0, 0.3) logger.warning(f"⏳ API 재시도 {attempt + 1}/{self.max_retries}: {response.status_code}, {delay:.1f}초 후") await asyncio.sleep(delay) continue @@ -511,7 +522,7 @@ class KoreaInvestmentAPI: last_exception = e if attempt < self.max_retries: - delay = self.retry_delay * (self.retry_backoff ** attempt) + delay = self.retry_delay * (self.retry_backoff ** attempt) + random.uniform(0, 0.3) logger.warning(f"⏳ API 타임아웃, {delay:.1f}초 후 재시도 ({attempt + 1}/{self.max_retries})") await asyncio.sleep(delay) continue @@ -930,12 +941,13 @@ class KoreaInvestmentAPI: ) -> Dict[str, Any]: """해외주식 주문 (미국주식 기준)""" try: + order_type = order_type.upper() # 방어적 정규화: 소문자 "buy"/"sell" 전달 시 잘못된 TR_ID 방지 account_number_prefix = self.account_number[:8] account_number_suffix = self.account_number[8:] - + # 모의/실전 구분 is_virtual = "vts" in self.base_url.lower() - + # TR_ID 설정 (미국 매수/매도) if order_type == "BUY": tr_id = "VTTT1002U" if is_virtual else "TTTT1002U" @@ -990,8 +1002,8 @@ class KoreaInvestmentAPI: "CTX_AREA_NK100": "" } ) - logger.info(f"📋 Balance API Full Response: {result}") - logger.info(f"📋 Balance API Keys: {result.keys()}") + logger.debug(f"📋 Balance API Full Response: {result}") + logger.debug(f"📋 Balance API Keys: {result.keys()}") if result.get('rt_cd') != '0': logger.error(f"❌ Balance API error - rt_cd={result.get('rt_cd')}, msg1={result.get('msg1')}") return result diff --git a/backend/app/services/swing/scheduler.py b/backend/app/services/swing/scheduler.py index 104481a..f2bc1bf 100644 --- a/backend/app/services/swing/scheduler.py +++ b/backend/app/services/swing/scheduler.py @@ -118,7 +118,7 @@ async def swing_scheduler() -> None: from app.services.korea_investment_api import korea_investment_api balance_resp = await korea_investment_api.get_balance() if balance_resp: - _total = float(balance_resp.get("output2", [{}])[0].get("tot_evlu_amt", 0)) + _total = float((balance_resp.get("output2") or [{}])[0].get("tot_evlu_amt", 0)) if _total > 0: mdd_result = await mdd_manager.update(_total) if mdd_result.get("action") != "NONE": diff --git a/backend/app/services/trade_executor.py b/backend/app/services/trade_executor.py index d7093f9..b5ab662 100644 --- a/backend/app/services/trade_executor.py +++ b/backend/app/services/trade_executor.py @@ -552,6 +552,9 @@ async def _execute_buy( if available_cash <= 0: try: balance = await korea_investment_api.get_balance() + # NOTE: output2가 빈 리스트([])일 때 balance.get()의 default([{}])는 적용되지 않으므로 + # 아래 `if output2:` 가드가 빈 리스트 케이스를 걸러낸다 (IndexError 방지 + 기존 + # available_cash/total_assets 캐시값을 의도적으로 덮어쓰지 않고 보존). output2 = balance.get("output2", [{}]) if output2: b = output2[0] if isinstance(output2, list) else output2 @@ -777,8 +780,20 @@ async def _execute_buy( logger.info(f"[BUY_SKIP] {stock_code}/{stock_name}: {_qz_reason} (invest={invest_amount:,.0f})") return {"action": "SKIP", "reason": _qz_reason, "stock": stock_name} - # 최소 매수 금액 가드: 50만원 미만이면 수수료 대비 비효율 - _MIN_BUY_AMOUNT = 500_000 + # 최소 매수 금액 가드: 수수료 대비 비효율 방지 (기본 config.MIN_BUY_AMOUNT_KRW, 50만원) + # 큰 계좌는 기존과 동일하게 config 하한(기본 50만원)이 그대로 적용된다. + # 소액 계좌(총자산이 작아 6% 예산 자체가 50만원 미만인 경우)는 이 하한 때문에 + # 전종목이 영구 차단되는 문제를 완화하기 위해, "config 하한"과 + # "총자산 × 적응형 포지션상한 × 0.9" 중 더 작은 값을 실효 하한으로 사용한다. + # (총자산이 충분히 크면 항상 config값이 더 작게 선택되어 기존 동작과 동일 — 소액 계좌만 완화됨.) + _MIN_BUY_AMOUNT = settings.MIN_BUY_AMOUNT_KRW + try: + if total_assets > 0 and _adaptive_position_cap > 0: + _small_account_cap = total_assets * _adaptive_position_cap * 0.9 + if _small_account_cap > 0: + _MIN_BUY_AMOUNT = min(_MIN_BUY_AMOUNT, _small_account_cap) + except Exception: + _MIN_BUY_AMOUNT = settings.MIN_BUY_AMOUNT_KRW _actual_amount = quantity * current_price if _actual_amount < _MIN_BUY_AMOUNT: logger.info(f"[SKIP] {stock_code}: 매수금액 {_actual_amount:,.0f}원 < 최소 {_MIN_BUY_AMOUNT:,.0f}원")