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