- MQL5 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| Article-23435-OLS-Generalization.mqproj | ||
| EconometricsA.mqh | ||
| EconometricsM.mqh | ||
| events2file.mq5 | ||
| news.mq5 | ||
| README.md | ||
| spread.mq5 | ||
Article-23435-OLS-Generalization
This repository is an article-derived reference project based on the original MQL5 article. It does not claim to reproduce the full original source code unless files are explicitly attached.
Overview
This repository is derived from the MQL5 article about extending ordinary least squares (OLS) for more realistic financial time-series conditions. The article focuses on two major violations of classical regression assumptions—heteroskedasticity and residual autocorrelation—and explains how to handle them in MQL5.
The article presents two complementary approaches:
- a Newey–West HAC correction for robust standard errors in linear regression
- a Conditional Least Squares (CLS) approach for ARMA-type models, solved iteratively
As a repository, this project serves as a reference for reconstructing or studying the econometric methods, MQL5 workflow, and example scripts discussed in the article.
Original Article
- Article ID: 23435
- Title: Неопределённость как модель (Часть 8): Обобщение МНК
- Author: Aleksey Nikolayev
- Publication date: 2026-08-17
- Categories: Statistics, Machine Learning
- Article URL: https://www.mql5.com/ru/articles/23435
- Author profile: https://www.mql5.com/ru/users/alexeynikolaev2
Repository Purpose
This repository exists to preserve and organize the technical ideas from the original MQL5 article in a reusable form.
A reader can use it to learn:
- when classical OLS becomes unreliable on market data
- how robust covariance estimation changes inference without changing OLS coefficients
- how to model autoregressive and moving-average error structure with CLS
- how econometric methods can be implemented procedurally in MQL5 for trading research
It may also help readers reuse the article’s structure for their own experiments in regression diagnostics, volatility analysis around macroeconomic events, and spread modeling.
Key Concepts
- Statistics
- Machine Learning
- Ordinary Least Squares
- Generalized regression under violated assumptions
- Heteroskedasticity
- Residual autocorrelation
- HAC covariance estimation
- Newey–West estimator
- Conditional Least Squares
- AR, MA, and ARMA modeling
- Levenberg–Marquardt optimization
- Forecast confidence and prediction intervals
- Econometric analysis in MQL5
Algorithm / Architecture Summary
The article describes two main algorithmic paths.
1. Newey–West robust regression workflow
- Build the dependent vector
yand regressor matrixX. - Validate dimensions, sample size, rank, and forecast input shape.
- Estimate regression coefficients with classical OLS.
- Compute residuals.
- If robust mode is enabled:
- estimate a heteroskedasticity-and-autocorrelation-consistent covariance structure
- choose the maximum lag using an empirical rule based on sample size
- apply Parzen kernel weights to off-diagonal lag contributions
- compute the sandwich covariance matrix for coefficients
- If robust mode is disabled:
- use the classical OLS covariance estimate
- Derive coefficient statistics:
- estimate
- standard error
- t-statistic
- p-value
- confidence interval
- Build point forecasts plus confidence and prediction intervals for a new regressor vector.
The article explicitly notes that the full residual covariance matrix should not be materialized directly in memory for large datasets; instead, the required reduced matrix product should be accumulated efficiently.
2. CLS for ARMA-type models
- Define the target series
yand model orderspandq. - Provide historical initialization vectors for lagged target values and lagged residuals.
- Validate sample size, parameter counts, convergence settings, and initialization vector sizes.
- Build an initial OLS model for the constant and autoregressive part.
- Initialize moving-average parameters, with MA terms starting from zero.
- Compute residuals from the current parameter vector.
- Run an iterative optimizer:
- recursively compute the Jacobian of residuals with respect to parameters
- form the Gram matrix
- apply Levenberg–Marquardt damping
- calculate a parameter update step
- recompute residuals and residual sum of squares
- accept or reject the step depending on whether the fit improves
- adapt the damping factor accordingly
- stop when the parameter update becomes small enough or when iteration limits are reached
- After convergence, compute the parameter covariance matrix from the final Jacobian.
- Produce coefficient statistics and a one-step-ahead forecast.
Practical examples discussed
- News volatility example: regress normalized intraday volatility on a nonlinear time-to-news regressor derived from an economic-calendar event schedule.
- Spread example: fit an ARMA(1,1) model to a log spread between two stocks to assess persistence and possible non-stationarity for mean-reversion trading.
Mentioned or Attached Files
Explicitly attached or listed in the article
EconometricsA.mqh— header file with functions for array-based simple regression, plotting, and statistical testsEconometricsM.mqh— header file with functions for vector/matrix-based multiple regression, plotting, and statistical testsnews.mq5— script for analyzing volatility as a function of proximity to macroeconomic newsevents2file.mq5— helper script for exporting event identifiers for a selected countryspread.mq5— script for analyzing the spread between two assetsMQL5.zip— archive containing all files
Mentioned external/archive references
- Attached ZIP archive is available: https://www.mql5.com/ru/articles/download/23435_259426.zip?s=2d4fc39891524cd88492aa6ccc86b427fc504ad07ed2fce1036a1c81956ba18a&t=1787052808
- Algo Forge project reference: https://forge.mql5.io/alexeynikolaev2/Article-23435-OLS-Generalization
Statistics
- Word count: Not available
- Reading time: Not available
- Images: Not available
- Code blocks: Not available
- Attached file count: 6
Tags
- mql5
- metatrader-5
- statistics
- machine-learning
- heteroskedasticity
- autocorrelation
- newey-west
- cls
- arma
- econometrics
- difficulty-advanced
Difficulty
Advanced
The article combines econometric theory, robust inference, ARMA-style residual modeling, recursive Jacobian construction, and iterative Levenberg–Marquardt optimization in MQL5. It is more advanced than introductory regression material and assumes familiarity with matrix methods, statistical inference, and time-series modeling.
Limitations
- The repository is article-derived and should be treated as a reference or reconstruction project unless the attached files are actually included in this repository.
- Full usability depends on whether the ZIP archive and listed files were preserved when building the repository.
- The article provides function signatures, code fragments, and file names, but a standalone production-ready package cannot be assumed unless all attached source files are present.
- The discussed methods rely on assumptions and applicability conditions:
- Newey–West is asymptotic and intended for sufficiently large samples.
- CLS is sensitive to initialization, sample size, and model specification.
- The spread-model example does not by itself provide a final stationarity verdict; the author explicitly notes that formal order selection and specialized stationarity testing are still needed.
Reference
- Original article: https://www.mql5.com/ru/articles/23435
- Author profile: https://www.mql5.com/ru/users/alexeynikolaev2
- MQL5 Algo Forge: https://forge.mql5.io/alexeynikolaev2/Article-23435-OLS-Generalization
