[2026-05-18] daily_summary DB 저장 추가 (daily_summary 테이블)
This commit is contained in:
+22
-8
@@ -464,23 +464,37 @@ class StockBot:
|
|||||||
# ─────────────────────────────────────────
|
# ─────────────────────────────────────────
|
||||||
|
|
||||||
async def daily_summary(self):
|
async def daily_summary(self):
|
||||||
"""당일 결산 로그 및 디스코드 알림"""
|
"""당일 결산 로그 및 디스코드 알림 + DB 저장"""
|
||||||
today = datetime.now().strftime("%Y-%m-%d")
|
today = datetime.now().strftime("%Y-%m-%d")
|
||||||
with get_conn() as conn:
|
with get_conn() as conn:
|
||||||
rows = conn.execute("""
|
rows = conn.execute("""
|
||||||
SELECT pnl FROM trades
|
SELECT pnl, fee FROM trades
|
||||||
WHERE date=? AND exit_time IS NOT NULL
|
WHERE date=? AND exit_time IS NOT NULL
|
||||||
""", (today,)).fetchall()
|
""", (today,)).fetchall()
|
||||||
|
|
||||||
pnls = [r[0] for r in rows if r[0] is not None]
|
pnls = [r[0] for r in rows if r[0] is not None]
|
||||||
total = len(pnls)
|
fees = [r[1] for r in rows if r[1] is not None]
|
||||||
wins = sum(1 for p in pnls if p > 0)
|
total = len(pnls)
|
||||||
losses = total - wins
|
wins = sum(1 for p in pnls if p > 0)
|
||||||
net = sum(pnls)
|
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)
|
await notify_daily_summary(total, wins, losses, net)
|
||||||
self.risk.reset_daily()
|
self.risk.reset_daily()
|
||||||
logger.info(f"결산: {total}회 / 승{wins} 패{losses} / {net:+,.0f}원")
|
logger.info(f"결산: {total}회 / 승{wins} 패{losses} / {net:+,.0f}원 (fee {total_fee:,.0f}원)")
|
||||||
|
|
||||||
|
|
||||||
# ─────────────────────────────────────────
|
# ─────────────────────────────────────────
|
||||||
|
|||||||
Reference in New Issue
Block a user