@@ -12,38 +12,68 @@ def test_mt5_initialization():
12
12
13
13
# Set environment variable directly in Python to ensure it's available
14
14
os .environ ['MT5_HEADLESS' ] = '1'
15
-
15
+
16
+ # Find MetaTrader 5 installation paths
17
+ possible_paths = [
18
+ r"C:\Program Files\MetaTrader 5\terminal64.exe" ,
19
+ r"C:\Program Files (x86)\MetaTrader 5\terminal64.exe" ,
20
+ os .path .join (os .environ .get ('APPDATA' , '' ), 'MetaTrader 5' , 'terminal64.exe' ),
21
+ os .path .join (os .environ .get ('LOCALAPPDATA' , '' ), 'MetaTrader 5' , 'terminal64.exe' ),
22
+ ]
23
+
24
+ # Add portable path if available
25
+ portable_path = os .environ .get ('MT5_PORTABLE_PATH' )
26
+ if portable_path :
27
+ possible_paths .insert (0 , os .path .join (portable_path , 'terminal64.exe' ))
28
+
29
+ # Check which paths exist
30
+ existing_paths = []
31
+ for path in possible_paths :
32
+ if os .path .exists (path ):
33
+ existing_paths .append (path )
34
+ print (f"Found MetaTrader 5 at: { path } " )
35
+
36
+ if not existing_paths :
37
+ print ("WARNING: No MetaTrader 5 installation found in common locations!" )
38
+
39
+ # Attempt 1: Initialize with minimal parameters
16
40
result = mt5 .initialize ()
17
41
print (f'Initial result: { result } , Error code: { mt5 .last_error ()} ' )
18
42
19
- # If the initial attempt fails, try additional methods
20
- if not result :
21
- print ('Initial attempt failed. Trying with standard path...' )
43
+ # Try each found path if initial attempt fails
44
+ if not result and existing_paths :
45
+ for idx , path in enumerate (existing_paths , 1 ):
46
+ print (f'Attempt { idx + 1 } : Trying with path: { path } ' )
47
+
48
+ # Ensure previous attempt is cleaned up
49
+ if idx > 1 :
50
+ mt5 .shutdown ()
51
+ time .sleep (2 )
52
+
53
+ # Try with this path
54
+ result = mt5 .initialize (
55
+ path = path ,
56
+ login = 0 ,
57
+ password = "" ,
58
+ server = "" ,
59
+ timeout = 60000
60
+ )
61
+
62
+ print (f'Result with path { path } : { result } , Error code: { mt5 .last_error ()} ' )
63
+
64
+ if result :
65
+ print (f'Successfully initialized with path: { path } ' )
66
+ break
22
67
23
- # Attempt 2: initialize with standard path
24
- result = mt5 .initialize (
25
- path = "C:\Program Files\MetaTrader 5\t erminal64.exe" ,
26
- login = 0 ,
27
- password = "" ,
28
- server = "" ,
29
- timeout = 60000
30
- )
31
-
32
- print (f'Result with standard path: { result } , Error code: { mt5 .last_error ()} ' )
33
-
34
- # If first attempt fails, try alternative approach
68
+ # If all path attempts fail, try with increased timeout
35
69
if not result :
36
- print ('Second attempt failed. Trying with increased timeout...' )
70
+ print ('All path attempts failed. Trying with increased timeout...' )
37
71
mt5 .shutdown ()
38
72
time .sleep (2 )
39
73
40
- # Attempt 3 : default init with longer timeout
74
+ # Final attempt : default init with longer timeout
41
75
result = mt5 .initialize (timeout = 120000 )
42
76
print (f'Result with increased timeout: { result } , Error code: { mt5 .last_error ()} ' )
43
-
44
- if not result :
45
- print ('All initialization attempts failed' )
46
- return False
47
77
48
78
# Check if initialization was successful
49
79
if result :
0 commit comments