I wanted to post up this example since I racked my head over its implementation for hours. This example demonstrates how to create jagged object dynamic arrays using the CArrayObj and CObject classes in the MQL4/5 standard library.
Tags: MQL4 MQL5 dynamic two-dimensional jagged object array standard library memory management
Inserted Code
//+------------------------------------------------------------------+ //| TwoDimensionalObjectArrayExample.mq5 | //| nicholishen | //| www.reddit.com/u/nicholishenFX | //+------------------------------------------------------------------+ #property copyright "nicholishen" #property link "www.reddit.com/u/nicholishenFX" #property version "1.00" #include <Arrays\ArrayObj.mqh> #define SORT_ASCENDING 0 #define SORT_DESCENDING 1 //+------------------------------------------------------------------+ //| Example Object | //+------------------------------------------------------------------+ class CSymbol : public CObject { private: string m_symbol; public: CSymbol(const string symbol):m_symbol(symbol){} string Symbol() const { return m_symbol; } double Ask() const { return SymbolInfoDouble(m_symbol,SYMBOL_ASK); } double Bid() const { return SymbolInfoDouble(m_symbol,SYMBOL_BID); } //--- Override the Compare method of CObject. It must have the same signature! int Compare(const CObject *node,const int mode=0) const override; }; int CSymbol::Compare(const CObject *node,const int mode=0) const override // This is the overridden compare method called by QuickSort() { //CSymbol *nodeCast = (CSymbol*)node; // Casting CObject pointer to pointer of this class can make code easier to read int compare = StringCompare(this.Symbol(),((CSymbol*)node).Symbol()); // Comparing string value for alphabetical sort return mode == SORT_ASCENDING ? compare : compare == 0 ? compare : compare == 1 ? -1 : 1; } //+------------------------------------------------------------------+ //| Example Group of Objects | //+------------------------------------------------------------------+ class CSymbolGroup : public CArrayObj // Inherits CArrayObj { public: //--- Add Symbol objects to dynamic array and sorts the array alphabetically void Add(const string symbol) { this.Add(new CSymbol(symbol)); this.Sort(SORT_ASCENDING);} //--- This method allows you to use [] like in a normal array //--- Return type needs to be the name of the class stored in array CSymbol *operator[](const int index) const { return((CSymbol*)At(index)); } }; //+------------------------------------------------------------------+ //| Example Array of Groups of Objects | //+------------------------------------------------------------------+ class CSymbolMaster : public CArrayObj { public: CSymbolGroup *operator[](const int index) const { return((CSymbolGroup*)At(index)); } }; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { CSymbolMaster Master; CSymbolGroup *group; //--- Group 1 group = new CSymbolGroup; group.Add("EURUSD"); group.Add("USDJPY"); group.Add("EURGBP"); Master.Add(group); //--- Group 2 group = new CSymbolGroup; group.Add("EURJPY"); group.Add("GBPJPY"); group.Add("CADCHF"); group.Add("CHFJPY"); group.Add("AUDNZD"); group.Add("CADJPY"); group.Add("EURAUD"); Master.Add(group); //--- for each element in master for(int i = 0; i < Master.Total();i++) { Print("Group ",i+1," has (",Master[i].Total(),") members"); //--- for each element in symbols group for(int j = 0; j < Master[i].Total();j++) { Print(Master[i][j].Symbol() , " Bid = ",Master[i][j].Bid() , " | Ask = ",Master[i][j].Ask() ); } } } //+------------------------------------------------------------------+ /*----------OUTPUT------------------ Group 1 has (3) members EURGBP Bid = 0.85112 | Ask = 0.85119 EURUSD Bid = 1.06431 | Ask = 1.06438 USDJPY Bid = 113.499 | Ask = 113.506 Group 2 has (7) members AUDNZD Bid = 1.06696 | Ask = 1.06714 CADCHF Bid = 0.76815 | Ask = 0.76833 CADJPY Bid = 87.16 | Ask = 87.178 CHFJPY Bid = 113.458 | Ask = 113.474 EURAUD Bid = 1.37971 | Ask = 1.37996 EURJPY Bid = 120.799 | Ask = 120.809 GBPJPY Bid = 141.923 | Ask = 141.94 */
Tags: MQL4 MQL5 dynamic two-dimensional jagged object array standard library memory management
Attached File(s)
TwoDimensionalObjectArrayExample.mq5
5 KB
|
402 downloads