MQL4|How to Normalize Double Type (Double) in MetaTrader 4 (MT4) MQL4 Programs

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

In MetaTrader 4 (MT4) MQL4 programs, it is common to round double type values to a specified number of decimal places. This article explains how to normalize double type values using the NormalizeDouble function in a way that is easy for beginners to understand.

What is Normalization?

Normalization refers to rounding double type values to a specified number of decimal places. This allows for handling values with a specific precision. For example, it is useful in cases where specific precision is required, such as stop values and limit values when placing orders.

Using the NormalizeDouble Function

To normalize double type values, use the NormalizeDouble function. This function rounds values to a specified number of decimal places.

Function Specification
double NormalizeDouble(double value, int digits)
  • value: The value to be normalized.
  • digits: The number of decimal places (0 to 8).
Sample Code

Below is a sample code that normalizes a double type value using the NormalizeDouble function.

double original_value = 0.123456789;
double normalized_value = NormalizeDouble(original_value, 5);
Print("Normalized value: " + DoubleToStr(normalized_value, 8));
Execution Result

Running the above sample code will yield the following result:

Normalized value: 0.12346000

Example of Usage

Below is an example of how to use the NormalizeDouble function in an actual trading program.

Usage Example When Placing Orders
double stop_loss = 0.987654321;
double take_profit = 1.123456789;

// Normalize stop loss and take profit values to 5 digits
stop_loss = NormalizeDouble(stop_loss, 5);
take_profit = NormalizeDouble(take_profit, 5);

// Place order
int ticket = OrderSend(Symbol(), OP_BUY, 1.0, Ask, 3, stop_loss, take_profit, "Sample Order", 0, 0, Blue);
if(ticket < 0) {
    Print("Order send error: " + ErrorDescription(GetLastError()));
} else {
    Print("Order send success: " + ticket);
}

In this example, the stop loss and take profit values are normalized to 5 digits when placing an order. This ensures that the order is placed with the appropriate precision.

Summary

By using the NormalizeDouble function, you can normalize double type values to a specified number of decimal places. This allows you to handle values with specific precision, enabling accurate value processing in trading programs. Beginners should first learn the basic usage of this function and apply it to actual 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.