An intelligent trading system combining machine learning and parallel computing for automated Forex trading. Hybrid Python + MetaTrader 5 architecture where Python handles analytics and model training, while MT5 executes trading operations.
Current accuracy on examination dataset: 64-65% with average profit significantly exceeding average loss.
- Overview
- Key Features
- System Architecture
- Installation
- Technical Details
- Usage
- Results
- Future Development
- Author
- License
This project implements a sophisticated multi-threaded algorithmic trading system that leverages:
- Machine Learning: XGBoost with two-level ensembling (Gradient Boosting + Bagging)
- Parallel Computing: Multiple currency pairs processed simultaneously in separate threads
- Advanced Risk Management: Portfolio-level risk allocation with dynamic position sizing
- Data Augmentation: 5x data multiplication through noise, scaling, shifting, and inversion
- Feature Engineering: 25+ technical indicators plus synthetic feature generation
The system is designed to trade multiple currency pairs simultaneously while maintaining strict portfolio risk limits and adapting to changing market conditions through intelligent clustering of market regimes.
- XGBoost Classifier with hyperparameter optimization via GridSearchCV
- Gaussian Mixture Models for market regime clustering (6 components)
- Recursive Feature Elimination (RFECV) for automatic feature selection
- BaggingClassifier wrapper for meta-ensembling
- 5-fold Cross-Validation for robust model evaluation
- Momentum: RSI, MACD, Momentum, ROC, TRIX
- Volatility: ATR, Bollinger Bands, Keltner Channels
- Oscillators: Stochastic, Williams %R, CCI, MFI
- Trend: Aroon, Efficiency Ratio, Fractal Analysis
- Volume: Volume SMA Ratio, Price-Volume Trend, Chaikin Oscillator
- Cyclical: Hour Sin/Cos, Day of Week Sin/Cos
- Noise Addition: Gaussian noise (ฯ = 0.01) to simulate microstructure
- Time Shifting: +1 hour offset to capture temporal patterns
- Scaling: Random scaling (0.9-1.1) for different volatility regimes
- Inversion: Price inversion to learn mirror patterns
- Portfolio Risk Limit: $1000 total across all positions
- Dynamic Position Sizing: ATR-based lot calculation per instrument
- Fixed Stop Loss: 300 pips with 800 pips take profit (1:2.67 risk/reward)
- Spread Filter: Maximum 35 pips spread allowed
- Position Limits: Maximum 6 concurrent positions
- Parallel Processing: 7 currency pairs in separate threads
- Thread-Safe Logging: Queue-based logging system
- Position Synchronization: Automatic position flag updates
- Graceful Shutdown: Coordinated thread termination
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MAIN THREAD โ
โ โข Portfolio Initialization โ
โ โข Position Size Calculation โ
โ โข Thread Coordination โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ
โ โ โ
โโโโโโโโโผโโโโโโโโ โโโโโโผโโโโโโ โโโโโโโผโโโโโโโ
โ EURUSD THREADโ โGBPUSD THDโ โ AUDUSD THD โ ... (7 threads)
โ โ โ โ โ โ
โ 1. Data Load โ โ โ โ โ
โ 2. Augment โ โ โ โ โ
โ 3. Label โ โ โ โ โ
โ 4. Cluster โ โ โ โ โ
โ 5. Feature โ โ โ โ โ
โ 6. Train โ โ โ โ โ
โ 7. Trade โ โ โ โ โ โ
โโโโโโโโโฌโโโโโโโโ โโโโโโฌโโโโโโ โโโโโโโฌโโโโโโโ
โ โ โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ
โ GLOBAL RESOURCES โ
โ โข POSITION_SIZES (Dict) โ
โ โข SYMBOL_TRADES (Dict) โ
โ โข log_queue (Queue) โ
โ โข all_symbols_done (Flag) โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ
โ MetaTrader 5 API โ
โ โข Quote Retrieval โ
โ โข Order Execution โ
โ โข Position Monitoring โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- Python 3.8+
- MetaTrader 5 Terminal
- Windows OS (MT5 requirement)
- Active MT5 account (demo or live)
pip install -r requirements.txtrequirements.txt:
numpy>=1.21.0
pandas>=1.3.0
MetaTrader5>=5.0.4
scikit-learn>=1.0.0
xgboost>=1.5.0
imbalanced-learn>=0.9.0
-
Install MetaTrader 5 and open demo/live account
-
Set terminal path in code:
TERMINAL_PATH: str = r"C:\Program Files\RoboForex MT5 Terminal\terminal64.exe"- Configure risk parameters:
TOTAL_PORTFOLIO_RISK: float = 1000.0 # Total portfolio risk (USD)
MAX_OPEN_TRADES: int = 6 # Maximum positions- Select currency pairs:
symbols = ["EURUSD", "GBPUSD", "AUDUSD", "NZDUSD", "USDCAD", "USDCHF", "EURGBP"]# 1. Load historical data (2021-2024)
raw_data = retrieve_data(symbol) # ~26,000 hourly bars
# 2. Create 25+ technical indicators
# RSI, MACD, ATR, Bollinger Bands, Stochastic, etc.
# 3. Data augmentation (5x increase)
augmented_data = augment_data(raw_data) # ~130,000 examples
# 4. Labeling with real trading conditions
labeled_data = label_data(data, symbol) # SL 300 pips, TP 800 pips
# 5. Feature clustering (GMM)
clustered_data = cluster_features_by_gmm(data, n_components=6)
# 6. Synthetic feature generation
generated_data = generate_new_features(data, num_features=10)
# 7. Feature selection (RFECV)
selected_data = feature_engineering(data, n_features_to_select=15)
# 8. XGBoost + Bagging training
model = train_xgboost_classifier(train_data, num_boost_rounds=1000)xgb.XGBClassifier(
objective='binary:logistic',
max_depth=5, # Prevent overfitting
learning_rate=0.2, # Learning rate
n_estimators=300, # Number of trees
subsample=0.01, # 1% data per tree (aggressive regularization)
colsample_bytree=0.1, # 10% features per tree
reg_alpha=1, # L1 regularization
reg_lambda=1 # L2 regularization
)# Per instrument:
risk_per_instrument = TOTAL_PORTFOLIO_RISK / len(symbols) # $142.86 for 7 pairs
# Risk per standard lot
risk_per_lot = 300 pips * tick_value
# Position size
volume = risk_per_instrument / risk_per_lot
# Normalize to broker requirements
normalized_volume = round(volume / lot_step) * lot_steppython trading_robot.pyThe system outputs detailed logs to console:
=== Symbol Analysis EURUSD ===
Point: 1e-05
Trade tick value: 1.0
Risk per 1 lot at SL 300 pips: 300.00 USD
Calculated position size: 0.476190 lot
Normalized size: 0.48 lot
FINAL RISK: 144.00 USD
Accuracy for symbol EURUSD: 64.23%
Position opened EURUSD: Buy, lot=0.48, spread=2.1 pips
The system automatically terminates when all_symbols_done = True flag is set by any thread, or via Ctrl+C.
- Examination dataset accuracy: 64-65%
- Average profitable trade: Significantly > average losing trade
- Several days of trading results: Positive (see screenshots in article)
- Profitability simulations: Encouraging at given win rate
Symbol: EURUSD
Entry: 1.0850
Stop Loss: 1.0820 (300 pips)
Take Profit: 1.0930 (800 pips)
Volume: 0.48 lot
Risk: $144
Potential Profit: $384
Risk/Reward: 1:2.67
-
Full MQL5 Migration
- Custom ML libraries in MQL5
- Eliminate Python-MT5 API latency
- 10-50x performance improvement
-
Graphical Interface
- Real-time dashboard monitoring
- Dynamic parameter management
- Alert and notification system
-
Enhanced Risk Management
- Real-time correlation analysis
- Monte Carlo Value-at-Risk
- Automatic hedging system
-
GAN for Synthetic Data
- Generate 25-year price series
- Preserve market statistical properties
- Solve data scarcity problem
-
Ensemble of 100 Models
- Specialization on market regimes
- Meta-classifier for regimes
- Unprecedented prediction accuracy
-
Google Cloud Platform Integration
- Massively parallel training
- 1000+ core virtual machines
- Auto-scaling resources
Yevgeniy Koshtenko
- ๐ Location: Kostanay, Kazakhstan
- ๐ผ Experience: 10 years in algorithmic trading (since 2016)
- ๐ Research: 100+ published articles on ML and trading
- ๐ป Codebase: 680,000 lines MQL5 + 1,200,000 lines Python
- ๐ฌ Expertise: Python, MQL5, ML, Quantum Computing, Data Engineering
- High-frequency arbitrage system (HFT)
- Computer vision for chart analysis (CNN)
- Quantum computing on IBM Quantum
- Central bank data mining + ML
- Biological neural networks for forecasting
- ๐ง Email: [koshtenco@gmail.com]
- ๐ GitHub: [Shtenco]
- ๐ MQL5: [my-mql5-profile]
This project is licensed under the MIT License. See the LICENSE file for details.
This trading robot is for educational purposes only. Trading in financial markets involves high risk of capital loss. The author is not responsible for any financial losses resulting from the use of this software. Always conduct thorough testing on demo accounts before using real funds.
- MetaQuotes for MetaTrader 5 platform
- MQL5 community for support and inspiration
- Developers of scikit-learn, XGBoost and other libraries
โญ If this project was useful to you, please star it on GitHub!