DB오류 처리중
This commit is contained in:
@@ -524,6 +524,35 @@ async def get_ai_analysis(stock_code: str, db: AsyncSession = Depends(get_db)):
|
||||
"bollinger_middle": float(analysis_record.bollinger_middle) if analysis_record.bollinger_middle else None,
|
||||
"bollinger_lower": float(analysis_record.bollinger_lower) if analysis_record.bollinger_lower else None,
|
||||
}
|
||||
else:
|
||||
# DB에 분석 데이터 없으면 KIS 일봉 차트로 직접 계산 (fallback)
|
||||
try:
|
||||
from app.services.mean_reversion_strategy import calculate_rsi, calculate_bollinger
|
||||
chart_resp = await korea_investment_api.get_daily_chart(stock_code, "D", limit=60)
|
||||
chart_data = chart_resp.get("output2", [])
|
||||
if len(chart_data) >= 20:
|
||||
closes = [float(c.get("stck_clpr", 0)) for c in reversed(chart_data)]
|
||||
highs = [float(c.get("stck_hgpr", 0)) for c in reversed(chart_data)]
|
||||
lows = [float(c.get("stck_lwpr", 0)) for c in reversed(chart_data)]
|
||||
rsi_val = calculate_rsi(closes)
|
||||
bb = calculate_bollinger(closes)
|
||||
ma5 = sum(closes[-5:]) / 5 if len(closes) >= 5 else None
|
||||
ma20 = sum(closes[-20:]) / 20 if len(closes) >= 20 else None
|
||||
ma60 = sum(closes[-60:]) / 60 if len(closes) >= 60 else None
|
||||
indicators = {
|
||||
"rsi": round(rsi_val, 4) if rsi_val else None,
|
||||
"ma5": round(ma5, 2) if ma5 else None,
|
||||
"ma20": round(ma20, 2) if ma20 else None,
|
||||
"ma60": round(ma60, 2) if ma60 else None,
|
||||
"stochastic_k": None,
|
||||
"stochastic_d": None,
|
||||
"bollinger_upper": round(bb["upper"], 2) if bb else None,
|
||||
"bollinger_middle": round(bb["middle"], 2) if bb else None,
|
||||
"bollinger_lower": round(bb["lower"], 2) if bb else None,
|
||||
}
|
||||
logger.info(f"📊 {stock_code} 지표 실시간 계산 (DB 없음 fallback)")
|
||||
except Exception as fe:
|
||||
logger.debug(f"지표 fallback 계산 실패 ({stock_code}): {fe}")
|
||||
|
||||
# 3. 규칙 기반 분석 로직
|
||||
recommendation = "HOLD" # 기본 추천: 보유
|
||||
@@ -654,7 +683,7 @@ async def get_ai_analysis(stock_code: str, db: AsyncSession = Depends(get_db)):
|
||||
# 4. 종합 결과 반환
|
||||
return {
|
||||
"stock_code": stock_code,
|
||||
"stock_name": output.get("stck_prdt_name", ""),
|
||||
"stock_name": output.get("hts_kor_isnm", "") or output.get("stck_prdt_name", ""),
|
||||
"current_price": current_price,
|
||||
"high_price": high_price,
|
||||
"low_price": low_price,
|
||||
@@ -663,7 +692,8 @@ async def get_ai_analysis(stock_code: str, db: AsyncSession = Depends(get_db)):
|
||||
"ai_analysis": analysis_text,
|
||||
"recommendation": recommendation,
|
||||
"confidence": confidence,
|
||||
"trend": trend
|
||||
"trend": trend,
|
||||
"analyzed_at": datetime.now().isoformat(),
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
|
||||
Reference in New Issue
Block a user