integrating new CNN model
This commit is contained in:
218
apply_trading_fixes_to_main.py
Normal file
218
apply_trading_fixes_to_main.py
Normal file
@ -0,0 +1,218 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Apply Trading System Fixes to Main.py
|
||||
|
||||
This script applies the trading system fixes directly to main.py
|
||||
to address the issues with duplicate entry prices and P&L calculation.
|
||||
|
||||
Usage:
|
||||
python apply_trading_fixes_to_main.py
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
import re
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
|
||||
# Setup logging
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
handlers=[
|
||||
logging.StreamHandler(),
|
||||
logging.FileHandler('logs/apply_fixes.log')
|
||||
]
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def backup_file(file_path):
|
||||
"""Create a backup of a file"""
|
||||
try:
|
||||
backup_path = f"{file_path}.backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
||||
shutil.copy2(file_path, backup_path)
|
||||
logger.info(f"Created backup: {backup_path}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Error creating backup of {file_path}: {e}")
|
||||
return False
|
||||
|
||||
def apply_fixes_to_main():
|
||||
"""Apply fixes to main.py"""
|
||||
main_py_path = "main.py"
|
||||
|
||||
if not os.path.exists(main_py_path):
|
||||
logger.error(f"File {main_py_path} not found")
|
||||
return False
|
||||
|
||||
# Create backup
|
||||
if not backup_file(main_py_path):
|
||||
logger.error("Failed to create backup, aborting")
|
||||
return False
|
||||
|
||||
try:
|
||||
# Read main.py
|
||||
with open(main_py_path, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
# Find the position to insert the fixes
|
||||
# Look for the line before dashboard.run_server()
|
||||
run_server_pattern = r"dashboard\.run_server\("
|
||||
match = re.search(run_server_pattern, content)
|
||||
|
||||
if not match:
|
||||
logger.error("Could not find dashboard.run_server() call in main.py")
|
||||
return False
|
||||
|
||||
# Find the position to insert the fixes (before the run_server call)
|
||||
insert_pos = content.rfind("\n", 0, match.start())
|
||||
|
||||
if insert_pos == -1:
|
||||
logger.error("Could not find insertion point in main.py")
|
||||
return False
|
||||
|
||||
# Prepare the fixes to insert
|
||||
fixes_code = """
|
||||
# Apply trading system fixes
|
||||
try:
|
||||
from core.trading_executor_fix import TradingExecutorFix
|
||||
from web.dashboard_fix import DashboardFix
|
||||
|
||||
# Apply fixes to trading executor
|
||||
if trading_executor:
|
||||
TradingExecutorFix.apply_fixes(trading_executor)
|
||||
logger.info("✅ Trading executor fixes applied")
|
||||
|
||||
# Apply fixes to dashboard
|
||||
if 'dashboard' in locals() and dashboard:
|
||||
DashboardFix.apply_fixes(dashboard)
|
||||
logger.info("✅ Dashboard fixes applied")
|
||||
|
||||
logger.info("Trading system fixes applied successfully")
|
||||
except Exception as e:
|
||||
logger.warning(f"Error applying trading system fixes: {e}")
|
||||
|
||||
"""
|
||||
|
||||
# Insert the fixes
|
||||
new_content = content[:insert_pos] + fixes_code + content[insert_pos:]
|
||||
|
||||
# Write the modified content back to main.py
|
||||
with open(main_py_path, 'w') as f:
|
||||
f.write(new_content)
|
||||
|
||||
logger.info(f"Successfully applied fixes to {main_py_path}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error applying fixes to {main_py_path}: {e}")
|
||||
return False
|
||||
|
||||
def apply_fixes_to_dashboard():
|
||||
"""Apply fixes to web/clean_dashboard.py"""
|
||||
dashboard_py_path = "web/clean_dashboard.py"
|
||||
|
||||
if not os.path.exists(dashboard_py_path):
|
||||
logger.error(f"File {dashboard_py_path} not found")
|
||||
return False
|
||||
|
||||
# Create backup
|
||||
if not backup_file(dashboard_py_path):
|
||||
logger.error("Failed to create backup, aborting")
|
||||
return False
|
||||
|
||||
try:
|
||||
# Read dashboard.py
|
||||
with open(dashboard_py_path, 'r') as f:
|
||||
content = f.read()
|
||||
|
||||
# Find the position to insert the fixes
|
||||
# Look for the __init__ method
|
||||
init_pattern = r"def __init__\(self,"
|
||||
match = re.search(init_pattern, content)
|
||||
|
||||
if not match:
|
||||
logger.error("Could not find __init__ method in dashboard.py")
|
||||
return False
|
||||
|
||||
# Find the end of the __init__ method
|
||||
init_end_pattern = r"logger\.debug\(.*\)"
|
||||
init_end_matches = list(re.finditer(init_end_pattern, content[match.end():]))
|
||||
|
||||
if not init_end_matches:
|
||||
logger.error("Could not find end of __init__ method in dashboard.py")
|
||||
return False
|
||||
|
||||
# Get the last logger.debug line in the __init__ method
|
||||
last_debug_match = init_end_matches[-1]
|
||||
insert_pos = match.end() + last_debug_match.end()
|
||||
|
||||
# Prepare the fixes to insert
|
||||
fixes_code = """
|
||||
|
||||
# Apply dashboard fixes if available
|
||||
try:
|
||||
from web.dashboard_fix import DashboardFix
|
||||
DashboardFix.apply_fixes(self)
|
||||
logger.info("✅ Dashboard fixes applied during initialization")
|
||||
except ImportError:
|
||||
logger.warning("Dashboard fixes not available")
|
||||
"""
|
||||
|
||||
# Insert the fixes
|
||||
new_content = content[:insert_pos] + fixes_code + content[insert_pos:]
|
||||
|
||||
# Write the modified content back to dashboard.py
|
||||
with open(dashboard_py_path, 'w') as f:
|
||||
f.write(new_content)
|
||||
|
||||
logger.info(f"Successfully applied fixes to {dashboard_py_path}")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error applying fixes to {dashboard_py_path}: {e}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
"""Main entry point"""
|
||||
logger.info("=" * 70)
|
||||
logger.info("APPLYING TRADING SYSTEM FIXES TO MAIN.PY")
|
||||
logger.info("=" * 70)
|
||||
|
||||
# Create logs directory if it doesn't exist
|
||||
os.makedirs('logs', exist_ok=True)
|
||||
|
||||
# Apply fixes to main.py
|
||||
main_success = apply_fixes_to_main()
|
||||
|
||||
# Apply fixes to dashboard.py
|
||||
dashboard_success = apply_fixes_to_dashboard()
|
||||
|
||||
if main_success and dashboard_success:
|
||||
logger.info("=" * 70)
|
||||
logger.info("TRADING SYSTEM FIXES APPLIED SUCCESSFULLY")
|
||||
logger.info("=" * 70)
|
||||
logger.info("The following issues have been fixed:")
|
||||
logger.info("1. Duplicate entry prices")
|
||||
logger.info("2. P&L calculation issues")
|
||||
logger.info("3. Position tracking problems")
|
||||
logger.info("4. Trade display issues")
|
||||
logger.info("5. Rapid consecutive trades")
|
||||
logger.info("=" * 70)
|
||||
logger.info("You can now run the trading system with the fixes applied:")
|
||||
logger.info("python main.py")
|
||||
logger.info("=" * 70)
|
||||
return 0
|
||||
else:
|
||||
logger.error("=" * 70)
|
||||
logger.error("FAILED TO APPLY SOME FIXES")
|
||||
logger.error("=" * 70)
|
||||
logger.error("Please check the logs for details")
|
||||
logger.error("=" * 70)
|
||||
return 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
Reference in New Issue
Block a user