This repository demonstrates a complete workflow for training and deploying neural networks directly inside MetaTrader 5. The goal is to show that the MQL5 language can handle custom machine learning models without relying on external tools.
This proof-of-concept aims to design and implement a reliable workflow for neural network training within the MQL5 ecosystem—an environment that typically lacks native machine learning support. By doing so it enables real-time trading strategies to leverage custom models without external dependencies.
- Native training and inference in MQL5
- Serialization of model weights for easy deployment
- Modular design supporting multiple network architectures
- Example Expert Advisor showing how to automate the process
The library currently implements three common network types, all inheriting from the INeuralNetworkModel interface:
- Multilayer Perceptron (MLP) – a basic feedforward network suitable for simple patterns.
- Recurrent Neural Network (RNN) – processes sequences and retains short-term memory.
- Long Short-Term Memory (LSTM) – an RNN variant that captures long-range dependencies.
Adding new architectures follows the same interface so the workflow can be extended easily.
The training pipeline consists of four stages:
-
Training Phase – Configure the network on the Strategy Tester Inputs tab with
TrainModeset totrue. Here you choose the architecture, set the training start date and other parameters. Training runs until the MAE target is reached using the Adam optimizer. -
Persistence Phase – After training completes, the weights are saved to a binary
.binfile under the platform's commonFilesdirectory. -
Model File and Training Log – The binary file itself appears unreadable if opened directly, which is normal because weights are stored in a compact format. This binary representation loads quickly inside MT5, making it efficient for the workflow.
-
Inference Phase – With
TrainModeset tofalse, the Expert Advisor loads the saved weights and begins producing live predictions.
This separation allows you to train once and deploy the same model on any chart or in live trading without modification.
C_MLP.mqh // Multi-layer perceptron implementation
C_RNN.mqh // Simple recurrent network implementation
C_LSTM.mqh // Long short-term memory implementation
INeuralNetworkModel.mqh // Interface for all models
WorkFlow_test.mq5 // Example Expert Advisor demonstrating the workflow
- Copy the
.mqhand.mq5files into your platform'sMQL5directory. - Open
WorkFlow_test.mq5in MetaEditor and adjust parameters as needed (such as training epochs or MAE target). - Run the Expert Advisor in Strategy Tester with TrainMode = true to generate a weight file.
- Set TrainMode = false and run again on the desired symbol and timeframe to execute live predictions.
Trained weights are stored under My_AI_Models inside the platform's common Files directory so they can be reused across accounts and charts.
The proof-of-concept uses only two input features—closing price and RSI—but the architecture is designed for easy expansion. Future updates may incorporate additional indicators (MACD, Bollinger Bands, volume) or raw OHLCV data.
- Support for advanced architectures such as GRU, CNN, or Transformer
- Reinforcement learning agents for dynamic risk management
- A richer library of technical indicators
This code is provided for educational and research purposes. Use it at your own risk when trading live markets.









