Skip to content

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.

Notifications You must be signed in to change notification settings

Shtenco/multi_threaded_trading_robot_with_machine_learning_python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 

Repository files navigation

Multi-Threaded Trading Robot with Machine Learning

Python MetaTrader5 XGBoost License

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.

๐Ÿ“‹ Table of Contents

๐ŸŽฏ Overview

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.

โšก Key Features

Machine Learning Pipeline

  • 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

Data Processing

Technical Indicators (25+)

  • 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

Data Augmentation Strategy

  1. Noise Addition: Gaussian noise (ฯƒ = 0.01) to simulate microstructure
  2. Time Shifting: +1 hour offset to capture temporal patterns
  3. Scaling: Random scaling (0.9-1.1) for different volatility regimes
  4. Inversion: Price inversion to learn mirror patterns

Risk Management

  • 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

Multi-Threading Architecture

  • 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

๐Ÿ—๏ธ System Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     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       โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ› ๏ธ Installation

Prerequisites

  • Python 3.8+
  • MetaTrader 5 Terminal
  • Windows OS (MT5 requirement)
  • Active MT5 account (demo or live)

Dependencies

pip install -r requirements.txt

requirements.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

Configuration

  1. Install MetaTrader 5 and open demo/live account

  2. Set terminal path in code:

TERMINAL_PATH: str = r"C:\Program Files\RoboForex MT5 Terminal\terminal64.exe"
  1. Configure risk parameters:
TOTAL_PORTFOLIO_RISK: float = 1000.0  # Total portfolio risk (USD)
MAX_OPEN_TRADES: int = 6              # Maximum positions
  1. Select currency pairs:
symbols = ["EURUSD", "GBPUSD", "AUDUSD", "NZDUSD", "USDCAD", "USDCHF", "EURGBP"]

๐Ÿ“Š Technical Details

Data Processing Pipeline

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

XGBoost Model Parameters

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
)

Position Size Calculation

# 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_step

๐Ÿš€ Usage

Starting the System

python trading_robot.py

Monitoring

The 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

Stopping the System

The system automatically terminates when all_symbols_done = True flag is set by any thread, or via Ctrl+C.

๐Ÿ“ˆ Results

Current Performance

  • 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

Trading Example

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

๐Ÿ”ฎ Future Development

Near-Term Improvements

  1. Full MQL5 Migration

    • Custom ML libraries in MQL5
    • Eliminate Python-MT5 API latency
    • 10-50x performance improvement
  2. Graphical Interface

    • Real-time dashboard monitoring
    • Dynamic parameter management
    • Alert and notification system
  3. Enhanced Risk Management

    • Real-time correlation analysis
    • Monte Carlo Value-at-Risk
    • Automatic hedging system

Ambitious Goals

  1. GAN for Synthetic Data

    • Generate 25-year price series
    • Preserve market statistical properties
    • Solve data scarcity problem
  2. Ensemble of 100 Models

    • Specialization on market regimes
    • Meta-classifier for regimes
    • Unprecedented prediction accuracy
  3. Google Cloud Platform Integration

    • Massively parallel training
    • 1000+ core virtual machines
    • Auto-scaling resources

๐Ÿ‘จโ€๐Ÿ’ป Author

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

Project Portfolio

  • 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

Connect

๐Ÿ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

โš ๏ธ Disclaimer

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.

๐Ÿ™ Acknowledgments

  • 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!

About

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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages