mql4algorithmic-tradingmql5mt4

How to get the number of decimal places for every Symbol() price


Certain Currency pairs display values to 5 decimal places (EURUSD), others 4 and so on. I wrote the code below to return the integer value of the decimal places minus one. This function just takes into consideration a few pairs. I would like to expand it to cater for all pairs. How can I find the number of decimal places for each Symbol() price?

int decimalPlacesForPairs()  {
   if ((_Symbol == "XAUUSD") || (_Symbol == "USOIL")) {
      return 1;
   }

   else if (_Symbol == "CADJPY") {
      return 2;
   }  

   else return 3;
}

Solution

  • In MQL4 you have access to a predefined variable int Digits. This function returns the number of digits after the decimal point.

    The example given is:

    Print(DoubleToStr(Close[0], Digits));

    Another way, and perhaps a better way in your case is to use MarketInfo. Here you can return the number of decimals places per symbol by inserting the symbol as a string variable.

    The example given:

    int vdigits = (int)MarketInfo("EURUSD",MODE_DIGITS);

    In your case you could have a function like the below:

    int decimalPlacesForPairs(string sPair)  {
       return MarketInfo(sPair),MODE_DIGITS);
    }
    

    And to call from your Main(){}:

    void Main()
    {
        decimalPlacesForPairs(Symbol());
        //or 
        //decimalPlacesForPairs("EURUSD");
    }