Loading…

Numerical Differentiation Kit

"Slope = (f(x+h) - f(x)) / h" is easy to write and easy to get quietly wrong. Pick h too large and the finite-difference formula's own truncation error dominates; pick it too small and floating-point cancellation in the numerator dominates instead, since f(x+h) and f(x) become nearly equal doubles being subtracted. Both failure modes look identical from the caller's side - a number comes back, it is simply wrong by an amount that depends on h in a way most code never checks. This kit gives the three standard finite-difference formulas (forward, backward, central) plus the correct step-size formula for each - central needs h ~ eps^(1/3) scaled to x0's magnitude, forward/backward need the larger h ~ sqrt(eps), and a central second derivative needs h ~ eps^(1/4) - so the truncation/rounding balance point is computed, not guessed. Richardson extrapolation combines a central estimate at h and at h/2 into a fourth-order result and gives a free error estimate along the way, useful when a caller wants better accuracy without hand-tuning h further. A gradient helper extends the same central-difference approach to an n-dimensional function: it hands out the 2*dims shifted evaluation points a gradient needs (each one shifting a single coordinate) and combines the resulting values back into a gradient vector. Following this catalogue's convention for numerical libraries, this header never calls your function itself - MQL4 has no function-pointer callback that survives a compiler-independent library build - so you evaluate your own function at the exact points it specifies and pass the resulting y-values back in. Notes and limitations All step-size formulas assume standard IEEE double precision and a reasonably well-scaled function (no extreme values or discontinuities near x0); a function with a sharp kink or a huge dynamic range near the evaluation point needs a hand-picked step, not the generic formula here. The gradient helper's node layout is a flat 1-D array (2*dims rows of dims columns each) rather than a true 2-D array, since MQL4 cannot size both dimensions of a multi-dimensional array at runtime - index it as offsets[row * dims + column] as the demo does. Richardson extrapolation assumes the underlying function is smooth enough for the O(h^2) error model to hold near x0; on a noisy or non-smooth function (e.g. finite-difference of a value already computed from noisy market data) the extrapolated result can be worse than the plain central estimate, not better - check NdRichardsonErrorEstimate rather than trusting the extrapolation blindly. This is finite-difference (numerical) differentiation only - it has no concept of symbolic/automatic differentiation and cannot exploit a known closed-form derivative even if one exists.

Script · MT5 · v1.0 · Free

About Numerical Differentiation Kit

Numerical Differentiation Kit is a script for MetaTrader 5. MQL source code included.

Files in the download: NumericalDifferentiation.mq5.mqh, NumericalDifferentiation.txt, NumericalDifferentiationDemo.ex5, NumericalDifferentiationDemo.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