# 🗄️📱 SQLite + Telegram Integration Guide **Version:** V1.8+ **Datum:** 26. November 2025 **Features:** Database Logging + Mobile Notifications --- ## 🎯 Was wird implementiert? ### 1. **SQLite Database** 🗄️ - Strukturiertes Trade Logging (statt JSON) - Schnelle Performance-Queries - Session/Confidence-basierte Analysen - Historische Datenbank ### 2. **Telegram Notifications** 📱 - Live Trade Entry/Exit Benachrichtigungen - Tägliche Performance Reports (22:00 UTC) - Wöchentliche Summaries (Sonntag 23:00 UTC) - Error Alerts - Bot Status Updates --- ## 📦 Neue Dateien | Datei | Beschreibung | |-------|--------------| | `trading_database.py` | SQLite Database Core | | `telegram_notifier.py` | Telegram Integration | | `infrastructure_patch.py` | Integration mit TradingBot | | `telegram_config.json` | Telegram Konfiguration (erstellen!) | | `trading_bot.db` | SQLite Database (automatisch erstellt) | --- ## 🚀 Setup Guide - Schritt für Schritt ### **PHASE 1: Telegram Bot erstellen (5 Minuten)** #### Schritt 1.1: Bot erstellen bei @BotFather 1. Öffne Telegram 2. Suche nach `@BotFather` 3. Sende `/newbot` 4. Folge den Anweisungen: - Bot Name: z.B. "My Trading Bot" - Bot Username: z.B. "mytrading_bot" (muss auf "_bot" enden) 5. **Kopiere den Bot Token** (z.B. `1234567890:ABCdefGHIjklMNOpqrsTUVwxyz`) #### Schritt 1.2: Chat ID herausfinden 1. Suche nach `@userinfobot` in Telegram 2. Sende `/start` 3. **Kopiere deine User ID** (z.B. `987654321`) #### Schritt 1.3: Telegram Config erstellen Erstelle eine neue Datei `telegram_config.json`: ```json { "bot_token": "1234567890:ABCdefGHIjklMNOpqrsTUVwxyz", "chat_id": "987654321", "notifications": { "trade_entry": true, "trade_exit": true, "daily_report": true, "weekly_report": true, "error_alerts": true }, "daily_report_time": "22:00", "weekly_report_day": "Sunday" } ``` **Wichtig:** Ersetze `bot_token` und `chat_id` mit deinen echten Werten! #### Schritt 1.4: Bot testen ```bash python telegram_notifier.py ``` **Erwartete Ausgabe:** ``` ✅ Telegram Bot connected: @mytrading_bot ✅ Created telegram_config_template.json ``` Wenn erfolgreich, sende eine Test-Nachricht: ```python from telegram_notifier import TelegramNotifier notifier = TelegramNotifier( bot_token='YOUR_BOT_TOKEN', chat_id='YOUR_CHAT_ID' ) notifier.send_message("🎉 Telegram Bot Test erfolgreich!") ``` Du solltest die Nachricht auf deinem Handy bekommen! 📱 --- ### **PHASE 2: SQLite Database einrichten (2 Minuten)** #### Schritt 2.1: Database erstellen ```bash python trading_database.py ``` **Erwartete Ausgabe:** ``` 🗄️ TRADING DATABASE - System Check ====================================================================== 📊 Tables created: 3 ✅ trades ✅ performance_summary ✅ bot_status 📈 Overall Statistics: Total Trades: 0 Win Rate: 0% Net Profit: $0 ====================================================================== ✅ Database ready! ``` #### Schritt 2.2: Existierende JSON Daten migrieren (optional) Wenn du bereits JSON Performance-Dateien hast: ```python from trading_database import TradingDatabase db = TradingDatabase("trading_bot.db") # Migriere alle JSON Files import glob json_files = glob.glob("trade_performance_*.json") for json_file in json_files: print(f"Migrating {json_file}...") db.migrate_from_json(json_file) db.close() ``` #### Schritt 2.3: Database prüfen ```python from trading_database import TradingDatabase db = TradingDatabase("trading_bot.db") # Zeige alle Trades trades = db.get_recent_trades(limit=10) print(f"Total trades in DB: {len(trades)}") # Session Performance session_perf = db.get_session_performance(days=30) for session, stats in session_perf.items(): print(f"{session}: {stats['total_profit']} profit, {stats['win_rate']}% WR") db.close() ``` --- ### **PHASE 3: Integration mit TradingBot (10 Minuten)** #### Schritt 3.1: Imports hinzufügen **Am Anfang deines Notebooks** (nach den MT5 Imports): ```python # ========================================== # INFRASTRUCTURE IMPORTS (NEU!) # ========================================== from infrastructure_patch import ( TradingInfrastructure, create_scheduled_reports ) from trading_database import TradingDatabase from telegram_notifier import TelegramNotifier ``` #### Schritt 3.2: Infrastructure initialisieren **Nach der MT5 Verbindung und VOR dem Scheduler**: ```python # ========================================== # INITIALIZE INFRASTRUCTURE # ========================================== print("🔧 Initializing Infrastructure...") infra = TradingInfrastructure( db_path="trading_bot.db", enable_telegram=True, # Telegram aktivieren enable_database=True # SQLite aktivieren ) # Send Bot Started Notification from session_filter_patch import SESSION_WHITELIST_CONFIG bot_config = { 'version': 'V1.8', 'enabled_sessions': SESSION_WHITELIST_CONFIG['enabled_sessions'], 'base_confidence': SESSION_WHITELIST_CONFIG['base_confidence'], 'max_risk_per_trade': SESSION_WHITELIST_CONFIG['max_risk_per_trade'] } infra.send_bot_started(bot_config) print("✅ Infrastructure ready!") ``` **Du solltest jetzt auf Telegram eine "Bot Started" Nachricht bekommen!** 📱🚀 #### Schritt 3.3: Trade Logging hinzufügen **Modifiziere deine `execute_trade_v2_adaptive` Funktion:** **VORHER** (ca. Zeile wo Position geöffnet wird): ```python result = mt.order_send(request) if result.retcode == mt.TRADE_RETCODE_DONE: logger.info(f"✅ {trade_type} position opened") # ... existing code ... ``` **NACHHER** (mit Infrastructure Logging): ```python result = mt.order_send(request) if result.retcode == mt.TRADE_RETCODE_DONE: logger.info(f"✅ {trade_type} position opened") # ========================================== # LOG TRADE ENTRY (NEU!) # ========================================== try: # Hole Position Info positions = mt.positions_get(symbol=symbol) if positions: position = positions[0] # Erstelle Trade Data trade_data = { 'ticket': position.ticket, 'position_id': position.identifier, 'symbol': symbol, 'strategy_name': strategy_name, 'type': 'BUY' if trade_type == 'BUY' else 'SELL', 'volume': volume, 'entry_price': position.price_open, 'sl_price': position.sl, 'tp_price': position.tp, 'entry_time': datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'session': rhythm_manager.get_current_session(), 'regime': regime, 'quality': quality, 'confidence': confidence, 'timeframe_alignment': timeframe_alignment, 'risk_amount': risk_amount, 'risk_pct': max_risk_per_trade } # Log to Database + Telegram infra.log_trade_entry(trade_data) except Exception as e: logger.error(f"⚠️ Infrastructure logging failed: {e}") # ========================================== # ... existing code continues ... ``` #### Schritt 3.4: Exit Logging hinzufügen **Wenn Trade geschlossen wird** (z.B. durch SL/TP): Dies hängt davon ab, wie dein Bot Exits erkennt. Falls du einen Position Monitor hast: ```python # Wenn Position geschlossen wurde if position_closed: exit_data = { 'exit_price': close_price, 'exit_time': datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'duration_hours': (close_time - open_time).total_seconds() / 3600, 'profit': profit, 'commission': commission, 'swap': swap, 'net_profit': net_profit, 'profit_pct': (net_profit / risk_amount) * 100 if risk_amount else 0, 'rr_ratio': abs(net_profit / risk_amount) if risk_amount else 0, 'exit_reason': 'tp' if hit_tp else 'sl' if hit_sl else 'manual' } # Log Exit infra.log_trade_exit(ticket, exit_data) ``` #### Schritt 3.5: Scheduled Reports hinzufügen **Nach dem Scheduler Setup**: ```python # ========================================== # SCHEDULER SETUP (existing) # ========================================== scheduler = BackgroundScheduler() # ... existing jobs ... # ========================================== # ADD SCHEDULED REPORTS (NEU!) # ========================================== create_scheduled_reports(infra, scheduler) # Start Scheduler scheduler.start() ``` --- ### **PHASE 4: Testing (5 Minuten)** #### Test 1: Infrastructure Status ```python # Prüfe ob alles läuft print("\n🔍 Infrastructure Status:") print(f" Database: {'✅' if infra.enable_database else '❌'}") print(f" Telegram: {'✅' if infra.enable_telegram else '❌'}") # Prüfe DB Stats if infra.db: stats = infra.db.get_overall_statistics(days=7) print(f"\n📊 Last 7 Days:") print(f" Trades: {stats.get('total_trades', 0)}") print(f" Win Rate: {stats.get('win_rate', 0)}%") print(f" Net Profit: ${stats.get('net_profit', 0):.2f}") ``` #### Test 2: Manual Telegram Test ```python # Sende Test Notification if infra.telegram: infra.telegram.send_message("🧪 Test: Infrastructure is working!") ``` #### Test 3: Database Query ```python # Query recent trades if infra.db: trades = infra.db.get_recent_trades(limit=5) print(f"\n📝 Recent Trades: {len(trades)}") for trade in trades: print(f" {trade['symbol']} {trade['type']} @ {trade['entry_price']}") ``` --- ## 📱 Was du auf Telegram sehen wirst ### Bot Started (sofort) ``` 🚀 TradingBot Started Version: V1.8 Time: 2025-11-26 14:30:00 UTC ⚙️ Configuration: Active Sessions: NY Confidence Threshold: 60% Max Risk/Trade: 1.0% ✅ Bot is now monitoring the market ``` ### Trade Entry (bei jedem Trade) ``` 🟢 Trade Opened Symbol: XAUUSD Type: BUY Entry: 2650.50 SL: 2645.50 | TP: 2660.50 Risk: $50.00 (1.0%) Volume: 0.1 lots 🇺🇸 Session: NY 📊 Confidence: 72.5% 🎯 Quality: EXCELLENT ⏰ 2025-11-26 17:15:00 UTC ``` ### Trade Exit (bei jedem Close) ``` ✅ Trade Closed 🟢 Symbol: XAUUSD Type: BUY Entry: 2650.50 Exit: 2660.50 Profit: +$100.00 Duration: 2.5h Exit Reason: TP R:R Ratio: 2.00 ⏰ 2025-11-26 19:45:00 UTC ``` ### Daily Report (täglich 22:00 UTC) ``` 📊 Daily Trading Report 📅 2025-11-26 ━━━━━━━━━━━━━━━━━━━━ Trades: 3 Wins: 2 | Losses: 1 🎯 Win Rate: 66.7% 💰 Net Profit: $150.00 Gross Profit: $200.00 Gross Loss: $-50.00 Avg Trade: $50.00 ━━━━━━━━━━━━━━━━━━━━ ✅ Bot Status: Running ``` ### Weekly Report (Sonntag 23:00 UTC) ``` 📈 Weekly Trading Report 📅 2025-11-20 to 2025-11-26 ━━━━━━━━━━━━━━━━━━━━ 📊 Overall Performance Total Trades: 21 Wins: 10 | Losses: 11 Win Rate: 47.6% 💰 Net Profit: $660.00 🟢 Profit Factor: 1.85 Avg Trade: $31.43 ━━━━━━━━━━━━━━━━━━━━ 📍 Session Performance 🇺🇸 NY: +$660.00 (47.6% WR) ✅ ━━━━━━━━━━━━━━━━━━━━ ✅ Bot Status: Running ``` --- ## 🔍 Nützliche Database Queries ### Query 1: Session Performance (letzte 30 Tage) ```python from trading_database import TradingDatabase db = TradingDatabase("trading_bot.db") session_perf = db.get_session_performance(days=30) for session, stats in session_perf.items(): print(f"{session.upper()}:") print(f" Trades: {stats['count']}") print(f" Win Rate: {stats['win_rate']}%") print(f" Profit: ${stats['total_profit']:.2f}") print() ``` ### Query 2: Confidence Analysis ```python conf_analysis = db.get_confidence_analysis(days=30) for conf_range, stats in conf_analysis.items(): print(f"Confidence {conf_range}:") print(f" Trades: {stats['count']}") print(f" Win Rate: {stats['win_rate']}%") print(f" Profit: ${stats['total_profit']:.2f}") print() ``` ### Query 3: Best/Worst Trades ```python # Best Trades db.cursor.execute(""" SELECT symbol, type, entry_price, exit_price, net_profit, session FROM trades WHERE status = 'closed' ORDER BY net_profit DESC LIMIT 5 """) print("🏆 Top 5 Trades:") for row in db.cursor.fetchall(): print(f" {row['symbol']} {row['type']}: ${row['net_profit']:.2f} ({row['session']})") # Worst Trades db.cursor.execute(""" SELECT symbol, type, entry_price, exit_price, net_profit, session FROM trades WHERE status = 'closed' ORDER BY net_profit ASC LIMIT 5 """) print("\n💔 Bottom 5 Trades:") for row in db.cursor.fetchall(): print(f" {row['symbol']} {row['type']}: ${row['net_profit']:.2f} ({row['session']})") db.close() ``` ### Query 4: Daily Breakdown ```python db.cursor.execute(""" SELECT DATE(entry_time) as date, COUNT(*) as trades, SUM(CASE WHEN net_profit > 0 THEN 1 ELSE 0 END) as wins, ROUND(SUM(net_profit), 2) as daily_profit FROM trades WHERE status = 'closed' AND entry_time >= datetime('now', '-30 days') GROUP BY DATE(entry_time) ORDER BY date DESC """) print("📅 Daily Performance (Last 30 Days):") for row in db.cursor.fetchall(): print(f" {row['date']}: {row['trades']} trades, {row['wins']} wins, ${row['daily_profit']}") ``` --- ## 🛠️ Troubleshooting ### Problem 1: Telegram nicht verbunden **Symptom:** `❌ Telegram connection failed` **Lösung:** 1. Prüfe `telegram_config.json` existiert 2. Prüfe Bot Token ist korrekt 3. Prüfe Chat ID ist korrekt 4. Teste mit: `python telegram_notifier.py` ### Problem 2: Database locked **Symptom:** `database is locked` **Lösung:** ```python # Stelle sicher nur eine Connection offen ist db.close() # ODER: Benutze Context Manager with TradingDatabase("trading_bot.db") as db: # queries... pass # automatisch geschlossen ``` ### Problem 3: Keine Notifications **Symptom:** Bot läuft, aber keine Telegram Nachrichten **Lösung:** 1. Prüfe `infra.enable_telegram` ist `True` 2. Sende Test-Nachricht manuell 3. Prüfe Bot ist nicht von @BotFather blockiert 4. Starte eine Konversation mit deinem Bot (sende `/start`) ### Problem 4: Alte Trades nicht in DB **Symptom:** Database zeigt 0 Trades **Lösung:** ```python # Migriere JSON Daten from infrastructure_patch import TradingInfrastructure infra = TradingInfrastructure() infra.migrate_json_files(".") # Migriert alle JSON Files ``` --- ## 📊 Performance Vorteile ### Vorher (nur JSON): - ❌ Schwer zu analysieren - ❌ Manuelle Performance-Berechnung - ❌ Keine Live Updates - ❌ Keine Session-Analyse ### Nachher (SQLite + Telegram): - ✅ Instant Queries - ✅ Automatische Reports - ✅ Mobile Benachrichtigungen - ✅ Session/Confidence Analytics - ✅ Historische Datenbank --- ## 🎯 Nächste Schritte ### Nach erfolgreicher Integration: 1. **1 Woche monitoren:** - Prüfe tägliche Telegram Reports - Verifiziere Database logging - Teste Queries 2. **Performance analysieren:** ```python # Nach 1 Woche stats = infra.get_performance_summary(days=7) print(stats) ``` 3. **Optional - Custom Queries:** - Erstelle eigene Performance-Metriken - Exportiere Daten für Excel/Charts - Backtesting mit historischen Daten 4. **Phase 2 Features** (später): - Position Scaling basierend auf Confidence - ML Signal Filter - Multi-Symbol Support --- ## ✅ Checklist - Ist alles fertig? - [ ] Telegram Bot erstellt (@BotFather) - [ ] Chat ID erhalten (@userinfobot) - [ ] `telegram_config.json` erstellt mit echten Werten - [ ] `python telegram_notifier.py` läuft ohne Fehler - [ ] `python trading_database.py` erstellt DB - [ ] Alte JSON Daten migriert (optional) - [ ] Infrastructure im Notebook initialisiert - [ ] Trade Entry Logging hinzugefügt - [ ] Trade Exit Logging hinzugefügt (wenn möglich) - [ ] Scheduled Reports aktiviert - [ ] Test-Trade durchgeführt → Telegram Notification erhalten - [ ] Database Query funktioniert - [ ] Bot auf VPS deployed --- ## 🎉 Zusammenfassung Mit **SQLite + Telegram** hast du jetzt: 🗄️ **Strukturierte Datenbank** für alle Trades 📱 **Live Mobile Notifications** für jeden Trade 📊 **Automatische Performance Reports** täglich & wöchentlich 🔍 **Schnelle Analytics** mit SQL Queries 📈 **Historische Daten** für Backtesting ⚠️ **Error Alerts** wenn etwas schief geht **V1.8 ist jetzt eine professionelle Trading-Platform!** 🚀 Bei Fragen oder Problemen: Check Troubleshooting oder frag nach! 💪