Multi-strategy MT5 research lab for ML/DL/time-series trading: data → modeling → backtests → tuning → prototype execution.
A comprehensive machine learning and deep learning trading framework that covers the entire workflow:
- Data loading from MetaTrader 5
- Feature engineering (technical indicators, custom features, labeling)
- Model training (RandomForest, XGBoost, LightGBM, deep learning models, etc.)
- Hyperparameter tuning (RandomizedSearchCV, GridSearchCV or Optuna)
- Time-based / walk-forward cross-validation
- Backtesting (VectorBT or simple custom code)
- Live trading integration with MetaTrader 5
- Regression on next-bar returns
- Multi-bar classification
- Double-barrier labeling (López de Prado style)
- Regime detection (simple up/down/sideways approach)
- Volatility-based labeling
- Momentum Strategy
- Pairs Trading (Cointegration)
- Pairs Trading (Clustering)
This project provides a flexible template for you to create and add your own custom labeling functions or feature engineering steps, allowing you to experiment with new ideas and strategies.
- Features
- Repository Structure
- Setup & Installation
- Usage
- Backtesting Notebooks
- Live Trading Scripts
- Key Modules
- Extending the Project
- Disclaimer
- License
- MetaTrader 5 data retrieval (
data_loader.py) - TA library for feature engineering (
ta.add_all_ta_features) - Multiple labeling methods: next-bar, multi-bar, double-barrier, regime detection, volatility-based etc.
- Time-based or walk-forward cross-validation to avoid data leakage
- RandomizedSearchCV or GridSearchCV for hyperparameter tuning
- VectorBT or custom backtesting scripts for performance evaluation
- Live trading scripts with real-time MetaTrader 5 order sending
# AlphaFlow ML & DL Trading Bot Repository Structure
AlphaFlow-MT5-ML-DL-Trading-Lab/
├── data/
│ ├── data_loader.py # MetaTrader 5 data retrieval
│
├── features/
│ ├── feature_engineering.py # Technical indicators, stationarity checks, custom features
│ ├── labeling_schemes.py # Labeling methods: next-bar, multi-bar, double-barrier, regime detection, volatility-based
│
├── models/
│ ├── model_training.py # Model selection, hyperparameter tuning (Optuna, GridSearchCV)
│ ├── saved_models/ # Folder for saved model pipelines (.pkl, .joblib)
│
├── backtests/
│ ├── simple_backtest.py # Simple event-driven backtest logic
│ ├── vectorbt_backtest.py # VectorBT-based backtesting template
│
├── live_trading/
│ ├── ... (live trading scripts)
│
├── notebooks/
│ ├── exploratory/
│ │ ├── eda_visualization.ipynb
│ │ ├── integrated_pipeline_pair_trading.ipynb
│ │ └── kalman_filters_solution.ipynb
│ ├── strategies/
│ │ ├── ml/
│ │ │ ├── backtesting/
│ │ │ │ ├── regression_returns.ipynb
│ │ │ │ ├── double_barrier_labeling.ipynb
│ │ │ │ ├── multi_bar_classification.ipynb
│ │ │ │ ├── multi_bar_classification_multisymbol.ipynb
│ │ │ │ ├── multi_bar_classification_multisymbol_core_features.ipynb
│ │ │ │ ├── multi_bar_classification_multisymbol_core_features_stocks.ipynb
│ │ │ │ ├── regime_detection.ipynb
│ │ │ │ ├── momentum_strategy.ipynb
│ │ │ │ ├── pairs_trading_cointegration.ipynb
│ │ │ │ └── pairs_trading_clustering.ipynb
│ │ │ └── live_trading/
│ │ │ ├── regression_returns.ipynb
│ │ │ ├── double_barrier_labeling.ipynb
│ │ │ ├── multi_bar_classification.ipynb
│ │ │ ├── multi_bar_classification_multisymbol_core_features.ipynb
│ │ │ ├── regime_detection.ipynb
│ │ │ ├── pairs_trading_cointegration.ipynb
│ │ │ └── pairs_trading_clustering.ipynb
│ │ └── dl/
│ │ ├── backtesting/
│ │ │ ├── regression_returns.ipynb
│ │ │ └── multi_bar_classification_multisymbol.ipynb
│ │ └── live_trading/
│ │ └── regression_returns.ipynb
│ └── time_series/
│ └── arima_sarima_var_lstm.ipynb
│
├── pyproject.toml
├── uv.lock
├── README.mdgit clone https://github.com/maghdam/AlphaFlow-MT5-ML-DL-Trading-Lab.git
cd AlphaFlow-MT5-ML-DL-Trading-Labconda create -n ml_trading python>=3.9
conda activate ml_tradingpip install uv
uv pip sync pyproject.toml- Make sure you have MetaTrader5 installed IC Markets MT5.
pip install jupyter- Navigate to
ml_notebooks/ordl_notebooks/, pick a relevant file (e.g.,02_backtests_multi_bar_classification.ipynb), and run it:jupyter notebook
- Inside the notebook, you can see how we do:
- Feature engineering
- Labeling
- Walk-forward splits
- Train & tune
- VectorBT or custom backtesting
- Navigate to
ml_notebooks/ordl_notebooks/, pick a relevant live trading file (e.g., 2_live_trading_multi_bar_classification.ipynb), or go tolive_trading/folder and pick the script for your labeling approach:regression_returns.pymulti_bar.pydouble_barrier.pyregime_detection.py
- Adjust MetaTrader 5 credentials (login, server, password) in the script.
- Run from terminal:
python live_trading/multi_bar.py.py
- The script will:
- Load the pipeline (e.g.,
best_rf_mb_pipeline.pkl) - Fetch new bars from MetaTrader 5
- Predict SHIFTED classes
[0, 1, 2]=> SHIFT back to[-1, 0, +1] - Place orders if signals = ±1
- Load the pipeline (e.g.,
data/data_loader.py: Connects to MetaTrader 5, fetches bars withcopy_rates_from_pos.features/feature_engineering.py: Uses the TA library and additional custom features (spreads, autocorrelation, etc.).features/labeling_schemes.py:calculate_future_return(...)create_labels_multi_bar(...)create_labels_double_barrier(...)create_labels_regime_detection(...)create_labels_volatility(...)
models/model_training.py:select_features_rf_reg(...)- Time-based splits, random/grid search for hyperparams.
backtests/:simple_backtest.pyorvectorbt_backtest.py
live_trading/:- Each script loads a pipeline (
.pkl), connects to MT5, and places trades based on predictions.
- Each script loads a pipeline (
- Add your own label: Create a new function in
features/labeling_schemes.py(e.g.create_labels_custom(...)that returns a new column with[-1, 0, +1](or your custom classes)). - Add your own features: Implement them in
features/feature_engineering.pyor create a new file. - Train a new model: Adapt
models/model_training.pyor your notebooks to handle new classifiers/regressors. - Explore new backtest approaches: Either integrate with
vectorbtin a notebook or write a custom.pyinbacktests/.
We share this code for learning and development/research purposes only. Nothing herein constitutes financial advice or a recommendation to trade real money. Trading involves substantial risk. Always do your own due diligence, consult professionals, and only risk capital you can afford to lose.
This project is licensed under the MIT License - see the LICENSE file for details.
Loaded best classification model from 'best_rf_mb_pipeline.pkl'
Out-of-Sample Accuracy: 0.5439
Running Full Backtest on the Last 5000 Bars...
Full Backtest Results:
Accuracy=0.54, Return=0.30%, Sharpe=1.21
Start 2021-12-01 16:00:00
End 2025-02-28 00:00:00
Period 832 days 12:00:00
Start Value 10000.0
End Value 12971.402323
Total Return [%] 29.714023
Benchmark Return [%] 24.515943
Max Gross Exposure [%] 100.0
Total Fees Paid 236.291366
Max Drawdown [%] 13.645737
Max Drawdown Duration 295 days 04:00:00
Total Trades 56
Total Closed Trades 56
Total Open Trades 0
Open Trade PnL 0.0
Win Rate [%] 60.714286
Best Trade [%] 13.156291
Worst Trade [%] -3.463323
Avg Winning Trade [%] 1.517335
Avg Losing Trade [%] -1.094661
Avg Winning Trade Duration 7 days 03:03:31.764705882
Avg Losing Trade Duration 1 days 00:00:00
Profit Factor 2.150809
Expectancy 53.060756
Sharpe Ratio 1.20547
Calmar Ratio 0.885441
Omega Ratio 1.156419
Sortino Ratio 1.793852
dtype: object
