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.
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;
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?
//+------------------------------------------------------------------+ //| 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 :-).
Discover new MetaTrader 5 opportunities with MQL5 community and services
- 2023.10.14
- www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
James Kirika Wanjiru #:
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 :-).
Your code copies the array twice, with two malloc calls.
This is in place and should be faster, but not tested for speed:
const int array_size = ArraySize(array); for(int cnt = 1; cnt < array_size; cnt++) { array[cnt - 1] = array[cnt]; } ArrayResize(array, array_size - 1);
EDIT:
According to docs, this has undefined behavior:
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.
Instead of copying each element of an array just 'move' the index: see: https://www.mql5.com/en/articles/3047
MQL5 Cookbook - Creating a ring buffer for fast calculation of indicators in a sliding window
- www.mql5.com
The ring buffer is the simplest and the most efficient way to arrange data when performing calculations in a sliding window. The article describes the algorithm and shows how it simplifies calculations in a sliding window and makes them more efficient.
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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 ...: