Loading…

Combinatorics Toolkit

The textbook way to enumerate combinations or permutations is recursive backtracking - simple to write, but it rebuilds the call stack for every single item, and it cannot be paused mid-enumeration without carrying that stack around by hand. For "try every 3-symbol subset of my 12-symbol watchlist" or "try every ordering of these 5 pending orders", an EA usually wants to advance one combination per tick to stay responsive, rather than block on a recursive generator that enumerates the whole set in one call. Both algorithms here are the standard lexicographic "next" step done in place: NextCombination advances a strictly-increasing index array to the next k-subset in lexicographic order (find the rightmost index that can still be incremented, bump it, then reset everything to its right to run consecutively), and NextPermutation is the classic algorithm taught for in-place next-permutation (find the rightmost ascent, swap it with the smallest larger value to its right, then reverse the descending suffix). Both run in O(n) worst case per step, hold their entire state in one caller-owned array, and never recurse. CombinationCount and PermutationCount compute C(n,k) and n! via a running product rather than factorials - factorials of even moderate n overflow a double far sooner than the multiplicative running product does. Notes and limitations - NextCombination and NextPermutation both operate on caller-owned arrays in place and return false once the sequence is exhausted - the caller is responsible for calling FirstCombination/FirstPermutation first and checking the return value of every subsequent Next call, same pattern as a standard do/while enumeration loop. - CombinationCount and PermutationCount return a double, which starts losing exact integer precision beyond 2^53 (about 9 quadrillion) and overflows to infinity beyond roughly 170! - genuinely large factorial counts need BigIntegerArithmetic.mqh (also in this catalogue) instead. - Enumerating a large n choose k (e.g. more than a few hundred thousand combinations) is still an O(number of combinations) walk by nature of the problem - this header makes each step cheap and non-recursive, it does not change the combinatorial explosion itself.

Script · MT4 · v1.0 · Free

About Combinatorics Toolkit

Combinatorics Toolkit is a script for MetaTrader 4. It shows several symbols in one panel. MQL source code included.

Files in the download: CombinatoricsToolkit.mqh, CombinatoricsToolkit.txt, CombinatoricsToolkitDemo.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: Pending Orders · Dashboards & Scanners · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria