Features: - Remote control via Telegram commands - /status - Bot status, positions, balance - /pause - Pause trading (no new trades) - /resume - Resume trading - /close - Close all positions (emergency) - /balance - Account balance & equity - /stats - Performance statistics - /help - Command help Integration: - Integrated into notebook (cells 27-29) - Wrapped execute_trade_v2_adaptive with pause check - Background service running parallel to bot - MT5 integration for positions & balance - Database integration for stats Safety: - Only authorized chat ID can send commands - /close requires confirmation - Instant pause/resume Files: - telegram_bot_commands.py - Main implementation - setup_telegram_bot.py - Setup & installation - TELEGRAM_BOT_COMMANDS_GUIDE.md - Complete documentation - Notebook updated with 3 new cells (27-29) Expected Impact: High - Full remote control from mobile phone
162 lines
6.2 KiB
Python
162 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Fügt Telegram Bot Command Cells ins Notebook hinzu
|
|
"""
|
|
|
|
import json
|
|
import sys
|
|
|
|
# Load notebook
|
|
notebook_path = "TradingBot_V1.6_Adaptive_Complete_CORRECTED.ipynb"
|
|
|
|
print(f"📖 Loading notebook: {notebook_path}")
|
|
with open(notebook_path, 'r', encoding='utf-8') as f:
|
|
notebook = json.load(f)
|
|
|
|
# Count cells
|
|
total_cells = len(notebook['cells'])
|
|
print(f"📊 Current cells: {total_cells}")
|
|
|
|
# Find position to insert (after session confidence filter cells, which should be around cell 26)
|
|
# We want to insert after cell 26 (the session confidence filter)
|
|
insert_position = 27 # After cell 26 (0-indexed: position 27)
|
|
|
|
# Create Cell 1: Telegram Bot Starter (Markdown Info)
|
|
telegram_info_cell = {
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## 🤖 Telegram Bot Commands - Remote Control\n",
|
|
"\n",
|
|
"**Status:** ACTIVE - Bot läuft im Hintergrund\n",
|
|
"\n",
|
|
"### 📱 Verfügbare Commands:\n",
|
|
"\n",
|
|
"**Bot Control:**\n",
|
|
"- `/status` - Bot Status, offene Positionen, Balance\n",
|
|
"- `/pause` - Trading pausieren (keine neuen Trades)\n",
|
|
"- `/resume` - Trading fortsetzen\n",
|
|
"- `/close confirm` - ALLE Positionen schließen (Emergency)\n",
|
|
"\n",
|
|
"**Information:**\n",
|
|
"- `/balance` - Aktueller Kontostand + Equity\n",
|
|
"- `/stats` - Performance Statistiken\n",
|
|
"- `/help` - Hilfe anzeigen\n",
|
|
"\n",
|
|
"### ✅ Features:\n",
|
|
"- Remote Control vom Handy\n",
|
|
"- Emergency Stop von überall\n",
|
|
"- Trading Pause/Resume\n",
|
|
"- Live Status & Balance Check\n",
|
|
"\n",
|
|
"### 🔒 Sicherheit:\n",
|
|
"- Nur deine Chat ID kann Commands senden\n",
|
|
"- `/close` requires confirmation\n",
|
|
"- `/pause` ist instant\n"
|
|
]
|
|
}
|
|
|
|
# Create Cell 2: Telegram Bot Commander Code
|
|
telegram_bot_cell = {
|
|
"cell_type": "code",
|
|
"execution_count": None,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# ==========================================\n",
|
|
"# TELEGRAM BOT COMMANDS - Background Service\n",
|
|
"# ==========================================\n",
|
|
"\n",
|
|
"from telegram_bot_commands import TelegramBotCommander, get_bot_controller\n",
|
|
"import threading\n",
|
|
"\n",
|
|
"# Start Telegram Bot in background\n",
|
|
"try:\n",
|
|
" print(\"🚀 Starting Telegram Bot Commander...\")\n",
|
|
" \n",
|
|
" bot_commander = TelegramBotCommander()\n",
|
|
" bot_thread = bot_commander.start_background()\n",
|
|
" \n",
|
|
" # Get controller for integration with execute_trade\n",
|
|
" bot_controller = get_bot_controller()\n",
|
|
" \n",
|
|
" print(\"✅ Telegram Bot is running in background!\")\n",
|
|
" print(\"📱 Available Commands:\")\n",
|
|
" print(\" /status - Bot status & positions\")\n",
|
|
" print(\" /pause - Pause trading\")\n",
|
|
" print(\" /resume - Resume trading\")\n",
|
|
" print(\" /close - Close all positions (requires confirm)\")\n",
|
|
" print(\" /balance - Account balance\")\n",
|
|
" print(\" /stats - Performance stats\")\n",
|
|
" print(\" /help - Show help\")\n",
|
|
" \n",
|
|
"except Exception as e:\n",
|
|
" print(f\"❌ Failed to start Telegram Bot: {e}\")\n",
|
|
" bot_controller = None\n"
|
|
]
|
|
}
|
|
|
|
# Create Cell 3: Integration with execute_trade
|
|
telegram_integration_cell = {
|
|
"cell_type": "code",
|
|
"execution_count": None,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# ==========================================\n",
|
|
"# INTEGRATION: Bot Controller mit execute_trade\n",
|
|
"# ==========================================\n",
|
|
"\n",
|
|
"# Original execute_trade_v2_adaptive function wrappen\n",
|
|
"if 'bot_controller' in dir() and bot_controller is not None:\n",
|
|
" \n",
|
|
" # Original Funktion sichern\n",
|
|
" if '_original_execute_trade_before_telegram' not in dir():\n",
|
|
" _original_execute_trade_before_telegram = execute_trade_v2_adaptive\n",
|
|
" \n",
|
|
" def execute_trade_with_telegram_control(*args, **kwargs):\n",
|
|
" \"\"\"\n",
|
|
" Wrapper der bot_controller.is_paused prüft\n",
|
|
" \"\"\"\n",
|
|
" # Check if trading is paused\n",
|
|
" if bot_controller.is_paused:\n",
|
|
" print(\"⏸️ Trading PAUSED via Telegram\")\n",
|
|
" print(f\" Reason: {bot_controller.pause_reason}\")\n",
|
|
" return\n",
|
|
" \n",
|
|
" # Execute original function\n",
|
|
" return _original_execute_trade_before_telegram(*args, **kwargs)\n",
|
|
" \n",
|
|
" # Replace execute_trade\n",
|
|
" execute_trade_v2_adaptive = execute_trade_with_telegram_control\n",
|
|
" \n",
|
|
" print(\"✅ execute_trade_v2_adaptive wrapped with Telegram control\")\n",
|
|
" print(\" Trading can now be paused/resumed via /pause and /resume\")\n",
|
|
"else:\n",
|
|
" print(\"⚠️ bot_controller not available, skipping integration\")\n"
|
|
]
|
|
}
|
|
|
|
# Insert cells
|
|
print(f"\n📝 Inserting 3 new cells at position {insert_position}...")
|
|
notebook['cells'].insert(insert_position, telegram_info_cell)
|
|
notebook['cells'].insert(insert_position + 1, telegram_bot_cell)
|
|
notebook['cells'].insert(insert_position + 2, telegram_integration_cell)
|
|
|
|
# Update cell count
|
|
new_total = len(notebook['cells'])
|
|
print(f"✅ New total cells: {new_total} (was {total_cells})")
|
|
|
|
# Save notebook
|
|
print(f"\n💾 Saving notebook...")
|
|
with open(notebook_path, 'w', encoding='utf-8') as f:
|
|
json.dump(notebook, f, indent=1, ensure_ascii=False)
|
|
|
|
print("✅ Notebook updated successfully!")
|
|
print(f"\n📋 Added cells:")
|
|
print(f" Cell {insert_position}: Telegram Bot Info (Markdown)")
|
|
print(f" Cell {insert_position + 1}: Telegram Bot Commander (Code)")
|
|
print(f" Cell {insert_position + 2}: execute_trade Integration (Code)")
|
|
print(f"\n🎯 Cells are now at positions {insert_position}-{insert_position + 2}")
|
|
print(f" (Previously the notebook had {total_cells} cells, now it has {new_total})")
|