1+ #!/usr/bin/env python3
2+ """Script to setup and configure MetaTrader 5 for automated trading."""
3+
4+ import os
5+ import sys
6+ import time
7+ from pathlib import Path
8+ import logging
9+
10+ try :
11+ import MetaTrader5 as Mt5
12+ except ImportError :
13+ print ("MetaTrader5 package not installed. Install it with: pip install MetaTrader5" )
14+ sys .exit (1 )
15+
16+ logging .basicConfig (level = logging .INFO )
17+ logger = logging .getLogger (__name__ )
18+
19+ def find_mt5_terminal ():
20+ """Find MetaTrader 5 terminal executable."""
21+ common_paths = [
22+ "C:\\ Program Files\\ MetaTrader 5\\ terminal64.exe" ,
23+ "C:\\ Program Files (x86)\\ MetaTrader 5\\ terminal.exe" ,
24+ os .path .expandvars ("%APPDATA%\\ MetaTrader 5\\ terminal64.exe" ),
25+ os .path .expandvars ("%PROGRAMFILES%\\ MetaTrader 5\\ terminal64.exe" ),
26+ ]
27+
28+ for path in common_paths :
29+ if os .path .exists (path ):
30+ return path
31+ return None
32+
33+ def check_trading_allowed ():
34+ """Check if trading is allowed and print current settings."""
35+ terminal_info = Mt5 .terminal_info ()
36+ logger .info ("Current MetaTrader 5 settings:" )
37+ logger .info (f"Connected: { terminal_info .connected } " )
38+ logger .info (f"Trade allowed: { terminal_info .trade_allowed } " )
39+ logger .info (f"DLLs allowed: { terminal_info .dlls_allowed } " )
40+
41+ return terminal_info .trade_allowed and terminal_info .dlls_allowed
42+
43+ def main ():
44+ """Main function to setup MetaTrader 5."""
45+ logger .info ("Looking for MetaTrader 5 terminal..." )
46+ terminal_path = find_mt5_terminal ()
47+
48+ if not terminal_path :
49+ logger .error ("Could not find MetaTrader 5 terminal. Please install it first." )
50+ sys .exit (1 )
51+
52+ logger .info (f"Found MetaTrader 5 terminal at: { terminal_path } " )
53+
54+ # Try to initialize MT5
55+ if not Mt5 .initialize (path = terminal_path ):
56+ logger .error (f"MetaTrader 5 initialization failed. Error code: { Mt5 .last_error ()} " )
57+ sys .exit (1 )
58+
59+ try :
60+ if check_trading_allowed ():
61+ logger .info ("Trading is already enabled!" )
62+ else :
63+ logger .warning ("\n Trading is not fully enabled. For CI environments, you need to:" )
64+ logger .warning ("1. Create a pre-configured terminal with these settings:" )
65+ logger .warning (" - Tools -> Options -> Expert Advisors:" )
66+ logger .warning (" - Enable 'Allow automated trading'" )
67+ logger .warning (" - Enable 'Allow DLL imports'" )
68+ logger .warning (" - Enable 'Allow WebRequest for listed URL'" )
69+ logger .warning ("\n 2. Package the pre-configured terminal with your CI pipeline" )
70+ logger .warning ("3. Use the pre-configured terminal path in your tests" )
71+ logger .warning ("\n Note: These settings cannot be enabled programmatically" )
72+ sys .exit (1 )
73+
74+ # Test symbol selection
75+ symbol = "EURUSD"
76+ logger .info (f"\n Testing symbol selection with { symbol } ..." )
77+ if not Mt5 .symbol_select (symbol , True ):
78+ logger .error (f"Failed to select symbol { symbol } " )
79+ sys .exit (1 )
80+
81+ symbol_info = Mt5 .symbol_info (symbol )
82+ if symbol_info is None :
83+ logger .error (f"Failed to get symbol info for { symbol } " )
84+ sys .exit (1 )
85+
86+ logger .info (f"Symbol { symbol } info:" )
87+ logger .info (f"Trade mode: { symbol_info .trade_mode } " )
88+ logger .info (f"Visible: { symbol_info .visible } " )
89+ logger .info (f"Bid: { symbol_info .bid } " )
90+ logger .info (f"Ask: { symbol_info .ask } " )
91+
92+ finally :
93+ Mt5 .shutdown ()
94+
95+ if __name__ == "__main__" :
96+ main ()
0 commit comments