DinexSpace Histogram and Binning Library
- librerie
- Versione: 1.0
DinexSpace Histogram and Binning Library is a library for MetaTrader 5 (MT5) that counts the values of a series in equal bins between two limits, for your indicator or Expert Advisor. You push each new value and read how many values fall in each bin, how many are under or over the limits and which bin holds the most, over all the values or over the last n, without writing the counting yourself.
How it works
- Call DxHbCreate with the number of bins, the lower and upper limit and the window (0 counts every value). You get a handle.
- Call DxHbPush for each new value (for example the change of each bar).
- Read DxHbBinCount for any bin, DxHbFrequency and DxHbCumulative for percents and running totals, DxHbMode for the bin with the most values, DxHbUnder and DxHbOver for the values outside the limits.
- For the number of bins, DxHbSturges and DxHbFdBins give the usual rules. DxHbFromArray counts a whole array at once.
What you get
- Bins and counts: the count and the percent of every bin, the values under and over the limits, the bin with the most values.
- Sliding window: count all the values, or only the last n: the oldest value leaves its bin when a new one comes in.
- Number of bins: the Sturges rule and the Freedman-Diaconis rule.
- Clear edges: each bin takes its lower edge and not its upper edge; the last bin also takes the upper limit.
- Checked: the example EA compares every bin count at every bar with a separate count against the edges of the bins, over all values and over two windows, with a large offset, and on a long synthetic series.
- Status codes with plain-English texts: bad setup, bad handle, bad bin, 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 DinexHistogram.ex5 to MQL5\Libraries.
- Add the import block with the functions you use (shown in the images and in the example EA).
- Open a histogram, push each new value, read the counts.
Definitions (b bins, lower limit lo, upper limit hi, width w = (hi − lo) / b)
- Bin i takes lo + i x w ≤ x < lo + (i + 1) x w for i = 0 to b − 2. The last bin takes lo + (b − 1) x w ≤ x ≤ hi.
- Under: values below lo. Over: values above hi. They belong to no bin.
- Frequency: 100 x (values in the bin) / (all values held, under and over included). Cumulative: the values in bins 0 to i.
- Mode: the bin with the most values; the lowest bin of a tie; none when no value is inside the limits.
- Sturges: bins = k + 1 where k is the smallest whole number with 2^k ≥ n. Freedman-Diaconis: width = 2 x IQR / n^(1/3), bins = the range divided by the width, rounded up.
Example EA (included in the description images)
On every new bar the example pushes the change of the close in points into four histograms (all values in 32 bins, a window in 7 bins, a larger window in 32 bins and a window with a large offset) and compares every count with a separate count. It also runs results worked out by hand, the rules for the number of bins, 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 histogram of past values describes those values.
- A value that lies exactly on the edge of two bins may fall in the next bin when the edge is not an exact binary number (for example 0.1), because of floating-point rounding.
- Up to 256 instances, up to 10000 bins, window up to 1000000 values.
Requirements
- MetaTrader 5, any symbol and timeframe.
