Connect your MetaTrader 4 Expert Advisors to MT4 Manager API with minimal code changes.
Note: This is the MT4 Connector component of the SoloDex platform. For complete setup including database, authentication, and monitoring features, see the main README.
- What is MT4 Dynamic Trailing?
- Quick Start
- EA Integration
- Telegram Bot Integration
- Troubleshooting
- Technical Reference
The MT4 Dynamic Trailing connector is a tool that enables your Expert Advisors to execute trades automatically through the MT4 Manager API using a simple file-based communication method.
- One-Click Setup: Simple installation and configuration
- Minimal EA Changes: Add just one function to your EA code
- Cross-Platform: Works on Windows, Mac, and Linux
- Automatic Order Management: Place, modify, and close trades
- Full Signal Support: Market orders, pending orders, and position management
- Telegram Integration: Control trades from your mobile device
- Double-click
scripts/START_CONNECTOR.bat - Follow the on-screen prompts to enter your MT4 server details
- Open a terminal in the project directory
- Run
./scripts/START_CONNECTOR.sh(you may need to make it executable withchmod +x scripts/START_CONNECTOR.sh) - Follow the on-screen prompts
The connector will:
- Check and install required dependencies
- Create necessary directories and files
- Guide you through MT4 connection setup
- Connect to your MT4 server
- Start monitoring for trading signals
Important: The MT4 Connector does NOT need to be installed in any specific directory. It works independently of your MT4 terminal installation.
- MT4 Manager API: Connects directly to the MT4 server over the network (e.g., 195.88.127.154:45543)
- No MT4 Terminal Required: Uses manager credentials to authenticate directly with the broker's server
- File-Based Communication: Your EA writes signals to a JSON file that the connector monitors
C:\Any\Directory\You\Want\MT4Connector\
├── mt4_api\
│ ├── mtmanapi.dll (Required - MT4 Manager API)
│ └── mtmanapi64.dll (Required - 64-bit version)
├── signals\
│ └── ea_signals.txt (EA writes signals here)
├── src\
│ └── (connector code)
└── START_WINDOWS.bat
- MT4 Terminal:
C:\Program Files (x86)\MetaTrader 4\ - MT4 Connector:
C:\MT4Connector\(or any location you prefer) - EA Signal File: Update your EA to write to the connector's signals directory
The connector acts as a bridge between your EA and the MT4 server, using the Manager API for direct server communication.
The MT4 Connector includes a Telegram bot that allows you to receive trading signals and manage trades from your mobile device.
-
Create a new bot using BotFather in Telegram:
- Open Telegram and search for
@BotFather - Send the command
/newbotand follow the instructions - Copy the API token provided by BotFather
- Open Telegram and search for
-
Configure the bot in MT4 Connector:
- Open
src/config.pyin the MT4 Connector directory - Set your bot token:
TELEGRAM_BOT_TOKEN = "YOUR_TOKEN_HERE" - Add your Telegram user ID:
TELEGRAM_ADMIN_IDS = [YOUR_USER_ID] - To find your user ID, chat with
@userinfobotin Telegram
- Open
-
Run the Telegram bot:
python src/run_with_telegram.py
- Receive Trading Signals: Get real-time notifications about new trading signals from your EAs
- Approve/Reject Trades: Make trading decisions directly from Telegram
- Modify Orders: Adjust parameters like volume, stop loss, or take profit
- Settings Management: Configure auto-approval and notification preferences
/start- Start the bot and show welcome message/help- Show help information/settings- Configure your trading preferences
For more details, see the Telegram Bot README.
- Copy this function to your Expert Advisor:
void SendSignalToConnector(string type, string symbol, double volume,
double stoploss = 0, double takeprofit = 0,
string comment = "", int magic = 0, double price = 0,
int ticket = 0) {
// Create a unique ID for this signal
string signal_id = IntegerToString(TimeCurrent()) + "_" + DoubleToString(volume, 2) + "_" + symbol;
// Open the signal file - UPDATE THIS PATH to match your installation!
int file = FileOpen("C:\\MT4_Dynamic_Trailing\\signals\\ea_signals.txt", FILE_WRITE|FILE_TXT);
if(file != INVALID_HANDLE) {
// Create JSON with required fields
string json = "{\"signal_id\":\"" + signal_id + "\",\"type\":\"" + type +
"\",\"symbol\":\"" + symbol + "\",\"volume\":" + DoubleToString(volume, 2) +
",\"login\":\"" + IntegerToString(AccountNumber()) + "\"";
// Add optional fields if provided
if(stoploss > 0) json += ",\"stoploss\":" + DoubleToString(stoploss, 5);
if(takeprofit > 0) json += ",\"takeprofit\":" + DoubleToString(takeprofit, 5);
if(comment != "") json += ",\"comment\":\"" + comment + "\"";
if(magic > 0) json += ",\"magic\":" + IntegerToString(magic);
if(price > 0) json += ",\"price\":" + DoubleToString(price, 5);
if(ticket > 0) json += ",\"ticket\":" + IntegerToString(ticket);
json += "}";
// Write the JSON to the file
FileWriteString(file, json);
FileClose(file);
Print("Signal sent to connector: ", json);
} else {
Print("Error opening signal file!");
}
}- Update the signal file path in the
FileOpen()function to match your installation directory:- Windows:
"C:\\MT4_Dynamic_Trailing\\signals\\ea_signals.txt" - Mac/Linux:
"Z:/path/to/MT4_Dynamic_Trailing/signals/ea_signals.txt"
- Windows:
// Buy 0.1 lot of EURUSD
SendSignalToConnector("buy", "EURUSD", 0.1);
// Sell 0.05 lots of GBPUSD with SL and TP
SendSignalToConnector("sell", "GBPUSD", 0.05, 1.3050, 1.2850);// Buy limit 0.1 lot of EURUSD at 1.1800
SendSignalToConnector("buy_limit", "EURUSD", 0.1, 0, 0, "", 0, 1.1800);
// Sell stop 0.2 lots of USDJPY at 109.50 with comment and magic number
SendSignalToConnector("sell_stop", "USDJPY", 0.2, 110.50, 108.50, "MA Cross", 12345, 109.50);// Close position with ticket #123456
SendSignalToConnector("close", "EURUSD", 0.0, 0, 0, "", 0, 0, 123456);
// Modify SL and TP on position #123456
SendSignalToConnector("modify", "EURUSD", 0.0, 1.1850, 1.1950, "", 0, 0, 123456);| Signal Type | Description | Required Parameters |
|---|---|---|
buy |
Market buy order | symbol, volume, login |
sell |
Market sell order | symbol, volume, login |
buy_limit |
Buy limit pending order | symbol, volume, login, price |
sell_limit |
Sell limit pending order | symbol, volume, login, price |
buy_stop |
Buy stop pending order | symbol, volume, login, price |
sell_stop |
Sell stop pending order | symbol, volume, login, price |
close |
Close an open position | symbol, login, ticket |
modify |
Modify an existing order | symbol, login, ticket, (+ parameters to modify) |
- Ensure Python 3.6+ is installed and added to your PATH
- Try running the script as Administrator
- Check the logs folder for error messages
- Run the application as Administrator
- Check if your antivirus has quarantined the DLL files
- Add an exception in your antivirus for the MT4_Dynamic_Trailing folder
- Ensure you have the Microsoft Visual C++ Redistributable installed
- Double-check your MT4 server address, port, username, and password
- Ensure your MT4 server allows API access
- Check if your firewall is blocking the connection
- Verify the EA is writing to the correct signal file location
- Ensure the signal file format matches the expected JSON format
- Check logs for any parsing errors
- Update the signal file path in the EA to match your installation directory
- Use double backslashes in file paths in MQL4 code
- Ensure MT4 has permission to write to the signals directory
- Navigate to the
logsfolder in your MT4_Dynamic_Trailing directory - Look for the most recent log file
- Open it with any text editor to see detailed error messages
To test only the MT4 API connection:
- Open a command prompt in the MT4_Dynamic_Trailing directory
- Run:
python -c "from src.mt4_api import MT4API; api = MT4API(); print(api.connect('your_server', port, 'username', 'password'))"
- Python 3.6 or higher
- MetaTrader 4 with Manager API access
- Internet connection to your MT4 server
MT4Connector/
├── README.md # This documentation file
├── LICENSE # MIT License file
├── CHANGELOG.md # Version history and changes
├── package.json # Project metadata and scripts
├── requirements.txt # Python dependencies
├── assets/ # Project assets
│ └── logos/ # Project logos and branding
│ └── logo.png # Main project logo
├── config/ # Configuration files
│ └── mt4-connector.rdp # Remote desktop connection file
├── docs/ # Documentation files
│ └── WINDOWS_SETUP_GUIDE.md # Windows setup guide
├── deploy/ # Deployment scripts and configurations
│ ├── WINDOWS_SERVER_CORE_SETUP.md
│ ├── deploy_to_windows_core.sh
│ ├── quick_deploy.sh
│ ├── quick_setup.ps1
│ └── setup_and_deploy.sh
├── examples/ # Example MT4 EA files
│ ├── SignalWriter.mq4 # Signal writer EA
│ ├── pumping_mode_example.py # Pumping mode example
│ └── websocket_client_example.html # WebSocket client example
├── logs/ # Log files directory
├── mt4_api/ # MT4 Manager API files
│ ├── MT4Manager.h # MT4 Manager API header
│ ├── MT4ManagerAPI.h # MT4 Manager API header
│ ├── MT4RestfulAPIWrapper.zip # REST API wrapper
│ ├── MT4RestfulAPIWrapper/ # REST API wrapper source
│ ├── mtmanapi.dll # MT4 Manager API DLL (32-bit)
│ ├── mtmanapi64.dll # MT4 Manager API DLL (64-bit)
│ ├── pumping_mode_design.md # Pumping mode design docs
│ └── logs/ # MT4 API specific logs
├── scripts/ # Setup and execution scripts
│ ├── 1) TEST_CONNECTION_WINDOWS.bat
│ ├── 2) START_WINDOWS.bat
│ ├── START_CONNECTOR.bat # Windows one-click starter script
│ ├── START_CONNECTOR.command # macOS one-click starter script
│ ├── START_CONNECTOR.sh # Linux one-click starter script
│ ├── deploy_to_ubuntu.sh # Ubuntu deployment script
│ ├── run.bat # Windows run script
│ ├── run.sh # Linux/macOS run script
│ ├── setup.bat # Windows setup script
│ ├── setup.sh # Linux/macOS setup script
│ └── setup_ubuntu.sh # Ubuntu setup script
├── signals/ # Signal files directory
│ ├── ea_signals.txt # Signal file monitored by the connector
│ └── ea_signals_example.txt # Example signal file
├── src/ # Source code
│ ├── app.py # Main application
│ ├── config.py # Configuration module
│ ├── dx_integration.py # Trading platform integration
│ ├── mt4_api.py # MT4 API wrapper
│ ├── mt4_event_dispatcher.py # Event dispatcher
│ ├── mt4_pumping.py # Pumping mode implementation
│ ├── mt4_real_api.py # Real MT4 API implementation
│ ├── mt4_websocket.py # WebSocket server
│ ├── run_mt4_connector.py # MT4 connector runner
│ ├── run_with_telegram.py # Telegram bot integration
│ ├── signal_processor.py # Signal processing logic
│ ├── logs/ # Source-specific logs
│ ├── signals/ # Source-specific signal files
│ └── utils/ # Utility modules
│ ├── debug_signal.py # Signal debugging utility
│ └── generate_test_signal.py # Test signal generator
├── telegram_bot/ # Telegram bot functionality
│ ├── README.md # Telegram bot documentation
│ ├── bot.py # Telegram bot implementation
│ └── signal_handler.py # Signal handling for Telegram
└── tests/ # Test suite
├── README.md # Testing documentation
├── run_all_tests.py # Test runner implementation
├── test_connection.py # Connection tests
├── test_config.py # Config module tests
├── test_dx_integration.py # Integration tests
├── test_error_handling.py # Error handling tests
├── test_integration.py # Integration tests
├── test_mt4_api.py # MT4 API tests
├── test_mt4_real_api.py # Real MT4 API tests
├── test_phase1_integration.py # Phase 1 integration tests
├── test_phase2_trading.py # Phase 2 trading tests
├── test_pumping_mode.py # Pumping mode tests
├── test_signal_monitoring.py # Signal monitoring tests
├── test_signal_processing.py # Signal processing tests
├── test_signal_writer.py # Signal writer tests
├── test_telegram_bot.py # Telegram bot tests
└── test_trade_signals.py # Trade signal tests
The connector processes trading signals in JSON format with these required fields:
signal_id: A unique identifier for the signaltype: The order type (buy, sell, etc.)symbol: The trading instrument symbolvolume: Trading volume/lot sizelogin: Your MT4 account number
Optional fields:
stoploss: Stop loss price leveltakeprofit: Take profit price levelcomment: Order commentmagic: Magic number for the orderprice: Price for pending ordersticket: Ticket number for modify/close operations
- Generate unique signal IDs to prevent duplicate execution
- Always check if the file opened successfully
- Use
Print()to log when signals are sent - Avoid sending too many signals in a short time
- Check logs if orders aren't being executed
- Always test in a demo account first
If you encounter issues:
- Check the Troubleshooting section above
- Examine the log files in the
logsdirectory - Contact support with details about your issue
This software is provided "as is" and is intended for educational and trading purposes only. Use at your own risk.
