fix(stock): Phase 2 결정엔진 견고화 (빈노드 제외·cur=0 손절·params기본값·NaN MA·테스트)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-31 22:00:02 +09:00
parent fba6dbf1fd
commit c756b20c77
2 changed files with 66 additions and 10 deletions

View File

@@ -7,6 +7,7 @@ import pandas as pd
from . import db
from . import price_fetcher
from .screener.engine import combine
def _krx_tickers() -> set:
@@ -36,8 +37,6 @@ def get_holdings() -> list[dict]:
# ---- Task 2.1: technical_posture ----
from .screener.engine import combine
def _score_nodes_and_weights():
"""NODE_REGISTRY에서 보유종목 매수강도 계산용 노드 인스턴스화."""
@@ -59,16 +58,24 @@ def technical_posture(ctx, tickers: list[str]) -> dict[str, float]:
scores[n.name] = n.compute(scoped, {})
except Exception:
scores[n.name] = pd.Series(0.0, index=scoped.master.index)
total = combine(scores, weights)
scores_ne = {k: s for k, s in scores.items() if not s.empty}
weights_ne = {k: w for k, w in weights.items() if k in scores_ne}
if not weights_ne:
return {}
total = combine(scores_ne, weights_ne)
return {t: float(total.get(t, 0.0)) for t in tickers if t in total.index}
# ---- Task 2.2: exit_rules ----
_DEFAULT_EXIT_PARAMS = {"stop_pct": 0.08, "take_pct": 0.25, "climax_vol_x": 3.0}
def _ma(closes: "pd.Series", window: int) -> Optional[float]:
if len(closes) < window:
return None
return float(closes.rolling(window).mean().iloc[-1])
val = closes.rolling(window).mean().iloc[-1]
return float(val) if pd.notna(val) else None
def exit_rules(holding: dict, ticker_prices: "pd.DataFrame", params: dict) -> dict:
@@ -76,6 +83,7 @@ def exit_rules(holding: dict, ticker_prices: "pd.DataFrame", params: dict) -> di
Note: momentum_loss는 compute_and_store 단계에서 집계하므로 여기서 설정하지 않는다.
"""
p = {**_DEFAULT_EXIT_PARAMS, **(params or {})}
flags = {"stop_loss": False, "ma50_break": False, "ma200_break": False,
"take_profit": False, "climax": False}
avg = holding.get("avg_price")
@@ -87,10 +95,10 @@ def exit_rules(holding: dict, ticker_prices: "pd.DataFrame", params: dict) -> di
last_close = float(closes.iloc[-1]) if len(closes) else cur
if cur is None:
cur = last_close
if cur and avg:
if cur < avg * (1 - params["stop_pct"]):
if cur is not None and avg:
if cur < avg * (1 - p["stop_pct"]):
flags["stop_loss"] = True
if (cur - avg) / avg >= params["take_pct"]:
if avg > 0 and (cur - avg) / avg >= p["take_pct"]:
flags["take_profit"] = True
ma50 = _ma(closes, 50)
ma200 = _ma(closes, 200)
@@ -106,7 +114,7 @@ def exit_rules(holding: dict, ticker_prices: "pd.DataFrame", params: dict) -> di
last_vol = vol.iloc[-1]
hi_ = float(tp["high"].astype(float).iloc[-1])
cl_ = float(tp["close"].astype(float).iloc[-1])
if avg_vol and last_vol >= avg_vol * params["climax_vol_x"] and hi_ > 0 and cl_ < hi_ * 0.97:
if avg_vol and last_vol >= avg_vol * p["climax_vol_x"] and hi_ > 0 and cl_ < hi_ * 0.97:
flags["climax"] = True
return flags
@@ -116,7 +124,8 @@ def exit_rules(holding: dict, ticker_prices: "pd.DataFrame", params: dict) -> di
ADD_SCORE = 70.0 # 이 이상이면 추가매수 후보
def decide_action(tech_score: float, exit_flags: dict, pnl: float | None) -> tuple[str, str]:
def decide_action(tech_score: float, exit_flags: dict, pnl: float | None,
add_score: float = ADD_SCORE) -> tuple[str, str]:
"""액션 결정 매트릭스: sell > trim > add > hold (우선순위 순).
Returns:
@@ -142,6 +151,6 @@ def decide_action(tech_score: float, exit_flags: dict, pnl: float | None) -> tup
if reasons:
return "trim", " · ".join(reasons)
# 추가매수
if tech_score is not None and tech_score >= ADD_SCORE:
if tech_score is not None and tech_score >= add_score:
return "add", f"기술적 강도 양호({tech_score:.0f})"
return "hold", "특이 신호 없음"