The fastes shift all element on Array to left (shift one position inside array)
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.
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;
//+------------------------------------------------------------------+ //| arrayshift.mq5 | //| Copyright 2023, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2023, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- ulong array[]; addelements(array); shiftleft(array); } //+------------------------------------------------------------------+ void addelements(ulong &array[]) //Add 10 elements to a dynamic array { for(int i=0;i<10;i++) { ArrayResize(array,i+1,i+2); array[i]=i; } } void shiftleft(ulong &array[]) //remove first element then shift the whole array one step to the left { for(int i=0;i<ArraySize(array);i++) Print(array[i]); //This prints the 0123456789 Print("Br"); ArrayReverse(array,0,ArraySize(array)); //flip array for(int i=0;i<ArraySize(array);i++) Print(array[i]); //This prints the 9876543210 Print("Br"); ArrayResize(array,ArraySize(array)-1,ArraySize(array)); //discard last elemet ArrayReverse(array,0,ArraySize(array)); for(int i=0;i<ArraySize(array);i++) Print(array[i]); //This prints the 123456789 note 1st element is gone }I know its an old thread but I had a task where I needed to shift all elements of array to the left, I came up with a simple code see the above script. NB, my code is open to critism alongside improvements feel free, after all we here to learn :-).
- 2023.10.14
- www.mql5.com
const int array_size = ArraySize(array); for(int cnt = 1; cnt < array_size; cnt++) { array[cnt - 1] = array[cnt]; } ArrayResize(array, array_size - 1);
ArrayCopy(array, array, 1, 0, count - 1); Moderator's edit 2026.02.20 : There is not such thing in the documentation, possibly the documentation was updated in the meantime. It's not an undefined behaviour in MQL5.
- www.mql5.com
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 ...: