Monotonic Deque Sliding Window
A rolling high/low, a Donchian channel, an ATR-style range over a window - all of them need "the extreme value among the last N" recomputed as new bars arrive. The obvious way is a loop over the window on every bar. That is O(window) per bar and O(bars times window) across a whole chart, and it is what most custom indicators that draw a rolling extreme actually do, because small windows on a chart never make the cost visible. It stops being free the moment the window runs into the hundreds and the indicator recalculates on every tick of a live symbol. The monotonic deque is the standard fix, and it earns its keep from a single observation: keep a deque of candidate values in strictly decreasing order for the maximum (or strictly increasing for the minimum). Before pushing a new value, pop everything at the back that the new value beats outright - those values can never be the answer again, because the new one is both newer and at least as extreme. What survives at the front is always the current window's extreme, and ageing a value out of the window is a check against the front only, never a rescan. Every value is pushed exactly once and popped at most once across the whole stream, which is what makes each individual push look like a loop yet cost O(1) amortised overall. One instance tracks the rolling minimum and the rolling maximum from the same input stream at once, because most of what wants one wants the other - a channel breakout needs both edges, and computing them separately would mean feeding the same values through two objects. The demo does not just assert this is correct - it runs a brute-force O(window) scan alongside the deque over thousands of synthetic bars and prints the moment the two would ever disagree, which is a stronger claim than most indicator code makes about its own rolling calculations. This class touches no chart, symbol or trade API at all - it is a pure algorithm over a caller-supplied stream of doubles - so the MQL5 port is functionally identical to the MQL4 source, unlike almost everything else in this catalogue. The only differences between the two files are the header comment and the file guard. Notes and limitations - Push() must be called in time order, oldest to newest, exactly once per bar or tick you want represented. There is no way to "unpush" a specific value or replace one already in the window - if the most recent value needs correcting (an updating tick before the bar closes), track that outside the class and call Push() only on bar close, or maintain a second instance for the closed-bar series. - GetMax() and GetMin() before the first Push() return false with the value left at 0.0. Check the return value rather than trusting a raw zero, the same convention as the rest of this catalogue's containers. - Memory is O(window) regardless of how many values have been pushed in total - the ring buffers are sized once at Init() and never grow. - This tracks EXTREMES, not arbitrary order statistics. It cannot answer "what is the median of the last N" - that needs a different structure (see the Streaming Quantile Estimator elsewhere in this catalogue).
Script · MT4 · v1.0 · Free
About Monotonic Deque Sliding Window
Monotonic Deque Sliding Window is a script for MetaTrader 4. It has 2 settings, such as Window size and Sample count. MQL source code included.
Files in the download: MonotonicDeque.mqh, MonotonicDeque.txt, MonotonicDequeDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Window size | 14 | Rolling window length |
| Sample count | 5000 | Synthetic values pushed through both methods |
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: ATR · Channels & Envelopes · MT4 Script · MQL4 Source Code