Skip to content

Commit bed2ce0

Browse files
committed
find the right path
1 parent 833b95c commit bed2ce0

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

test/integration/test_mt5_initialization.py

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,68 @@ def test_mt5_initialization():
1212

1313
# Set environment variable directly in Python to ensure it's available
1414
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
1640
result = mt5.initialize()
1741
print(f'Initial result: {result}, Error code: {mt5.last_error()}')
1842

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
2267

23-
# Attempt 2: initialize with standard path
24-
result = mt5.initialize(
25-
path="C:\Program Files\MetaTrader 5\terminal64.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
3569
if not result:
36-
print('Second attempt failed. Trying with increased timeout...')
70+
print('All path attempts failed. Trying with increased timeout...')
3771
mt5.shutdown()
3872
time.sleep(2)
3973

40-
# Attempt 3: default init with longer timeout
74+
# Final attempt: default init with longer timeout
4175
result = mt5.initialize(timeout=120000)
4276
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
4777

4878
# Check if initialization was successful
4979
if result:

0 commit comments

Comments
 (0)