Download MetaTrader 5

The fastes shift all element on Array to left (shift one position inside array)

To add comments, please log in or register
Do you know that MQL5 can create custom graphical tools?
Pavel Soukup
560
Pavel Soukup 2013.09.22 11:06 

Hi guys,

I have array, 100 elements and I need shift all elements left, index 99 bye bye :-) from index 98 to index 99, 97->98, 2->3 .... index 0 = zero

What is the fastes solution for it? Exist any special mql function?

Or basic solution ...:

int count=ArraySize(array);
for(int i=count-2; i>1; i--) array[i]=array[i-1];
array[0]=0;
whroeder1
14512
whroeder1 2013.09.22 13:08  
From my code
bool     ResizeBuffer(double& buffer[], int size){ // buffer can be 1D or 2D
   if(ArrayRange(buffer,0) != size){      // ArrayRange allows 1D or 2D arrays
      ArraySetAsSeries(buffer, false);    // Shift values B[2]=B[1]; B[1]=B[0]
      if( !MyArrayResize(buffer, size, "ResizeBuffer") )        return(false);
      ArraySetAsSeries(buffer, true);
   }
   return(true);
}
bool     MyArrayResize(double& arr[], int nRows, string msg){
   if( ArrayResize(arr, nRows) >= nRows)  return(true);
   DisableTrading( "ArrayResize("+nRows+") Failed: "+GetLastError() +" "+msg);
   return(false);
}
////////////////
double Typical[]; datetime TypicalCounted=0;
int start(){
  ResizeBuffer(Typical, Bars); 
  for(int iBar=iBarShift(NULL,0,TypicalCounted); iBar >=0; iBar--) 
     Typical[iBar] = (High[iBar]+Low[iBar]+Close[iBar])/3;
  TypicalCounted = TimeCurrent();

  Print("3 bars ago typical price was "+PriceToStr(Typical[3]));
No shifting required.
GreenMoney
379
GreenMoney 2013.09.22 14:02  
endy5:

I have array, 100 elements and I need shift all elements left, index 99 bye bye :-) from index 98 to index 99, 97->98, 2->3 .... index 0 = zero

What is the fastes solution for it? Exist any special mql function?

I don't know about the fastest, but you could simply use ArrayCopy() to shift the elements:

int count = ArraySize(array);
if (ArrayCopy(array, array, 1, 0, count - 1) == count - 1)
   array[0] = 0;
Roel
485
Roel 2015.04.01 19:18  
Old thread, I know, but this may help someone. It seems like the original code listed has "-2" and that should read "-1"  and it has ">1" which should read ">=1" - correct?
To add comments, please log in or register