File size: 3,032 Bytes
c99df4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

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()