Linear System Solver
Solving Ax = b comes up more often in trading code than it looks once you go past a single instrument: the least-squares hedge ratio between two correlated symbols, the weights that make several instruments contribute equal risk to a portfolio, or the normal equations behind any linear regression all reduce to a small square linear system once set up. The naive way to solve one - forward elimination dividing by whatever sits on the diagonal - is numerically fragile. A small pivot amplifies the rounding error in every row eliminated against it, and on anything past a toy example the answer can be wrong in a way that still looks plausible. This solver always partial-pivots: before eliminating a column, it swaps in whichever row has the largest magnitude entry in that column, which keeps the arithmetic well-conditioned. A system with no usable pivot in some column - singular, or close enough to it that the result cannot be trusted - is reported as a refusal (false) rather than as a division producing inf or NaN that then propagates silently into a position size or a risk weight. Two verification helpers are included because "the solver ran" is not the same as "the answer is right": LinearResidual computes b - Ax so you can see directly how well the solution satisfies the original equations, and LinearResidualMaxAbs reduces that to one number worth logging or asserting on. This header does not depend on MatrixMath.mqh even though the elimination idea overlaps - each CodeBase submission in this catalogue is downloaded and compiled entirely on its own, so it carries its own small, focused implementation rather than reaching into a sibling project. Notes and limitations - Solves square systems only (as many equations as unknowns). An over-determined system (more equations than unknowns, the usual case for a least-squares fit with real data) needs to be reduced to its normal equations first, as the demo's hedge-ratio example shows for one regressor. - LINEARSOLVER_EPSILON (1e-10) is the threshold below which a pivot is treated as zero. A technically invertible but severely ill-conditioned system may still be refused - the safer failure mode for trading code. - Complexity is O(n^3), fine for the small systems (a handful of instruments or regressors) that trading calculations typically produce. - Pure function, no state, no market or account access.
Script · MT4 · v1.0 · Free
About Linear System Solver
Linear System Solver is a script for MetaTrader 4. MQL source code included.
Files in the download: LinearSolver.mqh, LinearSolver.txt, LinearSolverDemo.mq4.
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: Hedging · Pivot Points · MT4 Script · MQL4 Source Code