import sys import os import MetaTrader5 as mt5 from datetime import datetime # Add project root to sys.path current_dir = os.path.dirname(os.path.abspath(__file__)) project_root = os.path.dirname(current_dir) sys.path.insert(0, project_root) try: import src.config as config except ImportError: print("Error: Could not import src.config") config = None def test_connection(): print("=== MT5 Connection Debugger ===") print(f"Python: {sys.version}") print(f"MT5 Package Version: {mt5.__version__}") print(f"MT5 Package Author: {mt5.__author__}") path = getattr(config, 'MT5_PATH', '') login = getattr(config, 'MT5_LOGIN', 0) server = getattr(config, 'MT5_SERVER', '') password = getattr(config, 'MT5_PASSWORD', '') print(f"\nConfigured Path: '{path}'") print(f"Configured Login: {login}") print(f"Configured Server: '{server}'") print(f"Configured Password: {'******' if password else 'Not Set'}") print("\nAttempting Initialization...") if path: if not mt5.initialize(path=path): print(f"FAILED: mt5.initialize(path='{path}')") print(f"Error Code: {mt5.last_error()}") return else: if not mt5.initialize(): print("FAILED: mt5.initialize()") print(f"Error Code: {mt5.last_error()}") print("Tip: If you have multiple MT5 terminals or it's installed in a custom location, set MT5_PATH in src/config.py") return print("SUCCESS: MT5 Initialized.") # Check Terminal Info terminal_info = mt5.terminal_info() if terminal_info: print("\nTerminal Info:") print(f" Path: {terminal_info.path}") print(f" Name: {terminal_info.name}") print(f" Company: {terminal_info.company}") print(f" Connected: {terminal_info.connected}") else: print("WARNING: Could not get terminal info.") # Check Account Info account_info = mt5.account_info() if account_info: print("\nAccount Info (Current):") print(f" Login: {account_info.login}") print(f" Server: {account_info.server}") print(f" Variable Margin: {account_info.margin_so_mode}") else: print("\nWARNING: No account currently logged in or accessible.") # Attempt Login if provided if login and password and server: print(f"\nAttempting Login to {login} on {server}...") authorized = mt5.login(login=login, password=password, server=server) if authorized: print("SUCCESS: Authorized.") account_info = mt5.account_info() print(f" Balance: {account_info.balance}") print(f" Equity: {account_info.equity}") else: print(f"FAILED: Login failed. Error: {mt5.last_error()}") mt5.shutdown() print("\nDisconnected.") if __name__ == "__main__": test_connection()