MQL4|How to Convert String to Data Types

※記事内に広告を含む場合があります。

In MetaTrader 4 (MT4) MQL4 programming, converting strings to various data types (Integer, Double, DateTime) makes it easier to process input data. This article explains these conversion methods in a way that is easy for beginners to understand.

What is Converting Strings to Data Types?

Converting strings to data types means transforming numbers or dates expressed as strings into corresponding data types. This allows calculations and data manipulation within the program.

Converting to Integer Type

To convert string data to Integer type, use the StrToInteger() function. This function converts a string representing an integer value to an Integer type.

Function Specification

int StrToInteger(string value)
  • value: String representing an integer value

Sample Code

string str_value = "1024";
int int_value = StrToInteger(str_value);
Print("Integer value: " + int_value);

Converting to Double Type

To convert string data to Double type, use the StrToDouble() function. This function converts a string representing a real number to a Double type.

Function Specification

double StrToDouble(string value)
  • value: String representing a real number

Sample Code

string str_value = "103.2812";
double double_value = StrToDouble(str_value);
Print("Double value: " + double_value);

Converting to DateTime Type

To convert string data to DateTime type, use the StrToTime() function. This function converts a string representing a date and time to a DateTime type. The format needs to be “yyyy.mm.dd hh”.

Function Specification

datetime StrToTime(string value)
  • value: String representing date and time

Sample Code

string str_date = "2024.07.07 12:30";
datetime datetime_value = StrToTime(str_date);
Print("DateTime value: " + TimeToStr(datetime_value));

Execution Results

Executing the sample code above yields the following results:

Integer value: 1024
Double value: 103.2812
DateTime value: 2024.07.07 12:30

Summary

By converting strings to various data types, processing data becomes simpler. In MQL4 programming, you can convert string data to Integer, Double, and DateTime types using the StrToInteger(), StrToDouble(), and StrToTime() functions. Understanding these basic uses will make processing input data in your program easier. Beginners should first learn how to use these functions and then apply them to actual trading programs.

※記事内に広告を含む場合があります。
ベアちゃん@東京シストレ: One of Japan's oldest MetaTrader FX automated trading developers 🔧. With over 15 years of professional experience, he boasts achievements such as winning first place in the Tradency Tournament '15 🥇 and being the runner-up in the 3rd EA-1 Grand Prix 🥈. He has also appeared on Radio Nikkei and is currently active as an executive of Trilogy Corporation (a registered investment advisory and agency business) [Registration number: 372, Director of the Kinki Finance Bureau, Ministry of Finance]. To join the board of Trilogy Corporation, he underwent a rigorous personal examination by the Kinki Finance Bureau, and his name is now registered with the Kinki Finance Bureau. Becoming an executive officer of a registered investment advisory and agency business requires passing this stringent personal examination. Utilizing his extensive knowledge as a former senior design engineer, he provides insights into the development of high-performance Expert Advisors (EAs), the latest trading technologies, and market analysis. He delivers professional strategies and tips to traders aiming to optimize their FX trading and maximize profits.

This website uses cookies.