Skip to content

πŸš€ Connect your MetaTrader 4 Expert Advisors to MT4 Manager API with minimal code changes. Cross-platform trading automation with Telegram bot integration, real-time data streaming, and comprehensive signal processing.

License

Notifications You must be signed in to change notification settings

michael-abdo/mt4-connector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MT4 Dynamic Trailing

MT4 Connector Logo

License: MIT Python Version Platform Build Status Test Coverage Version Telegram MT4

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.

Table of Contents

What is MT4 Dynamic Trailing?

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.

Key Features

  • 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

Quick Start

Windows Users

  1. Double-click scripts/START_CONNECTOR.bat
  2. Follow the on-screen prompts to enter your MT4 server details

Mac/Linux Users

  1. Open a terminal in the project directory
  2. Run ./scripts/START_CONNECTOR.sh (you may need to make it executable with chmod +x scripts/START_CONNECTOR.sh)
  3. Follow the on-screen prompts

What Happens Behind the Scenes

The connector will:

  1. Check and install required dependencies
  2. Create necessary directories and files
  3. Guide you through MT4 connection setup
  4. Connect to your MT4 server
  5. Start monitoring for trading signals

Installation Directory Information

Important: The MT4 Connector does NOT need to be installed in any specific directory. It works independently of your MT4 terminal installation.

How It Works:

  • 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

Directory Structure:

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

Example Setup:

  • 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.

Telegram Bot Integration

The MT4 Connector includes a Telegram bot that allows you to receive trading signals and manage trades from your mobile device.

Setting Up the Telegram Bot

  1. Create a new bot using BotFather in Telegram:

    • Open Telegram and search for @BotFather
    • Send the command /newbot and follow the instructions
    • Copy the API token provided by BotFather
  2. Configure the bot in MT4 Connector:

    • Open src/config.py in 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 @userinfobot in Telegram
  3. Run the Telegram bot:

    python src/run_with_telegram.py
    

Using the Telegram Bot

  • 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

Bot Commands

  • /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.

EA Integration

Basic Integration

  1. 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!");
   }
}
  1. 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"

Using the Function

Market Orders

// 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);

Pending Orders

// 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);

Position Management

// 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);

Supported Signal Types

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)

Troubleshooting

Common Issues

Application Won't Start

  • Ensure Python 3.6+ is installed and added to your PATH
  • Try running the script as Administrator
  • Check the logs folder for error messages

DLL Loading Failures

  • 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

Connection Issues

  • 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

Signal File Problems

  • 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

EA Integration Issues

  • 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

Checking Logs

  1. Navigate to the logs folder in your MT4_Dynamic_Trailing directory
  2. Look for the most recent log file
  3. Open it with any text editor to see detailed error messages

Testing MT4 API Connection

To test only the MT4 API connection:

  1. Open a command prompt in the MT4_Dynamic_Trailing directory
  2. Run: python -c "from src.mt4_api import MT4API; api = MT4API(); print(api.connect('your_server', port, 'username', 'password'))"

System Requirements

  • Python 3.6 or higher
  • MetaTrader 4 with Manager API access
  • Internet connection to your MT4 server

Project Structure

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

Technical Reference

Signal File Format

The connector processes trading signals in JSON format with these required fields:

  • signal_id: A unique identifier for the signal
  • type: The order type (buy, sell, etc.)
  • symbol: The trading instrument symbol
  • volume: Trading volume/lot size
  • login: Your MT4 account number

Optional fields:

  • stoploss: Stop loss price level
  • takeprofit: Take profit price level
  • comment: Order comment
  • magic: Magic number for the order
  • price: Price for pending orders
  • ticket: Ticket number for modify/close operations

Best Practices

  1. Generate unique signal IDs to prevent duplicate execution
  2. Always check if the file opened successfully
  3. Use Print() to log when signals are sent
  4. Avoid sending too many signals in a short time
  5. Check logs if orders aren't being executed
  6. Always test in a demo account first

Support

If you encounter issues:

  1. Check the Troubleshooting section above
  2. Examine the log files in the logs directory
  3. Contact support with details about your issue

License

This software is provided "as is" and is intended for educational and trading purposes only. Use at your own risk.

About

πŸš€ Connect your MetaTrader 4 Expert Advisors to MT4 Manager API with minimal code changes. Cross-platform trading automation with Telegram bot integration, real-time data streaming, and comprehensive signal processing.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published