@@ -40,88 +40,92 @@ jobs:
40
40
python -m pip install --upgrade pip
41
41
pip install MetaTrader5
42
42
43
- - name : Install Xvfb (Virtual Display)
43
+ - name : Create MT5 portable configuration
44
44
run : |
45
- choco install -y xvfb
45
+ # Create a portable MT5 directory
46
+ mkdir -p "$env:APPDATA\MetaTrader5Portable"
46
47
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
51
51
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
+ }
55
56
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
+ }
61
64
62
65
- name : Test MT5 initialization
63
66
run : |
64
- # Set display variable to use virtual display
65
- $env:DISPLAY = ":1"
66
-
67
67
python -c "
68
68
import sys, os, time
69
69
import MetaTrader5 as mt5
70
70
71
71
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'))
73
74
print('Initializing...')
74
75
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
76
81
result = mt5.initialize(
77
- path='C:\\Program Files\\MetaTrader 5\\terminal64.exe' ,
82
+ path=mt5_path ,
78
83
login=0,
79
84
password='',
80
85
server='',
81
- timeout=120000
86
+ timeout=60000
82
87
)
83
88
84
- print(f'Result: {result}, Error code: {mt5.last_error()}')
89
+ print(f'Result with portable path : {result}, Error code: {mt5.last_error()}')
85
90
91
+ # If first attempt fails, try alternative approach
86
92
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...')
114
94
mt5.shutdown()
115
95
time.sleep(2)
96
+
97
+ # Second attempt: default init
116
98
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()}')
118
100
101
+ # Third attempt: standard path
119
102
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}')
122
120
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)
127
131
"
0 commit comments