Here is a simple library that works with MQL4 and/or MQL5 that extends the usefulness of the MQL standard lib, more specifically CArrayObj which is a container class for a dynamic array of object pointers. This lib allows you to declare object pointer vectors similar to C++ and reference the pointer directly from the [] operator (all objects must inherit CObject directly or indirectly) which gives the developer the option to create n dimensional matrices of object pointers.
Since this inherits all the methods of CArrayObj, you will want to reference the documentation for that class to understand its capabilities. https://www.mql5.com/en/docs/standar...ures/carrayobj
The applications are endless but an example would be creating multiple baskets of currency pairs.
Since this inherits all the methods of CArrayObj, you will want to reference the documentation for that class to understand its capabilities. https://www.mql5.com/en/docs/standar...ures/carrayobj
The applications are endless but an example would be creating multiple baskets of currency pairs.
Inserted Code
//+------------------------------------------------------------------+ //| sorting objects.mq5 | //| nicholishen | //| www.reddit.com/u/nicholishenFX | //+------------------------------------------------------------------+ #property copyright "nicholishen" #property link "www.reddit.com/u/nicholishenFX" #property version "1.00" #include <Arrays\objvector.mqh> #define SORT_ASCENDING 1 #define SORT_DESCENDING 2 class MySymbol : public CObject { public: string name; double bid; MySymbol(string symbol): name(symbol), bid(::SymbolInfoDouble(symbol,SYMBOL_BID)) {} virtual int Compare(const CObject *node,const int mode=0) const override; }; // override CObject compare method for object sorting int MySymbol::Compare(const CObject *node,const int mode=0)const { MySymbol *that = (MySymbol*)node; int comp = ::StringCompare(this.name,that.name); if(mode == SORT_DESCENDING) return -comp; else return comp; } //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- Print("-----BEGIN-----"); //declaring a vector of vectors (matrix) objvector<objvector<MySymbol>> myMatrix; //declating a pointer to a vector of MySymbols and creating the first object in memory objvector<MySymbol> *myVector = new objvector<MySymbol>; myVector.Add(new MySymbol("USDJPY")); myVector.Add(new MySymbol("CADJPY")); myVector.Add(new MySymbol("GBPJPY")); myVector.Add(new MySymbol("EURUSD")); myMatrix.Add(myVector); myVector = new objvector<MySymbol>; myVector.Add(new MySymbol("GBPUSD")); myVector.Add(new MySymbol("EURCAD")); myMatrix.Add(myVector); myVector = new objvector<MySymbol>; myVector.Add(new MySymbol("GBPUSD")); myVector.Add(new MySymbol("CADJPY")); myVector.Add(new MySymbol("EURUSD")); myMatrix.Add(myVector); for(int i=0;i<myMatrix.Total();i++) { myMatrix[i].Sort(SORT_DESCENDING); Print("----Group ",i+1," -----"); for(int j=0;j<myMatrix[i].Total();j++) { Print("[",i,"][",j,"] ",myMatrix[i][j].name, " " , myMatrix[i][j].bid); } } Print("-----END-----"); } //+------------------------------------------------------------------+ /* result -----BEGIN----- ----Group 1 ----- [0][0] USDJPY 110.205 [0][1] GBPJPY 140.556 [0][2] EURUSD 1.11932 [0][3] CADJPY 81.876 ----Group 2 ----- [1][0] GBPUSD 1.27458 [1][1] EURCAD 1.50764 ----Group 3 ----- [2][0] GBPUSD 1.27458 [2][1] EURUSD 1.11932 [2][2] CADJPY 81.876 -----END----- */
Attached File(s)
objvector.mqh
< 1 KB
|
332 downloads