trading-platform/docs/01-arquitectura/ARQUITECTURA-INTEGRACION-MT4-MCP-LLM.md
rckrdmrd c1b5081208 feat(ml): Complete FASE 11 - BTCUSD update and comprehensive documentation alignment
ML Engine Updates:
- Updated BTCUSD with Polygon API data (2024-2025): 215,699 new records
- Re-trained all ML models: Attention (R²: 0.223), Base, Metamodel (87.3% confidence)
- Backtest results: +176.71R profit with aggressive_filter strategy

Documentation Consolidation:
- Created docs/99-analisis/_MAP.md index with 13 new analysis documents
- Consolidated inventories: removed duplicates from orchestration/inventarios/
- Updated ML_INVENTORY.yml with BTCUSD metrics and training results
- Added execution reports: FASE11-BTCUSD, correction issues, alignment validation

Architecture & Integration:
- Updated all module documentation with NEXUS v3.4 frontmatter
- Fixed _MAP.md indexes across all folders
- Updated orchestration plans and traces

Files: 229 changed, 5064 insertions(+), 1872 deletions(-)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-07 09:31:29 -06:00

253 lines
17 KiB
Markdown

---
id: "ARQUITECTURA-INTEGRACION-MT4-MCP-LLM"
title: "Arquitectura de Integracion MT4-MCP-LLM"
type: "Documentation"
project: "trading-platform"
version: "1.0.0"
created_date: "2026-01-04"
---
# Arquitectura de Integracion MT4-MCP-LLM
**Version:** 1.0.0
**Fecha:** 2026-01-04
**Autor:** Tech-Leader Agent
**Estado:** En Desarrollo
---
## Vision General
Este documento describe la arquitectura de integracion entre:
- **MetaTrader4 (MT4)** - Plataforma de trading forex
- **MCP Server** - Model Context Protocol para herramientas
- **LLM Agent** - Copiloto de trading con IA
- **ML Engine** - Modelos de prediccion de mercado
---
## Diagrama de Arquitectura
```
┌─────────────────────────────────────────────────────────────────────────────────────┐
│ TRADING PLATFORM ARCHITECTURE │
├─────────────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────────────────────────────┐ │
│ │ USER INTERFACES │ │
│ │ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │ │
│ │ │ Web App │ │ Chat Widget │ │ Telegram Bot │ │ CLI │ │ │
│ │ │ (React UI) │ │ (LLM UI) │ │ (Future) │ │ (Future) │ │ │
│ │ └───────┬───────┘ └───────┬───────┘ └───────┬───────┘ └───────┬───────┘ │ │
│ └──────────┼──────────────────┼──────────────────┼──────────────────┼─────────────┘ │
│ │ │ │ │ │
│ └──────────────────┼──────────────────┴──────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────────────────┐ │
│ │ API GATEWAY (Backend Express) │ │
│ │ :3000 │ │
│ └────────────────────────────────────┬────────────────────────────────────────────┘ │
│ │ │
│ ┌──────────────────────────┼────────────────────────────┐ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌──────────────────┐ ┌──────────────────┐ ┌──────────────────────────┐ │
│ │ LLM AGENT │ │ ML ENGINE │ │ TRADING AGENTS │ │
│ │ :8003 │ │ :8001 │ │ :8004 │ │
│ │ │ │ │ │ │ │
│ │ ┌──────────────┐ │ │ ┌──────────────┐ │ │ ┌────────┐ ┌────────┐ │ │
│ │ │ Ollama LLM │ │ │ │ AMD Detector │ │ │ │ Atlas │ │ Orion │ │ │
│ │ │ Llama 3 8B │ │ │ │ RangePredict │ │ │ │ (Cons) │ │ (Mod) │ │ │
│ │ │ (GPU) │ │ │ │ TPSLClass │ │ │ └────────┘ └────────┘ │ │
│ │ └──────────────┘ │ │ └──────────────┘ │ │ │ │
│ │ │ │ │ │ ┌────────┐ │ │
│ │ ┌──────────────┐ │ │ ┌──────────────┐ │ │ │ Nova │ │ │
│ │ │Trading Tools │◄┼─────┼─│ Signal API │ │ │ │ (Aggr) │ │ │
│ │ │ - MT4 Tools │ │ │ └──────────────┘ │ │ └────────┘ │ │
│ │ │ - ML Tools │ │ │ │ │ │ │
│ │ │ - Strategy │ │ │ │ │ │ │
│ │ └──────────────┘ │ │ │ │ │ │
│ └────────┬─────────┘ └──────────────────┘ └──────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌──────────────────────────────────────────────────────────────────────────────┐ │
│ │ MCP SERVER MT4 CONNECTOR │ │
│ │ :3605 │ │
│ │ ┌────────────────────────────────────────────────────────────────────────┐ │ │
│ │ │ MCP Tools: │ │ │
│ │ │ - mt4_get_account - mt4_execute_trade - mt4_get_quote │ │ │
│ │ │ - mt4_get_positions - mt4_close_position - mt4_modify_position │ │ │
│ │ └────────────────────────────────────────────────────────────────────────┘ │ │
│ └──────────────────────────────────────┬───────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌──────────────────────────────────────────────────────────────────────────────┐ │
│ │ MT4 GATEWAY SERVICE │ │
│ │ :8005 │ │
│ │ ┌────────────────────────────────────────────────────────────────────────┐ │ │
│ │ │ FastAPI Endpoints: │ │ │
│ │ │ - GET /api/mt4/account - GET /api/mt4/positions │ │ │
│ │ │ - POST /api/mt4/trade - DELETE /api/mt4/positions/{id} │ │ │
│ │ │ - GET /api/mt4/tick/{symbol} - PUT /api/mt4/positions/{id} │ │ │
│ │ └─────────────────────────────────────┬──────────────────────────────────┘ │ │
│ │ │ │ │
│ │ ┌─────────────────────────────────────▼──────────────────────────────────┐ │ │
│ │ │ MT4 Bridge Client (aiohttp) │ │ │
│ │ │ - Comunicacion con EA Bridge │ │ │
│ │ └────────────────────────────────────────────────────────────────────────┘ │ │
│ └──────────────────────────────────────┬───────────────────────────────────────┘ │
│ │ │
│ │ HTTP/WebSocket │
│ ▼ │
│ ┌──────────────────────────────────────────────────────────────────────────────┐ │
│ │ METATRADER 4 TERMINALS │ │
│ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ │
│ │ │ EA Bridge #1 │ │ EA Bridge #2 │ │ EA Bridge #3 │ │ │
│ │ │ (IC Markets) │ │ (Pepperstone) │ │ (XM) │ │ │
│ │ │ :8081 │ │ :8082 │ │ :8083 │ │ │
│ │ └─────────────────┘ └─────────────────┘ └─────────────────┘ │ │
│ └──────────────────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────────────────────────┐ │
│ │ DATA LAYER │ │
│ │ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │ │
│ │ │ PostgreSQL │ │ Redis │ │ Binance API │ │ Market Data │ │ │
│ │ │ :5432 │ │ :6379 │ │ (external) │ │ (external) │ │ │
│ │ └───────────────┘ └───────────────┘ └───────────────┘ └───────────────┘ │ │
│ └─────────────────────────────────────────────────────────────────────────────────┘ │
│ │
└───────────────────────────────────────────────────────────────────────────────────────┘
```
---
## Flujo de Operaciones
### 1. Flujo de Chat con LLM
```
Usuario (Chat)
LLM Agent (:8003)
├── Procesa mensaje
├── Detecta intencion
¿Requiere Tool?
├─[SI]─► Ejecuta Tool
│ │
│ ├── MT4 Tool ──► MCP Server ──► MT4 Gateway ──► MT4
│ ├── ML Tool ──► ML Engine
│ └── Strategy Tool ──► Trading Agents
│ │
│ ▼
│ Procesa resultado
└─[NO]─► Genera respuesta directa
Respuesta al Usuario
```
### 2. Flujo de Ejecucion de Trade
```
LLM Agent (Tool: execute_trade)
MCP Server MT4 (:3605)
│ (Valida request, formatea)
MT4 Gateway (:8005)
│ (Prepara orden, risk check)
MT4 Bridge Client
│ (HTTP POST /trade)
EA Bridge (MT4 Terminal)
│ (Ejecuta orden)
Broker Server
│ (Confirma ejecucion)
Response ──► MT4 Gateway ──► MCP Server ──► LLM Agent ──► Usuario
```
---
## Puertos de Servicios
| Servicio | Puerto | Descripcion |
|----------|--------|-------------|
| Frontend | 5173 | React UI (Vite) |
| Backend API | 3000 | Express.js Gateway |
| ML Engine | 8001 | FastAPI - Modelos ML |
| Data Service | 8002 | FastAPI - Market Data |
| LLM Agent | 8003 | FastAPI - Copiloto AI |
| Trading Agents | 8004 | FastAPI - Atlas/Orion/Nova |
| MT4 Gateway | 8005 | FastAPI - Bridge MT4 |
| MCP Server MT4 | 3605 | MCP Tools MT4 |
| PostgreSQL | 5432 | Base de datos |
| Redis | 6379 | Cache y sesiones |
| Ollama | 11434 | LLM Server (GPU) |
| EA Bridge #1 | 8081 | MT4 Terminal 1 |
| EA Bridge #2 | 8082 | MT4 Terminal 2 |
| EA Bridge #3 | 8083 | MT4 Terminal 3 |
---
## Componentes Implementados
### Completados
| Componente | Ubicacion | Estado |
|------------|-----------|--------|
| MT4 Bridge Client | `apps/mt4-gateway/src/providers/mt4_bridge_client.py` | OK |
| LLM Tools MT4 | `apps/llm-agent/src/tools/mt4_tools.py` | OK |
| LLM Tools ML | `apps/llm-agent/src/tools/ml_tools.py` | OK |
| ML Engine | `apps/ml-engine/` | OK |
| Trading Agents | `apps/trading-agents/` | OK |
### En Desarrollo
| Componente | Ubicacion | Estado |
|------------|-----------|--------|
| MT4 Gateway API | `apps/mt4-gateway/src/api/` | En progreso |
| MCP Server MT4 | `apps/mcp-mt4-connector/` | En progreso |
| Fine-tuning Pipeline | `apps/llm-agent/fine-tuning/` | En progreso |
| Strategy Analysis Tools | `apps/llm-agent/src/tools/strategy_analysis.py` | En progreso |
---
## Seguridad
### Autenticacion
- JWT tokens para API Gateway
- API keys para servicios internos
- Auth token para EA Bridge
### Validaciones
- Risk checks antes de ejecutar trades
- Validacion de volumenes y simbolos
- Rate limiting en endpoints criticos
---
## Referencias
- `docs/01-arquitectura/INTEGRACION-METATRADER4.md`
- `docs/01-arquitectura/INTEGRACION-LLM-LOCAL.md`
- `orchestration/PROXIMA-ACCION.md`
- `core/mcp-servers/README.md`
---
*Documento generado por Tech-Leader Agent*
*Trading Platform*