Extreme Value Theory Kit
The worst drawdown or the worst single-day loss seen so far is one observed point, not a bound. It says nothing about how much worse a genuinely extreme event could plausibly be, because a sample of a few thousand observations has, almost by definition, not yet sampled the far tail. Extreme value theory exists for exactly this question: under very mild conditions, the distribution of values above a high threshold converges to a Generalized Pareto Distribution (GPD) regardless of what the bulk of the data looks like, and the distribution of block maxima converges to a Generalized Extreme Value distribution (GEV). Both give a principled way to extrapolate a return level - "the loss expected to be exceeded once every N observations" - beyond anything actually observed. Two independent fitting routes are provided so they can be cross-checked against each other. The peaks-over-threshold route extracts every excess above a chosen threshold and fits the GPD shape and scale by the method of moments (Hosking & Wallis 1987) - a closed-form estimator that is more stable on short tail samples than numerical maximum likelihood, at some cost in asymptotic efficiency. The block-maxima route splits the series into equal windows, takes each window's maximum, and fits the GEV shape, scale and location by probability-weighted moments (Hosking 1990) - again closed-form, no iterative solver, and no matrix inversion. Both routes report a signed shape parameter xi: positive means a genuinely heavy, unbounded tail (the Pareto-type case relevant to most return series); zero is the exponential-tail special case; negative means a bounded tail. A naive normal-distribution assumption implicitly forces xi to negative infinity and will always understate tail risk versus a properly fitted GPD or GEV when the true tail is heavy. Notes and limitations - Method-of-moments GPD fitting is a closed-form approximation, not maximum likelihood - it is more robust on short tail samples but less statistically efficient than an iterative MLE fit, which this header does not implement. - The PWM GEV fit uses Hosking's rational approximation to convert the PWM ratio into the shape parameter xi rather than solving the exact PWM-to-GEV system; this is standard practice and accurate for the shape range normal financial tails fall in, but is itself an approximation, not exact. - Both routes assume the underlying data is stationary and the exceedances (or block maxima) are approximately independent. Strongly autocorrelated series (e.g. volatility clustering) violate this and will bias the fit - declustering the exceedances first is a standard EVT refinement this header does not implement. - Return levels are extrapolations beyond the observed data by construction. They carry genuine statistical uncertainty this header does not quantify (no confidence interval is returned) - treat them as estimates, not limits. - EvtGpdFitMoments and EvtGevFitPWM both refuse (return false) below a minimum sample size rather than returning an unreliable fit silently.
Script · MT4 · v1.0 · Free
About Extreme Value Theory Kit
Extreme Value Theory Kit is a script for MetaTrader 4. It has 5 settings, such as Observations, Threshold pct and Block size. MQL source code included.
Files in the download: ExtremeValueTheoryKit.mqh, ExtremeValueTheoryKit.txt, ExtremeValueTheoryKitDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Observations | 2000 | Number of synthetic daily observations |
| Threshold pct | 92.0 | Percentile used as the POT threshold |
| Block size | 20 | Observations per block for the GEV block-maxima fit |
| Return period | 250.0 | Return period in observations (e.g. one trading year) |
| Seed | 4 | Random seed |
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