Loading…

Robust Slope System

An ordinary least-squares trend line is pulled by every point in its window in proportion to how far that point sits from the line - which means a single outlier bar, a spike, a gap, one news candle, can drag the whole slope estimate off course. Theil-Sen avoids this by a different construction entirely: it computes the slope between every pair of points in the window, sorts all of those pairwise slopes, and takes the MEDIAN. An outlier pair's slope only matters for where it RANKS among the others, not how extreme its value is - the median simply does not care how far out the worst pair sits. This reimplements the same median-of-pairwise-slopes technique already published elsewhere in this catalogue as TheilSenEstimator.mqh / TheilSenTrendLine.mq4, inline in this file, because every CodeBase submission has to stand alone with no cross-project #include. Beyond a plain slope threshold, the entry filter also uses Sen's (1968) rank- based confidence interval for the slope - built from the normal approximation to Kendall's S-statistic variance, picking two order statistics out of the sorted pairwise-slope array at ranks derived from that variance - and requires the WHOLE interval to sit on one side of zero before treating a slope as real rather than noise that happened to exceed the raw threshold by chance. The position is closed as soon as the recomputed slope stops supporting the held direction, whether it has flattened, reversed, or (with InpRequireSignificant on) dropped out of statistical significance. Notes and limitations - The confidence interval is a normal approximation to Kendall's S-statistic variance, the standard tie-free textbook form - not the exact tie-corrected variance. Bar-indexed x values are rarely tied, so this is an accepted, standard approximation, not a shortcut unique to this implementation. - O(n^2) pairwise slopes is the honest complexity of the exact method; there is no faster algorithm without extra structure. This is irrelevant at InpLookbackBars=40 but worth knowing before pushing the lookback far higher. - Only one position is held at a time per chart/magic combination. - Requires AutoTrading enabled to open or close anything.

Expert advisor · MT5 · v1.0 · Free

About Robust Slope System

Robust Slope System is an expert advisor (trading robot) for MetaTrader 5 based on ATR. It increases the lot size after a losing trade (martingale) and can size trades from your balance and risk percent. It has 13 settings, such as Lookback bars, Slope threshold pts and Confidence level. MQL source code included.

Files in the download: RobustSlopeSystem.ex5, RobustSlopeSystem.mq5, RobustSlopeSystem.txt.

Trading robots can lose money. Check every setting and test on a demo account before using real funds.

Settings

SettingDefaultWhat it does
Lookback bars40Bars used to build the Theil-Sen slope estimate
Slope threshold pts3.0Minimum |slope| in points per bar required to enter
Confidence level0.90Confidence level for Sen's slope interval (e.g. 0.90, 0.95)
Require significanttrueRequire the confidence interval to exclude zero, not just the point estimate
Atr period14ATR period used to size the stop
Atr multiplier2.5Stop distance = ATR * this multiplier
Use risk sizingfalseSize by percent risk instead of a fixed lot
Risk percent1.0Percent of equity risked per trade, when risk sizing is on
Fixed lots0.10Lot size when risk sizing is off, or as a fallback
Magic number20260612Magic number for orders opened by this EA
Slippage pips3Slippage tolerance, in pips
Max retries5Attempts per trade action before giving up
Retry wait ms500Pause between attempts, milliseconds

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
  6. Test it first on a demo account or in the Strategy Tester.

Download the PDF guide

Tags: Trend · News Trading · Martingale · Risk & Lot Size · ATR · MT5 Expert Advisor · MQL5 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria