Decision Tree Classifier
Training a small decision tree on engineered features (an ATR ratio, a session flag, a distance from a moving average) to filter or size trades is a reasonable thing to want, and almost nothing in either platform's standard library or the usual CodeBase catalogue offers it. This library is a complete, self-contained CART trainer: it builds a binary tree from a samples-by-features dataset using Gini impurity to choose each split, respects a maximum depth and a minimum samples-per-leaf during growth, and then - separately - can prune the finished tree against data it never trained on. That last part matters more than it sounds. Depth limits and minimum leaf sizes control how big a tree is allowed to get, but they cannot tell the difference between a split that reflects a real pattern and one that just happened to reduce impurity on the specific noisy samples it was trained on. PruneDecisionTree performs real reduced-error pruning: starting from the leaves and working up, it collapses any internal node into a leaf whenever doing so does not increase the misclassification count on a separate validation set. A split that looked good during training but does not generalise gets removed after the fact - the demo's synthetic dataset includes a slice of deliberately mislabelled training samples specifically to show this happening, with the node count and validation accuracy printed both before and after. Two language limits shaped how the tree is stored, and both hold on MQL5 as much as on MQL4. Only the leftmost dimension of a dynamic array can be resized on either platform, so a samples-by-features matrix cannot be declared as `double features[][]`; DecisionTreeDataset instead holds one flat 1D array, and DtFeatureValue() computes the row/column offset itself. The tree also stores every node as a plain struct with a feature index and threshold in one flat array, walked by explicit index rather than a callback - genuinely the simplest design here, not a workaround, since nothing about tree traversal benefits from indirection through a function pointer or interface. PrintDecisionTree exports the trained tree as indented, human-readable text - every split's feature, threshold, sample count and Gini impurity, down to each leaf's predicted class - because the entire point of a CART tree over a larger black-box model is that a person can read why it makes each decision. The trainer, predictor and pruner are pure computation over caller-supplied arrays and structs, with no chart, indicator or trade calls anywhere inside the header - which is exactly why the algorithm itself required no changes at all between the MQL4 and MQL5 versions. Notes and limitations - Labels must be integers in [0, classCount-1]; classCount is supplied by the caller rather than inferred, so a class with zero training examples still gets a valid slot in every histogram. - The per-feature split search insertion-sorts each feature's values - O(n^2) per feature, the same trade-off Correlation.mqh and ClassificationMetrics.mqh make elsewhere in this catalogue - fine for dataset sizes a bar-based model normally produces (tens to low thousands of samples), not intended for very large offline training runs. - A validation node reached by zero samples during pruning is collapsed by default (0 errors either way is treated as "no evidence to keep the split") - pass a validation set large enough to exercise every branch you care about keeping. - This library trains and predicts; it does not read chart data, compute indicators, or manage risk, on either platform. Feature engineering and dataset assembly are the caller's responsibility. - Training is recursive with a call depth bounded by maxDepth - keep maxDepth to double digits, which is already deep for a Gini tree with a sane minSamplesLeaf, well within either platform's default stack.
Script · MT4 · v1.0 · Free
About Decision Tree Classifier
Decision Tree Classifier is a script for MetaTrader 4. It has 2 settings, such as Train samples and Val samples. MQL source code included.
Files in the download: DecisionTreeClassifier.mqh, DecisionTreeClassifier.txt, DecisionTreeClassifierDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Train samples | 120 | Number of synthetic training samples |
| Val samples | 60 | Number of synthetic validation samples |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Moving Average · ATR · Trading Sessions · Binary Options · MT4 Script · MQL4 Source Code