* feat: implement production-ready model stubs and enhanced trading environment - Refined PPOAgent, LSTMModel, and DreamerAgent with NaN/Inf robustness checks. - Enhanced TradingEnv with state management (balance, equity, position) and transaction costs. - Added ppo_kwargs for hyperparameter flexibility in PPOAgent. - Included new robustness tests in tests/test_model_robustness.py. Co-authored-by: triqbit <242833443+triqbit@users.noreply.github.com> * feat: refine model stubs and trading environment for production robustness - Refined PPOAgent, LSTMModel, and DreamerAgent with NaN/Inf robustness checks. - Enhanced TradingEnv with state management (balance, equity, position) and transaction costs. - Updated ExecutionFilter to support model stability, performance, and confidence layers. - Fixed Pydantic validation and health check test regressions. - Added walk_forward_efficiency to RobustnessMetrics. - Updated DEVELOPMENT_PLAN.md with completed tasks. Co-authored-by: triqbit <242833443+triqbit@users.noreply.github.com> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: triqbit <242833443+triqbit@users.noreply.github.com>
11 KiB
MT5 AI XAUUSD Trading Bot - Comprehensive Development Plan
Executive Summary
This document outlines the complete strategy for building a professional-grade AI/ML automated trading bot for MetaTrader 5 (MT5) focused on XAUUSD (Gold) trading. The project integrates cutting-edge machine learning techniques, risk management systems, and trading infrastructure from 25+ analyzed repositories into a unified, production-ready system.
Strategic Objective
Create a fully autonomous trading system capable of:
- 80-120% annual returns (backtested performance)
- Sharpe Ratio: 3.5-4.5+ (excellent risk-adjusted returns)
- Max Drawdown: <8% (conservative downside protection)
- Multi-algorithm approach: PPO + Dreamer V3 deep reinforcement learning
- 140+ market features (multi-timeframe, macro data, economic indicators)
- Live MT5 integration with comprehensive risk management
Phase 1: Repository Analysis & Component Extraction (Weeks 1-2)
Repositories to Fork & Merge
Top Priority (Must-Have Components):
- zero-was-here/tradingbot - DRL Trading with PPO/Dreamer
- AminHP/gym-mtsim - MT5 Gymnasium environment
- ilahuerta-IA/mt5_live_trading_bot - MT5 GUI & live trading
- nguyenviettuan96/mt5_AI_trading_bot - LSTM + RL fusion
- rezakarbasi/RL-agent-trader - Advanced RL patterns
- CodeDestroyer19/Neural-Network-MT5-Trading-Bot - Neural network trading
- Stefodan21/Forex-trading-bot - PPO + GPU acceleration
Secondary (Feature Extraction): 8-25. Additional repos for specialized components (ensemble methods, data pipelines, risk management)
Component Extraction Strategy
From each repository, extract:
- Core ML algorithm implementation (PPO, Dreamer, LSTM, etc.)
- MT5 platform integration code
- Data preprocessing pipelines
- Feature engineering modules
- Risk management systems
- Backtesting frameworks
- Live trading interfaces
Phase 2: Unified Architecture Design (Weeks 2-3)
Project Structure
mt5-ai-xauusd-trader/
├── config/
│ ├── credentials.json # API keys & broker settings
│ ├── trading_config.yaml # Strategy parameters
│ └── risk_management.yaml # Position sizing & limits
├── data/
│ ├── raw/ # XAUUSD OHLCV data (M5-D1)
│ ├── processed/ # Feature-engineered data
│ ├── macro/ # VIX, Oil, Bitcoin, DXY
│ └── economic_calendar/ # Economic events
├── src/
│ ├── environment/ # Gymnasium trading environment
│ │ ├── mt5_env.py # MT5 environment
│ │ ├── feature_engineer.py # 140+ feature generation
│ │ └── data_processor.py # Data pipeline
│ ├── models/
│ │ ├── ppo_trader.py # PPO implementation
│ │ ├── dreamer_trader.py # Dreamer V3 implementation
│ │ ├── lstm_network.py # LSTM + attention layers
│ │ └── ensemble.py # Multi-model ensemble
│ ├── trading/
│ │ ├── mt5_connector.py # MT5 API wrapper
│ │ ├── order_manager.py # Order execution logic
│ │ ├── risk_manager.py # Position sizing & risk limits
│ │ └── portfolio_manager.py # Multi-position tracking
│ ├── strategies/
│ │ ├── aggressive.py # High-risk strategy
│ │ ├── conservative.py # Low-drawdown strategy
│ │ └── swing_trade.py # Multi-day positions
│ └── utils/
│ ├── data_fetcher.py # Yahoo Finance, MT5 data
│ ├── backtester.py # Historical performance
│ └── logger.py # Trading logs & analytics
├── train/
│ ├── train_ppo.py # PPO training pipeline
│ ├── train_dreamer.py # Dreamer training pipeline
│ ├── train_ensemble.py # Ensemble training
│ └── trained_models/ # Saved .zip models
├── backtest/
│ ├── backtest_engine.py # Historical simulation
│ ├── crisis_validation.py # Crash scenario testing
│ └── results/ # Backtest reports
├── live/
│ ├── live_trading_mt5.py # Real-money execution
│ ├── paper_trading_mt5.py # Demo account testing
│ └── dashboard.py # Web monitoring UI
├── tests/
│ ├── test_environment.py
│ ├── test_models.py
│ └── test_mt5_integration.py
├── requirements.txt
├── setup.py
└── README.md
Phase 3: Core Development (Weeks 4-12)
3.1 Data Pipeline & Feature Engineering (Week 4)
Objectives:
- Collect 5+ years XAUUSD data (M5, M15, H1, H4, D1)
- Integrate macro data: VIX, Oil, Bitcoin, EURUSD, DXY, Silver
- Build economic calendar (1500+ events 2015-2025)
- Generate 140+ technical & macro features
Implementation:
Features to generate:
- Multi-timeframe indicators (RSI, MACD, Bollinger, ATR)
- Volume analysis (OBV, CMF, Money Flow)
- Trend detection (ADX, Parabolic SAR)
- Macro indicators (VIX, Oil correlation, DXY strength)
- Economic calendar impact scoring
- Market microstructure (order flow, spread analysis)
- Sentiment indicators (if integrated)
- Ensemble feature normalization
3.2 Environment Setup (Week 4)
Create Gymnasium MT5 Environment:
- Action space: Buy (1), Hold (0), Sell (-1), + position sizing
- Observation space: 140-dimensional state vector
- Reward function: Risk-adjusted returns (Sharpe ratio optimization)
- Episode termination: Daily/weekly cycles or stop-loss hits
3.3 RL Algorithm Implementation (Weeks 5-7)
Algorithm 1: PPO (Proximal Policy Optimization)
- Network: Actor-Critic with 3 hidden layers (256 neurons each)
- Batch size: 64-128
- Learning rate: 3e-4
- Gamma (discount): 0.99
- Entropy coef: 0.01 (exploration bonus)
Algorithm 2: Dreamer V3 (World Model)
- Recurrent State-Space Model (RSSM) for market dynamics
- Latent space dimension: 512
- Planning horizon: 15 steps
- Imagination rollouts for policy training
Algorithm 3: LSTM + Attention (Hybrid)
- Bidir LSTM: 2 layers, 128 hidden units
- Multi-head attention: 8 heads
- Temporal convolution for speed
3.4 MT5 Integration Layer (Weeks 7-8)
Direct MT5 Connection:
- MetaTrader5 Python SDK integration
- Real-time price feeds (bid/ask)
- Order placement & management
- Account info retrieval
- Historical data export
Alternative: MetaAPI Cloud:
- No VPS required
- Multi-broker support
- Automatic reconnection
- Trade copying capability
3.5 Risk Management System (Week 8)
Position Sizing:
- Kelly Criterion with 25% fractional scaling
- Dynamic sizing based on win rate & profit factor
- Max risk per trade: 1-2% of account
- Max daily loss: 5% stop
- Max correlation concentration: 3 open positions
Order Management:
- Automated stop-loss placement (2-3x ATR)
- Trailing stop activation at +2% profit
- Take-profit scaling (25% at TP1, 50% at TP2, 25% trail)
- Spread & slippage monitoring
Phase 4: Training Pipeline (Weeks 9-11)
4.1 Local Training Setup
- Support: CPU, MPS (Mac M1/M2/M3), CUDA (NVIDIA)
- Dataset: 2015-2023 XAUUSD (1M+ bars)
- Training steps: 1,000,000+ for PPO, 500,000+ for Dreamer
- Checkpoints: Every 50k steps
- Time: 2-8 days depending on hardware
4.2 Google Colab Fast Training
- Free T4/A100 GPUs
- Training time: 5-7 hours
- Notebook: colab_train_ppo.ipynb & colab_train_dreamer.ipynb
4.3 Model Evaluation
- Backtest on 2023 data (out-of-sample)
- Crisis validation: COVID-2020, Inflation-2022, Banking-2023
- Performance thresholds: Sharpe >2.5, Drawdown <15%, Returns >50%
Phase 5: Backtesting & Validation (Week 12)
Backtesting Framework
- Engine: Custom with realistic spreads/slippage
- Metrics: Returns, Sharpe, Max Drawdown, Win Rate, Profit Factor
- Periods: In-sample (2015-2022) & Out-of-sample (2023)
- Expected: 60-90% annual returns, 2.8-3.5 Sharpe
Paper Trading
- Demo account in MetaTrader 5
- Duration: Minimum 2-4 weeks
- Monitoring: Daily checks for drift/issues
- Validation: Compare live prices vs model predictions
Phase 6: Live Trading Deployment (Week 13+)
Pre-Live Checklist
- ✅ Backtest Sharpe >3.0
- ✅ Paper trading 2+ weeks
- ✅ Risk limits configured
- ✅ Kill switches active
- ✅ Monitoring dashboard ready
- ✅ Financial advisor consulted
Live Trading Parameters
- Account size: Start minimum ($5,000-$10,000)
- Max risk per trade: 1-2%
- Max daily loss: 5%
- Max positions: 3-5 concurrent
- Check interval: 60 seconds
- Monitoring: Daily review first month
Expected Live Performance (vs Backtest Decay)
- Backtest: 60-90% annual
- Paper: 40-70% (slippage/spread costs)
- Live: 30-60% (additional friction)
Technology Stack
Core ML Libraries
- Stable-Baselines3: PPO/A2C/TD3 implementations
- PyTorch: Deep learning & custom architectures
- Gymnasium: RL environment standard
- Dreamer: World model implementation (via JAX)
Data & Analysis
- Pandas: Data manipulation
- NumPy: Numerical computing
- TA-Lib: Technical indicators
- YFinance: Macro data fetching
MT5 Integration
- MetaTrader5 SDK: Python wrapper for MT5
- MetaAPI: Cloud trading alternative
Infrastructure
- Docker: Containerization for deployment
- GitHub Actions: CI/CD pipelines
- PostgreSQL: Trade logging database
- Flask/Dash: Web dashboard
Monitoring & Logging
- Logging: Structured trade logs
- Prometheus: Metrics collection
- Grafana: Performance dashboards
- AlertManager: Error/loss notifications
Success Criteria
| Metric | Target | Acceptable | Failure |
|---|---|---|---|
| Sharpe Ratio | 3.5-4.5+ | 2.5-3.5 | <2.0 |
| Annual Return | 80-120%+ | 50-80% | <30% |
| Max Drawdown | <8% | 8-15% | >15% |
| Win Rate | 60-65% | 55-60% | <50% |
| Profit Factor | 2.5-3.0+ | 2.0-2.5 | <1.5 |
Risk Mitigation
- Model Risk: Ensemble of PPO + Dreamer + LSTM
- Market Risk: Strict position sizing & stop-losses
- Tech Risk: Multiple MT5 connections (direct + MetaAPI fallback)
- Data Risk: Real-time validation & anomaly detection
- Operational Risk: Kill switches, daily monitoring, alerts
Timeline Summary
- Week 1-2: Repository analysis & component extraction
- Week 2-3: Architecture design
- Week 4: Data pipeline & environment setup
- Week 5-7: RL algorithm implementation
- Week 7-8: MT5 integration & risk management
- Week 9-11: Training pipeline
- Week 12: Backtesting & validation
- Week 13+: Live trading deployment
Total: 13+ weeks for MVP → Production
Next Steps
- ✅ Scaffold core enterprise-grade package structure.
- ✅ Implement dual-path MT5 connector (Native + MetaAPI).
- ✅ Implement institutional RiskEngine with ATR sizing and circuit breakers.
- ✅ Implement weighted signal ensemble logic.
- Begin Phase 3 data collection and feature engineering.
- ✅ Implement production-ready model stubs (PPO, LSTM, DreamerV3).
- ✅ Enhance Gymnasium-compatible XAUUSD trading environment.