@@ -40,88 +40,92 @@ jobs:
4040 python -m pip install --upgrade pip
4141 pip install MetaTrader5
4242
43- - name : Install Xvfb (Virtual Display)
43+ - name : Create MT5 portable configuration
4444 run : |
45- choco install -y xvfb
45+ # Create a portable MT5 directory
46+ mkdir -p "$env:APPDATA\MetaTrader5Portable"
4647
47- - name : Start MT5 with Virtual Display
48- run : |
49- # Set display variable to use virtual display
50- $env:DISPLAY = ":1"
48+ # Set environment variables to make MT5 run in headless mode
49+ echo "MT5_DISABLE_UI=1" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
50+ echo "MT5_PORTABLE_PATH=$env:APPDATA\MetaTrader5Portable" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
5151
52- # Start Xvfb
53- Start-Process -FilePath "C:\ProgramData\chocolatey\bin\xvfb.exe" -ArgumentList "-ac :1 -screen 0 1280x1024x24" -PassThru
54- Start-Sleep -Seconds 5
52+ # Copy MT5 files to portable location if needed
53+ if (-not (Test-Path "$env:APPDATA\MetaTrader5Portable\terminal64.exe")) {
54+ Copy-Item -Path "C:\Program Files\MetaTrader 5\*" -Destination "$env:APPDATA\MetaTrader5Portable\" -Recurse
55+ }
5556
56- # Start MT5 with virtual display
57- $process = Start-Process -FilePath "C:\Program Files\MetaTrader 5\terminal64.exe" -ArgumentList "/portable" -PassThru
58- $processId = $process.Id
59- Write-Host "Started MetaTrader 5 terminal with PID $processId using virtual display"
60- Start-Sleep -Seconds 45 # Give more time to initialize
57+ # Verify portable setup
58+ if (Test-Path "$env:APPDATA\MetaTrader5Portable\terminal64.exe") {
59+ Write-Host "MetaTrader 5 portable setup created successfully"
60+ } else {
61+ Write-Error "MetaTrader 5 portable setup failed"
62+ exit 1
63+ }
6164
6265 - name : Test MT5 initialization
6366 run : |
64- # Set display variable to use virtual display
65- $env:DISPLAY = ":1"
66-
6767 python -c "
6868 import sys, os, time
6969 import MetaTrader5 as mt5
7070
7171 print(f'MT5 version: {mt5.__version__}')
72- print('Display environment:', os.environ.get('DISPLAY'))
72+ print('MT5_DISABLE_UI:', os.environ.get('MT5_DISABLE_UI'))
73+ print('MT5_PORTABLE_PATH:', os.environ.get('MT5_PORTABLE_PATH'))
7374 print('Initializing...')
7475
75- # Try initialization with increased timeout
76+ # Try initialization using portable path and headless mode
77+ portable_path = os.environ.get('MT5_PORTABLE_PATH')
78+ mt5_path = os.path.join(portable_path, 'terminal64.exe')
79+
80+ # First attempt: using portable path
7681 result = mt5.initialize(
77- path='C:\\Program Files\\MetaTrader 5\\terminal64.exe' ,
82+ path=mt5_path ,
7883 login=0,
7984 password='',
8085 server='',
81- timeout=120000
86+ timeout=60000
8287 )
8388
84- print(f'Result: {result}, Error code: {mt5.last_error()}')
89+ print(f'Result with portable path : {result}, Error code: {mt5.last_error()}')
8590
91+ # If first attempt fails, try alternative approach
8692 if not result:
87- error_code = mt5.last_error()
88- error_messages = {
89- 10001: 'Connection error',
90- 10002: 'Reconnection error',
91- 10003: 'Connection timeout',
92- 10004: 'Internal error',
93- 10005: 'Invalid version',
94- 10006: 'Invalid account',
95- 10007: 'Unsupported trading mode',
96- 10008: 'No connection'
97- }
98- error_desc = error_messages.get(error_code, 'Unknown error')
99- print(f'Detailed error: {error_desc}')
100-
101- # Instead of exiting with error, try a different approach
102- # Recommended by GitHub repo ricardokj/mt5_python_actions
103- print('Trying alternative initialization approach...')
104-
105- # Use an approach similar to that used by ricardokj/mt5_python_actions
106- # This approach relies on portable mode without display requirements
107- import os
108- import subprocess
109-
110- # Initialize without displaying UI
111- os.environ['MT5_DISABLE_UI'] = '1'
112-
113- # Try a simpler initialization with fewer parameters
93+ print('First attempt failed. Trying default initialization...')
11494 mt5.shutdown()
11595 time.sleep(2)
96+
97+ # Second attempt: default init
11698 result = mt5.initialize(timeout=60000)
117- print(f'Alternative initialization result : {result}, Error: {mt5.last_error()}')
99+ print(f'Result with default init : {result}, Error code : {mt5.last_error()}')
118100
101+ # Third attempt: standard path
119102 if not result:
120- print('Both initialization methods failed.')
121- sys.exit(1)
103+ print('Second attempt failed. Trying with standard path...')
104+ mt5.shutdown()
105+ time.sleep(2)
106+ result = mt5.initialize(
107+ path='C:\\\\Program Files\\\\MetaTrader 5\\\\terminal64.exe',
108+ timeout=60000
109+ )
110+ print(f'Result with standard path: {result}, Error code: {mt5.last_error()}')
111+
112+ if not result:
113+ print('All initialization attempts failed')
114+ sys.exit(1)
115+
116+ print('MT5 initialized successfully')
117+ account_info = mt5.account_info()
118+ if account_info is not None:
119+ print(f'Account info: {account_info}')
122120 else:
123- print('MT5 initialized successfully')
124- print(f'Terminal info: {mt5.terminal_info()}')
125- mt5.shutdown()
126- sys.exit(0)
121+ print('No account info available (demo mode)')
122+
123+ symbol_info = mt5.symbol_info('EURUSD')
124+ if symbol_info is not None:
125+ print(f'Symbol info available: {symbol_info.name}')
126+ else:
127+ print('Symbol info not available')
128+
129+ mt5.shutdown()
130+ sys.exit(0)
127131 "
0 commit comments