MCP 수정중
This commit is contained in:
@@ -72,6 +72,9 @@ async def request_mcp_risk_assessment(
|
||||
- approve=False → 주문 스킵
|
||||
- MCP 장애 / 타임아웃 → fail-open (approve=True, 매매 중단 방지)
|
||||
"""
|
||||
if not settings.MCP_VALIDATION_ENABLED:
|
||||
return True, "mcp_disabled"
|
||||
|
||||
try:
|
||||
client = get_http_client()
|
||||
except RuntimeError:
|
||||
|
||||
@@ -3,7 +3,6 @@ from typing import List, Optional
|
||||
from datetime import datetime, date, timedelta
|
||||
import asyncio
|
||||
import logging
|
||||
import httpx
|
||||
from collections import namedtuple
|
||||
|
||||
from app.core.database import get_db, AsyncSessionLocal
|
||||
@@ -662,72 +661,49 @@ async def execute_auto_trading(db: AsyncSession = Depends(get_db)):
|
||||
|
||||
@router.get("/ai/{stock_code}")
|
||||
async def get_ai_recommendation(stock_code: str):
|
||||
from app.services.korea_investment_api import korea_investment_api
|
||||
"""종목 기술적 분석 — AI 미사용, 전략 엔진(RSI/BB/MA/ATR/ADX) 직접 계산."""
|
||||
from app.services.market_analyzer import analyze_stock_for_batch
|
||||
try:
|
||||
price_data = {}
|
||||
try:
|
||||
price_data = await korea_investment_api.get_current_price(stock_code)
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to fetch price data locally: {e}")
|
||||
rec = await analyze_stock_for_batch(stock_code, stock_code)
|
||||
if rec is None:
|
||||
raise HTTPException(status_code=404, detail="분석 데이터를 가져올 수 없습니다.")
|
||||
|
||||
news_data = []
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
mcp_url = f"{settings.MCP_SERVER_URL}/tools/call"
|
||||
payload = {
|
||||
"tool_name": "get_news",
|
||||
"parameters": {"stock_code": stock_code, "limit": 3}
|
||||
}
|
||||
response = await client.post(mcp_url, json=payload, timeout=10.0)
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
if result.get("status") == "success":
|
||||
data = result.get("result", {})
|
||||
if "news" in data:
|
||||
news_data = data["news"]
|
||||
else:
|
||||
news_data = data
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to fetch news via MCP: {e}")
|
||||
signals = rec.signals or []
|
||||
score = rec.score or 0
|
||||
|
||||
analysis_result = "AI 분석을 수행할 수 없습니다."
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
analyze_url = f"{settings.MCP_SERVER_URL}/analyze"
|
||||
market_summary = {}
|
||||
if "output" in price_data:
|
||||
output = price_data["output"]
|
||||
market_summary = {
|
||||
"current_price": output.get("stck_prpr"),
|
||||
"change_rate": output.get("prdy_ctrt"),
|
||||
"volume": output.get("acml_vol"),
|
||||
"high": output.get("stck_hgpr"),
|
||||
"low": output.get("stck_lwpr")
|
||||
}
|
||||
# 전략 신호 기반 요약 문자열 생성
|
||||
if score >= 3:
|
||||
summary = f"매수 신호 감지 (점수: {score}). 주요 신호: {', '.join(signals[:3])}"
|
||||
elif score <= -2:
|
||||
summary = f"매도/경고 신호 (점수: {score}). 주요 신호: {', '.join(signals[:3])}"
|
||||
else:
|
||||
summary = f"보유/관망 (점수: {score}). 신호: {', '.join(signals[:3]) if signals else '없음'}"
|
||||
|
||||
payload = {
|
||||
"analysis_type": "MARKET_ANALYSIS",
|
||||
"data": {
|
||||
"stock_code": stock_code,
|
||||
"market_data": market_summary,
|
||||
"news": news_data
|
||||
}
|
||||
}
|
||||
response = await client.post(analyze_url, json=payload, timeout=45.0)
|
||||
if response.status_code == 200:
|
||||
analysis_res = response.json()
|
||||
if "result" in analysis_res:
|
||||
analysis_result = analysis_res["result"]
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to fetch AI analysis: {e}")
|
||||
|
||||
return {
|
||||
"stock_code": stock_code,
|
||||
"analysis": analysis_result,
|
||||
"news": news_data,
|
||||
"market_data": price_data.get("output", {})
|
||||
"analysis": summary,
|
||||
"signals": signals,
|
||||
"score": score,
|
||||
"market_data": {
|
||||
"current_price": rec.current_price,
|
||||
"change_rate": rec.change_rate,
|
||||
"volume": rec.volume,
|
||||
"rsi": rec.rsi,
|
||||
"ma5": rec.ma5,
|
||||
"ma20": rec.ma20,
|
||||
"ma60": rec.ma60,
|
||||
"bollinger_upper": rec.bollinger_upper,
|
||||
"bollinger_lower": rec.bollinger_lower,
|
||||
"bollinger_middle": rec.bollinger_middle,
|
||||
"atr_14": rec.atr_14,
|
||||
"adx_14": rec.adx_14,
|
||||
"stochastic_k": rec.stochastic_k,
|
||||
"stochastic_d": rec.stochastic_d,
|
||||
}
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"AI Recommendation error: {e}")
|
||||
logger.error(f"Strategy analysis error for {stock_code}: {e}")
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
Reference in New Issue
Block a user