Loading…

Theil-Sen Robust Trend Line

An ordinary least squares trend line minimises squared error, which means every point pulls on the fit in proportion to the SQUARE of how far it sits from the line - one wide-range spike bar or one bad tick can noticeably tilt the whole line, even though it was one bar out of fifty. The Theil-Sen estimator (Sen, 1968; building on Theil, 1950) sidesteps this by computing the slope between every pair of points in the window and taking the MEDIAN of those pairwise slopes as the trend's slope. A single outlier point is only ever one endpoint of the (n-1) pairs it belongs to, out of n(n-1)/2 pairs total - it can shift the median a little, but it cannot dominate it the way it dominates a sum-of-squares fit. The intercept is set as the median of (close - slope * x) across all points in the window, the standard companion estimator to the median-slope line. A band is drawn either side of the line at a multiple of the residual's median absolute deviation (MAD) - a robust dispersion measure that, like the line itself, is not thrown off by the one or two bars the line was deliberately built to resist. Because the exact estimator needs every pairwise slope, it is genuinely O(n^2) work - not a shortcut or an approximation using a subsample of pairs. InpLookback is hard-capped at 150 bars (11,175 pairs), which stays fast in MQL4 while keeping this a real, complete Theil-Sen fit rather than a randomly-sampled one. Notes and limitations - Genuine O(n^2) pairwise-slope computation, capped at InpLookback=150 for performance - not a subsampled or approximate variant. - The confidence band is a MAD-based dispersion band around the robust line, not a formal statistical confidence interval derived from the Theil-Sen slope's own sampling distribution (which needs Kendall's tau tables this indicator does not implement) - treat it as a practical visual guide to residual spread, not a precise probability bound. - Fits use closed bars only and re-fit the whole window on every new bar, the same rolling behaviour as a standard regression channel - the line is not intended to hold a fixed shape as new bars close. - Does not repaint the already-closed portion of a PREVIOUS window's fit; each new bar simply produces a new fit over its own current window.

Indicator · MT4 · v1.0 · Free

About Theil-Sen Robust Trend Line

Theil-Sen Robust Trend Line is an indicator for MetaTrader 4. It has 2 settings, such as Lookback and Band mult. MQL source code included.

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

Settings

SettingDefaultWhat it does
Lookback50Trailing closed bars used for the fit (5-150)
Band mult1.5Band half-width, in residual MAD multiples

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: Trend · MT4 Indicator · MQL4 Source Code

MetaTrader 4 & 5 indicators · PolyMaria