Skip to main content
Glama
Riffmax2030-hub

metatrader-mcp-server

README.md
# ๐Ÿ“ˆ Riffmax-TradingBot-AI-RiffEx

> **Autonomous AI Trading Agent powered by Python and MetaTrader 5 (MT5) MCP** โ€” real-time market structure analysis, automated signal generation, intelligent order execution, and strict risk guardrails.

[![Python](https://img.shields.io/badge/Python-3.10%2B-blue?logo=python)](https://python.org)
[![MetaTrader 5](https://img.shields.io/badge/MetaTrader-5-green)](https://www.mql5.com/)
[![MCP Protocol](https://img.shields.io/badge/MCP-Model%20Context%20Protocol-orange)](https://modelcontextprotocol.io/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)
[![Status: Active](https://img.shields.io/badge/Status-Active-brightgreen)]()

---

## ๐Ÿ“Œ What it does

**Riffmax-TradingBot-AI-RiffEx** is an autonomous trading agent that operates directly on **MetaTrader 5 (MT5)** using Python and the **Model Context Protocol (MCP)**:

- ๐Ÿ“Š **Real-Time Market Analysis** โ€” Ingests live tick data, OHLCV candles, order books, and open positions directly through MT5 MCP tools.
- ๐Ÿง  **AI Decision Engine** โ€” Analyzes price action, market liquidity, and volatility metrics using Python-based quantitative models and LLM agent reasoning.
- โšก **Direct Order Execution** โ€” Places, tracks, modifies, and closes market and pending orders programmatically via the MT5 MCP interface.
- ๐Ÿ›ก๏ธ **Autonomous Risk Guardrails** โ€” Enforces dynamic lot sizing based on account equity, automatic stop-loss (SL), take-profit (TP), maximum drawdown thresholds, and spread filtering.
- ๐Ÿ“ **Structured Logging & Auditing** โ€” Records all signal evaluations, execution payloads, and deal receipts in JSON/CSV audit trails.

---

## ๐Ÿ—๏ธ Architecture

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚             AI Agent Decision Layer (Python)           โ”‚
โ”‚   โ€ข Technical Analysis  โ€ข Price Action  โ€ข Volatility   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                            โ”‚
               Model Context Protocol (MCP)
                            โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    MT5 MCP Server                      โ”‚
โ”‚   โ€ข get_candles_latest   โ€ข get_account_info            โ”‚
โ”‚   โ€ข place_market_order   โ€ข modify_position             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                            โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚               MetaTrader 5 (MT5) Terminal              โ”‚
โ”‚       Broker Execution ยท Live Market Liquidity         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

---

## ๐Ÿ› ๏ธ Stack

| Component | Technology | Description |
|---|---|---|
| **Language** | Python 3.10+ | Core trading engine and quantitative analysis |
| **Execution Protocol** | MT5 Model Context Protocol (MCP) | Tool-based interface for market data & order dispatch |
| **Broker Platform** | MetaTrader 5 (MT5) | Multi-asset execution terminal (Forex, Indices, Crypto) |
| **Data Processing** | NumPy, Pandas | Candle parsing, moving averages, volatility metrics |
| **Risk Management** | Custom Python Engine | Lot sizing, trailing stops, daily loss limits |
| **Configuration** | `.env` + `config.yaml` | Secure credential isolation |

---

## ๐Ÿš€ Quick Start

### Prerequisites
- Python 3.10+
- MetaTrader 5 terminal installed and logged into your broker account
- MT5 MCP server configured

### 1. Clone & Install

```bash
git clone https://github.com/Riffmax2030-hub/Riffmax-TradingBot-AI-RiffEx.git
cd Riffmax-TradingBot-AI-RiffEx
pip install -r requirements.txt
```

### 2. Configure Environment

```bash
cp .env.example .env
# Edit .env with your credentials โ€” NEVER commit real .env files
```

`.env.example` template:
```env
MT5_ACCOUNT=your_account_number
MT5_PASSWORD=your_password
MT5_SERVER=your_broker_server
MAX_RISK_PER_TRADE_PERCENT=1.0
MAX_DAILY_DRAWDOWN_PERCENT=3.0
```

### 3. Run the Trading Agent

```bash
python main.py
```

---

## ๐Ÿ“ Project Structure

```
Riffmax-TradingBot-AI-RiffEx/
โ”œโ”€โ”€ agent/
โ”‚   โ”œโ”€โ”€ market_analyzer.py    # Price action & indicator analysis
โ”‚   โ”œโ”€โ”€ decision_engine.py    # Signal generation logic
โ”‚   โ”œโ”€โ”€ mcp_bridge.py         # MT5 Model Context Protocol interface
โ”‚   โ””โ”€โ”€ risk_guard.py         # Dynamic lot sizing & risk limits
โ”œโ”€โ”€ config/
โ”‚   โ””โ”€โ”€ config.yaml           # Symbol lists, timeframe, max spread
โ”œโ”€โ”€ logs/                     # Trade logs & audit trails (gitignored)
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ test_market_analyzer.py
โ”‚   โ””โ”€โ”€ test_risk_guard.py
โ”œโ”€โ”€ .env.example              # Template with placeholders
โ”œโ”€โ”€ .gitignore                # Excludes credentials, logs, caches
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ README.md
```

---

## ๐Ÿ” Security & Risk Disclaimer

> [!CAUTION]
> **Financial & Credential Security:**
> - Never hardcode broker account numbers, server details, or passwords in code or commits.
> - All sensitive configurations are loaded strictly via environment variables.
> - Trading financial markets carries risk. Use a demo account for testing before any live deployment.

---

## ๐Ÿ“„ License

MIT ยฉ [Sherif Akanmu](https://github.com/Riffmax2030-hub)