Special Functions Library
MathErf/MathErfc simply do not exist in MQL4, and there is no gamma or beta function either, so any p-value, confidence interval, or distribution-fit calculation that needs one usually gets skipped or crudely faked. This library implements the three properly: the error function via the Abramowitz & Stegun 7.1.26 polynomial approximation (max error 1.5e-7, the same approximation family already used for a normal-tail approximation elsewhere in this catalogue, kept consistent rather than reinventing another one), the gamma function via the Lanczos approximation (accurate to roughly 15 significant digits over its domain, using natural-log-gamma internally so it never overflows the way a direct large-x gamma value would), and the regularised incomplete beta function via the classical continued-fraction algorithm - the textbook way to evaluate it to full double precision without a symbolic closed form. These three functions chain together into the calculations that actually show up in trading statistics: the normal CDF (built on erf) for a p-value or confidence band, and the incomplete beta function for an exact binomial confidence interval or a Student-t/F-distribution p-value, via the standard identity linking the binomial CDF to the incomplete beta function - which the demo uses directly to compute P(7 or more heads in 10 fair coin flips) as a worked example. SfSelfTest() runs seven checks against textbook known values (erf(1), Gamma(5)=4!, Gamma(0.5)=sqrt(pi), two incomplete-beta boundary values) and reports pass/fail with the actual error against each stated tolerance - a quick way to confirm the approximations behave as documented in a given build before relying on them for something that matters. Notes and limitations SfGamma/SfGammaLn are only implemented for x > 0; there is no reflection formula for zero or negative arguments (they return 0.0), since no calculation in this catalogue currently needs it - a caller needing the full domain should add the reflection formula (Gamma(x)*Gamma(1-x) = pi/sin(pi*x)) themselves. SfErf's stated accuracy is a maximum absolute error of about 1.5e-7 - fine for a significance test or a confidence band, not accurate enough for a calculation that itself demands double-precision (15-16 digit) accuracy in the tail probability. SfIncompleteBeta requires a > 0, b > 0 and 0 <= x <= 1; values outside that domain return 0.0 rather than raising an error, so check your inputs before trusting a zero result as meaningful. The continued-fraction evaluation inside SfIncompleteBeta is capped at 200 iterations; for the parameter ranges any realistic trading-statistics use needs (a, b roughly under a few thousand) this converges in well under 50 iterations, so the cap is a safety bound, not a practical limitation.
Script · MT4 · v1.0 · Free
About Special Functions Library
Special Functions Library is a script for MetaTrader 4. It has 1 setting, such as Z score. MQL source code included.
Files in the download: SpecialFunctions.mqh, SpecialFunctions.txt, SpecialFunctionsDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Z score | 1.96 | z-score to compute a two-sided p-value for |
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: MT4 Script · MQL4 Source Code