No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-08-18 13:04:34 +00:00
Article-23435-OLS-Generalization.mqproj Generated by MQL5 Wizard 2026-08-12 10:33:32 +03:00
EconometricsA.mqh new files added 2026-08-12 10:37:03 +03:00
EconometricsM.mqh new files added 2026-08-12 10:37:03 +03:00
events2file.mq5 new files added 2026-08-12 10:37:03 +03:00
news.mq5 new files added 2026-08-12 10:37:03 +03:00
README.md Обновить README.md 2026-08-18 13:04:34 +00:00
spread.mq5 new files added 2026-08-12 10:37:03 +03:00

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.

Article preview

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

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

  1. Build the dependent vector y and regressor matrix X.
  2. Validate dimensions, sample size, rank, and forecast input shape.
  3. Estimate regression coefficients with classical OLS.
  4. Compute residuals.
  5. 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
  1. If robust mode is disabled:
  • use the classical OLS covariance estimate
  1. Derive coefficient statistics:
  • estimate
  • standard error
  • t-statistic
  • p-value
  • confidence interval
  1. 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

  1. Define the target series y and model orders p and q.
  2. Provide historical initialization vectors for lagged target values and lagged residuals.
  3. Validate sample size, parameter counts, convergence settings, and initialization vector sizes.
  4. Build an initial OLS model for the constant and autoregressive part.
  5. Initialize moving-average parameters, with MA terms starting from zero.
  6. Compute residuals from the current parameter vector.
  7. 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
  1. After convergence, compute the parameter covariance matrix from the final Jacobian.
  2. 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 tests
  • EconometricsM.mqh — header file with functions for vector/matrix-based multiple regression, plotting, and statistical tests
  • news.mq5 — script for analyzing volatility as a function of proximity to macroeconomic news
  • events2file.mq5 — helper script for exporting event identifiers for a selected country
  • spread.mq5 — script for analyzing the spread between two assets
  • MQL5.zip — archive containing all files

Mentioned external/archive references

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