Skip to content

Commit ffa96f9

Browse files
committed
integration test from script
1 parent 435dd7c commit ffa96f9

File tree

2 files changed

+94
-84
lines changed

2 files changed

+94
-84
lines changed

.github/workflows/test-metatrader5-integration.yml

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -64,87 +64,5 @@ jobs:
6464
6565
- name: Test MT5 initialization
6666
run: |
67-
# Create a Python script file
68-
$pythonScript = @'
69-
import sys, os, time
70-
import MetaTrader5 as mt5
71-
72-
print(f'MT5 version: {mt5.__version__}')
73-
print('MT5_HEADLESS:', os.environ.get('MT5_HEADLESS'))
74-
print('MT5_PORTABLE_PATH:', os.environ.get('MT5_PORTABLE_PATH'))
75-
print('Initializing...')
76-
77-
# Try initialization using portable path and headless mode
78-
portable_path = os.environ.get('MT5_PORTABLE_PATH')
79-
mt5_path = os.path.join(portable_path, 'terminal64.exe')
80-
81-
print('Starting MT5 initialization...')
82-
83-
# Set environment variable directly in Python
84-
os.environ['MT5_HEADLESS'] = '1'
85-
86-
# Initialize with minimal parameters
87-
result = mt5.initialize()
88-
89-
print(f'Initial result: {result}, Error code: {mt5.last_error()}')
90-
91-
# If the initial attempt fails, try additional methods
92-
if not result:
93-
print('Initial attempt failed. Trying with portable path...')
94-
result = mt5.initialize(
95-
path=mt5_path,
96-
login=0,
97-
password='',
98-
server='',
99-
timeout=60000
100-
)
101-
102-
print(f'Result with portable path: {result}, Error code: {mt5.last_error()}')
103-
104-
# If first attempt fails, try alternative approach
105-
if not result:
106-
print('First attempt failed. Trying default initialization...')
107-
mt5.shutdown()
108-
time.sleep(2)
109-
110-
# Second attempt: default init
111-
result = mt5.initialize(timeout=60000)
112-
print(f'Result with default init: {result}, Error code: {mt5.last_error()}')
113-
114-
# Third attempt: standard path
115-
if not result:
116-
print('Second attempt failed. Trying with standard path...')
117-
mt5.shutdown()
118-
time.sleep(2)
119-
result = mt5.initialize(
120-
path='C:\\Program Files\\MetaTrader 5\\terminal64.exe',
121-
timeout=60000
122-
)
123-
print(f'Result with standard path: {result}, Error code: {mt5.last_error()}')
124-
125-
if not result:
126-
print('All initialization attempts failed')
127-
sys.exit(1)
128-
129-
print('MT5 initialized successfully')
130-
account_info = mt5.account_info()
131-
if account_info is not None:
132-
print(f'Account info: {account_info}')
133-
else:
134-
print('No account info available (demo mode)')
135-
136-
symbol_info = mt5.symbol_info('EURUSD')
137-
if symbol_info is not None:
138-
print(f'Symbol info available: {symbol_info.name}')
139-
else:
140-
print('Symbol info not available')
141-
142-
mt5.shutdown()
143-
sys.exit(0)
144-
'@
145-
146-
# Write the script to a file
147-
$pythonScript | Out-File -FilePath "test_mt5.py" -Encoding utf8
148-
149-
# Run the script
150-
python test_mt5.py
67+
# Run the MT5 initialization test script from the repository
68+
python test/integration/test_mt5_initialization.py
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import sys
2+
import os
3+
import time
4+
import MetaTrader5 as mt5
5+
6+
def test_mt5_initialization():
7+
"""Test MetaTrader5 initialization in various ways until one succeeds."""
8+
print(f'MT5 version: {mt5.__version__}')
9+
print('MT5_HEADLESS:', os.environ.get('MT5_HEADLESS'))
10+
print('MT5_PORTABLE_PATH:', os.environ.get('MT5_PORTABLE_PATH'))
11+
print('Initializing...')
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+
# Set environment variable directly in Python to ensure it's available
23+
os.environ['MT5_HEADLESS'] = '1'
24+
25+
result = mt5.initialize()
26+
print(f'Initial result: {result}, Error code: {mt5.last_error()}')
27+
28+
# If the initial attempt fails, try additional methods
29+
if not result and mt5_path:
30+
print('Initial attempt failed. Trying with portable path...')
31+
result = mt5.initialize(
32+
path=mt5_path,
33+
login=0,
34+
password='',
35+
server='',
36+
timeout=60000
37+
)
38+
39+
print(f'Result with portable path: {result}, Error code: {mt5.last_error()}')
40+
41+
# If first attempt fails, try alternative approach
42+
if not result:
43+
print('First attempt failed. Trying default initialization...')
44+
mt5.shutdown()
45+
time.sleep(2)
46+
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()}')
61+
62+
if not result:
63+
print('All initialization attempts failed')
64+
return False
65+
66+
# Check if initialization was successful
67+
if result:
68+
print('MT5 initialized successfully')
69+
70+
# Try to get account info as test
71+
account_info = mt5.account_info()
72+
if account_info is not None:
73+
print(f'Account info: {account_info}')
74+
else:
75+
print('No account info available (demo mode)')
76+
77+
# Try to get symbol info as test
78+
symbol_info = mt5.symbol_info('EURUSD')
79+
if symbol_info is not None:
80+
print(f'Symbol info available: {symbol_info.name}')
81+
else:
82+
print('Symbol info not available')
83+
84+
# Clean shutdown
85+
mt5.shutdown()
86+
return True
87+
88+
return False
89+
90+
if __name__ == "__main__":
91+
success = test_mt5_initialization()
92+
sys.exit(0 if success else 1)

0 commit comments

Comments
 (0)