No description
  • MQL5 74.8%
  • Python 16%
  • Common Lisp 9.2%
Find a file
2026-05-08 19:45:42 +02:00
.idea Standardize includes, fix activation constants, and add missing ENUM_LOSS_FUNCTION 2026-05-08 19:45:42 +02:00
Experts Standardize includes, fix activation constants, and add missing ENUM_LOSS_FUNCTION 2026-05-08 19:45:42 +02:00
Include Standardize includes, fix activation constants, and add missing ENUM_LOSS_FUNCTION 2026-05-08 19:45:42 +02:00
Scripts/NeuroNetworksBook Standardize includes, fix activation constants, and add missing ENUM_LOSS_FUNCTION 2026-05-08 19:45:42 +02:00
NeuroNetworksBook.mqproj convert 2025-05-30 16:12:34 +02:00
README.md Standardize includes, fix activation constants, and add missing ENUM_LOSS_FUNCTION 2026-05-08 19:45:42 +02:00

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 class CNet.
    • 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 StudyFileName to your exported CSV.
  • Configure architecture and hyperparameters.
  • The script will train the model and save it as a .net file in MQL5/Files.

3. Backtesting and Deployment

Use Experts/ea_template.mq5:

  • Set Model to your trained .net file.
  • 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::GetResults and CNet::Create now include robust pointer checks.
  • Unified Macros: CBufferType and TYPE macros in defines.mqh ensure 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:

  1. Include the main header:

    #include <NeuroNetworksBook\Include\realization\neuronnet.mqh>
    
  2. 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);
    
  3. 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/.