Article-22714-SLSQP-MQL5-Vo.../README.md

99 lines
4 KiB
Markdown
Raw Permalink Normal View History

2026-06-04 10:15:42 +00:00
# Article-22714-SLSQP-MQL5-Volatility-Library-Integration
2026-06-04 10:25:35 +00:00
## Overview
This reference project is based on an MQL5 article describing a native Sequential Least Squares Programming (SLSQP) optimizer for constrained nonlinear optimization and its integration into an existing conditional volatility library intended to mirror Python's `arch` module behavior.
The article focuses on:
- diagnosing parameter discrepancies between MetaTrader 5 and Python volatility models,
- replacing ALGLIB `minNLC` with SLSQP,
- exposing an object-oriented MQL5 interface for objective functions and constraints,
- and validating improved cross-platform agreement on ARCH/GARCH-family models.
## Original Article
- **Article ID:** 22714
- **Author:** Francis Dube
- **Publication date:** 2026.05.30
- **Category:** Articles / MQL5 programming / optimization / quantitative finance
- **URL:** https://www.mql5.com/en/articles/22714
## Repository Purpose
This repository should be understood as a reference/reconstruction project for the article’s code and architecture.
Its purpose is to preserve the implementation concepts presented in the article:
- an MQL5 port of the original Fortran SLSQP algorithm,
- a `CSlsqp` solver interface,
- constraint wrappers via `CConstraints`,
- objective wrappers via `CFunctor`,
- optional numerical or analytical gradients,
- and conditional integration into a volatility modeling library through the `__SLSQP__` macro.
## Key Concepts
- Sequential Least Squares Programming (SLSQP)
- constrained nonlinear optimization
- KKT conditions
- active-set constraint handling
- equality and inequality constraints
- finite-difference numerical differentiation
- analytical Jacobians/gradients
- conditional volatility models
- cross-platform parity with Python `arch`
- conditional compilation for optimizer selection
## Algorithm / Architecture Summary
The article describes an MQL5-native SLSQP implementation centered around `slsqp.mqh`, with an object-oriented class `CSlsqp` used to configure and execute optimization.
Key architectural elements mentioned in the article:
- **Objective interface**
- objective functions derive from `CFunctor`
- core function implemented through `orig_fun()`
- optional analytical gradient via `grad_fun()`
- **Constraint interface**
- constraints derive from `CConstraints`
- equality constraints use the form `h(x)=0`
- inequality constraints use the form `g(x)<=0`
- optional analytical Jacobian through `orig_grad()`
- **Differentiation support**
- numerical differentiation utilities are supplied through `num_diff.mqh`
- `ObjReturn` is extended to support scalar/vector outputs and gradient/Jacobian data
- **Solver configuration**
- bounds, tolerances, stopping conditions, maximum evaluations, and target stop values
- methods include `SetEqualityConstraints()`, `SetInequalityConstraints()`, `SetXtolRel()`, `SetFtolRel()`, `SetFtolAbs()`, `SetXtolAbs()`, `SetMaxEval()`, `SetStopVal()`, and `SetAcc()`
- **Execution result**
- optimization returns an `OptimizeResult` structure containing return code, function evaluations, iterations, solution, objective value, and gradient
- **Volatility library integration**
- the article shows conditional inclusion of `slsqp.mqh` in `mean.mqh`
- under `__SLSQP__`, HARX fitting uses SLSQP-based wrappers for objective and inequality constraints
- without the macro, the legacy ALGLIB path remains active
The article’s validation section reports that after integration, MQL5 model parameters and log-likelihood values become closely aligned with Python `arch` results for several ARCH/GARCH-family specifications.
## Tags
`MQL5`, `SLSQP`, `Optimization`, `Constrained-Optimization`, `ARCH`, `GARCH`, `Volatility-Modeling`, `Python-ARCH`, `Numerical-Differentiation`, `Quantitative-Finance`
## Difficulty
**Advanced**
Requires familiarity with:
- nonlinear constrained optimization,
- gradients and Jacobians,
- KKT conditions,
- MQL5 OOP patterns,
- and volatility model fitting workflows.
## Reference
Original MQL5 article: https://www.mql5.com/en/articles/22714