Loading…

Quantile Regression Engine

Ordinary least squares fits the conditional MEAN of y given x and assumes symmetric, evenly-spread residuals. Draw a +/-2 sigma channel around an OLS line and both rails are exactly as tight as each other, even though a real return series is usually skewed - sharper, faster moves on one side than the other - and its spread usually widens with the level of the series rather than staying constant. Quantile regression fits a chosen quantile of y given x directly: the conditional 0.05 quantile and the conditional 0.95 quantile are each their own line, so the resulting channel is genuinely asymmetric and can genuinely widen, which is the correct shape for an asymmetric risk model - a 5% downside bound behaves nothing like a symmetric standard-deviation band once the underlying data is skewed. JUDGEMENT CALL, stated plainly: exact quantile regression is normally solved as a linear programming problem (minimising the pinball/check loss), using a simplex or interior-point LP solver. MQL4 has no such solver, and hand-rolling one would be far more machinery than a CodeBase library should carry. This engine instead uses the well-known IRLS approximation to the same problem: the pinball loss's gradient is piecewise-constant (tau on one side of the residual, 1-tau on the other), so at each iteration every point is reweighted by the reciprocal of its absolute residual (asymmetrically, tau versus 1-tau either side of zero) and a plain weighted least squares line is refit, repeated until the fit stabilises. This is a real, named technique - a generalisation of the classical IRLS solution to median (L1/LAD) regression - and it converges reliably on the smooth, moderate-noise data this library targets. It is an iterative approximation to the LP solution, not the LP solution itself; a residual landing exactly at zero is guarded with a small epsilon in the reweighting step rather than left to divide by zero. FitQuantileRegression fits y = intercept + slope*x for one predictor x and one target y, at any tau strictly between 0 and 1 (tau=0.5 is the median regression line - itself a more outlier-resistant alternative to OLS even without needing an asymmetric channel). PinballLoss and TotalPinballLoss are exposed so a caller can verify a fit under the loss quantile regression actually optimises, rather than trusting convergence blindly. Notes and limitations - One predictor only (simple, not multiple, quantile regression). Fitting against more than one x variable needs a different, larger engine - not in scope for a "Lite"-effort CodeBase library. - This is an IRLS approximation to the exact LP solution, not the LP solution itself - see the judgement-call note above. It converges reliably on the smooth, moderate-sample data this library is aimed at; it is not a substitute for a dedicated statistics package's exact solver on a research-grade dataset. - extreme tau values (very close to 0 or 1) are clamped to 0.001/0.999 internally, since the pinball loss becomes degenerate at the exact boundary. A caller asking for tau=0 or tau=1 gets the clamped value's fit, not an error. - converged=false in the returned QuantileFit means the iteration cap was reached, or the weights collapsed and iteration stopped early - the intercept/slope returned are still the best estimate found, but treat them with more caution than a converged fit.

Script · MT4 · v1.0 · Free

About Quantile Regression Engine

Quantile Regression Engine is a script for MetaTrader 4. It has 1 setting, such as Points. MQL source code included.

Files in the download: QuantileRegressionEngine.mqh, QuantileRegressionEngine.txt, QuantileRegressionEngineDemo.mq4.

Settings

SettingDefaultWhat it does
Points60Number of synthetic points to fit against

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Channels & Envelopes · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria