feat: Centralize trading configuration
PROBLEM: - User identified lot size settings were chaotic and scattered - Settings across 3 cells (23, 25, 47) caused confusion - Multiple attempts needed to fix lot size (0.01 → 0.05 → 0.10) - Quote: "mir kommt das ganze ein bisschen chaotisch vor" SOLUTION: ✅ Created centralized TRADING_CONFIG in new Cell 6 ✅ Updated Cell 25 (calculate_position_size) to use config ✅ Updated Cell 27 (execute_trade_v2_adaptive) to use config ✅ Updated Cell 49 (ADAPTIVE_COMPLETE_CONFIG) to reference config CONFIGURATION STRUCTURE: - lot_sizing: min/max/default lot sizes - risk: max_risk_per_trade, max_positions, max_daily_loss - confidence: thresholds per session - atr: base_multiplier, period - news_filter: enabled, minutes_before/after - sessions: enabled sessions - symbols: primary trading symbol BENEFITS: ✅ Single source of truth for all settings ✅ Easy to find and change configuration ✅ Clear documentation in one place ✅ Prevents scattered hardcoded values ✅ Future changes require only editing Cell 6 FILES: - centralize_config.py: Script to add config cells - update_cells_to_use_config.py: Updates cells to use config NEXT STEPS: 1. Restart kernel in Jupyter 2. Run Cell 6 (TRADING_CONFIG) 3. Run all other cells 4. Verify bot uses 0.10 lot size 🎯 Generated with Claude Code Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Update Trading Cells to Use Centralized Config
|
||||
Updates Cells 25, 27, and 49 (formerly 23, 25, 47) to reference TRADING_CONFIG
|
||||
"""
|
||||
|
||||
import json
|
||||
import re
|
||||
|
||||
notebook_path = "TradingBot_V1.6_Adaptive_Complete_CORRECTED.ipynb"
|
||||
|
||||
print("=" * 70)
|
||||
print("🔄 UPDATING CELLS TO USE TRADING_CONFIG")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
# Load notebook
|
||||
with open(notebook_path, 'r', encoding='utf-8') as f:
|
||||
notebook = json.load(f)
|
||||
|
||||
print(f"Loaded notebook with {len(notebook['cells'])} cells")
|
||||
print()
|
||||
|
||||
changes_made = 0
|
||||
|
||||
# ============================================================================
|
||||
# CELL 25: calculate_position_size function
|
||||
# ============================================================================
|
||||
print("📍 Updating Cell 25: calculate_position_size()")
|
||||
print("-" * 70)
|
||||
|
||||
cell_25 = notebook['cells'][25]
|
||||
if cell_25['cell_type'] == 'code':
|
||||
source = ''.join(cell_25['source'])
|
||||
original = source
|
||||
|
||||
# Update hardcoded 0.10 returns to use config
|
||||
source = source.replace(
|
||||
'return 0.10',
|
||||
'return TRADING_CONFIG["lot_sizing"]["default_lot"]'
|
||||
)
|
||||
|
||||
# Update function signature default parameter
|
||||
source = source.replace(
|
||||
'def calculate_position_size(self, symbol, stop_loss_pips, max_risk_per_trade=0.02):',
|
||||
'def calculate_position_size(self, symbol, stop_loss_pips, max_risk_per_trade=None):'
|
||||
)
|
||||
|
||||
# Add parameter default from config
|
||||
if 'max_risk_per_trade=None):' in source:
|
||||
# Add check at beginning of function
|
||||
source = source.replace(
|
||||
' """\n Berechnet die Positionsgröße basierend auf Risiko\n """\n',
|
||||
' """\n Berechnet die Positionsgröße basierend auf Risiko\n """\n if max_risk_per_trade is None:\n max_risk_per_trade = TRADING_CONFIG["risk"]["max_risk_per_trade"]\n \n'
|
||||
)
|
||||
|
||||
if source != original:
|
||||
cell_25['source'] = source.split('\n')
|
||||
changes_made += 1
|
||||
print("✅ Updated calculate_position_size to use TRADING_CONFIG")
|
||||
print(f" - return 0.10 → TRADING_CONFIG['lot_sizing']['default_lot']")
|
||||
print(f" - max_risk_per_trade default from config")
|
||||
|
||||
# ============================================================================
|
||||
# CELL 27: execute_trade_v2_adaptive function
|
||||
# ============================================================================
|
||||
print()
|
||||
print("📍 Updating Cell 27: execute_trade_v2_adaptive()")
|
||||
print("-" * 70)
|
||||
|
||||
cell_27 = notebook['cells'][27]
|
||||
if cell_27['cell_type'] == 'code':
|
||||
source = ''.join(cell_27['source'])
|
||||
original = source
|
||||
|
||||
# Update function signature defaults
|
||||
source = source.replace(
|
||||
'def execute_trade_v2_adaptive(\n symbol="XAUUSD",\n atr_mult=1.5,\n base_confidence=60,\n max_risk_per_trade=0.01,',
|
||||
'def execute_trade_v2_adaptive(\n symbol=None,\n atr_mult=None,\n base_confidence=None,\n max_risk_per_trade=None,'
|
||||
)
|
||||
|
||||
# Add config loading at start of function
|
||||
function_start = ' """\n V1.6 Adaptive Complete Trade-Ausführung:\n - Position Control\n - Relaxed Parameter\n - Adaptive Rhythm Integration\n """\n '
|
||||
|
||||
config_init = ''' """\n V1.6 Adaptive Complete Trade-Ausführung:\n - Position Control\n - Relaxed Parameter\n - Adaptive Rhythm Integration\n """\n \n # ========================================================================\n # LOAD DEFAULTS FROM TRADING_CONFIG\n # ========================================================================\n if symbol is None:\n symbol = TRADING_CONFIG["symbols"]["primary"]\n if atr_mult is None:\n atr_mult = TRADING_CONFIG["atr"]["base_multiplier"]\n if base_confidence is None:\n base_confidence = TRADING_CONFIG["confidence"]["base_threshold"]\n if max_risk_per_trade is None:\n max_risk_per_trade = TRADING_CONFIG["risk"]["max_risk_per_trade"]\n if max_positions is None:\n max_positions = TRADING_CONFIG["risk"]["max_positions"]\n \n '''
|
||||
|
||||
source = source.replace(function_start, config_init)
|
||||
|
||||
# Update max_positions default
|
||||
source = source.replace(
|
||||
' max_positions=1,',
|
||||
' max_positions=None,'
|
||||
)
|
||||
|
||||
# Update volume calculations to use config
|
||||
# Line with min(0.2, max(0.10, ...
|
||||
source = source.replace(
|
||||
'volume = round(min(0.2, max(0.10, risk_amount / (adjusted_atr_mult * atr * 100))),2)',
|
||||
'volume = round(min(TRADING_CONFIG["lot_sizing"]["max_lot"], max(TRADING_CONFIG["lot_sizing"]["min_lot"], risk_amount / (adjusted_atr_mult * atr * 100))),2)'
|
||||
)
|
||||
|
||||
# Hardcoded volume = 0.10 fallbacks
|
||||
source = source.replace(
|
||||
' volume = 0.10',
|
||||
' volume = TRADING_CONFIG["lot_sizing"]["default_lot"]'
|
||||
)
|
||||
|
||||
source = source.replace(
|
||||
' volume = 0.10',
|
||||
' volume = TRADING_CONFIG["lot_sizing"]["default_lot"]'
|
||||
)
|
||||
|
||||
source = source.replace(
|
||||
' else:\n volume = 0.10',
|
||||
' else:\n volume = TRADING_CONFIG["lot_sizing"]["default_lot"]'
|
||||
)
|
||||
|
||||
if source != original:
|
||||
cell_27['source'] = source.split('\n')
|
||||
changes_made += 1
|
||||
print("✅ Updated execute_trade_v2_adaptive to use TRADING_CONFIG")
|
||||
print(f" - Function parameters now load from config")
|
||||
print(f" - Volume calculations use config min/max/default")
|
||||
print(f" - All hardcoded values replaced")
|
||||
|
||||
# ============================================================================
|
||||
# CELL 49: ADAPTIVE_COMPLETE_CONFIG (now cell 49 after insertion)
|
||||
# ============================================================================
|
||||
print()
|
||||
print("📍 Updating Cell 49: ADAPTIVE_COMPLETE_CONFIG")
|
||||
print("-" * 70)
|
||||
|
||||
# Find the config cell (search for ADAPTIVE_COMPLETE_CONFIG)
|
||||
config_cell_idx = None
|
||||
for i, cell in enumerate(notebook['cells']):
|
||||
if cell['cell_type'] == 'code':
|
||||
source = ''.join(cell.get('source', []))
|
||||
if 'ADAPTIVE_COMPLETE_CONFIG' in source and "'max_risk_per_trade':" in source:
|
||||
config_cell_idx = i
|
||||
break
|
||||
|
||||
if config_cell_idx is not None:
|
||||
print(f"Found ADAPTIVE_COMPLETE_CONFIG at cell {config_cell_idx}")
|
||||
cell = notebook['cells'][config_cell_idx]
|
||||
source = ''.join(cell['source'])
|
||||
original = source
|
||||
|
||||
# Add comment and reference to centralized config
|
||||
header_comment = '''# ============================================================================
|
||||
# NOTE: This config is DEPRECATED - use TRADING_CONFIG in Cell 6 instead
|
||||
# This is kept for backward compatibility only
|
||||
# ============================================================================
|
||||
|
||||
'''
|
||||
|
||||
# Update max_risk_per_trade to reference centralized config
|
||||
source = source.replace(
|
||||
"'max_risk_per_trade': 0.02, # 2% risk",
|
||||
"'max_risk_per_trade': TRADING_CONFIG['risk']['max_risk_per_trade'], # From centralized config"
|
||||
)
|
||||
|
||||
if not source.startswith('# ====='):
|
||||
source = header_comment + source
|
||||
|
||||
if source != original:
|
||||
cell['source'] = source.split('\n')
|
||||
changes_made += 1
|
||||
print(f"✅ Updated ADAPTIVE_COMPLETE_CONFIG")
|
||||
print(f" - Added deprecation notice")
|
||||
print(f" - Linked max_risk_per_trade to TRADING_CONFIG")
|
||||
|
||||
print()
|
||||
print("=" * 70)
|
||||
print(f"✅ CHANGES COMPLETE: {changes_made} cells updated")
|
||||
print("=" * 70)
|
||||
print()
|
||||
|
||||
if changes_made > 0:
|
||||
# Save notebook
|
||||
with open(notebook_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(notebook, f, indent=1, ensure_ascii=False)
|
||||
|
||||
print("✅ Notebook saved successfully!")
|
||||
print()
|
||||
print("📋 NEXT STEPS:")
|
||||
print("=" * 70)
|
||||
print()
|
||||
print("1. Open Jupyter Notebook")
|
||||
print("2. Restart Kernel (Kernel → Restart)")
|
||||
print("3. Run cells in order:")
|
||||
print(" a) Run Cell 6 (TRADING_CONFIG)")
|
||||
print(" b) Run all other cells")
|
||||
print("4. Verify bot uses 0.10 lot size")
|
||||
print()
|
||||
print("🎯 ALL SETTINGS NOW CENTRALIZED IN CELL 6!")
|
||||
print()
|
||||
print("To change lot size in future:")
|
||||
print(" → Just edit TRADING_CONFIG['lot_sizing'] in Cell 6")
|
||||
print(" → Restart kernel and re-run cells")
|
||||
print()
|
||||
print("=" * 70)
|
||||
else:
|
||||
print("⚠️ No changes were made")
|
||||
Reference in New Issue
Block a user