$ £ ¥
¥ £ $

How to Normalize Pips and Digits in MQL4

One of the first things you learn in Forex trading is the concept of pip. A pip is the smallest change in value that an exchange rate can make. This was true a long time ago, but nowadays we also need to consider an even smaller change, which is called pipette, which is equal to 0.1 of a pip. When working with trading automation and coding, it is necessary to know how to normalize the pips and the digits with MQL4 code, and you will see here why.

1 pip is a change of value by 0.0001 (non-JPY pairs)

1 pipette is a change of value of 0.00001 (non-JPY pairs)

So, if the EUR/USD currency pair rate changes from 1.0980 to 1.0995, it means that the price has increased by 15 pips (1.0995 - 1.0980 = 0.0015), or we can also say 150 pipettes, even if it doesn't sound as good as pips. Usually, currency exchange rates are expressed with 4 decimals like the one mentioned earlier. However, we can find other currency pairs with only 2 decimals — the ones involving JPY are the most popular. For example, USD/JPY is at 103.83; in this case, the pip represents the change of value by 0.01, and pipettes — by 0.001. In other words, a change in USD/JPY from 103.50 to 103.40 means a price drop of 10 pips (103.40 - 103.50 = -0.10).

Why does anyone need to consider this difference? How does it affect expert advisor?

Good questions!

Forex brokers today are almost all using a representation of prices/exchange rates with 5 decimal places (3 for JPY pairs), while traders usually still think in pips. If you see the image below you will see that all exchange rates have 5 decimals (except the JPY one).

Pip exchange rates with 5 and 3 decimal places

However, you need to consider that you may use a broker with only 4 decimals (unlikely, but possible), so your expert advisors must be able to adapt to the change. Furthermore, to calculate take-profit and stop-loss prices, we usually use pips, not pipettes (for example, 20 pips stop-loss or 30 pips take-profit).

For example, you want to buy EUR/USD quoted 1.09817 (this is what the broker is showing) and have a stop-loss 30 pips away. You know the stop-loss price must be 1.09817 - 0.0030 = 1.09517. MetaTrader MQL4 has a useful value Digits to tell you how many decimals are in the price. You can use this value to calculate the value to subtract or add to the price.

The code below shows the use of Digits to calculate the stop-loss price. This can be adapted to calculate many other values such as spread, slippage, take-profit, and so on.

//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict

// Example of stop-loss in pips.
double StopLoss = 30;

// Function to calculate the decimal places.
// Digits is a native variable in MetaTrader, which is assumes the value 
// of a number of digits after the point in a quote.
double CalculateNormalizedDigits()
   // If there are 3 or fewer digits (JPY, for example), then return 0.01, which is the pip value.
   if (Digits <= 3){
   // If there are 4 or more digits, then return 0.0001, which is the pip value.
   else if (Digits >= 4){
   // In all other cases, return 0.
   else return(0);

//| Script program start function                                    |
void OnStart()
   // Define the Normalized Digits calling the function CalculateNormalizedDigits.
   double nDigits = CalculateNormalizedDigits();

   // Print the Digits value and an example of stop-loss price calculated.
   Print("Number of Digits ", Digits, " - Stop Loss ", Ask - (StopLoss * nDigits));

If we run the script on a EUR/USD chart, the result is:

Normalized Stop-Loss Calculation Example on EUR/USD

While on a USDJPY chart, the result is:

Normalized Stop-Loss Calculation Example on USD/JPY

This simple code can detect and adjust the value to add or subtract, depending on how many decimals there are in the exchange rate.

Please keep this in mind when you are building your own EA.