Loading…

Streaming Quantile Estimator

The obvious way to compute a running median or percentile is to keep every value seen and sort or index into it on demand. That works for a bounded window of the last few hundred ticks. It stops working the moment "running" is meant literally - a spread monitor, latency tracker or slippage recorder that runs for weeks accumulates millions of values, and neither the memory nor the repeated sort cost is acceptable. This header implements the P-square algorithm (Jain and Chlamtac, 1985), which tracks one quantile using five moving markers - the minimum, two markers either side of the target quantile, and the maximum - updated incrementally as each value arrives. The stream itself is never stored. Memory and per-value update cost are both constant regardless of how long the stream runs. One CStreamingQuantile instance tracks one probability. Tracking several percentiles - a median, a p10 and a p90, say - means running several instances over the same stream side by side, each seeing every value once. The demo does exactly this in a single pass over one synthetic stream. The algorithm needs five observations before its marker structure exists at all; before that, GetQuantile() falls back to indexing directly into the raw buffered values, which is an honest approximation rather than the converged estimate. From the fifth observation onward every update is the real P-square step. The derivation assumes a strictly interior target quantile, so a request at or beyond the extremes (0 or 1) is clamped to a hair inside the range - a caller who genuinely wants the exact minimum or maximum should track that directly with MathMin/MathMax rather than asking this header to approximate it. The class is pure numerical algorithm - no order, position, symbol or account calls anywhere in it - so it needed no behavioural changes to port. Notes and limitations - This is an approximation, not an exact quantile. It converges as more values arrive but will not exactly match a sort-and-index result on a small or highly irregular sample, as the demo's exact-value comparison shows plainly. True on both platforms. - Extreme probabilities (near 0 or 1) are clamped and approximate the tail poorly relative to a dedicated minimum/maximum tracker - use MathMin and MathMax directly if that is genuinely what is needed. - Not thread safe in the sense that nothing in single-chart MQL execution is - a single instance is meant to be fed from one logical stream (one OnTick, one loop), not from overlapping call sites. - Distribution shape does not need to be known in advance, but the estimate converges faster on smooth, stable distributions than on ones with sudden regime shifts - after a shift the markers take a number of observations to re-settle around the new distribution.

Script · MT5 · v1.0 · Free

About Streaming Quantile Estimator

Streaming Quantile Estimator is a script for MetaTrader 5. It has 1 setting, such as Stream length. MQL source code included.

Files in the download: StreamingQuantiles.mq5.mqh, StreamingQuantiles.txt, StreamingQuantilesDemo.ex5, StreamingQuantilesDemo.mq5.

Settings

SettingDefaultWhat it does
Stream length5000Number of synthetic samples pushed through the trackers

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 → Scripts.
  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 script onto a chart to run it once.

Download the PDF guide

Tags: Arbitrage · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria