try again #30
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Test | Test MetaTrader5 Initialization | |
| on: | |
| push: | |
| branches: ['*'] | |
| pull_request: | |
| jobs: | |
| test: | |
| runs-on: windows-latest | |
| timeout-minutes: 15 | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.11' | |
| - name: Download and Install MetaTrader 5 | |
| run: | | |
| # Download MT5 setup | |
| Invoke-WebRequest -Uri "https://download.mql5.com/cdn/web/metaquotes.software.corp/mt5/mt5setup.exe" -OutFile mt5setup.exe | |
| # Install MT5 silently | |
| Start-Process -FilePath .\mt5setup.exe -ArgumentList "/auto" -Wait | |
| # Verify installation | |
| $mtPath = "C:\Program Files\MetaTrader 5\terminal64.exe" | |
| if (Test-Path $mtPath) { | |
| Write-Host "MetaTrader 5 installed successfully" | |
| } else { | |
| Write-Error "MetaTrader 5 installation failed" | |
| exit 1 | |
| } | |
| - name: Install Python dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install MetaTrader5 | |
| - name: Install Xvfb (Virtual Display) | |
| run: | | |
| choco install -y xvfb | |
| - name: Start MT5 with Virtual Display | |
| run: | | |
| # Set display variable to use virtual display | |
| $env:DISPLAY = ":1" | |
| # Start Xvfb | |
| Start-Process -FilePath "C:\ProgramData\chocolatey\bin\xvfb.exe" -ArgumentList "-ac :1 -screen 0 1280x1024x24" -PassThru | |
| Start-Sleep -Seconds 5 | |
| # Start MT5 with virtual display | |
| $process = Start-Process -FilePath "C:\Program Files\MetaTrader 5\terminal64.exe" -ArgumentList "/portable" -PassThru | |
| $processId = $process.Id | |
| Write-Host "Started MetaTrader 5 terminal with PID $processId using virtual display" | |
| Start-Sleep -Seconds 45 # Give more time to initialize | |
| - name: Test MT5 initialization | |
| run: | | |
| # Set display variable to use virtual display | |
| $env:DISPLAY = ":1" | |
| python -c " | |
| import sys, os, time | |
| import MetaTrader5 as mt5 | |
| print(f'MT5 version: {mt5.__version__}') | |
| print('Display environment:', os.environ.get('DISPLAY')) | |
| print('Initializing...') | |
| # Try initialization with increased timeout | |
| result = mt5.initialize( | |
| path='C:\\Program Files\\MetaTrader 5\\terminal64.exe', | |
| login=0, | |
| password='', | |
| server='', | |
| timeout=120000 | |
| ) | |
| print(f'Result: {result}, Error code: {mt5.last_error()}') | |
| if not result: | |
| error_code = mt5.last_error() | |
| error_messages = { | |
| 10001: 'Connection error', | |
| 10002: 'Reconnection error', | |
| 10003: 'Connection timeout', | |
| 10004: 'Internal error', | |
| 10005: 'Invalid version', | |
| 10006: 'Invalid account', | |
| 10007: 'Unsupported trading mode', | |
| 10008: 'No connection' | |
| } | |
| error_desc = error_messages.get(error_code, 'Unknown error') | |
| print(f'Detailed error: {error_desc}') | |
| # Instead of exiting with error, try a different approach | |
| # Recommended by GitHub repo ricardokj/mt5_python_actions | |
| print('Trying alternative initialization approach...') | |
| # Use an approach similar to that used by ricardokj/mt5_python_actions | |
| # This approach relies on portable mode without display requirements | |
| import os | |
| import subprocess | |
| # Initialize without displaying UI | |
| os.environ['MT5_DISABLE_UI'] = '1' | |
| # Try a simpler initialization with fewer parameters | |
| mt5.shutdown() | |
| time.sleep(2) | |
| result = mt5.initialize(timeout=60000) | |
| print(f'Alternative initialization result: {result}, Error: {mt5.last_error()}') | |
| if not result: | |
| print('Both initialization methods failed.') | |
| sys.exit(1) | |
| else: | |
| print('MT5 initialized successfully') | |
| print(f'Terminal info: {mt5.terminal_info()}') | |
| mt5.shutdown() | |
| sys.exit(0) | |
| " |