Article-20589-Volatility-Modeling-And-Forecasting-In-MQL5 ## Overview This project documents and organizes the concepts presented in an MQL5 article about a native library for univariate volatility modeling and forecasting. The article describes a modular framework inspired by Python's `arch` package, where a complete model is composed of: - a mean process, - a volatility process, - an error distribution. The implementation centers on a mean-model base class (`HARX`) that manages initialization, parameter estimation, simulation, and forecasting for the full model. ## Original Article - Article title: Volatility Modeling and Forecasting in MQL5 - Article ID: 20589 - Author: Francis Dube - Publication date: 2026.01.14 - Category: Articles / MQL5 programming / optimization / quantitative finance - URL: https://www.mql5.com/en/articles/20589 ## Repository Purpose This repository should be treated as a reference or reconstruction project based on the article text. Its purpose is to capture: - the described library architecture, - the exposed data structures and APIs, - the fitting and forecasting workflow, - the ARCH-LM diagnostic procedure, - the included demonstration of HAR equivalence via ARX with exogenous averages. ## Key Concepts - Modular volatility modeling in MQL5 - Mean/volatility/distribution separation - `ArchParameters` as a unified configuration struct - `ArchModelResult` and `ArchForecast` as result containers - HAR, HARX, AR, and ARX mean processes - Constant variance, ARCH, and GARCH-family process selection - Joint parameter estimation through `fit()` - Forecast generation via analytic, simulation, or bootstrap methods - Residual diagnostics using Engle's ARCH-LM test - Equivalence of HAR and ARX under averaged exogenous regressors ## Algorithm / Architecture Summary The article describes a class hierarchy where `HARX` acts as the base interface for mean models and also orchestrates the full volatility model. ### Model composition A model is formed from three components: 1. Mean process 2. Volatility process 3. Error distribution The mean process is the central object and owns the estimation workflow. ### Configuration Model setup is performed through an `ArchParameters` struct, which includes: - sample observations, - optional exogenous regressors, - lag specification, - mean model selection, - volatility model selection, - distribution selection, - holdout and scaling options, - GARCH order parameters, - bootstrap settings, - distribution initialization parameters, - RNG seeds. The `initialize()` method validates and normalizes these inputs, adjusts incompatible options where possible, builds lag matrices, and instantiates the selected volatility process and distribution. ### Estimation The article states that estimation uses ALGLIB's nonlinear constrained optimization via `CMinNLC`, minimizing a log-likelihood objective exposed by the model class. The returned `ArchModelResult` includes: - estimated parameters, - in-sample conditional volatility, - residuals, - covariance matrix, - fit range, - log-likelihood, - R-squared and adjusted R-squared helpers, - t-values, p-values, and standard errors. ### Forecasting Forecasts are returned in an `ArchForecast` struct with: - `mean` - `variance` - `residual_variance` The article describes support for: - analytic forecasts, - Monte Carlo simulation forecasts, - bootstrap forecasts. For models with exogenous variables, future exogenous matrices must be supplied. ### Validation Model adequacy is checked using `archlmtest()`, which performs a regression-based ARCH-LM test on squared residuals, optionally using standardized residuals. The result is encapsulated in `WaldTestStatistic`, providing: - test statistic, - degrees of freedom, - null and alternative hypotheses, - p-value, - chi-square critical values. ### Explicitly attached files in the article text - `MQL5/scripts/HAR_as_ARX_Demo.mq5` — demonstration script comparing HAR and ARX formulations ## Tags `mql5`, `volatility-modeling`, `arch`, `garch`, `har`, `arx`, `econometrics`, `time-series`, `forecasting`, `alglib` ## Difficulty Advanced ## Reference - MQL5 article: "Volatility Modeling and Forecasting in MQL5" - Demonstrated script: `HAR_as_ARX_Demo.mq5` - Related concepts referenced in the article: - Python `arch` package - ALGLIB `CMinNLC` - Engle's ARCH-LM test