Loading…

Polynomial Toolkit

A fitted polynomial (a regression curve, a spline segment, a smoothed price model) is only useful once you can actually query it - value at a point, slope at a point, where it crosses a level - and this toolkit is the glue that does that consistently over one coefficient convention (ascending order: coeffs[0] is the constant term, coeffs[n] the leading term of a degree-n polynomial). Evaluation uses Horner's method, the standard minimal-multiplication, best- stability approach. Differentiation and integration produce a new coefficient array directly (no numerical approximation needed - both are exact operations on a polynomial's coefficients). Root finding is layered: closed-form solutions for linear and quadratic equations using the numerically stable quadratic formula (which avoids the cancellation error the textbook formula suffers when b and sqrt(discriminant) are close in magnitude), a full Cardano's-method cubic solver including the trigonometric form for the three-distinct-real-roots case (the "casus irreducibilis", where the naive Cardano formula would need complex cube roots that cancel back to a real answer - MQL4 has no complex-number type, so the trigonometric identity is used instead), and general-purpose bisection and Newton's-method finders for anything higher degree or for confirming the closed-form results. Descartes' rule of signs gives a cheap upper bound on the number of positive real roots before running a numerical search, useful as a sanity check on how many roots to expect. Notes and limitations No closed-form solver above degree 3 - quartic and higher polynomials must use PtFindRootBisection (needs a bracketed sign change) or PtFindRootNewton (needs a starting guess reasonably close to the root, and can fail to converge or land on the wrong root from a poor starting point, as Newton's method always can). PtSolveCubic's three-real-roots branch uses a fixed 1e-12 threshold to classify the discriminant as effectively zero versus genuinely positive or negative; a cubic whose true discriminant sits extremely close to that threshold (a near-repeated root) can occasionally classify into the "wrong" of the two nearby branches, though the numeric result stays close to correct either way since the branches agree at the boundary. PtFindRootBisection requires the caller to supply a bracket that actually contains a sign change; it returns false rather than guessing if the bracket does not straddle a root, so scan for a bracket yourself (e.g. sampling PtEvaluate at a grid of points) before calling it on an unknown polynomial. Coefficient arrays are plain double[] - there is no struct wrapping a polynomial with its degree, by design, so passing a mismatched-length array to two different calls (evaluate at one length, derivative at another) is caught only by ArraySize, not by any type system.

Script · MT5 · v1.0 · Free

About Polynomial Toolkit

Polynomial Toolkit is a script for MetaTrader 5. MQL source code included.

Files in the download: PolynomialToolkit.mq5.mqh, PolynomialToolkit.txt, PolynomialToolkitDemo.ex5, PolynomialToolkitDemo.mq5.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria