-
-
Save hsharghi/0a0216c303dada43702159ddc40cbf53 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 ); | |
} | |
/* | |
* arrayBoolToStr | |
* This function outputs a one-dimensional array to a string separated by concatWith variable. | |
* @param bool 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 arrayBoolToStr( bool theArray[], string concatWith = "," ) { | |
string s = ""; | |
int a = ArraySize( theArray ); | |
for( int i = 0; i < a; i++ ) { | |
s = StringConcatenate( s, (theArray[i]?"true":"false"), concatWith ); | |
} | |
s = StringSubstr( s, 0, StringLen(s) - StringLen( concatWith ) ); | |
return ( s ); | |
} | |
/* | |
* 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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment