fix: 분석종목 누적 버그 수정
- get_rule_based_recommendations_realtime: date.today()(UTC) → KST 기준 오늘 날짜로 교체 - get_rule_based_recommendations_from_db: since_date 조회를 오늘(today_kst)만 조회하도록 수정 (timedelta(days=1) 제거) → 어제 데이터까지 포함되어 누적 표시되던 문제 해결
This commit is contained in:
@@ -19,10 +19,19 @@ from app.services.market_analyzer import (
|
||||
)
|
||||
from app.services.trade_executor import execute_auto_trade_background, execute_auto_trade
|
||||
from app.services.mean_reversion_strategy import MarketRegime
|
||||
import pytz
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_KST = pytz.timezone("Asia/Seoul")
|
||||
|
||||
|
||||
def _today_kst() -> date:
|
||||
"""KST 기준 오늘 날짜 반환 (UTC 기준 date.today() 대신 항상 이 함수 사용)"""
|
||||
return datetime.now(_KST).date()
|
||||
|
||||
|
||||
# 매매 규칙 namedtuple
|
||||
Rule = namedtuple('Rule', ['id', 'name', 'description', 'rule_type', 'priority', 'conditions', 'actions', 'is_active'])
|
||||
|
||||
@@ -77,7 +86,7 @@ async def get_recommendations(
|
||||
use_rsi: bool = Query(False, description="RSI 지표 사용"),
|
||||
use_bollinger: bool = Query(False, description="볼린저밴드 사용"),
|
||||
use_moving_average: bool = Query(False, description="이동평균선 사용"),
|
||||
use_stochastic: bool = Query(False, description="스토캐스틱 사용"),
|
||||
use_stochastic: bool = Query(False, description="스토케스틱 사용"),
|
||||
use_adx: bool = Query(False, description="ADX 사용"),
|
||||
use_obv: bool = Query(False, description="거래량(OBV) 사용"),
|
||||
use_volatility_breakout: bool = Query(False, description="변동성 돌파 전략 사용"),
|
||||
@@ -254,13 +263,13 @@ def is_mean_reversion_candidate(stock, conditions: dict, market_regime_str: str)
|
||||
|
||||
async def get_rule_based_recommendations_from_db(db: AsyncSession):
|
||||
active_rules = await _load_active_rules_from_db(db)
|
||||
|
||||
today_kst = datetime.now(KST).date()
|
||||
since_date = today_kst - timedelta(days=1)
|
||||
|
||||
# KST 기준 오늘만 조회 — timedelta(days=1) 제거로 어제 데이터 누적 표시 방지
|
||||
today_kst = _today_kst()
|
||||
result = await db.execute(
|
||||
select(StockAnalysis)
|
||||
.where(StockAnalysis.analysis_date >= since_date)
|
||||
.order_by(StockAnalysis.analysis_date.desc(), StockAnalysis.score.desc())
|
||||
.where(StockAnalysis.analysis_date == today_kst)
|
||||
.order_by(StockAnalysis.score.desc())
|
||||
)
|
||||
raw_stocks = result.scalars().all()
|
||||
seen = set()
|
||||
@@ -579,8 +588,8 @@ async def get_rule_based_recommendations_realtime():
|
||||
stock["change_rate"] = updated["change_rate"]
|
||||
|
||||
from app.services.analysis_db_service import bulk_upsert_analysis as _bulk_upsert
|
||||
from datetime import date as _date
|
||||
_today = _date.today()
|
||||
# KST 기준 오늘 날짜 사용 (UTC 기준 date.today() 사용 시 KST와 날짜 불일치 → UPSERT 대신 INSERT 반복 버그 수정)
|
||||
_today = _today_kst()
|
||||
_items = [(rec.stock_code, rec.stock_name, rec) for rec in valid_analyzed_stocks]
|
||||
async with AsyncSessionLocal() as _db:
|
||||
saved = await _bulk_upsert(_db, _items, _today)
|
||||
|
||||
Reference in New Issue
Block a user