- MQL5 74.8%
- Python 16%
- Common Lisp 9.2%
|
|
||
|---|---|---|
| .idea | ||
| Experts | ||
| Include | ||
| Scripts/NeuroNetworksBook | ||
| NeuroNetworksBook.mqproj | ||
| README.md | ||
NeuroNetworksBook
A comprehensive library for building, training, and deploying neural networks directly in MQL5 for MetaTrader 5.
Features
- Wide Variety of Layers: Supports fully connected (Base), Convolutional, LSTM, Attention, Multi-Head Attention, GPT-like blocks, Dropout, and Batch Normalization.
- OpenCL Acceleration: Many layers and training algorithms are optimized with OpenCL for high-performance computing.
- Flexible Architecture: Build complex network architectures using a collection of layer descriptions.
- Loss Functions: Multiple loss functions supported (MSE, Cross-Entropy, etc.).
- Optimization Algorithms: Support for various optimizers like SGD, Momentum, Adam, and more.
- Python Integration: Includes Python scripts for validating gradients and testing logic against standard implementations.
Project Structure
Include/realization/: Core MQL5 library files.neuronnet.mqh: Main dispatcher classCNet.neuronbase.mqh,neuronconv.mqh,neuronlstm.mqh, etc.: Individual layer implementations.bufferdouble.mqh: Data buffer management with OpenCL support.opencl_program.cl: OpenCL kernels for GPU acceleration.
Experts/: Example Expert Advisors using the library.Scripts/NeuroNetworksBook/: Test scripts and Python validation tools.Include/about_ai/: Educational resources and additional implementations.
Training and Deployment Workflow
The library provides a streamlined process for training and deploying models.
1. Data Preparation
Use scripts in Scripts/NeuroNetworksBook/initial_data/ to generate training data (CSV). For example, create_initial_data.mq5 can export indicator values and target outcomes.
2. Training
Use the Scripts/NeuroNetworksBook/TrainAndDeploy.mq5 script:
- Set
StudyFileNameto your exported CSV. - Configure architecture and hyperparameters.
- The script will train the model and save it as a
.netfile inMQL5/Files.
3. Backtesting and Deployment
Use Experts/ea_template.mq5:
- Set
Modelto your trained.netfile. - The EA will load the model, prepare real-time data, and execute trades based on network predictions.
Code Cleaning
The library has been updated for better stability:
- Improved Error Handling:
CNet::GetResultsandCNet::Createnow include robust pointer checks. - Unified Macros:
CBufferTypeandTYPEmacros indefines.mqhensure consistency across the library and Expert Advisors. - Refactored EA Template: Fixed logic for data input and result retrieval to match the core library's expected behavior.
Quick Start
To use the library in your Expert Advisor:
-
Include the main header:
#include <NeuroNetworksBook\Include\realization\neuronnet.mqh> -
Define your network architecture:
CArrayObj *descriptions = new CArrayObj(); // Input layer CLayerDescription *desc = new CLayerDescription(); desc.type = defNeuronBase; desc.count = 10; // Number of inputs desc.activation = None; descriptions.Add(desc); // Hidden layer desc = new CLayerDescription(); desc.type = defNeuronBase; desc.count = 20; desc.activation = ACT_TANH; descriptions.Add(desc); // Output layer desc = new CLayerDescription(); desc.type = defNeuronBase; desc.count = 1; desc.activation = ACT_SIGMOID; descriptions.Add(desc); -
Initialize and use the network:
CNet net; if(net.Create(descriptions)) { // Feed forward CBufferDouble *inputs = new CBufferDouble(); // ... fill inputs ... net.FeedForward(inputs); // Get results CBufferDouble *results; net.GetResults(results); }
Development
This project is part of a book on Neural Networks in Algorithmic Trading. Feel free to explore the code, run tests in Scripts/, and adapt the EA templates in Experts/.