Loading…

Array Utilities

Arrays on this platform come with resizing, copying and a built-in sort, but nothing for the reductions that show up in almost every EA that tracks a basket of tickets or a short price history: a sum, a mean, "which index is the minimum", or "is this value already in the list". Everyone reimplements these, and the membership test is the one that is usually wrong - comparing two doubles with == after they arrived by different arithmetic paths fails more often than it succeeds, because a lot size or price rarely round-trips to the exact same bit pattern twice. Every comparison-based function here (ArrayIndexOfD, ArrayContainsD, ArrayEqualsD) takes an epsilon and defaults to a sane one, so "is 0.1 already in this list of lot sizes" behaves the way a trader expects rather than the way IEEE 754 technically defines it. The reducers treat an empty array as a normal input, not an error condition. Sum of nothing is 0.0. Mean of nothing is 0.0, not a divide-by-zero. Min and max of nothing return false and leave the output index at -1, so a caller who forgets to check the return value gets an obviously invalid index rather than a stale value left over from a previous call. ArraySliceD clamps an out-of-range request instead of failing it - asking for the last 20 elements of a 12-element array returns all 12, which is what "give me everything I have" should mean in practice. ArrayReverseCopyD exists because the platform's own ArrayReverse() reverses in place; sometimes you need the original order and the reversed order side by side, and mutating the only copy you have forces an extra manual copy at every call site. Every function that needs one is written twice - once for double, once for int - rather than forcing a caller to pretend a ticket array is a price array. Notes and limitations - ArrayUniqueD is O(n^2). Fine for basket-sized state (tens of tickets, dozens of levels); not intended for deduplicating a tick series. - Sorting is out of scope here on purpose - see the separate ArraySort library for quicksort, argsort and binary search. - ArraySliceD and ArrayConcatD resize the destination array themselves; do not pass an array that also appears as a source, the result is undefined under the platform's pass-by-reference array semantics. - All double comparisons use an epsilon and therefore have a false-positive radius. Two genuinely distinct lot sizes closer together than the epsilon will be treated as the same value; the default of 1e-8 is far tighter than any real lot step, but pass a wider one deliberately if your domain needs it (currency pips, for instance).

Script · MT4 · v1.0 · Free

About Array Utilities

Array Utilities is a script for MetaTrader 4. MQL source code included.

Files in the download: ArrayUtils.mqh, ArrayUtils.txt, ArrayUtilsDemo.mq4.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 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: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria