#!/usr/bin/env python3 """ πŸ”§ Notebook Patch: Drawdown Protection Integration FΓΌgt automatisch Drawdown Protection ins Notebook ein """ import json import sys from pathlib import Path NOTEBOOK_PATH = "TradingBot_V1.6_Adaptive_Complete_CORRECTED.ipynb" def patch_notebook(): """Patcht das Notebook mit Drawdown Protection""" print("=" * 70) print("πŸ”§ NOTEBOOK PATCH: Drawdown Protection") print("=" * 70) # Load notebook print(f"\n1️⃣ Loading notebook: {NOTEBOOK_PATH}") try: with open(NOTEBOOK_PATH, 'r', encoding='utf-8') as f: notebook = json.load(f) except FileNotFoundError: print(f"❌ Error: Notebook not found: {NOTEBOOK_PATH}") print(" Make sure you're in the correct directory!") return False print(f"βœ… Loaded {len(notebook['cells'])} cells") # Find Session Filter cell print("\n2️⃣ Finding Session Filter cell...") session_filter_cell_index = None for i, cell in enumerate(notebook['cells']): if cell['cell_type'] == 'code': source = ''.join(cell['source']) if 'create_session_filtered_check' in source: session_filter_cell_index = i print(f"βœ… Found Session Filter at cell {i}") break if session_filter_cell_index is None: print("❌ Error: Could not find Session Filter cell!") print(" Looking for cell with 'create_session_filtered_check'") return False # Create new cell with Drawdown Protection print("\n3️⃣ Creating Drawdown Protection cell...") new_cell_source = """# ========================================== # TRADING CHECK: SESSION FILTER + DRAWDOWN PROTECTION # ========================================== from session_filter_patch import create_session_filtered_check from drawdown_protection import create_protected_trading_check, print_protection_status print("πŸ”§ Setting up Trading Check...") # 1. Session Filter base_trading_check = create_session_filtered_check(infra) print("βœ… Session Filter: Active (NY + Asian, Confidence β‰₯70)") # 2. Drawdown Protection trading_check = create_protected_trading_check(infra, base_trading_check) drawdown_protection = trading_check.protection print("βœ… Drawdown Protection: Active") print(f" β€’ Daily Loss Limit: ${drawdown_protection.max_daily_loss}") print(f" β€’ Weekly Loss Limit: ${drawdown_protection.max_weekly_loss}") print(f" β€’ Monthly Loss Limit: ${drawdown_protection.max_monthly_loss}") print(f" β€’ Max Consecutive Losses: {drawdown_protection.max_consecutive_losses}") print(f" β€’ Cooldown: {drawdown_protection.cooldown_hours}h") print("\\nπŸ›‘οΈ Trading Check is now fully protected!") """ new_cell = { "cell_type": "code", "execution_count": None, "metadata": {}, "outputs": [], "source": new_cell_source.split('\n') } # Replace the old Session Filter cell print(f"βœ… Replacing cell {session_filter_cell_index} with enhanced version") notebook['cells'][session_filter_cell_index] = new_cell # Backup original backup_path = NOTEBOOK_PATH.replace('.ipynb', '_backup_before_drawdown_patch.ipynb') print(f"\n4️⃣ Creating backup: {backup_path}") with open(backup_path, 'w', encoding='utf-8') as f: json.dump(notebook, f, indent=1, ensure_ascii=False) print("βœ… Backup created") # Save patched notebook print(f"\n5️⃣ Saving patched notebook: {NOTEBOOK_PATH}") with open(NOTEBOOK_PATH, 'w', encoding='utf-8') as f: json.dump(notebook, f, indent=1, ensure_ascii=False) print("βœ… Notebook patched successfully!") print("\n" + "=" * 70) print("βœ… PATCH COMPLETE!") print("=" * 70) print("\nNext steps:") print("1. Open Jupyter Notebook") print("2. Kernel β†’ Restart & Run All") print("3. Verify Drawdown Protection is active") print("\nBackup saved to:", backup_path) return True if __name__ == "__main__": success = patch_notebook() sys.exit(0 if success else 1)