Loading…

Simple Feedforward Neural Network

A single linear unit (see the companion Perceptron.mqh) can only ever draw a straight line between two classes. Plenty of real patterns are not linearly separable - the XOR truth table used in this submission's own demo is the textbook minimal example, four points where no straight line puts the two outcomes on opposite sides no matter how it is drawn. Adding one hidden layer of sigmoid units, trained with backpropagation, is enough to learn a curved decision boundary and solve exactly that class of problem. The header keeps the design as small as the job allows. Weights are two flat row-major arrays (input-to-hidden, hidden-to-output) plus their bias vectors, bundled in a SimpleNet struct - no class hierarchy, no configurable layer stack, because a second hidden layer is rarely what a trading-side model actually needs and every extra layer is another set of hyperparameters to tune blind. Training is online (one sample at a time, classic stochastic gradient descent) rather than batched, which keeps the header free of any opinion about how the caller stores its dataset - loop over your rows and call NetTrainStep once per row. Weight initialisation matters more than it looks: starting every weight at zero makes every hidden unit compute the identical gradient forever, so they never differentiate from each other no matter how long training runs. This header seeds small random values in [-0.5, 0.5] instead, which is the standard minimum fix for that failure mode. NetTrainStep mutates the network's weights in place and returns the loss from BEFORE that update, so a caller can plot a loss curve without paying for a second forward pass per step. Notes and limitations - One hidden layer only. Deeper networks need more careful initialisation and typically a different activation (ReLU) to train reliably, which is outside what this minimal header attempts. - Online (per-sample) gradient descent has no momentum, decay or regularisation - on noisy real-world data it will need a smaller learning rate and more epochs than the clean XOR demo suggests. - Sigmoid outputs sit in (0, 1). For a regression target outside that range, scale the target into (0, 1) before training and invert the scale on the prediction. - No persistence: NetInit always starts from fresh random weights. Saving and reloading trained weights is left to the caller (File writes, or a global variable store).

Script · MT5 · v1.0 · Free

About Simple Feedforward Neural Network

Simple Feedforward Neural Network is a script for MetaTrader 5. It has 4 settings, such as Hidden units, Learning rate and Epochs. MQL source code included.

Files in the download: NeuralNetwork.mq5.mqh, NeuralNetwork.txt, NeuralNetworkDemo.ex5, NeuralNetworkDemo.mq5.

Settings

SettingDefaultWhat it does
Hidden units4Hidden layer size
Learning rate0.5Gradient descent step size
Epochs4000Passes over the 4-row XOR table
Seed3Random seed for weight initialisation

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: AI & Neural Network · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria