Classic Perceptron Linear Classifier
The perceptron is the simplest possible trainable classifier: a weighted sum of the inputs plus a bias, thresholded to +1 or -1. What makes it worth a dedicated header rather than a five-line inline snippet is the training rule and what it guarantees. Novikoff's convergence theorem says that IF the two classes are linearly separable - some straight line or hyperplane can put every training point on the correct side - then the perceptron rule finds such a boundary in a finite number of updates, and it only ever updates the weights on a sample it currently gets wrong. Correctly classified samples are left untouched, which is a different and older idea to gradient descent. The theorem's condition is also its warning label: if the classes are NOT linearly separable, the rule does not fail gracefully, it oscillates forever and never converges, no matter how many epochs run. XOR is the textbook minimal example - no straight line separates it - and this submission's own demo deliberately trains on XOR after the solvable example specifically to make that failure visible on the same fixed epoch cap rather than letting it run unbounded. PerceptronTrainEpoch returns the number of corrections made during the pass; zero means the epoch converged, and a caller that keeps seeing a non-zero count after a generous number of epochs has a dataset that needs a non-linear model - see the companion NeuralNetwork.mqh, whose own demo solves that identical XOR table with one hidden layer. Nothing here should be confused with a full multi-layer network. A perceptron has no hidden layer, learns a single straight decision boundary, and cannot represent anything a straight line cannot separate. That is the whole point of shipping it as its own small, honestly-scoped library instead of quietly downgrading NeuralNetwork.mqh's capability with a "hidden=0" option. Notes and limitations - Guarantees convergence ONLY when the classes are linearly separable. There is no way to detect separability in advance short of running the rule and watching whether the correction count reaches zero. - Binary classification only, labels are +1/-1. Multi-class problems need either one-vs-rest with several perceptrons or a different model entirely. - No margin: the perceptron rule stops as soon as every point is on the correct side, which can leave the boundary sitting very close to some points. A support vector machine maximises margin instead; this header does not attempt to. - Pure function aside from the shared MathRand() stream; no market or account access.
Script · MT5 · v1.0 · Free
About Classic Perceptron Linear Classifier
Classic Perceptron Linear Classifier is a script for MetaTrader 5. It has 3 settings, such as Learning rate, Max epochs and Seed. MQL source code included.
Files in the download: Perceptron.mq5.mqh, Perceptron.txt, PerceptronDemo.ex5, PerceptronDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Learning rate | 1.0 | Perceptron rule step size |
| Max epochs | 50 | Cap on training passes |
| Seed | 11 | Random seed for weight initialisation |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code