@@ -10,58 +10,40 @@ def test_mt5_initialization():
10
10
print ('MT5_PORTABLE_PATH:' , os .environ .get ('MT5_PORTABLE_PATH' ))
11
11
print ('Initializing...' )
12
12
13
- # Try initialization using portable path and headless mode
14
- portable_path = os .environ .get ('MT5_PORTABLE_PATH' )
15
- if portable_path :
16
- mt5_path = os .path .join (portable_path , 'terminal64.exe' )
17
- else :
18
- mt5_path = None
19
-
20
- print ('Starting MT5 initialization...' )
21
-
22
13
# Set environment variable directly in Python to ensure it's available
23
14
os .environ ['MT5_HEADLESS' ] = '1'
24
15
25
16
result = mt5 .initialize ()
26
17
print (f'Initial result: { result } , Error code: { mt5 .last_error ()} ' )
27
18
28
19
# If the initial attempt fails, try additional methods
29
- if not result and mt5_path :
30
- print ('Initial attempt failed. Trying with portable path...' )
20
+ if not result :
21
+ print ('Initial attempt failed. Trying with standard path...' )
22
+
23
+ # Attempt 2: initialize with standard path
31
24
result = mt5 .initialize (
32
- path = "C:\\ Program Files\\ MetaTrader 5\\ terminal64 .exe ,
25
+ path = "C:\\ Program Files\\ MetaTrader 5\\ terminal64.exe" ,
33
26
login = 0 ,
34
- password = '' ,
35
- server = '' ,
27
+ password = "" ,
28
+ server = "" ,
36
29
timeout = 60000
37
30
)
38
31
39
- print (f'Result with portable path: { result } , Error code: { mt5 .last_error ()} ' )
32
+ print (f'Result with standard path: { result } , Error code: { mt5 .last_error ()} ' )
40
33
41
34
# If first attempt fails, try alternative approach
42
35
if not result :
43
- print ('First attempt failed. Trying default initialization ...' )
36
+ print ('Second attempt failed. Trying with increased timeout ...' )
44
37
mt5 .shutdown ()
45
38
time .sleep (2 )
46
39
47
- # Attempt 2: default init with timeout
48
- result = mt5 .initialize (timeout = 60000 )
49
- print (f'Result with default init: { result } , Error code: { mt5 .last_error ()} ' )
50
-
51
- # Attempt 3: standard path
52
- if not result :
53
- print ('Second attempt failed. Trying with standard path...' )
54
- mt5 .shutdown ()
55
- time .sleep (2 )
56
- result = mt5 .initialize (
57
- path = 'C:\\ Program Files\\ MetaTrader 5\\ terminal64.exe' ,
58
- timeout = 60000
59
- )
60
- print (f'Result with standard path: { result } , Error code: { mt5 .last_error ()} ' )
40
+ # Attempt 3: default init with longer timeout
41
+ result = mt5 .initialize (timeout = 120000 )
42
+ print (f'Result with increased timeout: { result } , Error code: { mt5 .last_error ()} ' )
61
43
62
- if not result :
63
- print ('All initialization attempts failed' )
64
- return False
44
+ if not result :
45
+ print ('All initialization attempts failed' )
46
+ return False
65
47
66
48
# Check if initialization was successful
67
49
if result :
0 commit comments