A Python application for automated copy trading using MetaTrader 5 (MT5). This application connects to trading accounts headlessly and provides the foundation for implementing copy trading strategies. Now supports multiple broker connections simultaneously!
- ✅ Headless MT5 Connection - Connects to MetaTrader 5 without opening the GUI
- ✅ Multi-Broker Support - Currently supports FundedNext with easy extensibility
- ✅ Multiple Account Connections - Connect to multiple accounts with the same broker/server
- ✅ Configuration-Based - All connection settings via config files
- ✅ Robust Error Handling - Comprehensive logging and error management
- ✅ Account Management - Real-time account information and balance monitoring
- 🚧 Copy Trading Logic - Foundation ready, implementation in progress
- Python 3.8+
- MetaTrader 5 installed and configured
- Trading account(s) with API access enabled
- Algorithm trading enabled in MT5 settings
-
Clone the repository:
git clone <repository-url> cd copy-trade
-
Install dependencies:
pip install -r requirements.txt
-
Configure MT5 for API access:
- Open MetaTrader 5
- Go to Tools → Options → Expert Advisors
- Enable "Allow algorithmic trading"
- Enable "Allow DLL imports"
- Apply settings and restart MT5
-
Copy the example configuration:
cp config.ini.example config.ini
-
Edit
config.iniwith your account details:[Connection] # All accounts will use the same broker and server broker = FundedNext server = FundedNext-Server 2 platform = MT5 [Account1] account = YOUR_FIRST_ACCOUNT_NUMBER password = YOUR_FIRST_PASSWORD [Account2] account = YOUR_SECOND_ACCOUNT_NUMBER password = YOUR_SECOND_PASSWORD # Add more accounts as needed [Account3] account = YOUR_THIRD_ACCOUNT_NUMBER # password = # Will prompt if not provided [Logging] level = INFO
For backward compatibility, you can still use the old single account configuration:
[Connection]
broker = FundedNext
server = FundedNext-Server 2
platform = MT5
account = YOUR_ACCOUNT_NUMBER
password = YOUR_PASSWORD# Connect to multiple accounts using config file
python main.py --config config.ini
# Enable verbose logging for debugging
python main.py --config config.ini --verbose# Use configuration file (legacy format)
python main.py --config config.ini
# Command line arguments
python main.py --broker FundedNext --server "FundedNext-Server 2" --account 123456
# No config file (will prompt for details)
python main.py2025-01-XX XX:XX:XX - INFO - Connecting to account1 (account 12345678)...
2025-01-XX XX:XX:XX - INFO - [account1] Attempting to connect to FundedNext (FundedNext-Server 2) account 12345678
2025-01-XX XX:XX:XX - INFO - [account1] Successfully connected to account 12345678
2025-01-XX XX:XX:XX - INFO - Added connection account1 successfully
2025-01-XX XX:XX:XX - INFO - Connecting to account2 (account 87654321)...
2025-01-XX XX:XX:XX - INFO - [account2] Attempting to connect to FundedNext (FundedNext-Server 2) account 87654321
2025-01-XX XX:XX:XX - INFO - [account2] Successfully connected to account 87654321
2025-01-XX XX:XX:XX - INFO - Added connection account2 successfully
2025-01-XX XX:XX:XX - INFO - Successfully connected to 2 out of 2 accounts
2025-01-XX XX:XX:XX - INFO - [account1] Account 12345678 on FundedNext-Server 2 - Balance: 15100.45 USD
2025-01-XX XX:XX:XX - INFO - [account2] Account 87654321 on FundedNext-Server 2 - Balance: 25250.78 USD
2025-01-XX XX:XX:XX - INFO - All connections established. Press Ctrl+C to disconnect and exit.
copy-trade/
├── src/
│ ├── broker_connection.py # Core MT5 connection logic + MultiBrokerManager
│ └── example.py # Example usage (single account)
├── main.py # Application entry point (supports multiple accounts)
├── config.ini.example # Configuration template (multiple accounts)
├── requirements.txt # Python dependencies
├── DEVELOPMENT.md # Development notes and roadmap
└── README.md # This file
The MultiBrokerManager class provides:
- Thread-safe operations for managing multiple connections
- Connection tracking with unique identifiers
- Bulk operations for connecting/disconnecting all accounts
- Account monitoring with real-time status updates
- Error isolation - one failing connection doesn't affect others
# Create manager
manager = MultiBrokerManager()
# Add connections
manager.add_connection("account1", broker, server, platform, account_num, password)
# Get specific connection
conn = manager.get_connection("account1")
# Get all connected accounts info
accounts = manager.get_connected_accounts()
# Disconnect all
manager.disconnect_all()| Broker | Status | Server Examples |
|---|---|---|
| FundedNext | ✅ Working | FundedNext-Server 2, FundedNext-Live2 |
| Others | 🚧 Planned | Easy to add via configuration |
-
MT5 Connection Management
- Headless initialization with credentials
- Robust error handling and retry logic
- Account information retrieval
- Clean connection lifecycle management
-
Multiple Account Support
- Simultaneous connections to multiple accounts
- Same broker/server with different credentials
- Thread-safe connection management
- Individual connection monitoring
-
Configuration System
- INI-based configuration for multiple accounts
- Command-line argument support
- Backward compatibility with single account mode
- Flexible server name mapping
-
Logging & Monitoring
- Comprehensive logging system with connection IDs
- Real-time account balance monitoring
- Connection status tracking for all accounts
- Copy Trading Engine
- Trade monitoring and replication between accounts
- Position management across multiple accounts
- Risk management rules
-
Advanced Copy Trading
- Master-slave account designation
- Proportional scaling based on account size
- Selective trade copying with filters
-
Advanced Risk Management
- Stop-loss and take-profit rules
- Maximum drawdown limits per account
- Position sizing algorithms
-
Web Interface
- Real-time monitoring dashboard for all accounts
- Configuration management
- Trade history and analytics
-
Notifications
- Email/SMS alerts for all accounts
- Trade execution notifications
- Error and status updates
-
Some Accounts Fail to Connect
- Check individual account credentials
- Verify all accounts use the same broker/server
- Review logs for specific error messages per account
-
MT5 Resource Limits
- MT5 may limit concurrent connections
- Consider connecting accounts sequentially if needed
- Monitor system resources (CPU, memory)
-
IPC Timeout Errors
- Ensure MT5 is closed completely before running
- Check that algorithmic trading is enabled in MT5
- Verify account credentials are correct
-
Connection Failures
- Confirm server name matches your broker exactly
- Check network connectivity
- Verify account has API access enabled
-
Permission Errors
- Run as administrator if needed
- Check antivirus/firewall settings
- Ensure MT5 installation is complete
- Check the logs for detailed error messages (includes connection IDs)
- Verify MT5 settings and account status for all accounts
- Test manual login to MT5 first
- Review the troubleshooting section in DEVELOPMENT.md
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
This software is for educational and research purposes. Use at your own risk. Always test thoroughly before using with real trading accounts. The developers are not responsible for any financial losses.