Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 866
TenOutOfTen Avatar asked Sep 15 '25 10:09

TenOutOfTen


2 Answers

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");
}
like image 122
Dean Avatar answered Sep 18 '25 09:09

Dean


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;
like image 37
King Trinity Avatar answered Sep 18 '25 09:09

King Trinity