Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

MQL5 Language Basics: STRING TYPES

//STRINGS, ARE USED TO STORE CHARACTER DATA, ie. text

//string str = "Any text";

//INDICATORS ==> FULFILMENT OF CERTAIN CONDITIONS(TRADING SIGNALS)

//EA ==> report trading results.

// DECLARING A STRING VARIABLE

//string str;

//-------assigned a value upon being declared(initialized to a value)

//string str = "Any text";

//STRINGS CAN BE SPLIT TO SEVERAL STRINGS

/*

string str = "A long string can be "

"split into several "

"substrings";

*/

//string str = "";

// BE IN MIND TO CHECK IF A STRING IS EMPTY

//if (str != " " && str != NULL){........}

//YOU CAN DO THIS AGAIN BY USING StringLen() FUNCTION

//if (StringLen(str) != 0){.......}


//CONCATENATING STRINGS

//CONCATENATING = BUILDING PHRASES USING WORDS

//IMPLIMENTED USING THE "+" SIGN

// An additional string is very often concartenated with the MAIN string.

// The same operation can be written in a much simpler way.

// you can implement the same using a single string

//string str1,str2,str3,str4,str5;

//CONVERTING VARIOUS VARIABLES TO A STRING

//To convert Integer variables (char,uchar,bool,short,ushort,int,uint,color,datetime,long,ulong)

//we need the IntegerToString() function

//To convert REAL variables (DOUBLE,FLOAT)

//we need the DoubleToString() function

//TimeToString() function can also be used to convert date and time to

//a standard format string (readily understandable by humans)

//ColorToString() function can also be used to convert color value

//YET, THERE IS ANOTHER METHOD FOR CONVERTING VARIABLES == {TYPE CASTING}

//=======PATTERN BASED FORMATTING OF STRINGS=======//

// to do this we use the StringFormat() function where the first parameter

// passed to this function is a message template, that indicates the the placers for
// inserting variables and the set output formats; - it is then followed

// by enumeration of all variables in the order in which they appear in the template

/// ===== STRINGFIND() ======///////

// IS USED to search for a substring. it returns the index of the first occurence

// ofthe substring in a string

// 1st parameter passed is where search is done

// 2nd determines the target substring

// 3rd (optional) can determine the position at which the search starts

//// =========stringsubstr() ======/////

// used to get a substring of a given length from a given position

//// SHORTTOSTRING() & CHARTOSTRING()

// CharToString() fn has a greater practical value

// used to present graphical objects, wingdings (obj_label)

//////======= stringgetcharacter() & stringsetcharacter()====///

// they work with the UNICODE codes

/////==== STRINGLEN()=====//////
int OnInit()

/*

// if (str != " " && str != NULL){

if (StringLen(str1) != 0){

Print(str1);

else Print("EMPTY STRING");

*/

//assign values

// str1 = "Programming";

// str2 = "in MQL5";

// str3 = "for MetaTrader 5";

// add up the strings

//str4 = str1 + " " + str2;

//str5 = str1 + " " + str3;

//str1 = str1 + " " + str2;

//str1 = str1 + " " + str3;

//str1 += str2;

//str1 += str3;

//OR

//str1 += str2 + str3;

//str3 = str1 + " " + str2 + " " + str3;

//-------you can perform the concatenation using comma (,)

//Print(str4);
//Print(str3);

// stringADD() function that allows us to add one string to another

//Print(str1," ",str2," ",str3);

/* StringAdd(str1," ");

StringAdd(str1,str2);

StringAdd(str1," ");

StringAdd(str1,str3);

Print(str1);

*/

//StringConcatenate() function allows you to simoultaneosly combine several things

// StringConcatenate(str1,str1," ",str2," ",str3);

// Print(str1);

/*

int x = 1;

string str = IntegerToString(x);

Print(str);

*/

//double x = 1.12345;

//string str = DoubleToString(x);

//The second parameter of this function determines the precison (the no. of dp)

/*

double x = 1.123456789;

string str = DoubleToString(x,7);


Print(str);

*/

//datetime time = TimeCurrent(); // current time

//string str1 = IntegerToString(time); // gives the numerical expression of time

//(i.e. seconds elapsed since Jan 1, 1970)

//string str2 = TimeToString(time); // contains the formatted time

// eg 2023.03.01 17:50

// (year,month,day,hour,minute)

// Print(str1);

//Print(str2);

// when calling the time to string fn, we have the option to specify the date and

// time format

//OPTIONS AVAILABLE

/*

string str1 = "Date & Time with minutes: " + TimeToString(time);

string str2 = "Date: " + TimeToString(time,TIME_DATE);

string str3 = "Time with minutes ONLY: " + TimeToString(time,TIME_MINUTES);

string str4 = "Time with seconds ONLY: " + TimeToString(time,TIME_SECONDS);

string str5 = "DATE & Time with SECONDS: "

+ TimeToString(time,TIME_DATE|TIME_SECONDS);

Print(str1);

Print(str2);

Print(str3);

Print(str4);
Print(str5);

*/

/*color colorvalue = clrRed;

string str1 = ColorToString(colorvalue,true); // true ==> color name

//is printed clrRed

string str2 = ColorToString(colorvalue,false); // false ==> RGB components

// 255,0,0

Print(str1);

Print(str2);

*/

/*

double x = 123.4567;

string str = (string)x;

Print(str);

*/

//=======PATTERN BASED FORMATTING OF STRINGS=======//

// character integer variables []//

/*

//The format of character integer variables is denoted by "i"

//The format of unsigned integer variables is denoted by "u"

uint variable1 = 1;

int variable2 = 2;

int variable3 = 3;

//----long addition

string str1 = "Variable1 = " + IntegerToString(variable1) + ", Variable2 = " + IntegerToString(variable2)

+ ", Variable3 = " + IntegerToString(variable3);


// ----short addition of the strings

string str2 = StringFormat("Variable1 = %u, Variable2 = %i, Variable3 = %i",

variable1,variable2,variable3);

Print(str1);

Print(str2);

*/

//The format of real numbers (float, double) is denoted by "f"

// double d = 5.5;

//string str = StringFormat("d = %f",d);

// you can set the required No. of the dp by using . followed by the no. of dp (.)

//string str = StringFormat("d = %.2f",d);

// You can specify the total length of the no. by writing "0" and a number that determines

// the length of the number at the hand right after the % and then specify the

// no. of dp (if necessary)

//string str = StringFormat("d = %06.2f",d);

// to print the % sign need to write it two times im a row, ie, "%%"

//string str = StringFormat("d = %06.2f%%",d);

//Print(str);

// for the integer types

/*

int variable = 123; //expected output = 00123

string str = StringFormat("Variable = %05i",variable);

Print(str);
*/

// if the no. of the digits specified is less than the no. of digits in the number,

// the output will still be done correctly.

/* int variable = 123; //expected output = 123

string str = StringFormat("Variable = %02i",variable);

Print(str);

*/

//The format of string variables (string) is denoted by "s"

/* string variable = "Any Text";

string str = StringFormat("String Value = %s",variable);

Print(str);

*/

/*

string str = "Programming in MQL5 for MetaTrader 5";

// get the position of the character 5

int position = StringFind(str,"5",0);

//output of the results

Print(IntegerToString(position));

string str2 = StringSubstr(str,15,4);

Print(str2);

*/

/*
//-------------- CHARTTOSTRING()------------//

string obj_name = "label";

ObjectCreate(0,obj_name,OBJ_LABEL,0,0,0); // creates label graphical obj

ObjectSetInteger(0,obj_name,OBJPROP_XDISTANCE,100);

ObjectSetInteger(0,obj_name,OBJPROP_YDISTANCE,100);

ObjectSetInteger(0,obj_name,OBJPROP_FONTSIZE,50);

ObjectSetString(0,obj_name,OBJPROP_FONT,"Wingdings");

//string Icon = 37;

ObjectSetString(0,obj_name,OBJPROP_TEXT,(string)CharToString(93));

ObjectSetInteger(0,obj_name,OBJPROP_COLOR,clrWhite);

*/

/////===== stringgetcharacter()=====/////

//the function allows one to get the code of the character at a given position

/* string str = "MQL5";

// get the unicode code of the character

ushort u_character1 = StringGetCharacter(str,0); // M code

ushort u_character2 = StringGetCharacter(str,1); // Q code

ushort u_character3 = StringGetCharacter(str,2); // L code

ushort u_character4 = StringGetCharacter(str,3); // 5 code

PrintFormat("%i, %i, %i, %1 ",u_character1,u_character2,

u_character3,u_character4);

*/

////////===== stringsetcharacter() =====/////

// allows one change the character code of a given character


// ALSO to add a character at the end of a string

/* string str = "MQ5";

///------- replace the character at a given position

StringSetCharacter(str,2,76); // outcome = MQL

///------ add the unicode character corresponding to the passed code

StringSetCharacter(str,3,53); // outcome = MQL5

Print(str);

*/

///===========---StringLen() fn======/////

string str = __FILE__;

int length = StringLen(str);

if (length > 10 ){

str = StringSubstr(str,0,10) + "---";

Print(str);

return(INIT_SUCCEEDED);

You might also like