Loading…

Probability Distribution Toolkit

Neither MQL4 nor MQL5 ships a statistics library. Anything that needs a p-value, a confidence interval, or a properly distributed random draw ends up either hard-coding a handful of table values or reaching for a crude approximation with no documented error bound. This library implements the four distributions almost everything in trading statistics needs - normal, Student t, chi squared and binomial - each with a probability density (or mass) function, a cumulative distribution function, and where it is meaningful, a quantile (inverse CDF) function. None of these are exact closed forms; they are numerical approximations, and this library says so plainly rather than presenting them as exact. NormalCDF uses the Abramowitz & Stegun 7.1.26 error function approximation (maximum absolute error 1.5e-7). NormalQuantile uses Acklam's rational approximation (relative error under 1.15e-9 across the entire (0,1) domain). Student t and chi squared are built on a Lanczos log-gamma function feeding regularized incomplete gamma and incomplete beta series - the same numerical machinery used by most statistical software packages under the hood, just implemented directly rather than imported. PD_RunAccuracyChecks compares five of these functions against known textbook reference values (the standard 95% z value, the chi squared 95% critical value at 1 degree of freedom, and an exact binomial CDF) and returns pass/fail with a full text report, so a caller does not have to take the approximation quality on faith - they can run the check themselves before relying on it. Sampling functions are included for the normal (Box-Muller transform) and binomial (n independent Bernoulli draws) distributions, built on MathRand(), for anyone who needs a properly distributed random draw rather than a bare uniform one - Monte Carlo position sizing tests, synthetic data for strategy stress testing, and similar work. Notes and limitations - These are numerical approximations, not exact closed forms. Each one's documented accuracy is stated in the source comments; run PD_RunAccuracyChecks yourself if the application is sensitive to the last few decimal places. - NormalQuantile, and by extension anything built on it, returns 0.0 for p outside the open interval (0,1) rather than +-infinity - treat that as a visible, checkable sentinel for "invalid input", not a real quantile. - BinomialPMF and BinomialCDF compute the binomial coefficient via log-gamma to avoid overflow, which keeps n in the hundreds safe; at very large n the normal approximation to the binomial is both faster and numerically simpler if that precision is not required. - MathRand() (used by both sampling functions) has a period and resolution suited to demos and lightweight Monte Carlo work, not cryptographic or research-grade randomness, on either platform.

Script · MT4 · v1.0 · Free

About Probability Distribution Toolkit

Probability Distribution Toolkit is a script for MetaTrader 4. It has 1 setting, such as Sample count. MQL source code included.

Files in the download: ProbabilityDistributions.mqh, ProbabilityDistributions.txt, ProbabilityDistributionsDemo.mq4.

Settings

SettingDefaultWhat it does
Sample count5Number of random samples to print per distribution

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