1- name : Test | Test MetaTrader5 Initialization
1+ name : MetaTrader5 CI Headless Setup
22
3- on :
4- push :
5- branches : ['*']
6- pull_request :
3+ on : [push]
74
85jobs :
9- test :
6+ build :
107 runs-on : windows-latest
11- timeout-minutes : 15
12-
138 steps :
14- - uses : actions/checkout@v4
9+ - name : Checkout repository
10+ uses : actions/checkout@v4
1511
1612 - name : Set up Python
1713 uses : actions/setup-python@v4
1814 with :
1915 python-version : ' 3.11'
2016
21- - name : Direct Download and Install MetaTrader 5
22- run : |
23- # Download latest MT5 setup directly from official source
24- Invoke-WebRequest -Uri "https://download.mql5.com/cdn/web/metaquotes.software.corp/mt5/mt5setup.exe" -OutFile mt5setup.exe
25-
26- # Install with modern silent parameters
27- Start-Process -FilePath .\mt5setup.exe -ArgumentList "/auto", "/portable" -Wait
28-
29- # Check standard and portable locations for both 32-bit and 64-bit versions
30- $possiblePaths = @(
31- "C:\Program Files\MetaTrader 5\terminal64.exe",
32- "C:\Program Files\MetaTrader 5\terminal.exe",
33- ".\MetaTrader 5\terminal64.exe",
34- ".\MetaTrader 5\terminal.exe",
35- "$env:APPDATA\MetaQuotes\Terminal\MetaTrader5\terminal64.exe",
36- "$env:APPDATA\MetaQuotes\Terminal\MetaTrader5\terminal.exe"
37- )
38-
39- $found = $false
40- foreach ($path in $possiblePaths) {
41- if (Test-Path $path) {
42- Write-Host "MetaTrader 5 found at: $path"
43- $found = $true
44- break
45- }
46- }
47-
48- if (-not $found) {
49- Write-Error "MetaTrader 5 installation not found in expected locations"
50-
51- # Search for installation
52- Write-Host "Searching for MT5 installation..."
53- $foundPaths = Get-ChildItem -Path "C:\" -Filter "terminal*.exe" -Recurse -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName
54- foreach ($path in $foundPaths) {
55- Write-Host "Found MT5 at: $path" "C:\" -Filter "terminal.exe" -Recurse -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName
56- }foundPaths += Get-ChildItem -Path "C:\" -Filter "terminal64.exe" -Recurse -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName
57-
58- # Add details about system architecture
59- $architecture = [System.Environment]::Is64BitOperatingSystem ? "64-bit" : "32-bit"
60- Write-Host "System Architecture: $architecture"
61- Write-Host "PowerShell Architecture: $([IntPtr]::Size * 8)-bit"
62- # Add details about system architecture
63- exit 1tecture = [System.Environment]::Is64BitOperatingSystem ? "64-bit" : "32-bit"
64- } Write-Host "System Architecture: $architecture"
65- Write-Host "PowerShell Architecture: $([IntPtr]::Size * 8)-bit"
66- - name : Install Python dependencies
67- run : |it 1
68- python -m pip install --upgrade pip
69- pip install MetaTrader5 pytest
70- - name : Install Python dependencies
71- - name : Configure MT5 for headless operation
72- run : |on -m pip install --upgrade pip
73- # Create data directory for portable mode
74- $mt5DataDir = ".\MT5_Data"
75- New-Item -Path $mt5DataDir -ItemType Directory -Force
76- n: |
77- # Set environment variables for headless operation
78- echo "MT5_HEADLESS=1" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
79- echo "MT5_TIMEOUT=60000" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Appendd
80- echo "MT5_DEBUG=1" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
81- # Set environment variables for headless operation
82- # Find MT5 path to pass to the test - check both 32-bit and 64-bit versions in a 32-bit environmentAppend
83- $mt5Path = ""EOUT=60000" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
84- $possiblePaths = @(| Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
85- "C:\Program Files\MetaTrader 5\terminal64.exe",
86- "C:\Program Files\MetaTrader 5\terminal.exe",h 32-bit and 64-bit versions
87- ".\MetaTrader 5\terminal64.exe",
88- ".\MetaTrader 5\terminal.exe",nal\MetaTrader5\terminal.exe",
89- "$env:APPDATA\MetaQuotes\Terminal\MetaTrader5\terminal64.exe",
90- "$env:APPDATA\MetaQuotes\Terminal\MetaTrader5\terminal.exe"
91- ) ".\MetaTrader 5\terminal64.exe", ".\MetaTrader 5\terminal64.exe",
92- "$env:APPDATA\MetaQuotes\Terminal\MetaTrader5\terminal64.exe" ".\MetaTrader 5\terminal.exe",
93- foreach ($path in $possiblePaths) {
94- if (Test-Path $path) {terminal.exe"
95- $mt5Path = $pathossiblePaths) {
96- $mt5Dir = Split-Path -Parent $mt5Path
97- echo "MT5_PATH=$mt5Path" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
98- echo "MT5_DIR=$mt5Dir" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
99- Write-Host "Setting MT5_PATH to $mt5Path"Path $env:GITHUB_ENV -Encoding utf8 -Append
100- Write-Host "Setting MT5_DIR to $mt5Dir"Path $env:GITHUB_ENV -Encoding utf8 -Append
101- Write-Host "Setting MT5_PATH to $mt5Path"echo "MT5_PATH=$mt5Path" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
102- # Create minimal configuration file to help with headless mode
103- $configLines = @(t5Path"
104- '[Common]',mal configuration file to help with headless modeetting MT5_DIR to $mt5Dir"
105- 'Login=0', = @(
106- 'ProxyEnable=0',uration file to help with headless mode
107- 'CertCheckDisable=1',
108- 'AutoUpdate=0',,
109- 'DisableStartupCompany=1',
110- 'EnableOpenCL=0',
111- 'News=0',tartupCompany=1',kDisable=1',
112- 'StartupCompany=0',
113- 'Community=0',any=1',
114- 'AutoUpdate.Enable=0'
115- ) 'Community=0', 'News=0',
116- $configPath = Join-Path $mt5Dir "config\default.ini"
117- New-Item -Path (Split-Path -Parent $configPath) -ItemType Directory -Force
118- $configLines | Out-File -FilePath $configPath -Encoding utf8
119- Write-Host "Created default config at $configPath"temType Directory -Force
120- $configLines | Out-File -FilePath $configPath -Encoding utf8$configPath = Join-Path $mt5Dir "config\default.ini"
121- break-Host "Created default config at $configPath"tem -Path (Split-Path -Parent $configPath) -ItemType Directory -Force
122- } $configLines | Out-File -FilePath $configPath -Encoding utf8
123- } break Write-Host "Created default config at $configPath"
124- }
125- - name : Configure system for MT5 communication
17+ - name : Download MetaTrader5 Installer
12618 run : |
127- # Create registry settings that can help with IPC communication
128- Write-Host "Configuring system registry for MT5 communication"
129- # Create registry settings that can help with IPC communicationme: Configure system for MT5 communication
130- # Ensure the MetaTrader registry key exists MT5 communication"
131- $registryPath = "HKCU:\Software\MetaQuotes"
132- if (-not (Test-Path $registryPath)) {existsry for MT5 communication"
133- New-Item -Path $registryPath -Force | Out-Null
134- }f (-not (Test-Path $registryPath)) { Ensure the MetaTrader registry key exists
135- New-Item -Path $registryPath -Force | Out-Null$registryPath = "HKCU:\Software\MetaQuotes"
136- # Configure Terminal settings if needed
137- $terminalPath = "$registryPath\Terminal"
138- if (-not (Test-Path $terminalPath)) {ed
139- New-Item -Path $terminalPath -Force | Out-Null
140- }f (-not (Test-Path $terminalPath)) { Configure Terminal settings if needed
141- New-Item -Path $terminalPath -Force | Out-Null$terminalPath = "$registryPath\Terminal"
142- # Set some registry values that may help with IPC
143- Set-ItemProperty -Path $terminalPath -Name "HeadlessMode" -Value 1 -Type DWORD -Force
144- Set-ItemProperty -Path $terminalPath -Name "AllowDllImport" -Value 1 -Type DWORD -Force
145- Set-ItemProperty -Path $terminalPath -Name "HeadlessMode" -Value 1 -Type DWORD -Force
146- # Check if Python DLLs are accessible from the MT5 installationlue 1 -Type DWORD -Force
147- $pythonDllPath = Join-Path (Split-Path -Parent (Get-Command python).Path) "python311.dll"
148- if (Test-Path $pythonDllPath) {ssible from the MT5 installationlPath -Name "AllowDllImport" -Value 1 -Type DWORD -Force
149- Write-Host "Python DLL found at: $pythonDllPath"Command python).Path) "python311.dll"
150- # Copy Python DLL to MT5 directory to help with IPC
151- if (Test-Path $env:MT5_DIR) {at: $pythonDllPath"-Path -Parent (Get-Command python).Path) "python311.dll"
152- Copy-Item -Path $pythonDllPath -Destination $env:MT5_DIR -Force
153- Write-Host "Copied Python DLL to MT5 directory"
154- } Copy-Item -Path $pythonDllPath -Destination $env:MT5_DIR -Force Copy Python DLL to MT5 directory to help with IPC
155- } else {Write-Host "Copied Python DLL to MT5 directory"Test-Path $env:MT5_DIR) {
156- Write-Host "Python DLL not found at expected location"
157- } else { Write-Host "Copied Python DLL to MT5 directory"
158- Write-Host "Python DLL not found at expected location" }
159- - name : Debug environment
160- run : |Python DLL not found at expected location"
161- Write-Host "Environment variables:"
162- Get-ChildItem Env:
163- Write-Host "Environment variables:"me: Debug environment
164- Write-Host "MT5 directory contents:"
165- if (Test-Path $env:MT5_DIR) {
166- Get-ChildItem -Path $env:MT5_DIR -Recurse -Depth 1 | Select-Object FullName
167- }f (Test-Path $env:MT5_DIR) {
168- Get-ChildItem -Path $env:MT5_DIR -Recurse -Depth 1 | Select-Object FullNameWrite-Host "MT5 directory contents:"
169- Write-Host "System processes:"
170- Get-Process | Where-Object { $_.Name -like "*MetaTrader*" -or $_.Name -like "*terminal*" } | Format-Table -AutoSize
171- Write-Host "System processes:" }
172- - name : Test MT5 initialization{ $_.Name -like "*MetaTrader*" -or $_.Name -like "*terminal*" } | Format-Table -AutoSize
173- run : |em processes:"
174- # Kill any existing MT5 processesike "*MetaTrader*" -or $_.Name -like "*terminal*" } | Format-Table -AutoSize
175- Get-Process | Where-Object { $_.Name -like "*MetaTrader*" -or $_.Name -like "*terminal*" } | ForEach-Object {
176- Write-Host "Killing process $($_.Name) with ID $($_.Id)"
177- Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue $_.Name -like "*terminal*" } | ForEach-Object {
178- } Write-Host "Killing process $($_.Name) with ID $($_.Id)" Kill any existing MT5 processes
179- Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue Get-Process | Where-Object { $_.Name -like "*MetaTrader*" -or $_.Name -like "*terminal*" } | ForEach-Object {
180- # Give system time to clean up processes
181- Start-Sleep -Seconds 5 SilentlyContinue
182- # Give system time to clean up processes}
183- # Set explicit environment variables that can help with IPC
184- $env:MT5_IPC_HOST = "localhost"
185- $env:MT5_IPC_PORT = "8228" # Use a standard port for IPC
186- $env:MT5_64BIT = "0" # Force 32-bit mode for the Python module$env:MT5_IPC_HOST = "localhost"
187-
188- # Create a custom settings file for the Python MT5 test
189- $settingsContent = @"work better in CI
190- [MT5]$mt5Process = Start-Process -FilePath $env:MT5_PATH -ArgumentList "/portable", "/config:default.ini", "/skipupdate", "/minimize" -PassThru
191- path=$($env:MT5_PATH)d)"to work better in CI
192- debug=1_PATH -ArgumentList "/portable", "/config:default.ini", "/skipupdate", "/minimize" -PassThru
193- timeout=120000# Give MT5 more time to initialize - increase wait timeWrite-Host "Started MT5 process with ID $($mt5Process.Id)"
194- headless=1
195- ipc_host=localhost
196- ipc_port=8228 still running and get detailsds 30
197- " @tinue
198- Set-Content -Path " .\mt5_settings.ini" -Value $settingsContent
199- Host "MT5 process is still running with ID $($mt5Process.Id)"ing = Get-Process -Id $mt5Process.Id -ErrorAction SilentlyContinue
200- # Start MT5 manually with a combination of flags known to work better in CIst | Out-String)"
201- $startArgs = @("/portable") else { Write-Host "MT5 process is still running with ID $($mt5Process.Id)"
202- Write-Host "Warning : MT5 process is no longer running" Write-Host "Process details: $($mt5Running | Format-List | Out-String)"
203- # Add config argument if it exists
204- $configPath = Join-Path (Split-Path -Parent $env:MT5_PATH) "config\default.ini"
205- if (Test-Path $configPath) {Use the existing test script with expanded timeout
206- $startArgs += "/config:default.ini"egration/test_mt5_initialization.py
207- }
208- : 1integration/test_mt5_initialization.py
209- $startArgs += "/skipupdate"
210-
211-
212-
213-
214-
215-
216-
217-
218-
219-
220-
221-
222-
223-
224-
225-
226-
227-
228-
229-
230-
231-
232-
233-
234-
235-
236-
237-
238-
19+ Invoke-WebRequest `
20+ -Uri "https://download.mql5.com/cdn/web/metaquotes.software.corp/mt5/mt5setup.exe" `
21+ -OutFile "mt5setup.exe"
22+ shell : pwsh
23923
24+ - name : Install MetaTrader5 silently
25+ run : |
26+ Start-Process -FilePath ".\mt5setup.exe" -ArgumentList "/silent" -Wait
27+ shell : powershell
24028
29+ - name : Kill any existing MT5 processes
30+ run : |
31+ Get-Process terminal64 -ErrorAction SilentlyContinue | Stop-Process -Force
32+ shell : powershell
24133
34+ - name : Launch MetaTrader5 Terminal in portable mode
35+ run : |
36+ Start-Process "C:\Program Files\MetaTrader 5\terminal64.exe" -ArgumentList "/portable"
37+ Start-Sleep -Seconds 15
38+ shell : powershell
24239
40+ - name : Install MetaTrader5 Python package
41+ run : pip install MetaTrader5
24342
43+ - name : Run your Python script
44+ run : |
45+ python - << 'EOF'
46+ import MetaTrader5 as mt5
24447
48+ if not mt5.initialize(path=r"C:/Program Files/MetaTrader 5/terminal64.exe", timeout=300000):
49+ print("initialize() failed, error code:", mt5.last_error())
50+ exit(1)
24551
246- MT5_VERBOSE: 1 # Enable verbose logging MT5_64BIT: 0 # Force 32-bit mode MT5_WAIT_PERIOD: 10000 # Wait 10 seconds between attempts MT5_DEBUG: 1 MT5_TIMEOUT: 180000 # Increase timeout to 3 minutes MT5_HEADLESS: 1 env: python test/integration/test_mt5_initialization.py Write-Host "Running Python test script with expanded timeout..." # Use the existing test script with expanded timeout } Write-Host "Warning: MT5 process is no longer running" } else { $env:MT5_VERBOSE = "1" # Add verbose flag to the test script for more detailed output Get-Process | Where-Object { $_.Name -like "*MetaTrader*" -or $_.Name -like "*terminal*" } | Format-Table -AutoSize Write-Host "All running processes:" # List all processes to debug Write-Host "Process details: $($mt5Running | Format-List | Out-String)" Write-Host "MT5 process is still running with ID $($mt5Process.Id)" if ($mt5Running) { $mt5Running = Get-Process -Id $mt5Process.Id -ErrorAction SilentlyContinue # Check process is still running and get details Start-Sleep -Seconds 45 Write-Host "Waiting 45 seconds for MT5 to initialize..." # Give MT5 more time to initialize - increase wait time Write-Host "Started MT5 process with ID $($mt5Process.Id)" $mt5Process = Start-Process -FilePath $env:MT5_PATH -ArgumentList $startArgs -PassThru Write-Host "Starting MT5 with arguments: $startArgs" $startArgs += "/minimize"
52+ # Example: print account info
53+ print(mt5.account_info())
24754
248- MT5_WAIT_PERIOD : 5000 # Wait 5 seconds between attempts MT5_DEBUG: 1 MT5_HEADLESS: 1
249- MT5_TIMEOUT : 120000 # Increase timeout to 2 minutes
250- MT5_DEBUG : 1
251- MT5_WAIT_PERIOD : 5000 # Wait 5 seconds between attempts
55+ mt5.shutdown()
56+ EOF
57+ shell : pwsh
0 commit comments