Theil-Sen Slope Estimator
Ordinary least-squares regression minimises squared error, which means a single extreme point - a news spike, a data gap, a bad tick - can pull the whole fitted line toward itself, sometimes badly. The Theil-Sen estimator takes the slope between every pair of points and uses the MEDIAN of those slopes instead of a squared-error fit: a small number of outlier pairs change where they rank among all the pairwise slopes, not where the median sits, so the line stays close to what the bulk of the data actually shows. TS_Compute returns the median-of-pairwise-slopes as the trend's slope, and the median of (y[i] - slope*x[i]) across all points as the intercept - both medians, so the intercept inherits the same outlier resistance rather than being solved afterward by an ordinary least-squares step layered on top of a robust slope. TS_ConfidenceInterval is the real Sen (1968) construction, not a bootstrap or a normal approximation applied to the slope value itself: it selects two order statistics out of the sorted array of pairwise slopes, at ranks derived from the normal approximation to the variance of Kendall's S statistic. This is genuinely distribution-free about the slope's own shape - it only assumes the pairs are approximately independent for the variance formula, the standard assumption behind this exact method. Computing every pairwise slope is O(n^2) - there is no faster EXACT algorithm for this method without extra structure such as a weighted median trick, and for the sample sizes this is meant for (a bar window, a run of closed trades, a rolling indicator segment) n^2 pairs is trivial work on modern hardware. Notes and limitations - Requires at least 4 points for a confidence interval to be computed (TS_Compute itself works from 2 points, but the interval needs enough pairwise slopes for the rank selection to land inside range). - The confidence-interval variance formula, n(n-1)(2n+5)/18, is the textbook tie-free form for Kendall's S statistic. With heavily tied x values (many points sharing the same x) this is an approximation rather than the exact tie-corrected variance - acceptable for a bar-indexed or time-indexed trend line, where x values are essentially never tied. - Vertical pairs (identical x, undefined slope) are silently skipped when building the pairwise-slope array rather than causing a division error. - This is a batch fit over an in-memory x/y pair, not an incremental online estimator - refitting on every new bar means recomputing all O(n^2) pairwise slopes again.
Script · MT4 · v1.0 · Free
About Theil-Sen Slope Estimator
Theil-Sen Slope Estimator is a script for MetaTrader 4. It has 1 setting, such as Confidence level. MQL source code included.
Files in the download: TheilSenEstimator.mqh, TheilSenEstimator.txt, TheilSenEstimatorDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Confidence level | 0.95 | Confidence level for the slope interval |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Trend · News Trading · MT4 Script · MQL4 Source Code