Loading…

Quantile Regression Trend Fan

A standard regression channel fits one ordinary least squares line through the mean of price and draws both rails at the same fixed offset either side of it. That is only correct when the residuals are symmetric and evenly spread, which real price data usually is not - moves are often sharper and faster in one direction than the other, and the spread often widens or narrows with the level of the series. A fixed-offset channel cannot represent any of that: both rails are always exactly as tight as each other by construction. This indicator instead fits five separate quantile regressions - 0.10, 0.25, 0.50, 0.75 and 0.90 - each one its own straight line through the conditional quantile of price at that level, not through the mean. Where the fan widens on one side more than the other, that widening is real: it means the upper (or lower) quantile of price genuinely spread out more over the window, not an artefact of a symmetric formula. Exact quantile regression is normally solved as a linear programming problem, minimising the pinball/check loss with a simplex or interior-point solver - neither exists in MQL4 and hand-rolling one would be far more machinery than a CodeBase indicator should carry. This instead uses the well-known IRLS (iteratively reweighted least squares) approximation: the pinball loss's gradient is piecewise-constant, so each iteration reweights every point by the reciprocal of its absolute residual (asymmetrically, tau versus 1-tau either side of zero) and refits a plain weighted least squares line, repeated until the fit stabilises. This is a real, named technique - a generalisation of the classical IRLS solution to median (L1) regression - not a disguised OLS channel with a different label. The fan re-fits on every newly closed bar over its own trailing window and is drawn only across that window, the same way MetaTrader's own Standard Deviation Channel behaves: as new bars close, the whole fan shifts forward and re-fits. That is intended rolling behaviour, not unintended repaint - each bar's value reflects the window that produced it at the time, and is never revised backward using future data. Notes and limitations - This is an IRLS approximation to the exact linear-programming quantile regression solution, not the LP solution itself. It converges reliably on the smooth, moderate-sample price data this indicator is aimed at; it is not a substitute for a dedicated statistics package's exact solver. - InpLookback is hard-capped at 300 bars, and the fit is O(n) per IRLS iteration with up to InpMaxIterations iterations, run five times per recompute - bounded deliberately so the indicator stays responsive. - The fan only uses closed bars in its fit (shift 1 upward); the forming bar is shown as a one-step extrapolation of each fitted line, not an extra data point in the regression itself. - Recomputes and fully redraws its own trailing window on every new bar - by design, not a bug; see the FULL DESCRIPTION above.

Indicator · MT4 · v1.0 · Free

About Quantile Regression Trend Fan

Quantile Regression Trend Fan is an indicator for MetaTrader 4. It has 3 settings, such as Lookback, Max iterations and Tolerance. MQL source code included.

Files in the download: QuantileRegressionFan.mq4, QuantileRegressionFan.txt.

Settings

SettingDefaultWhat it does
Lookback100Trailing closed bars used to fit each fan (10-300)
Max iterations60IRLS iteration cap per quantile fit
Tolerance1e-8IRLS convergence tolerance (sum of |intercept|+|slope| move)

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 → Indicators.
  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 indicator from the Navigator onto a chart and adjust the settings.

Download the PDF guide

Tags: Channels & Envelopes · Trend · Trailing Stop & Break-even · MT4 Indicator · MQL4 Source Code

MetaTrader 4 & 5 indicators · PolyMaria