Array values on ArraySetAsSeries

 

Hi


I have set a double array with the AS_SERIES flag using the ArraySetAsSeries function, but when I add a new value to array[0] the older values does not distribute to the other indexes, in fact it apears a loong number. See below the first 4 indexes


[0] 3315.0 (this is right)

[1] 9.881312916824931e-324

[2] 9.881312916824931e-324

[3] 9.881312916824931e-324


am I doing something wrong?


It is a dynamic array, and I'm using the ArrayResize function to add one index before througing any new values.


Thank you



 
Show your code if you need coding help.
 
mcronline:

Hi


I have set a double array with the AS_SERIES flag using the ArraySetAsSeries function, but when I add a new value to array[0] the older values does not distribute to the other indexes, in fact it apears a loong number. See below the first 4 indexes


[0] 3315.0 (this is right)

[1] 9.881312916824931e-324

[2] 9.881312916824931e-324

[3] 9.881312916824931e-324


am I doing something wrong?


It is a dynamic array, and I'm using the ArrayResize function to add one index before througing any new values.


Thank you



If you need to append an array with a new value, then that element always goes to the end. You might find it much easier to use the std lib array class instead where you can insert an element at a specified index and the remainder of the array adjusts to accommodate the new element. 

#include <Arrays\ArrayDouble.mqh>
void OnStart()
{
   double test_numbers[] = {1.,2.,3.,4.,5.};
   CArrayDouble array;
   array.AssignArray(test_numbers);
   array.Insert(0., 0);
   
   for(int i=0;i<array.Total();i++)
      printf("%f",array[i]);
}
https://www.mql5.com/en/docs/standardlibrary/datastructures/carraydouble
Documentation on MQL5: Standard Library / Data Collections / CArrayDouble
Documentation on MQL5: Standard Library / Data Collections / CArrayDouble
  • www.mql5.com
The CArrayDouble class provides the ability to work with a dynamic array of double variables. The class allows adding/inserting/deleting array elements, performing an array sorting, and searching in a sorted array. In addition, methods of working with files have been implemented.
 
Alain Verleyen:
Show your code if you need coding help.
renko_close[0] holds a tick price
double tops[];

ArrayResize(tops,2);

ArraySetAsSeries(tops,true);

tops[0] = 0;
tops[1] = 0;

ArrayResize(tops,ArraySize(tops) + 1);
tops[0] = renko_close[0]
 
mcronline:

Like I mentioned the new element is going to always be at the end. So either you need to manually shift your entire array or use the CArrayDouble with CArrayDouble::Insert method.


...or even better just add your new value to the end of the array and loop backward instead.

 
nicholishen:

Like I mentioned the new element is going to always be at the end. So either you need to manually shift your entire array or use the CArrayDouble with CArrayDouble::Insert method.

Thats not what this doc says


https://www.mql5.com/en/articles/567


Look for Array Indexing Order. It shows this example


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

"After the execution of this code, the values contained in the array should be 3, 2 and 1."

MQL5 Programming Basics: Arrays
MQL5 Programming Basics: Arrays
  • 2013.03.11
  • Dmitry Fedoseev
  • www.mql5.com
Arrays are an integral part of almost any programming language along with variables and functions. Many novice programmers are often afraid of arrays. It sounds strange but it is true! I can assure you that they are not scary at all. In fact, arrays are similar to regular variables. Without going into detail about the notation peculiarities...
 
mcronline: hen I add a new value to array[0] the older values does not distribute to the other indexe
Indicators have buffers which automatically shift. Arrays do not automatically do anything. You must do it in EA.
          Array indexing and resizing an AS_SERIES array - Indexes - MQL4 and MetaTrader 4 - MQL4 programming forum
 
nicholish en #:

If you need to append an array with a new value, then that element always goes to the end. You might find it much easier to use the std lib array class instead where you can insert an element at a specified index and the remainder of the array adjusts to accommodate the new element. 

https://www.mql5.com/en/docs/standardlibrary/datastructures/carraydouble
mcronline #:

Thats not what this doc says


https://www.mql5.com/en/articles/567


Look for Array Indexing Order. It shows this example


"After the execution of this code, the values contained in the array should be 3, 2 and 1."

You are talking about apples and oranges. Also the code is flawed, have you tried it yourself? Because it does not do what was advertised. Ask yourself what you would have to add so that the values would shift as you want.

A tip, you can shift the values manually but when you know what you are doing you will automate it with a loop.

Other than that the article is very useful for beginners. Everybody makes mistakes. Comment to the author, I am sure he will check and correct it.

Reason: