Join our fan page
- Views:
- 3419
- Rating:
- Published:
- 2020.12.26 17:01
- Updated:
- 2021.04.02 23:05
-
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance
A sorter class to sort an array based on other arrays.
//+------------------------------------------------------------------+ //| class MultiSort<TItem,TKey1,TKey2,TKey3>. | //| Usage: Sorter class to sort an array based on other arrays. | //+------------------------------------------------------------------+ template<typename TItem,typename TKey1,typename TKey2,typename TKey3> class MultiSort { public: //--- method to sort items by keys void SortBy(TItem& items[], TKey1& keys1[], TKey2& keys2[], TKey3& keys3[], bool ascending_order1=true, bool ascending_order2=true, bool ascending_order3=true); };
Note:
Empty keys arrays are ignored by the sorter.
For example to sort position tickets by position open time (as the only sort key):
//--- sort tickets by position open time only MultiSort<long,datetime,int,int> sorter; int empty[]; sorter.SortBy(tickets,times,empty,empty);
Here is a complete example
//+------------------------------------------------------------------+ //| MultiSort_demo.mq5 | //| Copyright © 2018, Amr Ali | //| https://www.mql5.com/en/users/amrali | //+------------------------------------------------------------------+ #property copyright "Copyright © 2018, Amr Ali" #property description "Multi sort example - Sort position tickets by symbol then by open time then by open price in accending or descending order." #property version "1.000" #property script_show_inputs #include <MultiSort.mqh> //--- input variables input bool mode_asc_symbols = true; // ascending order for symbols input bool mode_asc_times = true; // ascending order for time input bool mode_asc_prices = true; // ascending order for price //--- global variables long tickets[]= {88806796,90462225,91039722,91200504,90389608,88429143,89452623,91487721,89323610,90439077,89355691,88943822}; string symbols[]= {"USDCAD","EURCHF","GBPCHF","EURUSD","EURUSD","EURCHF","AUDUSD","EURUSD","EURUSD","GBPCHF","EURCHF","AUDUSD"}; datetime times[]= {D'2020.12.07 09:31:40',D'2020.12.10 09:55:30',D'2020.12.07 03:16:05',D'2020.12.07 03:45:39',D'2020.12.09 14:34:42', D'2020.12.08 14:00:13',D'2020.12.07 12:34:42',D'2020.12.08 17:37:28',D'2020.12.10 02:34:55',D'2020.12.09 12:00:09', D'2020.12.10 17:56:09',D'2020.12.07 08:30:14'}; double prices[]= {1.24428,1.35444,1.37244,1.28973,1.39795,1.39960,1.15292,1.19290,1.11015,1.17936,1.37226,1.34722}; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { Print("-------before sort-------"); for(int i=0; i<ArraySize(tickets); i++) PrintFormat("%8i | %8s | %8s | %8.5f",tickets[i],symbols[i],(string)times[i],prices[i]); //--- sort position tickets by symbol, open time and open price. MultiSort<long, string, datetime, double> multi_sorter; multi_sorter.SortBy(tickets, symbols, times, prices, mode_asc_symbols, mode_asc_times, mode_asc_prices); Print("-------MultiSort.SortBy(tickets,symbols,times,prices)-------"); for(int i=0; i<ArraySize(tickets); i++) PrintFormat("%8i | %8s | %8s | %8.5f",tickets[i],symbols[i],(string)times[i],prices[i]); } //+------------------------------------------------------------------+ /* Sample output -------before sort------- 88806796 | USDCAD | 2020.12.07 09:31:40 | 1.24428 90462225 | EURCHF | 2020.12.10 09:55:30 | 1.35444 91039722 | GBPCHF | 2020.12.07 03:16:05 | 1.37244 91200504 | EURUSD | 2020.12.07 03:45:39 | 1.28973 90389608 | EURUSD | 2020.12.09 14:34:42 | 1.39795 88429143 | EURCHF | 2020.12.08 14:00:13 | 1.39960 89452623 | AUDUSD | 2020.12.07 12:34:42 | 1.15292 91487721 | EURUSD | 2020.12.08 17:37:28 | 1.19290 89323610 | EURUSD | 2020.12.10 02:34:55 | 1.11015 90439077 | GBPCHF | 2020.12.09 12:00:09 | 1.17936 89355691 | EURCHF | 2020.12.10 17:56:09 | 1.37226 88943822 | AUDUSD | 2020.12.07 08:30:14 | 1.34722 -------MultiSort.SortBy(tickets,symbols,times,prices)------- 88943822 | AUDUSD | 2020.12.07 08:30:14 | 1.34722 89452623 | AUDUSD | 2020.12.07 12:34:42 | 1.15292 88429143 | EURCHF | 2020.12.08 14:00:13 | 1.39960 90462225 | EURCHF | 2020.12.10 09:55:30 | 1.35444 89355691 | EURCHF | 2020.12.10 17:56:09 | 1.37226 91200504 | EURUSD | 2020.12.07 03:45:39 | 1.28973 91487721 | EURUSD | 2020.12.08 17:37:28 | 1.19290 90389608 | EURUSD | 2020.12.09 14:34:42 | 1.39795 89323610 | EURUSD | 2020.12.10 02:34:55 | 1.11015 91039722 | GBPCHF | 2020.12.07 03:16:05 | 1.37244 90439077 | GBPCHF | 2020.12.09 12:00:09 | 1.17936 88806796 | USDCAD | 2020.12.07 09:31:40 | 1.24428 */
![EA Fibonacci Potential Entry - MT5](https://c.mql5.com/i/code/expert.png)
The 8 effective steps to build a robust day trading plan using Fibonacci retracement
![Free mt5 Copier](https://c.mql5.com/i/code/expert.png)
Copy trading has become such a critical feature of forex trading. Some people see this as a potential business opportunity, while for opensource die-hards like me, we believe in giving back to the community rather than putting a price tag on everything 'nice'. So here it comes. A free opensource trade copier, which you are freely allowed to modify and distribute according to MIT license terms. It still has limited features, but the essentials like lot normalization are there. Please note that this copier only works for trading terminals installed on the same machine. Please share back any upgrades, enhancements or bug fixes to the discussion. Enjoy!
![Simple EA using Bollinger, RSI and MA](https://c.mql5.com/i/code/expert.png)
Ea working well on EURUSD1 H1 with initial parameters, using a simple strategy based on Bollinger bands and RSI.
![merge sort - a merging method comparison-based sorting algorithm](https://c.mql5.com/i/code/library.png)
an efficient, general-purpose sorting algorithm