Feature Matrix Builder
Building a feature matrix by hand from several arrays looks simple and is the source of a specific, quiet bug: a lagged return needs `lag` bars of history that do not exist yet at the start of the series, a rolling standard deviation needs a full window, and an externally computed indicator often has its own separate warm-up on top of both. Stack a few of these into a matrix without tracking each column's own valid range and row 40 of one column does not correspond to row 40 of another - a model trained on that matrix has learned nothing about the market, only about an alignment bug. This header fixes that by marking every cell that has no defined value yet with an explicit sentinel (FEATURE_NA) instead of leaving it zero, which would be indistinguishable from a genuine zero return. Build() then scans for the first row where every column simultaneously has a real value and trims everything before it, so what comes out is a rectangular matrix with no hidden misalignment - or, with dropIncompleteRows set to false, the full staged range including the NA cells, for callers who want to inspect the warm-up directly. Three derived column types are built in - lagged simple return, rolling mean, rolling sample standard deviation - and AddColumn() accepts any externally computed series, typically an indicator buffer you already have, in the same chronological convention as the base series. A shorter external column is treated as having its own warm-up and aligned to the most recent rows; a longer one is truncated to the most recent values rather than silently shifting everything that follows it. The series convention is chronological, oldest first - index 0 is the oldest bar. On MQL4 this is the opposite of the platform's own Close[] array, which is newest first; on MQL5 it depends on whether ArraySetAsSeries has been applied to whatever price array you built. The demo does the reversal with one line and the comment explaining why. Notes and limitations - This assembles and aligns columns; it does not scale, normalise or select among them. Feed the output into whatever modelling or scaling step comes next (see FeatureScaler elsewhere in this catalogue). - FEATURE_NA is a large negative sentinel, not a true NaN - neither MQL4 nor MQL5 has a portable one. Always check FeatureIsNa() rather than comparing a value directly to a literal, since the exact sentinel is an implementation detail. - AddColumn() aligns purely by row count and position, not by timestamp. If an external column was computed on a different bar range than the base series, align it yourself before passing it in - this header has no way to detect that mismatch on its own. - Init() resets all columns added so far, so build the whole set of columns after each Init() call rather than across two separate ones.
Script · MT4 · v1.0 · Free
About Feature Matrix Builder
Feature Matrix Builder is a script for MetaTrader 4. It has 1 setting, such as Bar count. MQL source code included.
Files in the download: FeatureBuilder.mqh, FeatureBuilder.txt, FeatureBuilderDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Bar count | 30 | Length of the synthetic price series used in every example |
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: MT4 Script · MQL4 Source Code