With MetaTrader Terminal ( MQL4
), I try to have a reversed array, that I append ( prepend ) items to.
So, on every tick, myArray[0]
becomes the 'newest' value, and the previous value shifts to myArray[1]
and so on.
But its harder then it sounds.
I tried like this ->
double myArray = []; // GLOBAL Dynamic array
extern int maxArrayLength = 50; // EXTERN iterable
// -----------------------------------------------------------------------
bool prependToReversedDoubleArray( double& theArray[], double value, int maxLength ) {
int size = ArraySize( theArray ); // LOCAL size
ArraySetAsSeries( theArray, false ); // Normalize the array ( left to right )
ArrayResize( theArray, size + 1 ); // Extend array length
Alert( "test = ", size );
theArray[size] = value; // Insert the new value
ArraySetAsSeries( theArray, true ); // Reverse the array again
if ( ArraySize( theArray ) > maxLength ) {
ArrayResize( theArray, maxLength );
}
return( true );
}
prependToReversedDoubleArray( myArray, 0.1234, maxArrayLength );
thanks for all the good intel! I learned a lot from the explanations :)
The problem I had was, how to append values to the 'beginning' of a reversed array ( write to array[0]
and shift the rest up ).
It still not non-blocking and probably not the fastest way, but it works for now.
Here is the solution, it also takes a 'maxLength' value, that keeps the array size as desired :) :
int prependToReversedDoubleArray( double &theArray[],
double value,
int maxLength
)
{ int newSize = ArraySize( theArray ) + 1;
ArraySetAsSeries( theArray, false ); // Normalize the array (left to right)
ArrayResize( theArray, newSize ); // Extend array length
theArray[newSize-1] = value; // Insert the new value
ArraySetAsSeries( theArray, true ); // Reverse the array again
if ( maxLength > 0
&& newSize > maxLength ) // Check if the max length is reached
{ newSize = maxLength;
ArrayResize( theArray, maxLength );
}
return( newSize );
}