MetaTrader 4

by 8nite

Not rated
GitHub

About

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

Details

Author
8nite
Categories
Developer Tools, Other, Finance, API
Tags
#trading

1. Install MCP Server (Linux Machine - 192.168.50.X)

2. Install HTTP Bridge (Windows Machine - 192.168.50.161)

- Copy thewindows-server/folder to your Windows machine - Install Node.js on Windows if not already installed - Navigate to thewindows-serverfolder and run:

3. Install MT4 Expert Advisor (Windows Machine)

- Copymcp/MCP_Ultimate.mq4to your MT4MQL4/Experts/folder - Compile the Expert Advisor in MetaEditor - Attach the Expert Advisor to any chart in MT4 - Configure EA inputs:

- EnableFileReporting: true (for enhanced reporting)
- EnableBacktestTracking: true (for backtest monitoring)
- EnableVisualMode: true (for on-chart status indicators)
- Configure market data tracking options as needed
- CopyMT4_Files/MCPBridge_Unified.mq4to your MT4MQL4/Experts/folder
- Follow same compilation and attachment process
- Enhanced functionality but not as comprehensive as Ultimate version
- CopyMT4_Files/MCPBridge.mq4to your MT4MQL4/Experts/folder
- Follow same compilation and attachment process
- Limited to basic MCP functionality without enhanced reporting

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

- 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

The integration uses HTTP communication between the MCP server and MT4:
- MCP Server(Linux - Node.js/TypeScript) - Provides tools for Claude Code
- HTTP Bridge(Windows - Node.js) - Converts HTTP requests to file I/O
- MT4 Expert Advisor(Windows - MQL4) - Handles trading operations
- Network Communication- JSON-based data exchange via HTTP API

1. Install MCP Server (Linux Machine - 192.168.50.X)

2. Install HTTP Bridge (Windows Machine - 192.168.50.161)

- Copy thewindows-server/folder to your Windows machine - Install Node.js on Windows if not already installed - Navigate to thewindows-serverfolder and run:

3. Install MT4 Expert Advisor (Windows Machine)

- Copymcp/MCP_Ultimate.mq4to your MT4MQL4/Experts/folder - Compile the Expert Advisor in MetaEditor - Attach the Expert Advisor to any chart in MT4 - Configure EA inputs:

- EnableFileReporting: true (for enhanced reporting)
- EnableBacktestTracking: true (for backtest monitoring)
- EnableVisualMode: true (for on-chart status indicators)
- Configure market data tracking options as needed
- CopyMT4_Files/MCPBridge_Unified.mq4to your MT4MQL4/Experts/folder
- Follow same compilation and attachment process
- Enhanced functionality but not as comprehensive as Ultimate version
- CopyMT4_Files/MCPBridge.mq4to your MT4MQL4/Experts/folder
- Follow same compilation and attachment process
- Limited to basic MCP functionality without enhanced reporting

4. Configure Claude Code (Linux Machine)

Add the MCP server to your Claude Code configuration:

MT4_HOST=192.168.50.161 MT4_PORT=8080 claude mcp add mt4-server npm run start
{ "mcpServers": { "mt4-server": { "command": "node", "args": ["dist/index.js"], "env": { "MT4_HOST": "192.168.50.161", "MT4_PORT": "8080" } } } }

Once configured, you can use these tools in Claude Code:

Use the get_account_info tool to see my MT4 account details
Get current market data for EURUSD using get_market_data
Place a BUY order for 0.1 lots of EURUSD with stop loss at 1.0850 and take profit at 1.0950
Show me all my open positions using get_positions
Run a backtest on MACD Sample Expert Advisor for EURUSD H1 from 2024-01-01 to 2024-12-31 with initial deposit 10000
Show me all available Expert Advisors for backtesting
Get the results from the last backtest with detailed information
Check the current status of a running backtest
Show me all EAs in the active development folder
Upload MyStrategy EA to MetaTrader for compilation
Compile MyStrategy EA and show any errors or warnings
cd ea-strategies ./develop.sh new MyNewStrategy SimpleMA_Template.mq4 ./develop.sh edit MyNewStrategy
├── 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

The Expert Advisor creates these files in MT4'sMQL4/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

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: SetMT4_REPORTS_PATHenvironment variable to customize the reports directory (default:/tmp/mt4_reports)

- 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

- File I/O based communication has some latency
- Limited to MT4 platform capabilities
- Requires MT4 to be running with the Expert Advisor attached
- No real-time streaming data (periodic updates only)
- MCP Server not responding: Check that MT4 is running with the Expert Advisor
- No market data: Ensure the symbols are available in your MT4 Market Watch
- Orders not executing: Verify that automated trading is enabled in MT4
- File access errors: Check MT4 data path configuration and file permissions
- Backtest status/results not available:

- Check if API endpoints are responding (server will auto-fallback to files)
- Ensure EA is writing tomt4_reports/directory using the provided template
- VerifyMT4_REPORTS_PATHenvironment variable is set correctly

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

To enable file-based reporting in your Expert Advisors:
- Copy theEA_FileReporting_Template.mq4code into your EA
- CallWriteBacktestStatus()periodically during backtesting
- CallWriteBacktestResults()when backtest completes
- 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.mq4is the most comprehensive MCP solution combining all functionality:

- 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

- 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
- Usemcp/MCP_Ultimate.mq4for complete functionality
- Configure all options in EA inputs based on your needs
- Full MCP functionality with enhanced visual feedback and monitoring

- MCP Infrastructure: All MCP-related files inmcp/folder
- Strategy Separation: User strategies instrategies/folder (git ignored)
- Development Workspace: EA development tools inea-strategies/(git ignored)
- Legacy Support: Previous versions maintained inMT4_Files/for compatibility

SeeMCPBridge_Unified_Guide.mdfor detailed setup instructions on legacy versions.

This is a web browser that enables your coding agent, such as Claude Code, to visit websites on your behalf and assist you in identifying bugs or creating UI test cases.

Interact with over 100 cryptocurrency exchange APIs using the CCXT library.

An MCP server and OpenAI function calling service for interacting with the Deriv API.

An MCP server for the Fyers API v3, featuring automated OAuth authentication.

An MCP server for the Zerodha Kite Connect API, featuring fully automated authentication without manual token handling.

Hosted, Stateless & Multitenant Kite MCP server enables AI assistants to access market data, manage portfolios, and execute trading operations through Zerodha Kite.

Access prediction market data from the PolyMarket API.

A server for local interactions with the QuantConnect API.

A demo MCP server that provides rude responses to stock queries.

An MCP server for interacting with the Trading Simulator API to simulate trading activities.

The Capital.com MCP Server lets your AI assistant talk to your trading account directly. Market data, position checks, trade previews – all in plain language, without leaving your AI tool.

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.