""" scripts/daily_summary.py — Tages-Zusammenfassung für E-Mail ============================================================ Gibt eine HTML-Zusammenfassung aller Trades des gestrigen Tages aus. Aufruf: python daily_summary.py [YYYY-MM-DD] """ import sys import sqlite3 import datetime from pathlib import Path DB_PATH = Path(__file__).parent.parent / "oil_widget_history.db" def run(target_date: datetime.date) -> str: ts_start = int(datetime.datetime(target_date.year, target_date.month, target_date.day, 0, 0, 0).timestamp()) ts_end = ts_start + 86400 conn = sqlite3.connect(DB_PATH) conn.row_factory = sqlite3.Row c = conn.cursor() c.execute(""" SELECT setup, direction, pnl, closed_by, exit_time, entry_price, exit_price FROM trades WHERE exit_time >= ? AND exit_time < ? AND exit_time IS NOT NULL ORDER BY exit_time ASC """, (ts_start, ts_end)) trades = [dict(r) for r in c.fetchall()] conn.close() if not trades: return f"

Keine Trades am {target_date.strftime('%d.%m.%Y')}.

" total = len(trades) wins = sum(1 for t in trades if (t["pnl"] or 0) > 0) pnl_sum = sum(t["pnl"] or 0 for t in trades) wr = wins / total * 100 # Pro Setup setups: dict[str, dict] = {} for t in trades: s = t["setup"] or "NO_SETUP" if s not in setups: setups[s] = {"n": 0, "wins": 0, "pnl": 0.0} setups[s]["n"] += 1 setups[s]["wins"] += 1 if (t["pnl"] or 0) > 0 else 0 setups[s]["pnl"] += t["pnl"] or 0 best = max(trades, key=lambda t: t["pnl"] or 0) worst = min(trades, key=lambda t: t["pnl"] or 0) def fmt_pnl(v): sign = "+" if v >= 0 else "" return f"{sign}{v:.2f}€" def row_color(pnl): if pnl > 0: return "#1a4731" if pnl < 0: return "#3d1a1a" return "#1a1e24" # ── HTML ────────────────────────────────────────────────────────────────── header_color = "#1f6feb" if pnl_sum >= 0 else "#b02020" pnl_color = "#3fb950" if pnl_sum >= 0 else "#f85149" rows = "" for s, d in sorted(setups.items(), key=lambda x: -x[1]["pnl"]): wr_s = d["wins"] / d["n"] * 100 bg = row_color(d["pnl"]) rows += f""" {s} {d['n']} {wr_s:.0f}% {fmt_pnl(d['pnl'])} """ import time as _time best_time = _time.strftime("%H:%M", _time.localtime(best["exit_time"])) worst_time = _time.strftime("%H:%M", _time.localtime(worst["exit_time"])) html = f"""

Oil Trading — {target_date.strftime('%d.%m.%Y')}

Trades {total}
Win Rate {wr:.0f}% ({wins}/{total})
PnL {fmt_pnl(pnl_sum)}

Pro Setup

{rows}
Setup N WR PnL
Bester Trade {fmt_pnl(best['pnl'])} {best.get('setup','—')} {best_time}
Schlechtester {fmt_pnl(worst['pnl'])} {worst.get('setup','—')} {worst_time}
""" return html if __name__ == "__main__": if len(sys.argv) > 1: target = datetime.date.fromisoformat(sys.argv[1]) else: target = datetime.date.today() - datetime.timedelta(days=1) sys.stdout.reconfigure(encoding="utf-8") print(run(target))