Question / Array Cutting/Resize

 

I would like to cutting the array to keep fresh 1,000 elements and discard old 1,000 elements. The following code works in MT4, but it seems not to be work in MT5.

Is there any method? Thanks.

//if the array size more than 2,000, then decrease array size to 1,000
int ArrayCutting()
{
   int size_limit=2000,size_new=1000;
   int size=ArraySize(m_wavePr_pt);
   
   if(size>size_limit)
   {
      ArraySetAsSeries(m_wavePr_pt,true);
      
      ArrayResize(m_wavePr_pt,size_new);
      
      ArraySetAsSeries(m_wavePr_pt,false);
   }
   return(0);
}
 
remoton:

I would like to cutting the array to keep fresh 1,000 elements and discard old 1,000 elements. The following code works in MT4, but it seems not to be work in MT5.

Is there any method? Thanks.


If the resize does not works, then find out what error did you get and read the doc about ArrayResize , there you can read that you have to declare your array as a dynamic array.

And you don't have to set - reset the array to timeseries just to resize it.  

if (ArrayResize (my_arr, 100)) == -1)
   {
   Print (GetLastError());
   }


 
phi.nuts:

If the resize does notworks, then find out what ArrayResize do, there you can read that you have to declare your array as a dynamic array.

And you don't have to set - reset the array to timeseries.  

 

Thanks, phi.nuts.

The array declared as dynamic array. In MT4, above code keeps the last added 1,000 elements after ArrayResize(...) statement. But in MT5, after ArrayResize(...) statement, there is old elements in the array and newly added elements deleted. Anyway, forget the code. Is there any method to keep the last added elements  when contracting the array?

 

Why don't you use ArrayCopy to temp array then re-cooy to your original one?
 
codersguru:

Why don't you use ArrayCopy to temp array then re-cooy to your original one?

Many thanks codersguru,

solved :

 

int ArrayCutting()
{
   int size_limit=2000,size_new=1000;
   int size=ArraySize(m_wavePr_pt);
   double   tempPr[];
   
   if(size>size_limit)
   {
      ArrayCopy(tempPr,m_wavePr_pt,0,size_new+1,size_new);
      ArrayResize(m_wavePr_pt,size_new);
      ArrayCopy(m_wavePr_pt,tempPr,0,0,size_new);
   }
   return(0);
}
 
remoton:

Many thanks codersguru,

solved :

 

You're welcome!