3

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;
}
TenOutOfTen
  • 467
  • 6
  • 14

2 Answers2

1

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");
}
Dean
  • 2,326
  • 3
  • 13
  • 32
  • This **does not answer the problem**. If one reads the O/P, the Question is ***explicit* to say "expand it to cater for all pairs**. How can I find the number of decimal places **for *each* `Symbol()` price?"** – user3666197 Oct 02 '19 at 11:27
  • Of course it does, depending on the context. If the user is planning on using this in an EA and loads it to a chart, the above **WILL** return the decimals for that product it has been loaded on. – Dean Oct 02 '19 at 12:01
  • This **does not answer the problem**. The code is missing an iterator, that would cover the requirement to **"expand it to cater for all pairs"** - i.e. **for *each* `Symbol()`** as provided by the Broker, part of which is listed in the Market Watch --- which part of the code is still missing ... – user3666197 Oct 02 '19 at 16:28
0

Place your Digits or Pips in the global area.

double pips;
double ticksize = MarketInfo(NULL,MODE_DIGITS);
     if(ticksize==2||ticksize==3||ticksize==4||ticksize==5)
     pips=ticksize*10;
     else pips =ticksize;