Loading…

Rolling Median Filter

A moving median is a genuinely different smoother from a moving average: instead of averaging every price in the window, it reports the middle value once the window is sorted. That makes it immune to a single outlier bar - one wild spike or a bad tick pushes an ordinary average, but it barely moves a median, since one value out of many is still just one value away from the middle. The naive way to compute a rolling median is to sort the whole window from scratch at every single bar - correct, but wasteful, since only one price actually changes between one bar's window and the next one's. This implementation instead keeps a running sorted copy of the current window and, moving forward one bar at a time, removes the one price leaving the window and inserts the one price entering it, both located with a binary search into the already-sorted array rather than a fresh full sort. The full O(n log n) sort only happens once, to build the very first window; every step after that is a targeted remove-and-insert. The median itself is read straight off the middle of the maintained sorted array - the value at the centre for an odd window, or the average of the two centre values for an even one. Notes and limitations - Not recursive and never repaints - each bar's median depends only on that bar's own fixed window of past prices. - Needs InpPeriod bars of history before it starts plotting. - The remove/insert step is O(n) worst case per bar because the underlying array still needs to shift elements after the binary search locates the position - the saving over the naive approach is dropping the O(n log n) sort cost on every bar, not achieving a theoretical O(log n) update (which needs a balanced tree or skip list, deliberately not used here to keep the implementation self-contained and auditable in plain MQL4).

Indicator · MT5 · v1.0 · Free

About Rolling Median Filter

Rolling Median Filter is an indicator for MetaTrader 5. It has 2 settings, such as Period and Applied price. MQL source code included.

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

Settings

SettingDefaultWhat it does
Period21Window size (odd values give a clean single-price median)
Applied pricePRICE_CLOSEApplied price

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

Download the PDF guide

Tags: Moving Average · MT5 Indicator · MQL5 Source Code

MetaTrader 4 & 5 indicators · PolyMaria