Discussion of article "MQL5 Programming Basics: Arrays" - page 3

 
Ernie Gunning:

ok lets try the english version of MQL.


Thanks for your code post. It saved me time. I tried using the MQL arrays and they were confusing. I then was very depressed that I need to once again need to write basic structures that was supposed to be there BUT then I found your code that saved me some research time on arrays and how to make them grow\increase dynamically. AWESOME thanks.

I hope i can give back to you! the code below works for all data types. It will work on objects too but the Contains (search) method might not work. I only tested it on types (double, int, bool). String might give you problem as well and the code might need to be extended.



Then you can declare it for all types you need like so:




hope this helps anyone


Please disregard my suggestion above to use the Dynamic array for any data type. There is already a generic CArrayList declared. please use this. I encountered a problem using objects that was resolved on this thread: https://www.mql5.com/en/forum/358432

Using CArrayList gives error
Using CArrayList gives error
  • 2020.12.20
  • www.mql5.com
Hi Guys, Thanks for your time. I'm struggling to use the CArrayList in the generics folder. Are these interfaces and classes complete...
 
I have already read some good articles from you. This one was no exception.
 

It seems a error in the function arrayResize(), in the example the content of the array is 1, 1, 3 in normal indexing not 1, 2, 3. See this example: 

//+------------------------------------------------------------------+
//|                                                        false.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   double ar[]; // Array
ArrayResize(ar,2); // Prepare the array
ar[0]=1; // Set the values
ar[1]=2; 
ArraySetAsSeries(ar,true); // Change the indexing order
ArrayResize(ar,3); // Increase the array size
ar[0]=3; // Set the value for the new array element
Alert(ar[0]," ",ar[1]," ",ar[2]); // Print array values
ArraySetAsSeries(ar,false);
Alert("Normal indexing: ", ar[0]," ",ar[1]," ",ar[2]); // Print array values
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
In added the function arraySetAsSeries() it seems be affected by function arrayresieze, se this example: 
//+------------------------------------------------------------------+
//|                                                   errorindex.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   double ar[]; // Array
ArrayResize(ar,2); // Prepare the array
ar[0]=9; // Set the values
ar[1]=8; 
ArrayResize(ar,6); // Increase the array size
Alert("Normal resize: ", ar[0]," ",ar[1]," ",ar[2]," ", ar[3], " ", ar[4], " ", ar[5]);
ArraySetAsSeries(ar,true); // Change the indexing order
ArrayResize(ar,4); // Increase the array size
ArraySetAsSeries(ar, false);
Alert("See the random element added: ", ar[0]," ",ar[1]," ",ar[2]," ", ar[3]);
ArraySetAsSeries(ar,true); // Change the indexing order 0
ar[0]=8; // Set the value for the new array element
Alert("Modify the first as serlies: ", ar[0]," ",ar[1]," ",ar[2]," ", ar[3]); // Print array values
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Steps:

1. ar = {}
2. ar = {9, 8}

3. ar = {9, 8, 0, 8, 0, 0} 

4. Set as series true:

ar = {0, 0, 8, 0, 8, 9}

5. Resize to 4

ar = {0, 0, 8, 0}

6. Set as series false:

ar = {0, 8, 0, 0}

7. Set as series true:

ar = {0, 0, 8, 0}

8. Modify the first element a[0]

ar = {8, 0, 8, 0}


I don't know what array resize (3 -5) takes the values and copy in the new positions, 6 in forward takes random values, I think. I prefer resize() first and then setasseries() like this: 

//+------------------------------------------------------------------+
//|                                            indexingarraytest.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   double ar[]; // Array
ArrayResize(ar,2); // Prepare the array
ar[0]=1; // Set the values
ar[1]=2;
ArrayResize(ar,3); // Increase the array size
Alert("Redimension to 3 normal: ", ar[0], " ", ar[1], " ", ar[2]); 
ArraySetAsSeries(ar,true); // Change the indexing order
Alert("Redimension to 3 series: ", ar[0], " ", ar[1], " ", ar[2]); 
ar[0]=8; // Set the value for the new array element
Alert("Change the first element: ", ar[0]," ",ar[1]," ",ar[2]); // Print array values
ArraySetAsSeries(ar, false);
Alert("Normal renew: ", ar[0]," ",ar[1]," ",ar[2]); // Print array values
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+