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.
## Table of Contents
1. [Features](#features)
2. [Repository Structure](#repository-structure)
3. [Setup & Installation](#setup--installation)
4. [Usage](#usage)
- Backtesting Notebooks
- Live Trading Scripts
5. [Key Modules](#key-modules)
6. [Extending the Project](#extending-the-project)
7. [Disclaimer](#disclaimer)
8. [License](#license)
## Features
- **MetaTrader 5** data retrieval (`data_loader.py`)
- **TA** library for feature engineering (`ta.add_all_ta_features`)
- Make sure you have **MetaTrader5** installed [IC Markets MT5](https://www.icmarkets.com/global/en/forex-trading-platform-metatrader/metatrader-5).
### 4. (Optional) Install Jupyter Notebook:
```bash
pip install jupyter
```
## Usage
### Backtesting Notebooks
1. Navigate to `ml_notebooks/` or `dl_notebooks/`, pick a relevant file (e.g., `02_backtests_multi_bar_classification.ipynb`), and run it:
```bash
jupyter notebook
```
2. Inside the notebook, you can see how we do:
- Feature engineering
- Labeling
- Walk-forward splits
- Train & tune
- VectorBT or custom backtesting
### Live Trading Scripts
1. Navigate to `ml_notebooks/` or `dl_notebooks/`, pick a relevant live trading file (e.g., 2_live_trading_multi_bar_classification.ipynb), or go to `live_trading/` folder and pick the script for your labeling approach:
- `regression_returns.py`
- `multi_bar.py`
- `double_barrier.py`
- `regime_detection.py`
2. Adjust **MetaTrader 5 credentials** (login, server, password) in the script.
3. Run from terminal:
```bash
python live_trading/multi_bar.py.py
```
4. 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
## Key Modules
- **`data/data_loader.py`**: Connects to MetaTrader 5, fetches bars with `copy_rates_from_pos`.
- **`features/feature_engineering.py`**: Uses the **TA** library and additional custom features (spreads, autocorrelation, etc.).
- **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.py` or create a new file.
- **Train a new model**: Adapt `models/model_training.py` or your notebooks to handle new classifiers/regressors.
- **Explore new backtest approaches**: Either integrate with `vectorbt` in a notebook or write a custom `.py` in `backtests/`.
## Disclaimer
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.
## License
This project is licensed under the **MIT License** - see the [LICENSE](LICENSE) file for details.
## Backtest Results - US30 - H4
```
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