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
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 |//+------------------------------------------------------------------+intOnInit()
{
//--- indicator buffers mapping//---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &time[],
constdouble &open[],
constdouble &high[],
constdouble &low[],
constdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &spread[])
{
//---double ar[]; // ArrayArrayResize(ar,2); // Prepare the array
ar[0]=1; // Set the values
ar[1]=2;
ArraySetAsSeries(ar,true); // Change the indexing orderArrayResize(ar,3); // Increase the array size
ar[0]=3; // Set the value for the new array elementAlert(ar[0]," ",ar[1]," ",ar[2]); // Print array valuesArraySetAsSeries(ar,false);
Alert("Normal indexing: ", ar[0]," ",ar[1]," ",ar[2]); // Print array values//--- return value of prev_calculated for next callreturn(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 |//+------------------------------------------------------------------+intOnInit()
{
//--- indicator buffers mapping//---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &time[],
constdouble &open[],
constdouble &high[],
constdouble &low[],
constdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &spread[])
{
//---double ar[]; // ArrayArrayResize(ar,2); // Prepare the array
ar[0]=9; // Set the values
ar[1]=8;
ArrayResize(ar,6); // Increase the array sizeAlert("Normal resize: ", ar[0]," ",ar[1]," ",ar[2]," ", ar[3], " ", ar[4], " ", ar[5]);
ArraySetAsSeries(ar,true); // Change the indexing orderArrayResize(ar,4); // Increase the array sizeArraySetAsSeries(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 elementAlert("Modify the first as serlies: ", ar[0]," ",ar[1]," ",ar[2]," ", ar[3]); // Print array values//--- return value of prev_calculated for next callreturn(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 |//+------------------------------------------------------------------+intOnInit()
{
//--- indicator buffers mapping//---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &time[],
constdouble &open[],
constdouble &high[],
constdouble &low[],
constdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &spread[])
{
//---double ar[]; // ArrayArrayResize(ar,2); // Prepare the array
ar[0]=1; // Set the values
ar[1]=2;
ArrayResize(ar,3); // Increase the array sizeAlert("Redimension to 3 normal: ", ar[0], " ", ar[1], " ", ar[2]);
ArraySetAsSeries(ar,true); // Change the indexing orderAlert("Redimension to 3 series: ", ar[0], " ", ar[1], " ", ar[2]);
ar[0]=8; // Set the value for the new array elementAlert("Change the first element: ", ar[0]," ",ar[1]," ",ar[2]); // Print array valuesArraySetAsSeries(ar, false);
Alert("Normal renew: ", ar[0]," ",ar[1]," ",ar[2]); // Print array values//--- return value of prev_calculated for next callreturn(rates_total);
}
//+------------------------------------------------------------------+
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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
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:
In added the function arraySetAsSeries() it seems be affected by function arrayresieze, se this example: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: