forked from rosh/NeuroBook
221 lines
9.1 KiB
Markdown
221 lines
9.1 KiB
Markdown
# Neural Networks for Algorithmic Trading with MQL5
|
|
|
|
|
|
Examples and source code from the book [Neural Networks for Algorithmic Trading with MQL5](https://www.mql5.com/en/neurobook) - author [Dmitriy Gizlyk](https://www.mql5.com/en/users/dng/publications)
|
|
|
|
This repository serves as a practical addition to the book. It contains ready-to-use **scripts** for data preparation and model training, **a neural network library for MQL5**, examples of **CNN / LSTM / Attention / GPT-like architectures**, and **a trading advisor template** for testing models in real market conditions via MetaTrader 5.
|
|
|
|

|
|
|
|
## Links
|
|
|
|
- Book (online): https://www.mql5.com/en/neurobook
|
|
- CodeBase publication (archive with examples): https://www.mql5.com/en/code/48097
|
|
- Repository (Algo Forge): https://forge.mql5.io/rosh/NeuroBook
|
|
|
|
|
|
|
|
## Contents
|
|
|
|
- **MQL5 Neural Network Library** (Core): `Include/realization/`
|
|
Implementation of networks, layers, and auxiliary components (including OpenCL support and positional encoding).
|
|
- **Activation Functions**: `Include/about_ai/activation/` (`activations.mqh`, `activations.py`)
|
|
- **Dataset Preparation for Training and Testing**: `Scripts/initial_data/`
|
|
Script `create_initial_data.mq5` generates file `study_data.csv` and `test_data.csv` in `MQL5/Files`.
|
|
- **Models and Tests** (MQL5 + Python):
|
|
- Perceptron and gradient check: `Scripts/perceptron/`
|
|
- Convolutional Neural Networks (CNN): `Scripts/convolution/`
|
|
- Recurrent Networks (LSTM): `Scripts/rnn/`
|
|
- Self-Attention / Multi-Head (examples): `Scripts/attention/`
|
|
- Batch Normalization: `Scripts/batch_norm/`
|
|
- Dropout: `Scripts/dropout/`
|
|
- GPT-like architecture (example): `Scripts/gpt/`
|
|
- **OpenCL Acceleration / Performance Testing**:
|
|
`Scripts/opencl_test.mq5`, `Include/algotrading/mult_vect_ocl.*`
|
|
- **Trading Expert Advisor Template** for applying `.net` models: `Experts/ea_template.mq5`
|
|
- **Python Template for Experiments**: `Scripts/template.py` (TensorFlow/Keras + MetaTrader5)
|
|
|
|
|
|
|
|
## Repository Structure
|
|
|
|
```text
|
|
NeuroBook/
|
|
├─ Experts/
|
|
│ └─ ea_template.mq5
|
|
├─ Include/
|
|
│ ├─ about_ai/activation/ # activation functions (MQL5 + Python)
|
|
│ ├─ algotrading/ # OpenCL utilities (multiplication example)
|
|
│ └─ realization/ # neural network and layer implementations in MQL5
|
|
├─ Scripts/
|
|
│ ├─ initial_data/ # data preparation (CSV in MQL5/Files)
|
|
│ ├─ perceptron/ # Perceptron + gradient check
|
|
│ ├─ convolution/ # CNN examples + gradient check
|
|
│ ├─ rnn/ # LSTM examples + gradient check
|
|
│ ├─ attention/ # attention / multi-head attention
|
|
│ ├─ batch_norm/ # batch norm
|
|
│ ├─ dropout/ # dropout
|
|
│ ├─ gpt/ # GPT-like example
|
|
│ ├─ opencl_test.mq5 # CPU vs OpenCL speed comparison
|
|
│ └─ template.py # Python training/testing template
|
|
├─ KIYA/ # standalone mini-project/scaffold (mqproj)
|
|
└─ NeuroBook.mqproj # MetaEditor project
|
|
````
|
|
|
|
|
|
## Table of Contents by Book Chapters
|
|
|
|
Below is a “map” of the chapters and where to find the corresponding examples in this repository.
|
|
|
|
### Chapter 1 — [Neural Network Building Blocks](https://www.mql5.com/en/neurobook/index/about_ai)
|
|
|
|
* Activation functions, core AI concepts, and laying the foundation.<br>
|
|
📁 `Include/about_ai/activation/`
|
|
|
|
### Chapter 2 — [MetaTrader 5 Capabilities for Algorithmic Trading](https://www.mql5.com/en/neurobook/index/algotrading)
|
|
|
|
* Practical use of MT5 tools, computation acceleration, and more.<br>
|
|
📁 `Scripts/opencl_test.mq5`, `Include/algotrading/`, `Include/realization/opencl.*`
|
|
|
|
### Chapter 3 — [Your First MQL5 Model: Data → Model → Test](https://www.mql5.com/en/neurobook/index/realization)
|
|
|
|
* Dataset preparation, training, and saving the model.<br>
|
|
📁 `Scripts/initial_data/`, `Scripts/perceptron/`
|
|
|
|
### Chapter 4 — [Core Layer Types: Convolutions and Recurrent Networks](https://www.mql5.com/en/neurobook/index/main_layer_types)
|
|
|
|
* CNN and LSTM: implementation and testing.<br>
|
|
📁 `Scripts/convolution/`, `Scripts/rnn/`
|
|
|
|
### Chapter 5 — [Attention Mechanisms](https://www.mql5.com/en/neurobook/index/transformer)
|
|
|
|
* Self‑Attention and Multi‑Head Self‑Attention, advanced data analysis techniques.<br>
|
|
📁 `Scripts/attention/`, `Include/realization/neuronattention.mqh`, `neuronmhattention.mqh`<br>
|
|
Additionally: a GPT‑like architecture example: 📁 `Scripts/gpt/`, `Include/realization/neurongpt.mqh`
|
|
|
|
### Chapter 6 — [Improving Convergence: BatchNorm and Dropout](https://www.mql5.com/en/neurobook/index/improvement_realization)
|
|
|
|
* Practical stabilization and regularization techniques.<br>
|
|
📁 `Scripts/batch_norm/`, `Scripts/dropout/`
|
|
|
|
### Chapter 7 — [Testing Trading Strategies with Trained Models](https://www.mql5.com/en/neurobook/index/trade_check)
|
|
|
|
* Using models inside an Expert Advisor and validating them under live-like conditions.<br>
|
|
📁 `Experts/ea_template.mq5`
|
|
Recommended to test in the [Strategy Tester](https://www.metatrader5.com/en/automated-trading/strategy-tester)
|
|
|
|
|
|
|
|
## Quick Start
|
|
|
|
### 1) Install into MetaTrader 5
|
|
|
|
1. Open MetaTrader 5 → **File → Open Data Folder**
|
|
2. Go to the `MQL5` directory
|
|
3. Copy the repository contents (**Experts / Include / Scripts / …**) into your `MQL5/` folder
|
|
4. Open MetaEditor (F4) and compile the required files (F7)
|
|
|
|
> Tip: it’s more convenient to work via `NeuroBook.mqproj` — open the project in MetaEditor and compile directly from the project tree.
|
|
|
|
### 2) End-to-end example: data → training → Expert Advisor
|
|
|
|
**Step A. Generate datasets (CSV)**
|
|
Run the script:
|
|
|
|
* `Scripts/initial_data/create_initial_data.mq5`
|
|
|
|
By default, it will create:
|
|
|
|
* `MQL5/Files/study_data.csv` — training dataset
|
|
* `MQL5/Files/test_data.csv` — test dataset
|
|
|
|
⚠️ Important:
|
|
|
|
* before running, make sure the terminal has historical quotes downloaded for the selected period;
|
|
* the script uses standard indicators (RSI/MACD) and a ZigZag example.
|
|
|
|
**Step B. Train the model in MQL5 and save `.net`**
|
|
For example, run:
|
|
|
|
* `Scripts/perceptron/perceptron_test.mq5`
|
|
|
|
After completion it saves (example):
|
|
|
|
* `MQL5/Files/Study.net` — the model file
|
|
and an error/loss log (example):
|
|
* `MQL5/Files/loss_study.csv`
|
|
|
|
**Step C. Use the model in an Expert Advisor**
|
|
Compile and run:
|
|
|
|
* `Experts/ea_template.mq5`
|
|
|
|
Key input parameters in `ea_template`:
|
|
|
|
* `Model` — model file name (e.g., `Study.net`)
|
|
* `Common` — **where to look for the model file**
|
|
|
|
* `false`: `MQL5/Files` of the current terminal (training scripts typically save models here)
|
|
* `true`: the terminal’s common folder ([FILE_COMMON](https://www.mql5.com/en/docs/constants/io_constants/fileflags)) if you store models there
|
|
* `UseOpenCL` — enable OpenCL (if supported)
|
|
* `TimeFrame`, `BarsToPattern`, and trading parameters — adjust to your testing idea
|
|
|
|
Then test in the Strategy Tester: [https://www.metatrader5.com/en/automated-trading/strategy-tester](https://www.metatrader5.com/en/automated-trading/strategy-tester)
|
|
|
|
|
|
|
|
## Python Part (Optional)
|
|
|
|
This repository includes Python scripts for experimentation and cross-checking approaches, including a template:
|
|
|
|
* `Scripts/template.py`
|
|
|
|
It:
|
|
|
|
* connects to the installed terminal via the `MetaTrader5` module,
|
|
* reads `study_data.csv` and `test_data.csv` from `MQL5/Files`,
|
|
* trains a model in TensorFlow/Keras,
|
|
* saves `model.h5` back into `MQL5/Files`.
|
|
|
|
Typical dependencies (example):
|
|
|
|
```bash
|
|
pip install MetaTrader5 pandas numpy matplotlib tensorflow
|
|
```
|
|
|
|
> More details on terminal and [environment settings](https://www.metatrader5.com/en/terminal/help/startworking/settings#ea)
|
|
|
|
|
|
## OpenCL
|
|
|
|
Many examples can be accelerated with OpenCL (the `UseOpenCL` parameter).
|
|
To benchmark performance, use `Scripts/opencl_test.mq5` (CPU vs OpenCL comparison).
|
|
|
|
If OpenCL fails to initialize, check your GPU/CPU drivers and OpenCL support on your system.
|
|
|
|
|
|
|
|
|
|
## Notes
|
|
|
|
- All examples are intended for learning and experimentation.
|
|
Always test thoroughly in the
|
|
[Strategy Tester](https://www.metatrader5.com/en/automated-trading/strategy-tester)
|
|
or on a demo account before using real funds.
|
|
|
|
- The `Scripts/Python` folder contains examples for integrating MetaTrader 5 with Python.
|
|
Make sure to enable the appropriate [terminal settings](https://www.metatrader5.com/en/terminal/help/startworking/settings#ea):
|
|
|
|
|
|
|
|
## License and Usage
|
|
|
|
All materials are part of the book "MQL5 Programming for Traders" and the MQL5/MetaTrader 5 ecosystem.
|
|
Please respect copyright and the MQL5.com/MetaQuotes terms when distributing or reusing code.
|
|
|
|
|
|
|
|
## Contributing
|
|
|
|
* Bugs/improvements: use [**Issues**](https://forge.mql5.io/rosh/NeuroBook/issues).
|
|
* Pull Requests: welcome. Submit changes by part for easier review and alignment with the book.
|