DinexSpace Rolling Quantile Library
- librerie
- Versione: 1.0
DinexSpace Rolling Quantile Library is a library for MetaTrader 5 (MT5) that keeps the last n values of a series in sorted order, for your indicator or Expert Advisor. You push each new value and read the median, the quartiles, any percentile and the place of a value inside the window, without writing the sorted structure yourself.
How it works
- Call DxRqCreate with the window (1 to 100000 values). You get a handle.
- Call DxRqPush for each new value (for example the close of each new bar).
- When the window is full, read DxRqMedian, DxRqQ1, DxRqQ3, DxRqIqr, DxRqQuantile for any percent from 0 to 100, or DxRqBelow and DxRqAtOrBelow for the place of a value. Before that the functions return EMPTY_VALUE.
- Or give a whole array to DxRqSeries and get the series of one percentile back.
What you get
- Median and quartiles: minimum, maximum, median, first and third quartile and the inter-quartile range of the last n values.
- Any percentile: from 0 to 100 percent, by linear interpolation between neighbouring ranks.
- Place of a value: the percent of the window below a value, and the percent at or below it.
- Equal values kept as they are: a window of 5, 5, 5 has three values of 5.
- Checked: the example EA compares eleven outputs at every bar with a separate calculation that copies and sorts the window again, on closes, on rounded closes with many equal values and on values with a large offset, and on a long synthetic series.
- Status codes with plain-English texts: bad window, bad handle, bad percent, bad value, no instance available.
- Light: each program has its own instances. No DLLs, no web requests, no Sleep. The library reads no market data.
How to use it
- Copy DinexRollingQuantile.ex5 to MQL5\Libraries.
- Add the import block with the functions you use (shown in the images and in the example EA).
- Open a window, push each new value, read the result.
Definitions (window of n values, sorted s(0) ... s(n − 1))
- Quantile at p percent (0 to 100): h = (n − 1) x p / 100, i = the whole part of h, f = h − i, result = s(i) + f x (s(i + 1) − s(i)). At 100 percent the result is s(n − 1). This is the method called R-7 in the statistics literature and used by the QUARTILE.INC function of spreadsheets.
- Median: the quantile at 50. First quartile: 25. Third quartile: 75. IQR: third quartile − first quartile.
- Below(x): 100 x (number of values less than x) / n. AtOrBelow(x): the same with less than or equal to.
Example EA (included in the description images)
On every new bar the example pushes the close price into two windows of different sizes, into a window that gets the close rounded to three decimals and into one that gets the close plus a large offset, and at the end compares every output with a separate calculation. It also runs a long synthetic series, results worked out by hand, the error codes and the whole-array call. It prints the number of checks and the number of failed checks.
Notes
- The library works on the numbers you give it. It reads no market data, gives no entries or exits and does not predict any move. A percentile of past values describes those values.
- Up to 256 instances, window from 1 to 100000 values. A push costs time in proportion to the window because the sorted array is shifted; very large windows are slower.
Requirements
- MetaTrader 5, any symbol and timeframe.
