Labsco
8nite logo

MetaTrader 4

โ˜… 17

from 8nite

Integrate with the MetaTrader 4 trading platform to access trading functions and data via an HTTP bridge and Expert Advisor.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedFreeAdvanced setup

MCP MetaTrader 4 Server

A Model Context Protocol (MCP) server that provides cross-platform integration with MetaTrader 4 trading platform over network.

Author: 8nite (8nite@qmlab.io)

Features

  • Account Information: Get real-time account details (balance, equity, margin, etc.)
  • Market Data: Retrieve current market prices for trading symbols
  • Order Management: Place market and pending orders
  • Position Management: View and close open positions
  • Trading History: Access historical trading data
  • Backtesting: Run backtests on Expert Advisors with detailed configuration
  • Expert Advisor Management: List and manage available EAs
  • File-Based Fallback: Robust status and results tracking when API endpoints fail
  • EA Development: Remote EA creation, editing, sync, and compilation with error reporting
  • Cross-Platform: Linux MCP server connects to Windows MT4 via HTTP

Architecture

The integration uses HTTP communication between the MCP server and MT4:

  1. MCP Server (Linux - Node.js/TypeScript) - Provides tools for Claude Code
  2. HTTP Bridge (Windows - Node.js) - Converts HTTP requests to file I/O
  3. MT4 Expert Advisor (Windows - MQL4) - Handles trading operations
  4. Network Communication - JSON-based data exchange via HTTP API

EA Development Workflow

List Local EAs

Show me all EAs in the active development folder

Sync EA to MT4

Upload MyStrategy EA to MetaTrader for compilation

Compile EA

Compile MyStrategy EA and show any errors or warnings

Create New EA from Template

cd ea-strategies
./develop.sh new MyNewStrategy SimpleMA_Template.mq4
./develop.sh edit MyNewStrategy

File Structure

โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ index.ts                    # MCP server implementation
โ”œโ”€โ”€ mcp/                            # MCP infrastructure files
โ”‚   โ””โ”€โ”€ MCP_Ultimate.mq4           # Ultimate unified MCP Bridge with all features
โ”œโ”€โ”€ strategies/                     # User trading strategies (git ignored)
โ”‚   โ””โ”€โ”€ [User strategy EAs]        # Custom trading strategies for personal use
โ”œโ”€โ”€ ea-strategies/                  # EA development workspace (git ignored)
โ”‚   โ”œโ”€โ”€ templates/                  # EA templates and examples
โ”‚   โ”œโ”€โ”€ active/                     # EAs under development
โ”‚   โ”œโ”€โ”€ compiled/                   # Successfully compiled EAs
โ”‚   โ”œโ”€โ”€ logs/                       # Compilation logs and error reports
โ”‚   โ”œโ”€โ”€ develop.sh                  # Development helper script
โ”‚   โ””โ”€โ”€ README.md                   # EA development guide
โ”œโ”€โ”€ MT4_Files/                      # Legacy MCP files
โ”‚   โ”œโ”€โ”€ MCPBridge_Unified.mq4      # Unified MCP Bridge with enhanced reporting
โ”‚   โ””โ”€โ”€ MCPBridge.mq4              # Legacy MCP Bridge (basic functionality)
โ”œโ”€โ”€ dist/                          # Compiled JavaScript
โ”œโ”€โ”€ MCPBridge_Unified_Guide.md     # Complete setup guide for unified EA
โ”œโ”€โ”€ bridge-endpoints-needed.md     # Required HTTP bridge endpoints
โ”œโ”€โ”€ package.json
โ””โ”€โ”€ tsconfig.json

Data Files

The Expert Advisor creates these files in MT4's MQL4/Files/ folder:

  • account_info.txt - Account information
  • market_data_[SYMBOL].txt - Market data for each symbol
  • positions.txt - Open positions
  • order_commands.txt - Incoming order commands
  • close_commands.txt - Position close commands

File-Based Reporting (New Feature)

For enhanced backtest monitoring, EAs can write to the reports directory:

  • mt4_reports/backtest_status.json - Real-time backtest status and progress
  • mt4_reports/backtest_results.json - Detailed backtest results and statistics
  • mt4_reports/backtest_report.html - Optional HTML report for detailed analysis

The MCP server automatically falls back to reading these files when API endpoints are unavailable.

Configuration: Set MT4_REPORTS_PATH environment variable to customize the reports directory (default: /tmp/mt4_reports)

Security Notes

  • This integration is for educational/personal use
  • Never share your MT4 login credentials
  • Test thoroughly on a demo account before using with real money
  • The file-based communication is simple but not encrypted

Environment Variables

  • MT4_HOST: IP address of Windows machine running MT4 (default: 192.168.50.161)
  • MT4_PORT: Port number for HTTP bridge (default: 8080)
  • MT4_REPORTS_PATH: Directory path for EA report files (default: /tmp/mt4_reports)

EA Integration Guide

To enable file-based reporting in your Expert Advisors:

  1. Copy the EA_FileReporting_Template.mq4 code into your EA
  2. Call WriteBacktestStatus() periodically during backtesting
  3. Call WriteBacktestResults() when backtest completes
  4. Ensure MT4 has write permissions to the reports directory

The MCP server will automatically detect and use these files when API endpoints fail.

MCP Ultimate EA Features

MCP_Ultimate.mq4 is the most comprehensive MCP solution combining all functionality:

Key Features

  • Complete MCP Bridge Functions: Account info, market data, order management
  • Enhanced Backtest Reporting: Real-time status and comprehensive results with JSON format
  • Visual Indicators: On-chart status display showing MCP connection and performance
  • Advanced Market Data Tracking: Configurable tracking for major/minor/exotic pairs and commodities
  • Comprehensive Logging: Detailed operation logs with automatic rotation
  • Live Trading Support: Works seamlessly for both backtesting and live trading
  • File-Based Fallback: Automatic fallback to file-based reporting when API endpoints fail
  • Debug and Performance Monitoring: Real-time operation counters and performance metrics

Configuration Options

  • MCP Bridge Settings: Update intervals, reporting toggles, visual mode
  • Reporting Configuration: JSON format, detailed logs, maximum log files
  • Market Data Settings: Selectable tracking for different currency pair types

Quick Setup

  1. Use mcp/MCP_Ultimate.mq4 for complete functionality
  2. Configure all options in EA inputs based on your needs
  3. Full MCP functionality with enhanced visual feedback and monitoring

Organization Benefits

  • MCP Infrastructure: All MCP-related files in mcp/ folder
  • Strategy Separation: User strategies in strategies/ folder (git ignored)
  • Development Workspace: EA development tools in ea-strategies/ (git ignored)
  • Legacy Support: Previous versions maintained in MT4_Files/ for compatibility

See MCPBridge_Unified_Guide.md for detailed setup instructions on legacy versions.