Loading…

Numerical Integration Kit

Turning a fitted probability density into an expected value, or a payoff curve into its area, needs a definite integral - and MQL4 has no quadrature routine built in. The naive fallback most hand-rolled code reaches for is a plain rectangle or trapezoid sum, which converges slowly and needs a lot of sample points to get an accurate answer. This header offers two standard, higher-order alternatives. Composite Simpson's rule fits a parabola through every three consecutive samples and is exact for any cubic polynomial. Gauss-Legendre quadrature places its sample points at specific, non-evenly-spaced positions chosen so that a 3-point rule is exact up to degree 5 and a 5-point rule is exact up to degree 9 - far more accuracy per function evaluation than an evenly spaced rule, which matters when each evaluation itself is expensive. MQL4 has no reliable, portable way to pass a function pointer into a reusable library call across every compiler configuration this catalogue targets, so the API here takes the opposite approach: the library hands out exactly where to sample (SimpsonNodes / GaussLegendre3Nodes / GaussLegendre5Nodes), the caller evaluates its own function at those positions (which can freely read chart data, account state or a fitted distribution's parameters while doing so), and passes the resulting y-values back in for the weighted sum. This keeps the header trivially self-contained while still letting it integrate a real, stateful payoff, not just a pure mathematical formula. The Gauss-Legendre node and weight values are the standard published Abramowitz & Stegun Table 25.4 constants, not an approximation derived at runtime. Notes and limitations - This header integrates a caller-supplied SET OF SAMPLES at prescribed node positions, not an arbitrary function pointer - see the full description for why. It cannot integrate a function the caller has not already evaluated at the exact nodes the node-generator functions return. - Composite Simpson requires an even number of intervals; SimpsonNodes and SimpsonIntegrate both reject an odd n. - Gauss-Legendre is only offered at 3-point and 5-point order. A higher- order rule (7-point, 9-point) would need its own published node/weight table added to the header; none is included here. - Both rules assume a smooth (continuous, well-behaved) integrand. Neither handles a genuinely discontinuous payoff (a step function, a knock-out barrier) accurately - a discontinuity inside [a,b] should be split into separate integrals on either side of it before calling either rule.

Script · MT4 · v1.0 · Free

About Numerical Integration Kit

Numerical Integration Kit is a script for MetaTrader 4. MQL source code included.

Files in the download: NumericalIntegration.mqh, NumericalIntegration.txt, NumericalIntegrationDemo.mq4.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria