Skip to content

Instantly share code, notes, and snippets.

@currencysecrets
Last active February 13, 2021 12:53
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save currencysecrets/4473582 to your computer and use it in GitHub Desktop.
Save currencysecrets/4473582 to your computer and use it in GitHub Desktop.
Array utility functions for MetaTrader 4. Helpful functions for doing some of the repetitive tasks in MQL.
/*
* clearIntArray
* This function deletes all items in array (sets it to 0) and resizes array according to size (default = 1).
* @param int& theArray - passing the array by reference
* @param int size - size of the array (default = 1)
* @return int blank array of size
*/
int clearIntArray( int& theArray[], int size = 0 ) {
ArrayResize( theArray, size );
if ( size > 0 ) { ArrayInitialize( theArray, 0 ); }
return( theArray );
}
/*
* clearDoubleArray
* This function deletes all items in array (sets it to 0) and resizes array according to size (default = 1).
* @param int& theArray - passing the array by reference
* @param int size - size of the array (default = 1)
* @return int blank array of size
*/
int clearDoubleArray( double& theArray[], int size = 0 ) {
ArrayResize( theArray, size );
if ( size > 0 ) { ArrayInitialize( theArray, 0 ); }
return( theArray );
}
/*
* clearStringArray
* This function deletes all items in array (sets it to 0) and resizes array according to size (default = 1).
* @param string& theArray - passing the array by reference
* @param int size - size of the array (default = 1)
* @return int blank array of size
*/
int clearStringArray( string& theArray[], int size = 0 ) {
ArrayResize( theArray, size );
if ( size > 0 ) { ArrayInitialize( theArray, 0 ); }
return( theArray );
}
/*
* addToIntArray
* This function appends an integer value to an integer array.
* @param int& theArray - passing the array by reference
* @param int val - the item to be appended (no checks on val)
* @return int the array with appended value
*/
int addToIntArray( int& theArray[], int val ) {
ArrayResize( theArray, ArraySize( theArray ) + 1 );
theArray[ ArraySize( theArray ) - 1 ] = val;
return( theArray );
}
/*
* addToDoubleArray
* This function appends a double value to a double array.
* @param double& theArray - passing the array by reference
* @param double val - the item to be appended (no checks on val)
* @return double the array with appended value
*/
double addToDoubleArray( double& theArray[], double val ) {
ArrayResize( theArray, ArraySize( theArray ) + 1 );
theArray[ ArraySize( theArray ) - 1 ] = val;
return( theArray );
}
/*
* addToStringArray
* This function appends a string value to a string array.
* @param string& theArray - passing the array by reference
* @param string val - the item to be appended (no checks on val)
* @return string the array with appended value
*/
string addToIntArray( string& theArray[], string val ) {
ArrayResize( theArray, ArraySize( theArray ) + 1 );
theArray[ ArraySize( theArray ) - 1 ] = val;
return( theArray );
}
/*
* arrayIntToStr
* This function outputs a one-dimensional array to a string separated by concatWith variable.
* @param int theArray - the array of items you seek to print
* @param string concatWith - the string you wish to concatenate items with (default = ",")
* @return string the array concatenated to a string
*/
string arrayIntToStr( int theArray[], string concatWith = "," ) {
string s = "";
int a = ArraySize( theArray );
for( int i = 0; i < a; i++ ) {
s = StringConcatenate( s, theArray[i], concatWith );
}
s = StringSubstr( s, 0, StringLen(s) - StringLen( concatWith ) );
return ( s );
}
/*
* arrayDoubleToStr
* This function outputs a one-dimensional array to a string separated by concatWith variable.
* @param double theArray - the array of items you seek to print
* @param string concatWith - the string you wish to concatenate items with (default = ",")
* @return string the array concatenated to a string
*/
string arrayDoubleToStr( double theArray[], string concatWith = "," ) {
string s = "";
int a = ArraySize( theArray );
for( int i = 0; i < a; i++ ) {
s = StringConcatenate( s, theArray[i], concatWith );
}
s = StringSubstr( s, 0, StringLen(s) - StringLen( concatWith ) );
return ( s );
}
/*
* arrayStringToStr
* This function outputs a one-dimensional array to a string separated by concatWith variable.
* @param string theArray - the array of items you seek to print
* @param string concatWith - the string you wish to concatenate items with (default = ",")
* @return string the array concatenated to a string
*/
string arrayStringToStr( string theArray[], string concatWith = "," ) {
string s = "";
int a = ArraySize( theArray );
for( int i = 0; i < a; i++ ) {
s = StringConcatenate( s, theArray[i], concatWith );
}
s = StringSubstr( s, 0, StringLen(s) - StringLen( concatWith ) );
return ( s );
}
@WinstonN
Copy link

WinstonN commented Jan 9, 2018

Hi Hi

When I include this file in my EA and compile it I get the following errors

Test.mq4 : information: Compiling 'Test.mq4'
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4 : information: Including 'ArrayFunctions.mq4'
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4(8,24) : warning 41: arrays passed by reference only
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4(88,27) : warning 41: arrays passed by reference only
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4(105,33) : warning 41: arrays passed by reference only
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4(122,33) : warning 41: arrays passed by reference only
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4(11,12) : error 120: 'theArray' - invalid array access
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4(24,12) : error 120: 'theArray' - invalid array access
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4(36,22) : error 166: 'ArrayInitialize' - no one of the overloads can be applied to the function call
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4 : information: error could be one of 8 function(s)
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4 : information: error    built-in 'ArrayInitialize'
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4 : information: error    built-in 'ArrayInitialize'
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4 : information: error    built-in 'ArrayInitialize'
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4 : information: error    built-in 'ArrayInitialize'
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4 : information: error    built-in 'ArrayInitialize'
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4 : information: error    built-in 'ArrayInitialize'
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4 : information: error    built-in 'ArrayInitialize'
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4 : information: error    built-in 'ArrayInitialize'
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4(37,12) : error 120: 'theArray' - invalid array access
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4(50,12) : error 120: 'theArray' - invalid array access
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4(63,12) : error 120: 'theArray' - invalid array access
Z:\home\win\MT4\MQL4\include\ArrayFunctions.mq4(76,12) : error 120: 'theArray' - invalid array access
Result: 7 error(s), 4 warning(s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment