4646 mkdir -p "$env:APPDATA\MetaTrader5Portable"
4747
4848 # 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
49+ echo "MT5_HEADLESS =1" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
5050 echo "MT5_PORTABLE_PATH=$env:APPDATA\MetaTrader5Portable" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
5151
5252 # Copy MT5 files to portable location if needed
@@ -64,80 +64,87 @@ jobs:
6464
6565 - name : Test MT5 initialization
6666 run : |
67- python "
68- import sys, os, time
69- import MetaTrader5 as mt5
70-
71- print(f'MT5 version: {mt5.__version__}')
72- print('MT5_DISABLE_UI:', os.environ.get('MT5_DISABLE_UI'))
73- print('MT5_PORTABLE_PATH:', os.environ.get('MT5_PORTABLE_PATH'))
74- print('Initializing...')
75-
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- print('Starting MT5 initialization...')
81-
82- # Set environment variable directly in Python
83- os.environ['MT5_HEADLESS'] = '1'
84-
85- # Initialize with minimal parameters
86- result = mt5.initialize()
67+ # Create a Python script file
68+ $pythonScript = @'
69+ import sys, os, time
70+ import MetaTrader5 as mt5
71+
72+ print(f'MT5 version : {mt5.__version__}')
73+ print('MT5_HEADLESS:', os.environ.get('MT5_HEADLESS'))
74+ print('MT5_PORTABLE_PATH:', os.environ.get('MT5_PORTABLE_PATH'))
75+ print('Initializing...')
76+
77+ # Try initialization using portable path and headless mode
78+ portable_path = os.environ.get('MT5_PORTABLE_PATH')
79+ mt5_path = os.path.join(portable_path, 'terminal64.exe')
80+
81+ print('Starting MT5 initialization...')
82+
83+ # Set environment variable directly in Python
84+ os.environ['MT5_HEADLESS'] = '1'
85+
86+ # Initialize with minimal parameters
87+ result = mt5.initialize()
88+
89+ print(f'Initial result : {result}, Error code: {mt5.last_error()}')
90+
91+ # If the initial attempt fails, try additional methods
92+ if not result :
93+ print('Initial attempt failed. Trying with portable path...')
94+ result = mt5.initialize(
95+ path=mt5_path,
96+ login=0,
97+ password='',
98+ server='',
99+ timeout=60000
100+ )
101+
102+ print(f'Result with portable path : {result}, Error code: {mt5.last_error()}')
103+
104+ # If first attempt fails, try alternative approach
105+ if not result :
106+ print('First attempt failed. Trying default initialization...')
107+ mt5.shutdown()
108+ time.sleep(2)
87109
88- print(f'Initial result: {result}, Error code: {mt5.last_error()}')
110+ # Second attempt: default init
111+ result = mt5.initialize(timeout=60000)
112+ print(f'Result with default init : {result}, Error code: {mt5.last_error()}')
89113
90- # If the initial attempt fails, try additional methods
114+ # Third attempt: standard path
91115 if not result :
92- print('Initial attempt failed. Trying with portable path...')
116+ print('Second attempt failed. Trying with standard path...')
117+ mt5.shutdown()
118+ time.sleep(2)
93119 result = mt5.initialize(
94- path=mt5_path,
95- login=0,
96- password='',
97- server='',
120+ path='C:\\Program Files\\MetaTrader 5\\terminal64.exe',
98121 timeout=60000
99122 )
100-
101- print(f'Result with portable path: {result}, Error code: {mt5.last_error()}')
102-
103- # If first attempt fails, try alternative approach
123+ print(f'Result with standard path : {result}, Error code: {mt5.last_error()}')
124+
104125 if not result :
105- print('First attempt failed. Trying default initialization... ')
106- mt5.shutdown( )
107- time.sleep(2)
108-
109- # Second attempt: default init
110- result = mt5.initialize(timeout=60000)
111- print(f'Result with default init : {result}, Error code: {mt5.last_error() }')
112-
113- # Third attempt: standard path
114- if not result:
115- print('Second attempt failed. Trying with standard path... ')
116- mt5.shutdown()
117- time.sleep(2 )
118- result = mt5.initialize(
119- path='C:\\\\Program Files\\\\MetaTrader 5\\\\terminal64.exe',
120- timeout=60000
121- )
122- print(f'Result with standard path: {result}, Error code: {mt5.last_error()}' )
123-
124- if not result:
125- print('All initialization attempts failed')
126- sys.exit(1)
126+ print('All initialization attempts failed ')
127+ sys.exit(1 )
128+
129+ print('MT5 initialized successfully')
130+ account_info = mt5.account_info()
131+ if account_info is not None :
132+ print(f'Account info : {account_info }')
133+ else :
134+ print('No account info available (demo mode)')
135+
136+ symbol_info = mt5.symbol_info('EURUSD ')
137+ if symbol_info is not None :
138+ print(f'Symbol info available : {symbol_info.name}' )
139+ else :
140+ print('Symbol info not available')
141+
142+ mt5.shutdown( )
143+ sys.exit(0 )
144+ ' @
145+
146+ # Write the script to a file
147+ $pythonScript | Out-File -FilePath "test_mt5.py" -Encoding utf8
127148
128- print('MT5 initialized successfully')
129- account_info = mt5.account_info()
130- if account_info is not None:
131- print(f'Account info: {account_info}')
132- else:
133- print('No account info available (demo mode)')
134-
135- symbol_info = mt5.symbol_info('EURUSD')
136- if symbol_info is not None:
137- print(f'Symbol info available: {symbol_info.name}')
138- else:
139- print('Symbol info not available')
140-
141- mt5.shutdown()
142- sys.exit(0)
143- "
149+ # Run the script
150+ python test_mt5.py
0 commit comments