Polynomial Regression
A straight-line fit understates a curved relationship - a price series that accelerates or decelerates, not just trends, needs a polynomial to describe properly. Fitting one by least squares means solving a system of normal equations, and that is where most hand-rolled implementations go wrong in one of two ways: they skip pivoting during elimination, or they do not check whether the requested degree is even solvable for the data given. This library builds the normal equations directly from sums of powers of x (no design matrix held in memory, so point count is not a memory concern) and solves them with Gaussian elimination using PARTIAL PIVOTING - at each elimination step, the largest available entry in the working column is swapped to the pivot position before continuing. This is not a stylistic choice. Powers of x for anything above a cubic span wildly different magnitudes (x versus x^4, say), and without pivoting a small pivot value amplifies rounding error badly enough to produce visibly wrong coefficients on ordinary price data. A degree at or above the number of points is refused outright by PolyRegressionFit, returning false rather than a coefficient array. That is not a fit, it is a system with more unknowns than constraints, and it either has no unique solution or fits noise exactly - a different kind of wrong that looks deceptively perfect (R-squared of 1.0) until it is used to predict anything outside the training points. Duplicate x values are refused for the same underlying reason: the normal-equation matrix becomes singular and Gaussian elimination detects it as a pivot that never rises above the numerical noise floor. The header has no #include of MatrixMath.mqh or any other project in this catalogue - the small Gaussian elimination solver it needs is implemented directly, because every CodeBase submission must stand alone when downloaded by itself. Notes and limitations - Always check the boolean return of PolyRegressionFit before using coeffs - on failure the array is left empty (size 0), and PolyRegressionEval on an empty array returns 0.0 rather than crashing, but that 0.0 is not a real prediction. - High degrees on data with a narrow x range can still be numerically fragile even with pivoting, because the underlying normal-equation approach is inherently less stable than an orthogonal basis (Chebyshev polynomials, for instance). For degree 4 and below on typical bar-index ranges this is not a practical concern; treat degree 5+ results with more scepticism and verify R-squared makes sense. - A high R-squared at a high degree is not automatically a good sign - a degree close to the point count fits noise as well as signal (visible in the "degree 5 on 5 points" edge case in the demo, which is refused entirely rather than allowed to produce a perfect but meaningless fit). - For a straight-line fit specifically, use LinearRegression.mqh from this same catalogue - it is simpler, faster, and numerically exact for degree 1 rather than routed through the general normal-equation solver.
Script · MT4 · v1.0 · Free
About Polynomial Regression
Polynomial Regression is a script for MetaTrader 4. It has 1 setting, such as Point count. MQL source code included.
Files in the download: PolyRegression.mqh, PolyRegression.txt, PolyRegressionDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Point count | 15 | How many points to generate for the quadratic/cubic fits |
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: Trend · MT4 Script · MQL4 Source Code