From d61c2b80e1d6bbd27138acf9c758d8d77ed9e6d3 Mon Sep 17 00:00:00 2001 From: jongjae Date: Mon, 18 May 2026 13:54:13 +0900 Subject: [PATCH] =?UTF-8?q?[2026-05-18]=20daily=5Fsummary=20DB=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5=20=EC=B6=94=EA=B0=80=20(daily=5Fsummary=20=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EB=B8=94)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/app/main.py b/app/main.py index ed3c762..3024d02 100644 --- a/app/main.py +++ b/app/main.py @@ -464,23 +464,37 @@ class StockBot: # ───────────────────────────────────────── async def daily_summary(self): - """당일 결산 로그 및 디스코드 알림""" + """당일 결산 로그 및 디스코드 알림 + DB 저장""" today = datetime.now().strftime("%Y-%m-%d") with get_conn() as conn: rows = conn.execute(""" - SELECT pnl FROM trades + SELECT pnl, fee FROM trades WHERE date=? AND exit_time IS NOT NULL """, (today,)).fetchall() - pnls = [r[0] for r in rows if r[0] is not None] - total = len(pnls) - wins = sum(1 for p in pnls if p > 0) - losses = total - wins - net = sum(pnls) + pnls = [r[0] for r in rows if r[0] is not None] + fees = [r[1] for r in rows if r[1] is not None] + total = len(pnls) + wins = sum(1 for p in pnls if p > 0) + losses = total - wins + gross_pnl = sum(p for p in pnls if p > 0) - abs(sum(p for p in pnls if p < 0)) + total_fee = sum(fees) + net = sum(pnls) + mdd = min(self.risk.daily_pnl / self.risk.init_cash * 100, 0.0) + stopped = 0 if self.risk.can_trade() else 1 + + # daily_summary 테이블 저장 + with get_conn() as conn: + conn.execute(""" + INSERT OR REPLACE INTO daily_summary + (date, total_trades, win_trades, lose_trades, + gross_pnl, total_fee, net_pnl, max_drawdown, trading_stopped) + VALUES (?,?,?,?,?,?,?,?,?) + """, (today, total, wins, losses, gross_pnl, total_fee, net, mdd, stopped)) await notify_daily_summary(total, wins, losses, net) self.risk.reset_daily() - logger.info(f"결산: {total}회 / 승{wins} 패{losses} / {net:+,.0f}원") + logger.info(f"결산: {total}회 / 승{wins} 패{losses} / {net:+,.0f}원 (fee {total_fee:,.0f}원)") # ─────────────────────────────────────────