MQL4 prepend value to array

Back to topics list To post a new topic, please log in or register
avatar
6
DutchKevv 2015.12.15 14:21
 

Hi there,

 

This is my first post, so I hope its gonna make any sense :) 

 

How can I have a reversed Array (like a timeseries), and prepend a new element into the Array and throw the last one out (if over max-length).

 So Array[0] is the newest value and Array[49] is the last value..

I tried like this ->

 

double myArray = []; // Dynamic array
extern int maxArrayLength = 50;
bool prependToReversedDoubleArray(double& theArray[], double value, int maxLength) {
   
   int size = ArraySize(theArray);
   
   // Normalize the array (left to right)
   ArraySetAsSeries(theArray, false);
   
   // Extend array length
   ArrayResize(theArray, size+1);
   
   Alert("test = ", size);
   
   // Insert the new value
   theArray[size] = value;
   
   // Reverse the array again
   ArraySetAsSeries(theArray, true);
   
   if (ArraySize(theArray) > maxLength) {
      ArrayResize(theArray, maxLength);
   }
   
   return(true);
}
prependToReversedDoubleArray(myArray, 0.1234, maxArrayLength)

 

 But it will always Alert the array has a length of 2 :s 

 Alert("test = ", size); // 2

 Why is it so damn hard to create an Array :(

 

 

 

 

avatar
12985
WHRoeder 2015.12.15 14:35 #
 
Your code as written is correct.
avatar
6
DutchKevv 2015.12.15 14:42 #
 

That is very nice to hear indeed :p Thanks

 

But it does give problems, like length is always 2..

 

The length should be growing until [maxArrayLength] 50.. Do you have any idea why it never gets longer then 2?

 

Thanks! 

avatar
12985
WHRoeder 2015.12.15 15:05 #
 
DutchKevv: Do you have any idea why it never gets longer then 2?
There are no mind readers here. The posted code is correct. Therefor the problem is elsewhere.
avatar
6
DutchKevv 2015.12.15 15:15 #
 
WHRoeder:
DutchKevv: Do you have any idea why it never gets longer then 2?
There are no mind readers here. The posted code is correct. Therefor the problem is elsewhere.

Roger. 

 

Thanks for reviewing the code.. Good to know i'm on the right track. 

 

Thanks 

avatar
Moderator
3404
GumRai 2015.12.15 23:34 #
 
double myArray = []; // Dynamic array

I don't know how your code could compile

should be

double myArray[]; // Dynamic array

 As WH says, problem is elsewhere. You are probably either modifying the value of maxArrayLength or resizing the array in another section of code.

avatar
6
DutchKevv 2015.12.18 15:09 #
 

Hi @GumRai, 

 

You are correct, it wasn't part of the code, I just typed it for this Forum post..

 

Anyway, it is fixed :) @WHRoeder, you were right to. The problem was somewhere else.. The code worked fine.

 This is the final code I use ->

 

int prependToReversedDoubleArray(double &theArray[],double value,int maxLength) 
  {

   int newSize=ArraySize(theArray)+1;

// Normalize the array (left to right)
   ArraySetAsSeries(theArray,false);

// Extend array length
   ArrayResize(theArray,newSize);

// Insert the new value
   theArray[newSize-1]=value;

// Reverse the array again
   ArraySetAsSeries(theArray,true);

// Check if the max length is reached
   if(maxLength>0 && newSize>maxLength) 
     {
      ArrayResize(theArray,maxLength);
      newSize=maxLength;
     }

   return(newSize);
  }

 

Thanks! 

Back to topics list  

To add comments, please log in or register