fix(journal): 장중 일지 자동 생성 + 전략명 unknown + profit_factor 단위 오류

- 장중 10분 주기 journal_intraday_scheduler 신설 (SELECT 전용, 쓰기는 db_worker 큐)
  - 매도 후 3분 유예(JOURNAL_INTRADAY_GRACE_MIN), trade_id in-flight TTL 30분
  - order_id 로 해소된 trade_id 에 일지가 있으면 INSERT skip (중복 방지)
- 16:00 배치 enqueue_trade_journal 이 strategy_type 을 넘기지 않아 전 일지가 'unknown' 이던 결함 수정
- analyze_performance profit_factor 를 손익률(%) 합이 아닌 금액 기준으로 (1.22 → 0.62)
- 일지 대상 SELL 상태에 PARTIAL 포함 (성과 집계 필터는 불변)
- journals 단건 생성: 중복 행 시 500 대신 409, JOURNAL_SELL_STATUSES 사용
- 16:00 배치 조회일수 config 화, 손익 0 기간 PF 오경고 제거

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-01 17:00:05 +09:00
parent b4e958b477
commit e4346d30fb
7 changed files with 419 additions and 21 deletions

View File

@@ -131,12 +131,13 @@ async def generate_journal_for_trade_id(
analyze_performance,
generate_journal_for_trade,
create_journal_record,
JOURNAL_SELL_STATUSES,
)
# 기존 일지 확인
existing = await db.execute(
select(TradeJournal).where(TradeJournal.trade_id == trade_id)
)
if existing.scalar_one_or_none():
if existing.scalars().first(): # 중복 행이 있어도 500 이 아니라 409
raise HTTPException(status_code=409, detail="이미 일지가 존재합니다.")
# 거래 조회
@@ -146,7 +147,7 @@ async def generate_journal_for_trade_id(
trade = trade_result.scalar_one_or_none()
if not trade:
raise HTTPException(status_code=404, detail="거래 이력을 찾을 수 없습니다.")
if trade.order_type != "SELL" or trade.status not in ("COMPLETED", "FILLED"):
if trade.order_type != "SELL" or trade.status not in JOURNAL_SELL_STATUSES:
raise HTTPException(status_code=400, detail="완료된 매도 거래에 대해서만 일지를 생성할 수 있습니다.")
perf = await analyze_performance(db, days=30)