A robust, production-ready system for copying pending orders between MetaTrader 5 terminals with advanced features including order tracking, orphan management, and flexible execution modes.
- Features
- System Requirements
- Installation Guide
- Quick Start Guide
- Configuration
- Usage
- Testing
- Troubleshooting
- Advanced Configuration
- Pending Order Copying: Automatically copy new pending orders from source to target terminals
- Active Position Management: Monitor and manage active positions that were created when copied pending orders get triggered
- Multi-Terminal Support: Copy orders from one source terminal to multiple target terminals
- Real-time Synchronization: Continuously monitor for new pending orders and position changes
The MT5 Order Copier is designed to handle two distinct types of trading instruments:
- Pending Orders: Orders waiting to be triggered (BUY_LIMIT, SELL_LIMIT, BUY_STOP, SELL_STOP)
- Active Positions: Live trades that resulted from triggered pending orders
- Source Monitoring: Continuously monitors the source terminal for new pending orders
- Order Analysis: Identifies new pending orders that haven't been copied yet
- Target Processing: For each target terminal:
- Maps symbols according to
symbol_mapping
configuration - Adjusts lot sizes using
lot_multiplier
settings - Applies
min_lot_size
andmax_lot_size
constraints - Filters orders based on
allowed_order_types
- Maps symbols according to
- Order Copying: Places equivalent pending orders on target terminals
- State Tracking: Records the relationship between source and target orders
- Trigger Detection: Monitors when pending orders get triggered and become active positions
- Position Tracking: Tracks active positions that originated from copied pending orders
- Lifecycle Management: Continues monitoring until positions are closed on the source
- Synchronization: Ensures target positions remain aligned with source position status
- Orphan Detection: Identifies orders/positions on targets that no longer exist on source
- Cleanup Actions: Based on
orphan_management.kill_orphaned_orders
setting:True
: Automatically closes orphaned orders/positionsFalse
: Logs orphans for manual review
- Safety Checks: Performs multiple verification cycles before taking action
What the System DOES:
- ✅ Copies New Pending Orders: Copies
BUY_LIMIT
,SELL_LIMIT
,BUY_STOP
, andBUY_STOP_LIMIT
orders from the source to one or more target terminals. - ✅ Updates Modified Pending Orders: If a pending order on the source terminal is modified (e.g., price, stop-loss, take-profit, or expiration is changed), the system will update the corresponding order on the target terminals.
- ✅ Synchronizes Position SL/TP: When a copied pending order becomes an active position, the system monitors the source position and synchronizes any changes to its Stop Loss (SL) and Take Profit (TP) to the target positions.
- ✅ Manages Orphaned Orders and Positions: Detects and handles orders and positions on target terminals that no longer have a corresponding entry on the source terminal. Depending on the configuration, it can automatically cancel/close them.
- ✅ Applies Risk Management: Uses flexible position sizing (lot multiplier, min/max lot size), symbol mapping, and order type filtering for each target terminal.
- ✅ Tracks State Persistently: Saves the state of all copied orders and positions, allowing it to resume tracking correctly after a restart.
What the System DOES NOT DO:
- ❌ Copy Active Positions: It does not copy positions that are already active on the source terminal. It only manages positions that originate from a copied pending order.
- ❌ Copy Market Orders: It does not copy trades that are executed manually as market orders.
The system maintains a persistent state file (data/order_tracker_state.json
) that stores:
- Mapping between source and target orders
- Order status and lifecycle information
- Position tracking data
- System statistics and performance metrics
This ensures that if the system restarts, it can resume monitoring existing copied orders and positions without duplication or loss of tracking.
- Pending Orders: The system copies BUY_LIMIT, SELL_LIMIT, BUY_STOP, SELL_STOP orders
- Active Positions: When a copied pending order gets triggered and becomes an active position, the system tracks and manages it
- Position Lifecycle: Monitors active positions until they are closed on the source terminal
- Orphan Management: Handles orders/positions that exist on targets but not on source
- Flexible Position Sizing: Configure lot multipliers for each target terminal
- Symbol Mapping: Map different symbol names between brokers
- Order Type Filtering: Choose which order types to copy for safety
- Risk Management: Set maximum lot sizes, order limits, and position limits
- State Persistence: Maintains complete order and position tracking across system restarts
- Multiple Execution Modes: Run once, scheduled, or continuous operation
- Comprehensive Logging: Detailed logging with rotation and multiple levels
- Built-in Testing: Comprehensive test suite for validation
- Statistics Tracking: Real-time monitoring of system performance
- Operating System: Windows 10/11 (required for MetaTrader 5)
- Python: 3.7 or higher (3.9+ recommended)
- MetaTrader 5: Latest version installed
- Memory: Minimum 4GB RAM (8GB+ recommended for multiple terminals)
- Storage: At least 1GB free space for logs and state files
The system requires the following Python packages (automatically installed via requirements.txt
):
- MetaTrader5 (>=5.0.45) - Core MT5 API for terminal communication
- schedule (>=1.2.0) - Task scheduling for automated execution
- pandas (>=1.5.0) - Data manipulation and analysis
- numpy (>=1.21.0) - Numerical computing support
- pyyaml (>=6.0) - Configuration file parsing
- python-dateutil (>=2.8.0) - Enhanced date/time handling
- psutil (>=5.9.0) - System and process monitoring
Option A: Clone from Repository
git clone <repository-url>
cd mt5-order-copier
Option B: Download ZIP
- Download the ZIP file from the repository
- Extract to your desired directory
- Open Command Prompt/PowerShell in the extracted folder
Check Python Installation:
python --version
If Python is not installed, download from python.org
Create Virtual Environment (Recommended):
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
Verify Installation:
python test_imports.py
- Install MT5: Download from your broker or MetaQuotes
- Enable Algorithmic Trading:
- Open MT5 → Tools → Options → Expert Advisors
- Check "Allow algorithmic trading"
- Check "Allow DLL imports"
- Test Manual Connections: Ensure you can manually log into all terminals
copy config_sample.py config.py
Open config.py
and modify the essential settings:
# Source terminal (where orders originate)
SOURCE_TERMINAL = {
'MT5_ACCOUNT': 12345678, # Your MT5 account number
'MT5_PASSWORD': 'your_password', # Your MT5 password
'MT5_SERVER': 'YourBroker-Demo', # Your broker's server
'MT5_TERMINAL_PATH': r'C:\Program Files\MetaTrader 5\terminal64.exe'
}
# Target terminal (where orders are copied)
TARGET_TERMINALS = {
'MyTargetAccount': {
'MT5_ACCOUNT': 87654321,
'MT5_PASSWORD': 'target_password',
'MT5_SERVER': 'TargetBroker-Demo',
'MT5_TERMINAL_PATH': r'C:\Program Files\MetaTrader 5\terminal64.exe',
'lot_multiplier': 1.0, # Same lot size as source
'symbol_mapping': {
'EURUSD': 'EURUSD', # Direct symbol mapping
'GBPUSD': 'GBPUSD'
}
}
}
python test_config.py
python test_system.py
One-time execution:
python main.py
Continuous monitoring:
Edit config.py
to set:
EXECUTION_MODE = 'continuous'
Then run:
python main.py
The system uses a Python configuration file (config.py
) with the following main sections:
- SOURCE_TERMINAL: The MT5 terminal from which orders are copied
- TARGET_TERMINALS: Dictionary of target terminals where orders are copied to
- EXECUTION_MODE: How the system runs (once, scheduled, continuous)
- LOGGING_CONFIG: Logging settings and output options
- SYSTEM_CONFIG: Global system settings and constraints
Edit config.py
to configure your terminals and settings:
# =============================================================================
# SOURCE TERMINAL CONFIGURATION
# =============================================================================
SOURCE_TERMINAL = {
'MT5_ACCOUNT': 12345678, # Your MT5 account number
'MT5_PASSWORD': 'your_source_password', # Your MT5 password
'MT5_SERVER': 'YourBroker-Demo', # Broker server name
'MT5_TERMINAL_PATH': r'C:\Program Files\MetaTrader 5\terminal64.exe', # Path to MT5 executable
'timeout': 10000 # Connection timeout (ms)
}
# =============================================================================
# TARGET TERMINALS CONFIGURATION
# =============================================================================
TARGET_TERMINALS = {
'MyTargetAccount': {
# Connection settings
'MT5_ACCOUNT': 87654321,
'MT5_PASSWORD': 'target_password',
'MT5_SERVER': 'TargetBroker-Demo',
'MT5_TERMINAL_PATH': r'C:\Program Files\MetaTrader 5\terminal64.exe',
'timeout': 10000,
# Position sizing
'lot_multiplier': 1.0, # Same size as source
'min_lot_size': 0.01, # Minimum lot size
'max_lot_size': 100.0, # Maximum lot size
# Order filtering
'allowed_order_types': [
'BUY_LIMIT', 'SELL_LIMIT', 'BUY_STOP', 'SELL_STOP'
],
# Symbol mapping
'symbol_mapping': {
'EURUSD': 'EURUSD',
'GBPUSD': 'GBPUSD'
},
# Orphan management (applies to both pending orders and active positions)
'orphan_management': {
'kill_orphaned_orders': True, # Auto-close orphaned orders/positions
'max_orphan_checks': 3 # Verification cycles before action
},
# Order limits
'max_pending_orders': {
'enabled': True,
'max_orders': 50
}
}
}
# =============================================================================
# EXECUTION MODE
# =============================================================================
EXECUTION_MODE = 'once' # Options: 'once', 'scheduled', 'continuous'
# =============================================================================
# LOGGING CONFIGURATION
# =============================================================================
LOGGING_CONFIG = {
'level': 'INFO', # DEBUG, INFO, WARNING, ERROR
'file_path': 'logs/mt5_order_copier.log', # Log file location
'max_file_size': 10, # MB before rotation
'backup_count': 5, # Number of backup files
'console_output': True # Also log to console
}
TARGET_TERMINALS = {
'ConservativeAccount': {
'MT5_ACCOUNT': 87654321,
'MT5_PASSWORD': 'password',
'MT5_SERVER': 'Broker-Demo',
'MT5_TERMINAL_PATH': r'C:\MT5\terminal64.exe',
# Conservative position sizing
'lot_multiplier': 0.1, # Only 10% of source position
'min_lot_size': 0.01,
'max_lot_size': 1.0, # Cap at 1 lot maximum
# Only limit orders (safer)
'allowed_order_types': ['BUY_LIMIT', 'SELL_LIMIT'],
# Strict order limits
'max_pending_orders': {
'enabled': True,
'max_orders': 5 # Maximum 5 pending orders
},
# Conservative orphan handling
'orphan_management': {
'kill_orphaned_orders': False, # Keep orphans for manual review
'max_orphan_checks': 5
}
}
}
TARGET_TERMINALS = {
'BrokerA_Account': {
'MT5_ACCOUNT': 11111111,
'MT5_PASSWORD': 'password_a',
'MT5_SERVER': 'BrokerA-Live',
'MT5_TERMINAL_PATH': r'C:\MT5_BrokerA\terminal64.exe',
'lot_multiplier': 0.5,
# BrokerA uses standard symbols
'symbol_mapping': {
'EURUSD': 'EURUSD',
'GBPUSD': 'GBPUSD',
'USDJPY': 'USDJPY',
'GOLD': 'XAUUSD'
}
},
'BrokerB_Account': {
'MT5_ACCOUNT': 22222222,
'MT5_PASSWORD': 'password_b',
'MT5_SERVER': 'BrokerB-Live',
'MT5_TERMINAL_PATH': r'C:\MT5_BrokerB\terminal64.exe',
'lot_multiplier': 0.3,
# BrokerB uses different symbol naming
'symbol_mapping': {
'EURUSD': 'EURUSD.m', # Micro account suffix
'GBPUSD': 'GBPUSD.m',
'USDJPY': 'USDJPY.m',
'GOLD': 'GOLD.spot' # Different gold symbol
}
}
}
# Run every 30 seconds during trading hours
EXECUTION_MODE = 'scheduled'
SCHEDULE_CONFIG = {
'interval_seconds': 30, # Run every 30 seconds
'max_runtime_hours': 12, # Stop after 12 hours
'trading_hours': { # Optional: only run during specific hours
'enabled': True,
'start_time': '09:00', # Start at 9 AM
'end_time': '17:00', # End at 5 PM
'timezone': 'UTC'
}
}
# Run continuously with minimal delay
EXECUTION_MODE = 'continuous'
CONTINUOUS_CONFIG = {
'delay_seconds': 5, # 5-second delay between cycles
'max_runtime_hours': 0, # 0 = unlimited runtime
'health_check_interval': 300, # Check system health every 5 minutes
'auto_restart_on_error': True # Restart on critical errors
}
Run the copying process once and exit:
EXECUTION_MODE = 'once'
Run on a regular schedule:
EXECUTION_MODE = 'scheduled'
SCHEDULE_CONFIG = {
'interval_seconds': 60,
'max_iterations': 0 # 0 = unlimited
}
Run continuously with minimal delay:
EXECUTION_MODE = 'continuous'
CONTINUOUS_CONFIG = {
'delay_seconds': 5,
'max_runtime_hours': 0 # 0 = unlimited
}
LOGGING_CONFIG = {
'level': 'INFO', # DEBUG, INFO, WARNING, ERROR, CRITICAL
'file_path': 'logs/mt5_order_copier.log', # Path to the log file
'max_file_size': 10, # Maximum size of the log file in MB before rotation
'backup_count': 5, # Number of backup log files to keep
'console_output': True, # Whether to output logs to the console
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s' # Log message format
}
SYSTEM_CONFIG = {
'connection_timeout': 90, # Integer: Maximum time in seconds to wait for a connection to the MT5 terminal.
'max_retries': 5, # Integer: Maximum number of times to retry an operation (e.g., connection, order placement) on failure.
'retry_delay': 5, # Integer/Float: Delay in seconds between retries.
'log_level': 'INFO', # DEPRECATED: Use LOGGING_CONFIG['level'] instead.
'log_file': 'mt5_copier.log' # DEPRECATED: Use LOGGING_CONFIG['file_path'] instead.
}
Before running the system with real accounts, perform these essential tests:
python test_imports.py
Verifies all required modules are properly installed.
python test_config.py
Validates your configuration file for syntax and logical errors.
python test_system.py
Runs comprehensive unit tests for all system components.
Create a minimal test configuration with demo accounts:
# test_connection_config.py
SOURCE_TERMINAL = {
'MT5_ACCOUNT': 12345678,
'MT5_PASSWORD': 'demo_password',
'MT5_SERVER': 'YourBroker-Demo',
'MT5_TERMINAL_PATH': r'C:\Program Files\MetaTrader 5\terminal64.exe'
}
TARGET_TERMINALS = {
'TestTarget': {
'MT5_ACCOUNT': 87654321,
'MT5_PASSWORD': 'demo_password',
'MT5_SERVER': 'YourBroker-Demo',
'MT5_TERMINAL_PATH': r'C:\Program Files\MetaTrader 5\terminal64.exe',
'lot_multiplier': 0.01, # Very small lots for testing
'symbol_mapping': {'EURUSD': 'EURUSD'}
}
}
EXECUTION_MODE = 'once'
Then test:
python main.py test_connection_config.py
- Python environment setup correctly
- All dependencies installed
- MT5 terminals can connect manually
- Configuration file syntax is valid
- Demo account connections work
- System can retrieve orders from source
- System can place test orders on target
- Logging system works properly
- State tracking files are created
# Run with default configuration
python main.py
# Run with custom configuration file
python main.py custom_config.py
# Run in test mode (dry run)
python main.py --test-mode
# Show help and available options
python main.py --help
# Run with verbose logging
python main.py --verbose
Runs the copying process once and exits:
EXECUTION_MODE = 'once'
Runs on a regular schedule:
EXECUTION_MODE = 'scheduled'
SCHEDULE_CONFIG = {
'interval_seconds': 60, # Run every 60 seconds
'max_runtime_hours': 8 # Stop after 8 hours
}
Runs continuously with minimal delay:
EXECUTION_MODE = 'continuous'
CONTINUOUS_CONFIG = {
'delay_seconds': 5, # 5-second delay between cycles
'max_runtime_hours': 0 # 0 = unlimited runtime
}
For production use, you can run the system as a Windows service:
- Install the service wrapper:
pip install pywin32
-
Create a service script (see documentation for details)
-
Install and start the service:
python service_installer.py install
python service_installer.py start
-
Always Test with Demo Accounts First
- Never run the system on live accounts without thorough testing
- Use demo accounts that mirror your live account setup
- Test all scenarios: new orders, modifications, cancellations
-
Start Small
- Begin with very small lot multipliers (0.01 or 0.1)
- Limit the number of target terminals initially
- Monitor the system closely during initial runs
-
Risk Management
# Conservative settings for beginners 'lot_multiplier': 0.1, # Only 10% of source position 'max_lot_size': 1.0, # Cap maximum position size 'max_pending_orders': { 'enabled': True, 'max_orders': 10 # Limit total orders }
-
Monitor Continuously
- Check log files regularly
- Monitor account balances
- Set up alerts for errors
- Have a manual override plan
-
Ensure MT5 terminals are properly configured:
- Enable algorithmic trading
- Configure login credentials
- Test manual connections
-
Configure symbol mappings if using different brokers:
'symbol_mapping': { 'EURUSD.m': 'EURUSD', # Source -> Target 'GBPUSD.m': 'GBPUSD' }
-
Set appropriate lot multipliers:
'lot_multiplier': 0.5, # Copy with half the lot size 'min_lot_size': 0.01, 'max_lot_size': 10.0
Control which order types are copied:
'allowed_order_types': [
'BUY_LIMIT',
'SELL_LIMIT',
'BUY_STOP',
'SELL_STOP',
'BUY_STOP_LIMIT',
'SELL_STOP_LIMIT'
]
Configure how orphaned orders are handled:
'orphan_management': {
'kill_orphaned_orders': True, # Enable orphan killing
'max_orphan_checks': 3 # Checks before killing
}
- main.py: Application entry point and execution control
- config.py: Configuration management and validation
- order_manager.py: Core order copying and synchronization logic
- mt5_connector.py: MT5 terminal connection and operations
- order_tracker.py: Order state tracking and orphan management
- utils.py: Utility functions and helpers
- Source Connection: Connect to source terminal and retrieve pending orders
- Target Processing: For each target terminal:
- Connect and retrieve existing orders
- Identify new orders to copy
- Detect modified orders to update
- Handle orphaned orders
- State Management: Update tracking state and save to disk
- Statistics: Log processing statistics and performance metrics
The system maintains persistent state in data/order_tracker_state.json
:
- Source order tracking
- Target order relationships
- Orphan check counters
- Terminal activity status
Logs are written to the configured log file (default: logs/mt5_order_copier.log
):
2024-01-15 10:30:15,123 - INFO - Starting order copying process for all terminals
2024-01-15 10:30:16,456 - INFO - Retrieved 5 orders from source terminal
2024-01-15 10:30:17,789 - INFO - Successfully copied order 123456 to Terminal1 as 789012
2024-01-15 10:30:18,012 - INFO - Processing statistics: 3 copied, 1 updated, 0 cancelled
Error: 'python' is not recognized as an internal or external command
Solutions:
- Install Python from python.org
- During installation, check "Add Python to PATH"
- Restart Command Prompt after installation
- Try
py
instead ofpython
Error: pip install failed
or ModuleNotFoundError
Solutions:
# Upgrade pip first
python -m pip install --upgrade pip
# Install with verbose output to see errors
pip install -r requirements.txt -v
# Install packages individually if batch fails
pip install MetaTrader5>=5.0.45
pip install schedule>=1.2.0
pip install pandas>=1.5.0
Error: ImportError: No module named 'MetaTrader5'
Solutions:
- Ensure you're on Windows (MT5 API only works on Windows)
- Install the official MetaTrader5 package:
pip uninstall MetaTrader5
pip install MetaTrader5
- Verify MT5 terminal is installed
Error: SyntaxError
or NameError
in config.py
Solutions:
- Copy from
config_sample.py
again - Check for missing quotes around strings
- Verify proper Python dictionary syntax
- Use raw strings for file paths:
r'C:\path\to\file'
Error: Failed to initialize MT5
or Login failed
Diagnostic Steps:
# Test manual connection first
1. Open MT5 terminal manually
2. Try logging in with your credentials
3. Verify server name exactly matches
Solutions:
- Check Credentials: Verify login, password, and server name
- Enable Algo Trading: Tools → Options → Expert Advisors → Allow algorithmic trading
- Check Terminal Path: Verify the path to terminal64.exe is correct
- Close Other Connections: Ensure MT5 isn't already connected elsewhere
- Firewall/Antivirus: Add MT5 and Python to firewall exceptions
Error: Terminal already in use
or Connection timeout
Solutions:
- Use different MT5 installations for different accounts
- Install MT5 in separate folders:
C:\MT5_Source\terminal64.exe C:\MT5_Target1\terminal64.exe C:\MT5_Target2\terminal64.exe
- Increase timeout values in configuration
- Stagger connection attempts
Diagnostic Steps:
- Check if source has pending orders:
python -c "from mt5_connector import MT5Connector; print('Testing source connection')"
- Verify symbol mappings are correct
- Check order type filters
- Review lot size constraints
Solutions:
- Symbol Mapping: Ensure source symbols map to valid target symbols
- Order Types: Verify allowed_order_types includes the order types you want
- Lot Sizes: Check min_lot_size and max_lot_size constraints
- Account Permissions: Verify target account allows pending orders
Error: Symbol not found
or Invalid symbol
Solutions:
# Check available symbols on target terminal
'symbol_mapping': {
'EURUSD.m': 'EURUSD', # Suffix mapping
'EUR/USD': 'EURUSD', # Format conversion
'GOLD': 'XAUUSD' # Different naming
}
Error: Invalid lot size
or Lot size out of range
Solutions:
# Adjust lot size settings
'lot_multiplier': 0.1, # Reduce position size
'min_lot_size': 0.01, # Broker minimum
'max_lot_size': 1.0, # Conservative maximum
Solutions:
- Increase execution intervals
- Reduce log verbosity to INFO or WARNING
- Monitor system resources
- Check network latency to broker servers
Solutions:
- Limit number of target terminals
- Reduce log file retention
- Monitor with Task Manager
- Restart system periodically
Edit config.py
:
LOGGING_CONFIG = {
'level': 'DEBUG', # Change from INFO to DEBUG
'console_output': True
}
Test individual components:
# Test MT5 connection only
python -c "from mt5_connector import MT5Connector; c = MT5Connector(); print(c.connect(SOURCE_TERMINAL, 'Test'))"
# Test configuration loading
python -c "from config import load_config; print(load_config())"
# Test order retrieval
python -c "from order_manager import OrderManager; om = OrderManager(config); print(len(om._get_source_orders()))"
Q: Can I copy orders from one broker to multiple brokers?
A: Yes, you can configure multiple target terminals in the TARGET_TERMINALS
dictionary. Each target can have different settings, lot multipliers, and symbol mappings.
Q: Does this work with live accounts? A: Yes, but we strongly recommend testing thoroughly with demo accounts first. Start with small lot sizes and conservative settings when moving to live accounts.
Q: Can I copy orders between different MT5 installations? A: Yes, you can specify different MT5 installation paths for each terminal in the configuration.
Q: What happens if my internet connection is lost? A: The system will attempt to reconnect based on the timeout settings. Pending orders that were already placed will remain, but new orders won't be copied until connection is restored.
Q: Why are my orders not being copied? A: Common causes:
- Source terminal not connected
- Target terminal login issues
- Symbol not available on target broker
- Order type not in
allowed_order_types
- Lot size outside min/max limits
- Maximum pending orders limit reached
Q: Can I modify the lot size of copied orders?
A: Yes, use the lot_multiplier
setting. For example, 0.5
will copy orders at half the original size, 2.0
will double them.
Q: How do I handle different symbol names between brokers?
A: Use the symbol_mapping
configuration:
'symbol_mapping': {
'EURUSD': 'EURUSD.m', # Source: Target
'GOLD': 'XAUUSD' # Map GOLD to XAUUSD
}
Q: What are orphaned orders and positions? A: Orphans are orders or positions that exist on target terminals but no longer have corresponding orders/positions on the source terminal. This can happen when:
- Source pending orders are manually cancelled
- Source positions are manually closed
- Connection issues cause synchronization problems
The system can automatically remove these if
kill_orphaned_orders
is enabled.
Q: Can I filter which order types to copy?
A: Yes, use the allowed_order_types
setting to specify which order types should be copied (e.g., only limit orders for safety). This only applies to pending orders - active positions are managed regardless of their original order type.
Q: Does the system copy existing active positions? A: No, the system only copies new pending orders. However, it will track and manage active positions that result from previously copied pending orders getting triggered.
Q: What happens when a copied pending order gets triggered? A: When a pending order becomes an active position, the system automatically starts tracking that position. It will monitor the position until it's closed on the source terminal, and can manage orphaned positions if they're closed on source but remain open on targets.
Q: Can I copy stop-loss and take-profit levels? A: The system copies the original pending order with its SL/TP levels. However, if you manually modify SL/TP levels after the order is placed, those modifications are not automatically copied to target terminals.
Q: Is it safe to run this on live accounts? A: The system is designed with safety in mind, but you should:
- Test thoroughly on demo accounts
- Start with small lot sizes
- Use conservative risk management settings
- Monitor the system closely
- Have stop-loss and take-profit levels set
Q: What if the system places too many orders?
A: Use the max_pending_orders
setting to limit the number of pending orders. The system will stop copying new orders when this limit is reached.
Q: Can I stop the system immediately?
A: Yes, press Ctrl+C
to stop the system gracefully. It will finish processing current operations and save state before exiting.
Q: How often does the system check for new orders? A: This depends on the execution mode:
once
: Runs once and exitsscheduled
: Runs at specified intervals (configurable)continuous
: Runs continuously with minimal delay (configurable)
Q: Does the system use a lot of system resources? A: No, the system is lightweight and designed for minimal resource usage. It only connects to MT5 when needed and uses efficient polling.
Q: Can I run multiple instances? A: Yes, but ensure each instance uses different configuration files and log files to avoid conflicts.
Q: I get "Terminal not found" errors. What should I do? A: Check that:
- MT5 is installed and the path in config is correct
- MT5 terminal is not already running
- You have proper permissions to access the MT5 installation
Q: Orders are copied but with wrong lot sizes. Why? A: Check:
lot_multiplier
settingmin_lot_size
andmax_lot_size
limits- Broker's minimum lot size requirements
- Account type (micro, mini, standard)
Q: The system stops working after some time. What's wrong? A: Common causes:
- MT5 terminal disconnected from broker
- Network connectivity issues
- Broker server maintenance
- System running out of memory (check logs)
Check the log files for specific error messages.
If you encounter issues:
- Check the FAQ section above - Most common questions are answered there
- Review the troubleshooting section - Step-by-step solutions for common problems
- Check log files - Look at
logs/mt5_order_copier.log
for error messages - Ensure all prerequisites are met - Verify Python, MT5, and dependencies
- Test with demo accounts first - Always validate your setup safely
- Contact support with:
- Error messages from logs
- Configuration file (remove sensitive data)
- System specifications
- Steps to reproduce the issue
# Quick system test
python main.py --test-mode
# Run with verbose logging
python main.py --verbose
# Check Python and dependencies
python --version
pip list | findstr MetaTrader5
# View recent logs
type logs\mt5_order_copier.log | more
# Check if MT5 is running
tasklist | findstr terminal64
order-copier/
├── main.py # Main application entry point
├── config.py # Your configuration file
├── config_sample.py # Sample configuration template
├── requirements.txt # Python dependencies
├── test_system.py # System testing script
├── order_manager.py # Core order management logic
├── mt5_connector.py # MT5 connection handling
├── order_tracker.py # Order state tracking
├── utils.py # Utility functions
├── logs/ # Log files directory
│ └── mt5_order_copier.log
├── data/ # System state files
│ └── order_tracker_state.json
└── README.md # This documentation
Setting | Purpose | Example | Applies To |
---|---|---|---|
lot_multiplier |
Scale position sizes | 0.5 (half size), 2.0 (double) |
Both orders & positions |
min_lot_size |
Minimum lot size limit | 0.01 |
Both orders & positions |
max_lot_size |
Maximum lot size limit | 100.0 |
Both orders & positions |
allowed_order_types |
Filter which order types to copy | ['BUY_LIMIT', 'SELL_LIMIT'] |
Pending orders only |
symbol_mapping |
Map symbol names between brokers | {'GOLD': 'XAUUSD'} |
Both orders & positions |
max_pending_orders |
Limit total pending orders | {'enabled': True, 'max_orders': 10} |
Pending orders only |
kill_orphaned_orders |
Auto-close orphaned orders/positions | True or False |
Both orders & positions |
max_orphan_checks |
Verification cycles before orphan action | 3 |
Both orders & positions |
Pending Order Settings:
allowed_order_types
: Controls which types of pending orders are copied from sourcemax_pending_orders
: Prevents excessive pending orders on target terminals
Position Management Settings:
lot_multiplier
: Applied when copying pending orders, affects resulting position sizemin_lot_size
/max_lot_size
: Enforced for both initial orders and resulting positions
Orphan Management (Critical for Both):
kill_orphaned_orders
: WhenTrue
, automatically closes orders/positions that exist on target but not sourcemax_orphan_checks
: Number of verification cycles before taking orphan cleanup action
Symbol Mapping (Universal):
- Maps symbol names between different brokers for both pending orders and active positions
- Essential when brokers use different symbol naming conventions
This MT5 Order Copier system automatically copies pending orders from one MetaTrader 5 terminal to one or more target terminals, and then manages the resulting active positions when those orders get triggered. Here's what you need to know to get started:
- Windows computer with Python 3.7+
- MetaTrader 5 installed
- MT5 accounts (demo recommended for testing)
- Basic understanding of trading concepts
- Install Python and required packages
- Download this order copier system
- Copy
config_sample.py
toconfig.py
- Edit
config.py
with your MT5 account details - Test the system:
python main.py --test-mode
- Run the system:
python main.py
- Always test with demo accounts first
- Start with small lot sizes (
lot_multiplier: 0.1
) - Use conservative settings initially
- Monitor the system closely when running
- Have proper risk management in place
- Copy orders between multiple brokers
- Automatic lot size scaling
- Symbol name mapping for different brokers
- Order type filtering for safety
- Orphaned order management
- Comprehensive logging and monitoring
- Flexible execution modes (once, scheduled, continuous)
- Read the FAQ section for common questions
- Check the Troubleshooting section for problems
- Review log files for error details
- Test with the built-in test mode
Remember: This system is a powerful tool that can significantly impact your trading. Take time to understand all settings and test thoroughly before using with live accounts.
The system provides comprehensive statistics:
# Access statistics programmatically
stats = order_manager.get_statistics()
print(f"Orders copied: {stats['processing_stats']['orders_copied']}")
print(f"Orders updated: {stats['processing_stats']['orders_updated']}")
print(f"Orders cancelled: {stats['processing_stats']['orders_cancelled']}")
'symbol_mapping': {
# Forex pairs
'EURUSD.m': 'EURUSD',
'GBPUSD.m': 'GBPUSD',
# Indices
'US30.cash': 'US30',
'SPX500.cash': 'SPX500',
# Commodities
'GOLD.spot': 'XAUUSD',
'SILVER.spot': 'XAGUSD'
}
# Lot size constraints
'lot_multiplier': 0.1, # Risk 10% of source position
'min_lot_size': 0.01, # Minimum position size
'max_lot_size': 1.0, # Maximum position size
# Order limits
'max_pending_orders': {
'enabled': True,
'max_orders': 20 # Limit total pending orders
}
'Terminal1': {
# Conservative settings
'lot_multiplier': 0.5,
'allowed_order_types': ['BUY_LIMIT', 'SELL_LIMIT'],
'orphan_management': {
'kill_orphaned_orders': False # Keep orphans
}
},
'Terminal2': {
# Aggressive settings
'lot_multiplier': 2.0,
'allowed_order_types': ['BUY_STOP', 'SELL_STOP'],
'orphan_management': {
'kill_orphaned_orders': True,
'max_orphan_checks': 1 # Quick orphan removal
}
}
- Credential Protection: Store passwords securely, consider environment variables
- Access Control: Limit system access to authorized users
- Log Security: Protect log files from unauthorized access
- Network Security: Use secure connections when possible
- Backup Strategy: Regular backups of configuration and state files
- Execution Intervals: Balance between responsiveness and resource usage
- Log Levels: Use appropriate log levels for production
- Terminal Limits: Set reasonable maximum order limits
- Resource Monitoring: Monitor CPU and memory usage
- Network Optimization: Minimize connection overhead
- Monitor log files for errors
- Review system statistics
- Update configuration as needed
- Backup state files regularly
- Test with demo accounts first
- Check log files for error messages
- Verify MT5 terminal connections
- Test with minimal configuration
- Review recent configuration changes
- Check system resources and permissions
This system is provided as-is for educational and development purposes. Use at your own risk in live trading environments.
Trading financial instruments involves substantial risk of loss and is not suitable for all investors. This software is provided for educational purposes only and should be thoroughly tested before use in live trading environments. The authors are not responsible for any financial losses incurred through the use of this software.