Permutations and Combinations
This professional-grade solution for MetaTrader 5 helps traders achieve greater efficiency in their daily workflow. This library provides a collection of modular, reusable code. It is utilized by developers to organize common functions, allowing for the integration of complex logic across multiple Expert Advisors, indicators, or scripts without the need for code duplication.
How to Setup and Use Permutations and Combinations
1. Storage: Place library files in the MQL/Libraries directory to ensure they are accessible to your projects.
2. Implementation: Include the library in your code using the #import directive, ensuring you match the exact function names and parameters.
3. Compilation: Ensure the library is present in the directory before you compile your main EA or script, as the compiler links them during this phase.
4. Management: Keep libraries organized in sub-folders if you manage many custom functions to maintain a clean project structure.
Frequently Asked Questions
Q: What is a library file used for? A: Libraries store reusable code modules, allowing you to centralize common logic used by multiple EAs or indicators.
Q: Is a library executable? A: No, libraries are non-executable files containing functions; they must be imported into an EA, indicator, or script to function.
Q: Can I update a library while the platform is running? A: You should compile your EA or script after updating a library to ensure the latest code changes are integrated.
Description & Settings
It doesn't happen very often, but every once in awhile one needs to iterate over all of the combinations or permutations of a set of objects. Or more specifically, given a set of N objects, you want to consider k of them at a time (for each combination or permutation).
Practically, combinations and permutations algorithms can be used as a brute-force approach to solve certain problems, by trying every possible combination or permutation, until the optimal solution is found.
The circular k-permutations algorithm can be used in real world to solve parts of the Traveling Salesman Problem (TSP) or other similar problems like finding the currencies candidate for Triangular Arbitrage in Forex.
Below is a solution to each of these problems and more. The following generic algorithms permit to visit every combination or permutation of a sequence of length N, taken k items at time.
Performance Considerations
Every effort has been made to make this API library as fast as possible. Many algorithms and libraries for combinations/permutations were reviewed, and lot of benchmarks, code profiling and micro-optimizations were performed.
The Heap's algorithm in this library was re-implemented in a more efficient way that provides faster performance than its standard implementation.
The script 'CurrencyArbitrage_demo.mq5'The included script scans all the possible 28 currency pairs from the 8 major currencies in Forex (usd, eur, gbp, aud, nzd, jpy, cad, chf). If an arbitrage opportunity that satisfies the profit threshold is found, it will be written to the the 'Experts' log along with the potential profit rate.The script is a demonstration code that could be further implemented into an expert advisor / indicator to scan the market continuously for arbitrage opportunities.