Matrix And Vector Math
Both MQL4 and MQL5's dynamic arrays only resize their first dimension, so a matrix that can actually be built, returned and reused across functions has to be a flat 1D array addressed in row-major order, with the row and column counts carried alongside it explicitly - on either platform. This library standardises that representation and provides the arithmetic on top of it - add, subtract, scale, transpose, multiply, determinant, and inverse - so every other library in this catalogue that needs matrix work (regression, portfolio risk, statistical arbitrage) can share one correct implementation of the awkward part instead of each rolling its own. Multiplication and every shape-dependent operation check the dimensions before touching the data. A * B with mismatched inner dimensions returns false rather than reading past the end of an array or silently producing a matrix of the wrong shape - a mismatch here is exactly the kind of bug that looks like a plausible number until a downstream calculation quietly goes wrong days later. Determinant and inverse are the two operations that punish a naive implementation. Both are built on one shared Gauss-Jordan elimination routine that always pivots on the largest available entry in the working column before eliminating it (partial pivoting). Skipping that step and dividing by whatever happens to sit on the diagonal is the standard way a "working" determinant routine returns a number with no correct digits on anything but a toy input, because a small pivot amplifies the rounding error in every subsequent row. A matrix without an inverse - a singular matrix, determinant effectively zero - is reported as a refusal (false) rather than as inf or NaN silently propagating into whatever used it next. Notes and limitations - Every function takes the row/column counts explicitly rather than storing them. This is intentional - it makes a shape mismatch a caught error instead of a guess, but it means the caller is responsible for keeping the counts consistent with the array it passes. - Determinant and inverse are O(n^3) via elimination, appropriate for the small matrices trading calculations typically need (hedge ratios, covariance blocks of a handful of symbols). It is not tuned for large dense systems. - MATRIXMATH_EPSILON (1e-10) is the threshold below which a pivot is treated as zero. A matrix that is technically invertible but extremely ill-conditioned may still be reported as singular - that is the safer failure mode for trading code. - No dependency on any other library in this catalogue, per the self-containment rule - LinearSolver.mqh solves Ax=b with its own independent elimination routine rather than including this header.
Script · MT5 · v1.0 · Free
About Matrix And Vector Math
Matrix And Vector Math is a script for MetaTrader 5. MQL source code included.
Files in the download: MatrixMath.mq5.mqh, MatrixMath.txt, MatrixMathDemo.ex5, MatrixMathDemo.mq5.
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: Pivot Points · MT5 Script · MQL5 Source Code